Skip to content

Commit

Permalink
Mobile Authentication Mobile
Browse files Browse the repository at this point in the history
Added mobile authentication library module
Added example use of mobile authentication library module
  • Loading branch information
Aidan Laing committed Feb 7, 2018
1 parent 071ff19 commit e884bd6
Show file tree
Hide file tree
Showing 47 changed files with 2,074 additions and 3 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':mobileauthentication')

implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.bc.gov.mobileauthenticationandroidexample">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="ca.bc.gov.mobileauthentication.screens.redirect.RedirectActivity"
android:launchMode="singleInstance">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="bcgov" />
</intent-filter>
</activity>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,12 +1,108 @@
package ca.bc.gov.mobileauthenticationandroidexample

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import ca.bc.gov.mobileauthentication.MobileAuthenticationClient
import ca.bc.gov.mobileauthentication.common.exceptions.NoRefreshTokenException
import ca.bc.gov.mobileauthentication.common.exceptions.RefreshExpiredException
import ca.bc.gov.mobileauthentication.common.exceptions.TokenNotFoundException
import ca.bc.gov.mobileauthentication.data.models.Token

class MainActivity : AppCompatActivity() {

private var client: MobileAuthenticationClient? = null

private val tag = "MOBILE_AUTH"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val authEndpoint = "https://dev-sso.pathfinder.gov.bc.ca/auth/realms/mobile/protocol/openid-connect/auth"
val baseUrl = "https://dev-sso.pathfinder.gov.bc.ca/"
val clientId = "secure-image"
val realmName = "mobile"
val redirectUri = "bcgov://android"

client = MobileAuthenticationClient(this, baseUrl, realmName,
authEndpoint, redirectUri, clientId)

client?.authenticate()
}

override fun onDestroy() {
super.onDestroy()
client?.clear()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
client?.handleAuthResult(requestCode, resultCode, data, object: MobileAuthenticationClient.TokenCallback {
override fun onError(throwable: Throwable) {
Log.e(tag, throwable.message)
}
override fun onSuccess(token: Token) {
Log.d(tag, "Authenticate Success")
getToken()
}
})
}

private fun getToken() {
client?.getToken(object: MobileAuthenticationClient.TokenCallback {
override fun onError(throwable: Throwable) {
when (throwable) {
is RefreshExpiredException -> {
Log.e(tag, "Refresh token is expired. Please re-authenticate.")
}
is NoRefreshTokenException -> {
Log.e(tag, "No Refresh token associated with Token")
}
is TokenNotFoundException -> {
Log.e(tag, "No Token was found")
}
}
}

override fun onSuccess(token: Token) {
Log.d(tag, "Get Success")
refreshToken()
}
})
}

private fun refreshToken() {
client?.refreshToken(object: MobileAuthenticationClient.TokenCallback {
override fun onError(throwable: Throwable) {
Log.e(tag, throwable.message)
when (throwable) {
is RefreshExpiredException -> {
Log.e(tag, "Refresh token is expired. Please re-authenticate.")
}
is NoRefreshTokenException -> {
Log.e(tag, "No Refresh token associated with Token")
}
}
}

override fun onSuccess(token: Token) {
Log.d(tag, "Refresh Success")
deleteToken()
}
})
}

private fun deleteToken() {
client?.deleteToken(object: MobileAuthenticationClient.DeleteCallback {
override fun onError(throwable: Throwable) {
Log.e(tag, throwable.message)
}

override fun onSuccess() {
Log.d(tag, "Delete Success")
}
})
}
}
1 change: 1 addition & 0 deletions mobileauthentication/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
60 changes: 60 additions & 0 deletions mobileauthentication/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {

compileSdkVersion 26

defaultConfig {
minSdkVersion 23
targetSdkVersion 26
compileSdkVersion 26
buildToolsVersion '26.0.2'
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
postprocessing {
removeUnusedCode false
removeUnusedResources false
obfuscate false
optimizeCode false
proguardFile 'proguard-rules.pro'
}
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation "com.android.support:appcompat-v7:26.1.0"
implementation "com.android.support:customtabs:26.1.0"
implementation "com.android.support.constraint:constraint-layout:1.0.2"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.21"

implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
implementation "io.reactivex.rxjava2:rxjava:2.1.7"
implementation "io.reactivex.rxjava2:rxkotlin:2.2.0"

implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.9.1"

testImplementation "junit:junit:4.12"
testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
}

repositories {
mavenCentral()
}
21 changes: 21 additions & 0 deletions mobileauthentication/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
12 changes: 12 additions & 0 deletions mobileauthentication/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.bc.gov.mobileauthentication">

<application>

<activity
android:name="ca.bc.gov.mobileauthentication.screens.redirect.RedirectActivity"
android:theme="@style/AppTheme" />

</application>

</manifest>
Loading

0 comments on commit e884bd6

Please sign in to comment.