From 1bf56da7979f5f7ab74eb100bbd47318ad013bc3 Mon Sep 17 00:00:00 2001 From: Omer Habib <30689349+omerhabib26@users.noreply.github.com> Date: Wed, 15 Feb 2023 18:39:16 +0500 Subject: [PATCH] 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 --- OpenEdXMobile/build.gradle | 2 +- .../gradle_scripts/AndroidHelper.gradle | 2 + .../mobile/social/microsoft/MicrosoftAuth.kt | 73 ++++++++++++------- build.gradle | 1 + 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/OpenEdXMobile/build.gradle b/OpenEdXMobile/build.gradle index d25bd2e413..3a193658c9 100644 --- a/OpenEdXMobile/build.gradle +++ b/OpenEdXMobile/build.gradle @@ -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" diff --git a/OpenEdXMobile/gradle_scripts/AndroidHelper.gradle b/OpenEdXMobile/gradle_scripts/AndroidHelper.gradle index f2ac97bbe6..04b58bb066 100644 --- a/OpenEdXMobile/gradle_scripts/AndroidHelper.gradle +++ b/OpenEdXMobile/gradle_scripts/AndroidHelper.gradle @@ -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 { diff --git a/OpenEdXMobile/src/main/java/org/edx/mobile/social/microsoft/MicrosoftAuth.kt b/OpenEdXMobile/src/main/java/org/edx/mobile/social/microsoft/MicrosoftAuth.kt index 93b6d26721..c99c99187c 100644 --- a/OpenEdXMobile/src/main/java/org/edx/mobile/social/microsoft/MicrosoftAuth.kt +++ b/OpenEdXMobile/src/main/java/org/edx/mobile/social/microsoft/MicrosoftAuth.kt @@ -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?) { + 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") } } diff --git a/build.gradle b/build.gradle index 5a8eb24eda..2459b38c44 100644 --- a/build.gradle +++ b/build.gradle @@ -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" }