vmurin / react-native-azure-auth

React Native library implementing Azure AD OAuth2 API
MIT License
85 stars 65 forks source link

Getting Null value when trying to refresh a token for an Azure B2C authentication #133

Closed GeraltRivvia closed 2 years ago

GeraltRivvia commented 2 years ago

I am using the code below to login and authenticate with B2C and it works as expected.

onLogin = async () => { try {
let tokens = await azureAuth.webAuth.authorize({scope: 'openid profile', p: 'B2C_signupandsignin' ,prompt:'login'}) console.log('CRED>>>', tokens); var info = jwt_decode(tokens.rawIdToken); this.setState({ accessToken: tokens.rawIdToken, userId: tokens.userId, user: ''});
} catch (error) { Alert.alert('Error during Azure operation', error); } }

However, when I try to refresh the token using the acquireTokenSilent method, the token only gets a null value. The same code works for B2B authentication though since a new token is being generated.

The method only accepts the scope and the userId as parameters and maybe it is returning null since the token endpoint for B2C has the policy as an additional parameter.

_refreshToken = async () => { let userIdFromToken = await AsyncStorage.getItem('user_id'); // value from tokens

    // Try to get cached token or refresh an expired ones
    let tokens = await azureAuth.auth.acquireTokenSilent({scope: 'openid profile', userId: userIdFromToken});

    console.log("tokens CRED>>" + tokens);

    if (!tokens) 
    {
      // No cached tokens or the requested scope defines new not yet consented permissions
      // Open a window for user interaction
      //prompt user to login
    }             
}

I tried looking for the refresh token to pass this manually to the token endpoint using a separate fetch but it is not available on the token attributes/properties when I looked and as mentioned on the past threads, the refreshtoken is not available.

Is there a way around this? Or maybe I am just missing something?

Thank you.

GeraltRivvia commented 2 years ago

Hello. Just bumping this up.

vmurin commented 2 years ago

Hi,

sorry, I don't have any experience with B2C, but what I can surely state - it is not an library issue, but some setting issue or just a standard behavior of Azure for this business case.

As I understood, Azure don't provide a refresh token in your case, so you can not update access token and getting null back. It could be two reasons for this:

  1. you don't provide some needed scope in your authorization request to receive refresh token
  2. or your business case (I mean Azure business case in terms of B2C) does not support refresh tokens at all

I both cases the only solution is to study thoroughly the documentation or ask Azure support for help.

I will close the issue as not related to the library directly. Please feel free to reopen it, if your will find out that the case is caused by some wrong or missing functionality. Thanks!