MaikuB / flutter_appauth

A Flutter wrapper for AppAuth iOS and Android SDKs
269 stars 239 forks source link

Don't receive the access Token. #368

Open angelocacaceMSC opened 1 year ago

angelocacaceMSC commented 1 year ago

Hi, i'm using the library. I'm trying to the authorizeAndExchangeCode, but the function return me only the idToken. I need the accessToken but it is null.

`final AuthorizationTokenResponse? result =
          await _appAuth.authorizeAndExchangeCode(
        AuthorizationTokenRequest(_clientId, _redirectUrl,
            serviceConfiguration: _serviceConfiguration,
            scopes: _scopes,
            promptValues: ['login']),
      );
      print("${result?.idToken}");
      print("ACCESS TOKEN: ${result!.accessToken}");

      if (result != null) {
        _processAuthTokenResponse(result);
        // await _testApi(result);
      }
    } catch (_) {
      _clearBusyState();
    }
  }`

Someone had the same problem? How i can resolve?

vicdotdevelop commented 1 year ago

What scopes are you passing @angelocacaceMSC ?

angelocacaceMSC commented 1 year ago

What scopes are you passing @angelocacaceMSC ?

final List<String> _scopes = <String>[ 'openid', 'offline_access', ];

I'm passing this scopes @vicdotdevelop .

angelocacaceMSC commented 1 year ago

@MaikuB can you tell me why i receive a null access token? Which parameter should I check?

MaikuB commented 1 year ago

Not something I can help you with. OAuth in general depends on how you configure things and what is expected by the identity provider (IdP) you use so this would be something you'd need to look further yourself. There could be things in the IdP's documentation you need to refer to

Dahkenangnon commented 1 year ago

@MaikuB can you tell me why i receive a null access token? Which parameter should I check?

Hello @angelocacaceMSC

Is you issue resolve ? If not, can you tel me which identity provider you use ? Is internal ou a public identify provider ?

These details can help .

Dahkenangnon commented 1 year ago

I recommend you also to look your client configuration on your Identity provider. Have you configure the right grant_type, the right scope ? Can this client request for any acess_token ?

If for example, you're using Google Sign in, the purpose of this is not to request any api, it just 'Login With', and thus, no need for the IdP to issue accessToken.

Like I said previously, it's will be helpful to have:

  1. Your flutter configurations
  2. How you configure your app on the authorization server
  3. Etc.
angelocacaceMSC commented 1 year ago

Hi @Dahkenangnon, I solved it regarding android devices. Now I am able to login correctly and receive the tokens.

But when I started testing on iOS devices I found other problems. In particular, when I try to reach the Azure page, by clicking the "Login" button my app crashes.

I configured the Info.plist file as suggested, and also tried to edit the AppDelegate.swift file, but without finding the right match.

I am sure that the problem is precisely the configuration between Info.plist, AppDelegate.swift and Azure portal.

Have you ever had any of these problems? Could you give me some help?

Dahkenangnon commented 1 year ago

Hi @angelocacaceMSC

Nice to hear it work now for Android.

About iOs, can you give more details please ?

angelocacaceMSC commented 1 year ago

@Dahkenangnon Of course!

Basically I have always tested on an Android device, but when I had to test on iOS I encountered the following problems.

I have a simple home page, with a "Login" button that redirects on the web to the Azure portal for login. For Android everything works, but for iOS the app crashes as soon as I click the Login button.

Xcode informs me that there has been an unexpected breakdown of the Runner.

In a first phase I had not yet updated the Info.plist file with all the information necessary to make the library work. From the Azure portal I created the app for iOS devices and retrieved the information related to redirectUri, clientID, etc ...

I have found some suggestions regarding the changes in the AppDelegate.swif file, but despite that it doesn't work.

The steps I followed were the following:

Register your app

  1. Create App Registration in Azure Portal

    • In Authentication, add iOS platform and fill in your bundle id
    • Make note of the MSAL Configuration
  2. Add Keychain Sharing capability

    • In Xcode, under your applications Signing and Capabilities, add Keychain Sharing
    • Keychain Group should be com.microsoft.adalcache
    • Completely fine to have multiple Keychain Groups
    • This allows MSAL to use the keychain to share Microsoft Authentication sessions
  3. Set up URL Schemes Add the following CFBundleURLTypes to your Info.plist file. Remember to replace the bundle id.

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>msauth.[BUNDLE_ID]</string>
        </array>
    </dict>
</array>
  1. Allow MSAL to use Microsoft Authenticator if it is installed
    • Add the following LSApplicationQueriesSchemes to your Info.plist file.
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>msauthv2</string>
    <string>msauthv3</string>
</array>
  1. Handle the redirect callback
    • Import MSAL
  ...
  import MSAL
  ...
  1. Ensure that the minimum target is set to iOS 11

But the app continue to crash on Login.