MaikuB / flutter_appauth

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

authorizeAndExchangeCode does not return to Flutter on iOS build #457

Closed ma-vai closed 8 months ago

ma-vai commented 8 months ago

On our iOS build, the call to await authorizeAndExchangeCode(AuthorizationTokenRequest request) never returns back to Flutter with the AuthorizationTokenResponse object.

Here is our example call:

    final String outlookClientId = "<Our Azure Registered App Client Id>;
    final String outlookRedirectUrl = "ourcustomscheme://auth-callback";
    final outlookAuthServiceConfiguration = const AuthorizationServiceConfiguration(
      authorizationEndpoint: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
      tokenEndpoint: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
    );

    try {
      FlutterAppAuth appAuth = const FlutterAppAuth();
      debugPrint("CALLING MS AUTH");
      final AuthorizationTokenResponse? result = await appAuth.authorizeAndExchangeCode(
        AuthorizationTokenRequest(
          outlookClientId,
          outlookRedirectUrl,
          scopes: outlookCalendarScopes,
          serviceConfiguration: outlookAuthServiceConfiguration,
          //preferEphemeralSession: true,
        ),
      );
      debugPrint("DONE CALLING MS AUTH");
    } on Exception catch (e) {
      if (kDebugMode) debugPrint("Error performing Microsoft auth: ${e.toString()}");
    } catch (e) {
      if (kDebugMode) debugPrint("Error performing Microsoft auth: ${e.toString()}");
    }

The CALLING MS AUTH is correctly printed.

When authorizeAndExchangeCode is called, the plugin properly navigates us to the Microsoft login flow. However, upon successful login of a regular Outlook email account, the popped browser goes away and then nothing happens.

The DONE CALLING MS AUTH debug print is never called and so we are never able to get the token information from the result.

The catch blocks to not trigger either nor is anything printed to the console by anything else.

This same code works correctly in our Android build, but we cannot get it going in the iOS build.

Here is our relevant edit to our Info.plist file

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>ourcustomscheme</string>
            </array>
        </dict>
    </array>

We have also double-checked that our redirectURL is correctly registered in the "Authentication" section of the registered Application in Azure, which we can confirm via the working Android build.

This setup can be copied by anyone by making an Azure registered app and using its clientId and registering the redirect url with it, and using the serviceConfiguration URLs from the code above.

Any help would be greatly appreciated. Thanks!

Flutter Doctor (verbose)

[✓] Flutter (Channel stable, 3.13.7, on macOS 13.6 22G120 darwin-arm64, locale en-US)
    • Flutter version 3.13.7 on channel stable at /Users/developer/dev/flutter_sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2f708eb839 (3 days ago), 2023-10-09 09:58:08 -0500
    • Engine revision a794cf2681
    • Dart version 3.1.3
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/developer/Library/Android/sdk
    • Platform android-33, build-tools 33.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A240d
    • CocoaPods version 1.12.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.83.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.74.0

[✓] Connected device (3 available)
    • Test’s iPhone (mobile) • ########-################ • ios            • iOS 17.0.1 21A340
    • macOS (desktop)          • macos                     • darwin-arm64   • macOS 13.6 22G120 darwin-arm64
    • Chrome (web)             • chrome                    • web-javascript • Google Chrome 117.0.5938.149

[✓] Network resources
    • All expected network resources are available.

• No issues found!
MaikuB commented 8 months ago

This is something you would need to check on your end as there's nothing further within the plugin around this. Did you check also check the FAQs to see if this relates to what you're experiencing https://github.com/MaikuB/flutter_appauth/tree/master/flutter_appauth#faqs?

ma-vai commented 8 months ago

@MaikuB , the trailing slash fixed it. Thanks for pointing us in that direction, we missed the FAQ blurb.