Skip to content

Commit e884bd6

Browse files
author
Aidan Laing
committed
Mobile Authentication Mobile
Added mobile authentication library module Added example use of mobile authentication library module
1 parent 071ff19 commit e884bd6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2074
-3
lines changed

.idea/gradle.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ android {
2424

2525
dependencies {
2626
implementation fileTree(dir: 'libs', include: ['*.jar'])
27+
implementation project(':mobileauthentication')
2728

2829
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
2930
implementation 'com.android.support:appcompat-v7:26.1.0'

app/src/main/AndroidManifest.xml

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,36 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="ca.bc.gov.mobileauthenticationandroidexample">
44

5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
58
<application
69
android:allowBackup="true"
10+
android:fullBackupContent="false"
711
android:icon="@mipmap/ic_launcher"
812
android:label="@string/app_name"
913
android:roundIcon="@mipmap/ic_launcher_round"
1014
android:supportsRtl="true"
1115
android:theme="@style/AppTheme">
16+
1217
<activity android:name=".MainActivity">
1318
<intent-filter>
1419
<action android:name="android.intent.action.MAIN" />
15-
1620
<category android:name="android.intent.category.LAUNCHER" />
1721
</intent-filter>
1822
</activity>
23+
24+
<activity
25+
android:name="ca.bc.gov.mobileauthentication.screens.redirect.RedirectActivity"
26+
android:launchMode="singleInstance">
27+
<intent-filter android:autoVerify="true">
28+
<action android:name="android.intent.action.VIEW" />
29+
<category android:name="android.intent.category.DEFAULT" />
30+
<category android:name="android.intent.category.BROWSABLE" />
31+
<data android:scheme="bcgov" />
32+
</intent-filter>
33+
</activity>
34+
1935
</application>
2036

2137
</manifest>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,108 @@
11
package ca.bc.gov.mobileauthenticationandroidexample
22

3+
import android.content.Intent
34
import android.support.v7.app.AppCompatActivity
45
import android.os.Bundle
6+
import android.util.Log
7+
import ca.bc.gov.mobileauthentication.MobileAuthenticationClient
8+
import ca.bc.gov.mobileauthentication.common.exceptions.NoRefreshTokenException
9+
import ca.bc.gov.mobileauthentication.common.exceptions.RefreshExpiredException
10+
import ca.bc.gov.mobileauthentication.common.exceptions.TokenNotFoundException
11+
import ca.bc.gov.mobileauthentication.data.models.Token
512

613
class MainActivity : AppCompatActivity() {
714

15+
private var client: MobileAuthenticationClient? = null
16+
17+
private val tag = "MOBILE_AUTH"
18+
819
override fun onCreate(savedInstanceState: Bundle?) {
920
super.onCreate(savedInstanceState)
1021
setContentView(R.layout.activity_main)
22+
23+
val authEndpoint = "https://dev-sso.pathfinder.gov.bc.ca/auth/realms/mobile/protocol/openid-connect/auth"
24+
val baseUrl = "https://dev-sso.pathfinder.gov.bc.ca/"
25+
val clientId = "secure-image"
26+
val realmName = "mobile"
27+
val redirectUri = "bcgov://android"
28+
29+
client = MobileAuthenticationClient(this, baseUrl, realmName,
30+
authEndpoint, redirectUri, clientId)
31+
32+
client?.authenticate()
33+
}
34+
35+
override fun onDestroy() {
36+
super.onDestroy()
37+
client?.clear()
38+
}
39+
40+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
41+
super.onActivityResult(requestCode, resultCode, data)
42+
client?.handleAuthResult(requestCode, resultCode, data, object: MobileAuthenticationClient.TokenCallback {
43+
override fun onError(throwable: Throwable) {
44+
Log.e(tag, throwable.message)
45+
}
46+
override fun onSuccess(token: Token) {
47+
Log.d(tag, "Authenticate Success")
48+
getToken()
49+
}
50+
})
51+
}
52+
53+
private fun getToken() {
54+
client?.getToken(object: MobileAuthenticationClient.TokenCallback {
55+
override fun onError(throwable: Throwable) {
56+
when (throwable) {
57+
is RefreshExpiredException -> {
58+
Log.e(tag, "Refresh token is expired. Please re-authenticate.")
59+
}
60+
is NoRefreshTokenException -> {
61+
Log.e(tag, "No Refresh token associated with Token")
62+
}
63+
is TokenNotFoundException -> {
64+
Log.e(tag, "No Token was found")
65+
}
66+
}
67+
}
68+
69+
override fun onSuccess(token: Token) {
70+
Log.d(tag, "Get Success")
71+
refreshToken()
72+
}
73+
})
74+
}
75+
76+
private fun refreshToken() {
77+
client?.refreshToken(object: MobileAuthenticationClient.TokenCallback {
78+
override fun onError(throwable: Throwable) {
79+
Log.e(tag, throwable.message)
80+
when (throwable) {
81+
is RefreshExpiredException -> {
82+
Log.e(tag, "Refresh token is expired. Please re-authenticate.")
83+
}
84+
is NoRefreshTokenException -> {
85+
Log.e(tag, "No Refresh token associated with Token")
86+
}
87+
}
88+
}
89+
90+
override fun onSuccess(token: Token) {
91+
Log.d(tag, "Refresh Success")
92+
deleteToken()
93+
}
94+
})
95+
}
96+
97+
private fun deleteToken() {
98+
client?.deleteToken(object: MobileAuthenticationClient.DeleteCallback {
99+
override fun onError(throwable: Throwable) {
100+
Log.e(tag, throwable.message)
101+
}
102+
103+
override fun onSuccess() {
104+
Log.d(tag, "Delete Success")
105+
}
106+
})
11107
}
12108
}

mobileauthentication/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

mobileauthentication/build.gradle

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
apply plugin: 'com.android.library'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
9+
compileSdkVersion 26
10+
11+
defaultConfig {
12+
minSdkVersion 23
13+
targetSdkVersion 26
14+
compileSdkVersion 26
15+
buildToolsVersion '26.0.2'
16+
versionCode 1
17+
versionName "1.0"
18+
19+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
20+
21+
}
22+
23+
buildTypes {
24+
release {
25+
postprocessing {
26+
removeUnusedCode false
27+
removeUnusedResources false
28+
obfuscate false
29+
optimizeCode false
30+
proguardFile 'proguard-rules.pro'
31+
}
32+
}
33+
}
34+
35+
}
36+
37+
dependencies {
38+
implementation fileTree(dir: 'libs', include: ['*.jar'])
39+
40+
implementation "com.android.support:appcompat-v7:26.1.0"
41+
implementation "com.android.support:customtabs:26.1.0"
42+
implementation "com.android.support.constraint:constraint-layout:1.0.2"
43+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.21"
44+
45+
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
46+
implementation "io.reactivex.rxjava2:rxjava:2.1.7"
47+
implementation "io.reactivex.rxjava2:rxkotlin:2.2.0"
48+
49+
implementation "com.squareup.retrofit2:retrofit:2.3.0"
50+
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
51+
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
52+
implementation "com.squareup.okhttp3:logging-interceptor:3.9.1"
53+
54+
testImplementation "junit:junit:4.12"
55+
testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
56+
}
57+
58+
repositories {
59+
mavenCentral()
60+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="ca.bc.gov.mobileauthentication">
3+
4+
<application>
5+
6+
<activity
7+
android:name="ca.bc.gov.mobileauthentication.screens.redirect.RedirectActivity"
8+
android:theme="@style/AppTheme" />
9+
10+
</application>
11+
12+
</manifest>

0 commit comments

Comments
 (0)