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

Commit

Permalink
feat: Update Microsoft Library to support Android S+ devices (#1752)
Browse files Browse the repository at this point in the history
* feat: Update Microsoft Library to support Android S+ devices

- Update Microsoft Auth library
- Optimise MicrosoftAuth.kt to support new methods
fix: LEARNER-7712
  • Loading branch information
omerhabib26 committed Feb 15, 2023
1 parent 265fc86 commit 1bf56da
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
2 changes: 1 addition & 1 deletion OpenEdXMobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ dependencies {
implementation 'com.google.android.gms:play-services-cast-framework:21.2.0'

implementation 'com.facebook.android:facebook-login:15.2.0'
implementation 'com.microsoft.identity.client:msal:0.3+'
implementation 'com.microsoft.identity.client:msal:4.2.0'
implementation 'commons-lang:commons-lang:2.6'
implementation 'com.google.code.gson:gson:2.9.0'
implementation "org.greenrobot:eventbus:3.3.1"
Expand Down
2 changes: 2 additions & 0 deletions OpenEdXMobile/gradle_scripts/AndroidHelper.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class AndroidHelper {
"client_id" "$clientId"
"authorization_user_agent" "DEFAULT"
"redirect_uri" "msauth://" + project.APPLICATION_ID + "/" + URLEncoder.encode(signatureHash, "UTF-8")
"broker_redirect_uri_registered" false
"account_mode" "MULTIPLE"
authorities([{
"type" "AAD"
audience {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,72 @@ package org.edx.mobile.social.microsoft

import android.app.Activity
import android.content.Intent
import com.microsoft.identity.client.AcquireTokenParameters
import com.microsoft.identity.client.AuthenticationCallback
import com.microsoft.identity.client.IAccount
import com.microsoft.identity.client.IAuthenticationResult
import com.microsoft.identity.client.IMultipleAccountPublicClientApplication
import com.microsoft.identity.client.IPublicClientApplication
import com.microsoft.identity.client.PublicClientApplication
import com.microsoft.identity.client.exception.MsalException
import org.edx.mobile.R
import org.edx.mobile.social.ISocialImpl

class MicrosoftAuth(activity: Activity?) : ISocialImpl(activity) {
private var microsoftClient: PublicClientApplication? = null
private var microsoftClient: IMultipleAccountPublicClientApplication? = null

override fun login() {
microsoftClient = PublicClientApplication(this.activity, R.raw.auth_config)
microsoftClient?.acquireToken(activity, SCOPES, object : AuthenticationCallback {
override fun onSuccess(authenticationResult: IAuthenticationResult) {
callback?.onLogin(authenticationResult.accessToken)
logger.debug("Microsoft Logged in successfully.")
}
PublicClientApplication.createMultipleAccountPublicClientApplication(this.activity,
R.raw.auth_config,
object : IPublicClientApplication.IMultipleAccountApplicationCreatedListener {
override fun onCreated(application: IMultipleAccountPublicClientApplication) {
microsoftClient = application
microsoftClient?.acquireToken(
AcquireTokenParameters.Builder()
.startAuthorizationFromActivity(activity)
.withScopes(SCOPES)
.withCallback(object : AuthenticationCallback {
override fun onSuccess(authenticationResult: IAuthenticationResult?) {
callback?.onLogin(authenticationResult?.accessToken)
logger.debug("Microsoft Logged in successfully.")
}

override fun onError(exception: MsalException) {
callback?.onError(exception)
logger.error(exception, true)
}
override fun onError(exception: MsalException) {
callback?.onError(exception)
logger.error(exception, true)
}

override fun onCancel() {
callback?.onCancel()
logger.debug("Microsoft Log in canceled.")
}
})
override fun onCancel() {
callback?.onCancel()
logger.debug("Microsoft Log in canceled.")
}
})
.build()
)
}

override fun onError(exception: MsalException) {
logger.error(exception, true)
callback?.onError(exception)
}
})
}

override fun logout() {
microsoftClient?.getAccounts { accounts ->
if (accounts.isNotEmpty()) {
for (account in accounts) {
// Pass empty callback because sometime throw `NullPointerException`
// due to null callback
microsoftClient?.removeAccount(
account) { }
}
microsoftClient?.getAccounts(object : IPublicClientApplication.LoadAccountsCallback {
override fun onTaskCompleted(result: MutableList<IAccount>?) {
result?.forEach { microsoftClient?.removeAccount(it) }
}
}

override fun onError(exception: MsalException?) {
logger.error(exception, true)
}
})
}

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

companion object {
private val SCOPES = arrayOf("https://graph.microsoft.com/User.Read")
private val SCOPES = listOf("User.Read")
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ allprojects {
maven { url 'https://jitpack.io' }
maven { url "https://appboy.github.io/appboy-android-sdk/sdk" }
maven { url "https://appboy.github.io/appboy-segment-android/sdk" }
maven { url 'https://pkgs.dev.azure.com/MicrosoftDeviceSDK/DuoSDK-Public/_packaging/Duo-SDK-Feed/maven/v1' }
}
project.apply from: "${rootDir}/constants.gradle"
}
Expand Down

0 comments on commit 1bf56da

Please sign in to comment.