Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 1bf56da

Browse files
committed
feat: Update Microsoft Library to support Android S+ devices (#1752)
* feat: Update Microsoft Library to support Android S+ devices - Update Microsoft Auth library - Optimise MicrosoftAuth.kt to support new methods fix: LEARNER-7712
1 parent 265fc86 commit 1bf56da

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed

OpenEdXMobile/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ dependencies {
148148
implementation 'com.google.android.gms:play-services-cast-framework:21.2.0'
149149

150150
implementation 'com.facebook.android:facebook-login:15.2.0'
151-
implementation 'com.microsoft.identity.client:msal:0.3+'
151+
implementation 'com.microsoft.identity.client:msal:4.2.0'
152152
implementation 'commons-lang:commons-lang:2.6'
153153
implementation 'com.google.code.gson:gson:2.9.0'
154154
implementation "org.greenrobot:eventbus:3.3.1"

OpenEdXMobile/gradle_scripts/AndroidHelper.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class AndroidHelper {
118118
"client_id" "$clientId"
119119
"authorization_user_agent" "DEFAULT"
120120
"redirect_uri" "msauth://" + project.APPLICATION_ID + "/" + URLEncoder.encode(signatureHash, "UTF-8")
121+
"broker_redirect_uri_registered" false
122+
"account_mode" "MULTIPLE"
121123
authorities([{
122124
"type" "AAD"
123125
audience {

OpenEdXMobile/src/main/java/org/edx/mobile/social/microsoft/MicrosoftAuth.kt

+47-26
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,72 @@ package org.edx.mobile.social.microsoft
22

33
import android.app.Activity
44
import android.content.Intent
5+
import com.microsoft.identity.client.AcquireTokenParameters
56
import com.microsoft.identity.client.AuthenticationCallback
7+
import com.microsoft.identity.client.IAccount
68
import com.microsoft.identity.client.IAuthenticationResult
9+
import com.microsoft.identity.client.IMultipleAccountPublicClientApplication
10+
import com.microsoft.identity.client.IPublicClientApplication
711
import com.microsoft.identity.client.PublicClientApplication
812
import com.microsoft.identity.client.exception.MsalException
913
import org.edx.mobile.R
1014
import org.edx.mobile.social.ISocialImpl
1115

1216
class MicrosoftAuth(activity: Activity?) : ISocialImpl(activity) {
13-
private var microsoftClient: PublicClientApplication? = null
17+
private var microsoftClient: IMultipleAccountPublicClientApplication? = null
18+
1419
override fun login() {
15-
microsoftClient = PublicClientApplication(this.activity, R.raw.auth_config)
16-
microsoftClient?.acquireToken(activity, SCOPES, object : AuthenticationCallback {
17-
override fun onSuccess(authenticationResult: IAuthenticationResult) {
18-
callback?.onLogin(authenticationResult.accessToken)
19-
logger.debug("Microsoft Logged in successfully.")
20-
}
20+
PublicClientApplication.createMultipleAccountPublicClientApplication(this.activity,
21+
R.raw.auth_config,
22+
object : IPublicClientApplication.IMultipleAccountApplicationCreatedListener {
23+
override fun onCreated(application: IMultipleAccountPublicClientApplication) {
24+
microsoftClient = application
25+
microsoftClient?.acquireToken(
26+
AcquireTokenParameters.Builder()
27+
.startAuthorizationFromActivity(activity)
28+
.withScopes(SCOPES)
29+
.withCallback(object : AuthenticationCallback {
30+
override fun onSuccess(authenticationResult: IAuthenticationResult?) {
31+
callback?.onLogin(authenticationResult?.accessToken)
32+
logger.debug("Microsoft Logged in successfully.")
33+
}
2134

22-
override fun onError(exception: MsalException) {
23-
callback?.onError(exception)
24-
logger.error(exception, true)
25-
}
35+
override fun onError(exception: MsalException) {
36+
callback?.onError(exception)
37+
logger.error(exception, true)
38+
}
2639

27-
override fun onCancel() {
28-
callback?.onCancel()
29-
logger.debug("Microsoft Log in canceled.")
30-
}
31-
})
40+
override fun onCancel() {
41+
callback?.onCancel()
42+
logger.debug("Microsoft Log in canceled.")
43+
}
44+
})
45+
.build()
46+
)
47+
}
48+
49+
override fun onError(exception: MsalException) {
50+
logger.error(exception, true)
51+
callback?.onError(exception)
52+
}
53+
})
3254
}
3355

3456
override fun logout() {
35-
microsoftClient?.getAccounts { accounts ->
36-
if (accounts.isNotEmpty()) {
37-
for (account in accounts) {
38-
// Pass empty callback because sometime throw `NullPointerException`
39-
// due to null callback
40-
microsoftClient?.removeAccount(
41-
account) { }
42-
}
57+
microsoftClient?.getAccounts(object : IPublicClientApplication.LoadAccountsCallback {
58+
override fun onTaskCompleted(result: MutableList<IAccount>?) {
59+
result?.forEach { microsoftClient?.removeAccount(it) }
4360
}
44-
}
61+
62+
override fun onError(exception: MsalException?) {
63+
logger.error(exception, true)
64+
}
65+
})
4566
}
4667

4768
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}
4869

4970
companion object {
50-
private val SCOPES = arrayOf("https://graph.microsoft.com/User.Read")
71+
private val SCOPES = listOf("User.Read")
5172
}
5273
}

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ allprojects {
99
maven { url 'https://jitpack.io' }
1010
maven { url "https://appboy.github.io/appboy-android-sdk/sdk" }
1111
maven { url "https://appboy.github.io/appboy-segment-android/sdk" }
12+
maven { url 'https://pkgs.dev.azure.com/MicrosoftDeviceSDK/DuoSDK-Public/_packaging/Duo-SDK-Feed/maven/v1' }
1213
}
1314
project.apply from: "${rootDir}/constants.gradle"
1415
}

0 commit comments

Comments
 (0)