Generating Access Token through OAuth2 Returns Null in Flutter/Dart

95 views Asked by At

My app is in Flutter/Dart. I am trying to generate an access token through OAuth2 with Azure credentials. Once I login with my credentials, it successfully authorizes, but it doesn't redirect automatically to the app, and when I exit out of page to go back to the app, it prints the token to be null.

Code:

OAuth2Client client = OAuth2Client(
        redirectUri: 'https://oauth.pstmn.io/v1/browser-callback',
        customUriScheme: '${customUri}',
        authorizeUrl: 'https://login.microsoftonline.com/${tenantID}/oauth2/authorize',
        tokenUrl: 'https://login.microsoftonline.com/${tenantID}/oauth2/token',
      );

      try{
        AccessTokenResponse tknResp = await client.getTokenWithAuthCodeFlow(
          clientId: '${clientID}',
          clientSecret: '${clientSecret}',
          scopes: ['api://${tenantID}/Users.Login'],
        );
        print("TOKEN IS: ${await tknResp.tokenType} ${await tknResp.scope} ${await    tknResp.accessToken}");

      }catch(e){
        print("TOKEN ERROR IS: ${e}");
      }

Note: I run it through it Android Studio

Thank you!

I tried running the code above, but I received Null as the Access Token.

1

There are 1 answers

0
Mohamed Irshad On

There is an alternate approach for your Azure auth problem. this one doesn't use OAuth though.

azure_silent_auth

Try this package. but you need to know the client-id, tenant-id and you need to add http://localhost:3000 redirect URI to the azure app authorization on azure portal.

This package has solution for one of your issue. i,e not redirecting back to the application after authentication. use the PCAuthenticator instead of the DefaultAuthenticator provided in the example.

PCAuthenticator creates a separate window for loading the auth screen instead of redirecting it to the browser. also this new window is closed after authentication is done. you can modify this and create your own custom authenticator and use it in AzureAuth constructor too if you wish.

as far as your second problem, after successful login you can get the access token using AzureAuth->getAccessToken when ever and where ever you want. it even refresh the token for you if it is expired.

This package by default saves the access token securely, so that you can do a silent login the next time using AzureAuth->silentLogin.

Hope this helps.

class AuthenticationHandler {
  static final AuthenticationHandler _instance =
      AuthenticationHandler._internal();

  factory AuthenticationHandler() {
    return _instance;
  }

  AuthenticationHandler._internal();

  // !! USE PCAuthenticator here instead of DefaultAuthenticator
  final AzureAuth _microsoftAuthenticator = AzureAuth(
    authenticatorProvider: DefaultAuthenticator(
      'https://login.microsoftonline.com/{tenant-id}/',
      ["openId", "offline_access"],
      '{client-id}',
      '&prompt=select_account',
      3000, // localhost redirect uri port
    ),
  );

  Future<void> login() async {
    await _microsoftAuthenticator.login();
  }

  Future<void> silentLogin() async {
    await _microsoftAuthenticator.silentLogin();
  }

  Future<void> logout() async {
    await _microsoftAuthenticator.logout();
  }
}