microsoft-authentication-library-for-android icon indicating copy to clipboard operation
microsoft-authentication-library-for-android copied to clipboard

Add ClientException wrapper for native auth

Open Yuki-YuXin opened this issue 1 year ago • 0 comments

Goal:

The SDK currently throws Exceptions in some flows (e.g. ClientException when an empty username is passed, refresh token exceptions, etc.). We should refactor this and replace it with exceptions being wrapped into an Error class and returned as a normal SDK response. Calling applications shouldn't crash, and shouldn't require try-catch blocks. This will also involve updating the sample app (removing try-catch surrounding SDK methods being called)

Investigation summary:

  • The incorrect configuration throws MsalClientException which it's inevitable and unsolvable by error class wrapper.
  • Under CommandResultUtil.kt, the Exception has been wrapped as UnknownError. This works for the scenario "empty username". (Question: Empty username should use UnknownError here or separate code block in NativeAuthApplication Answer: Separate code block in NativeAuthApplication in order to keep align with the IOS side)
 if (this.status != ICommandResult.ResultStatus.COMPLETED) {
        var exception: Exception? = null
        var exceptionMessage: String? = ""

        if (this.result is Exception) {
            exception = this.result as Exception
            exceptionMessage = exception.message
        }

        return com.microsoft.identity.common.java.nativeauth.controllers.results.INativeAuthCommandResult.UnknownError(
            error = UNSUCCESSFUL_COMMAND_ERROR,
            errorDescription = exceptionMessage,
            exception = exception,
            correlationId = this.correlationId
        ) as ExpectedType
  • BaseNativeAuthController throws ClientException. They could only be called in NativeAuthMsalController and we didn't.
  • NativeAuthMsalController throws IOException::class, ClientException::class, ServiceException::class mainly from acquireTokenSilent used by AcquireTokenNoFixedScopesCommand (accountState.getAccessToken). It will be handled by is Exception in getAccessToken`
  • verifyNoUserIsSignedIn() throws MsalClientException

Changes summary:

  1. Add ClientException error in Error.kt
  2. Use try catch block with corresponding errors / client exception error in interface methods.

Company PRs:

common: https://github.com/AzureAD/microsoft-authentication-library-common-for-android/pull/2385 native sample app: https://github.com/Azure-Samples/ms-identity-ciam-native-auth-android-sample/pull/24

Yuki-YuXin avatar Apr 16 '24 11:04 Yuki-YuXin