Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSAL for Android fails performing B2C login

I'm using 0.2.2 version of Microsoft Authentication Library (MSAL) Preview for Android library to perform Azure AD B2C login in my native Android app. The library opens the browser to start login process. Afterwards I log in successfully and it navigates me back to the app. Inside AuthenticationCallback, I get the following error:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
      at com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter.getExpiresOn(MicrosoftStsAccountCredentialAdapter.java:231)
      at com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter.createAccessToken(MicrosoftStsAccountCredentialAdapter.java:78)
      at com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter.createAccessToken(MicrosoftStsAccountCredentialAdapter.java:45)
      at com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache.save(MsalOAuth2TokenCache.java:112)
      ...

When I debug and trace the library code, it seems like the library gets the expires_in field from TokenResponse as null. Is there any idea how it can be happening?

And also here is my raw config file for the library:

{
  "client_id" : "XXX",
  "authorization_user_agent" : "DEFAULT",
  "redirect_uri" : "msalXXX://auth",
  "authorities" : [
    {
      "type": "B2C",
      "authority_url": "https://TTT.b2clogin.com/tfp/TTT.onmicrosoft.com/B2C_1_susi/"
    }
  ]
}

where XXX is client id, and TTT is tenant name.

I also enabled logging for the library. Here it's after it gets back from browser:

D:  [2019-04-09 11:22:44 - {"thread_id":"2","correlation_id":"b843f0f5-d446-480c-9c63-cfcc9ad74e51"}] Completing acquire token... Android 28
D:  [2019-04-09 11:22:44 - {"thread_id":"2","correlation_id":"b843f0f5-d446-480c-9c63-cfcc9ad74e51"}] Auth code is successfully returned from webview redirect. Android 28
D:  [2019-04-09 11:22:44 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Network status: connected Android 28
D:  [2019-04-09 11:22:44 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Creating TokenRequest... Android 28
D:  [2019-04-09 11:22:44 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Requesting token... Android 28
D:  [2019-04-09 11:22:44 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Performing token request... Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Getting TokenResult from HttpResponse... Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Init: TokenResult Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Saving tokens... Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Creating Account Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Creating account from TokenResponse... Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Init: MicrosoftAccount Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Using Subject as uniqueId Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] The preferred username is not returned from the IdToken. Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] realm is not returned from server. Use utid as realm. Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Init: MicrosoftStsAccount Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] alternative_account_id: null Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] alternative_account_id was null. Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Avatar URL: null Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Avatar URL was null. Android 28
D:  [2019-04-09 11:22:45 - {"thread_id":"360","correlation_id":"270f3416-1332-42e4-8672-c8ae748c0006"}] Interactive request failed with Exception Android 28
    java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
        at com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter.getExpiresOn(MicrosoftStsAccountCredentialAdapter.java:231)
        at com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter.createAccessToken(MicrosoftStsAccountCredentialAdapter.java:78)
        at com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter.createAccessToken(MicrosoftStsAccountCredentialAdapter.java:45)
  ...
like image 769
Ugurcan Yildirim Avatar asked Apr 08 '19 15:04

Ugurcan Yildirim


People also ask

What is B2C Msal?

The Microsoft Authentication Library for JavaScript (MSAL. js) enables JavaScript developers to authenticate users with social and local identities using Azure Active Directory B2C (Azure AD B2C).

What is Msal cache?

MSAL maintains a token cache (or two caches for confidential client applications) and caches a token after it's been acquired. In many cases, attempting to silently get a token will acquire another token with more scopes based on a token in the cache.

What is Microsoft authentication library Msal?

The Microsoft Authentication Library (MSAL) enables developers to acquire security tokens from the Microsoft identity platform to authenticate users and access secured web APIs. It can be used to provide secure access to Microsoft Graph, other Microsoft APIs, third-party web APIs, or your own web API.

What is Msal iframe?

The Microsoft Authentication Library for JavaScript (MSAL. js) uses hidden iframe elements to acquire and renew tokens silently in the background. Azure AD returns the token back to the registered redirect_uri specified in the token request(by default this is the app's root page).


1 Answers

The browser tab should close automatically when the auth succeeds and Azure AD B2C calls back to the app. It's possible that you might mis-configured the app or their is a bug in the specific browser you're using (we've seen this before on smaller browsers, so the data could help).

With respect to Azure AD B2C, I'd highly discourage using WebViews as Google and other identity providers explicitly disable WebView support.

I'd recommend you to enable logging and share them with me and file an issue on the library if needed(https://github.com/AzureAD/microsoft-authentication-library-for-android/wiki).

like image 71
Mohit Verma Avatar answered Oct 11 '22 18:10

Mohit Verma