flutter_appauth

repository·master·Indexed 43 days ago

https://github.com/maikub/flutter_appauth

A Flutter plugin providing a unified interface for native AppAuth SDKs to enable OAuth2 and OpenID Connect authentication and authorization. It supports authorization code exchange, token refreshing, and session termination via the FlutterAppAuth class, with specific configurations for Android and iOS/macOS redirect schemes.

Tokens
8.3K
Snippets
26
Records
34
Agent score
55%

What's inside flutter_appauth

  1. Use Ephemeral Sessions on iOS and macOS

    master

    On iOS (13+) and macOS, you can use preferEphemeralSession = true to start an ephemeral browser session. This prevents the system warning: "app_name" Wants to Use "domain_name" to Sign In.

    Important: If you use preferEphemeralSession = true for the sign-in call, you must also use it for the endSession call; otherwise, the login session will remain active in the browser.

  2. Configure Android redirect scheme via AndroidManifest.xml

    master

    Alternatively, you can configure the redirect URI directly by adding an intent-filter for net.openid.appauth.RedirectUriReceiverActivity to your AndroidManifest.xml. This method allows you to specify both a scheme and a host.

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.example.my_app">
    ...
    <activity
            android:name="net.openid.appauth.RedirectUriReceiverActivity"
            android:theme="@style/Theme.AppCompat.Translucent.NoTitleBar"
            android:exported="true"
            tools:node="replace">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:scheme="<your_custom_scheme>"
                  android:host="<your_custom_host>"/>
        </intent-filter>
    </activity>
    ...
  3. Configure Android redirect scheme via build.gradle

    master

    To enable redirects for the AppAuth flow on Android, you must specify a custom scheme in your app's build.gradle or build.gradle.kts file using manifestPlaceholders.

    Important:

    • Ensure <your_custom_scheme> is entirely lowercase to avoid redirect issues.
    • Use the += operator instead of = when modifying manifestPlaceholders to avoid errors related to multidex in newer Flutter SDK versions.

    If you encounter the error Attribute application@name at AndroidManifest.xml... requires a placeholder substitution, ensure you are using +=.

    android {
        ... 
        defaultConfig {
            ... 
            manifestPlaceholders.putAll(
                mapOf(
                    "appAuthRedirectScheme" to "<your_custom_scheme>"
                )
            )
        }
    }
  4. Configure iOS/macOS redirect scheme via Info.plist

    master

    To specify the custom scheme for iOS or macOS, add a CFBundleURLTypes entry to your Info.plist file. Replace <your_custom_scheme> with your desired lowercase scheme.

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string><your_custom_scheme></string>
            </array>
        </dict>
    </array>
  5. Use the flutter_appauth_example project

    master
    The flutter_appauth_example project serves as a reference implementation demonstrating how to integrate and use the flutter_appauth plugin within a Flutter application. It is designed as a starting point for developers looking to implement OAuth2/OpenID Connect flows using this plugin.
  6. Customize the iOS launch screen assets

    master

    To change the image displayed during the app's launch on iOS, you can either replace the existing image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode to manage the assets visually.

    To use Xcode:

    1. Open the iOS workspace using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your new image assets into the asset catalog.
    open ios/Runner.xcworkspace
  7. Troubleshoot Android redirect issues

    master

    No Redirect to app after login

    If the authorization flow does not return to your Flutter app even though the intent filter is set and RedirectUriReceiverActivity is invoked, check your AndroidManifest.xml for the attribute android:taskAffinity="". Removing this attribute typically resolves the issue.

  8. Handle user cancellation and platform errors

    master

    Detecting User Cancellation

    When a user cancels the browser session, the plugin throws a FlutterAppAuthUserCancelledException. This should be handled gracefully rather than treated as a critical error.

    Handling Platform Errors

    For other errors originating from the underlying platform SDKs, catch FlutterAppAuthPlatformException. You can access detailed error information, including RFC-compliant error codes, via the details property of type FlutterAppAuthPlatformErrorDetails.

    try {
      await appAuth.authorize(...);
    } on FlutterAppAuthUserCancelledException catch (e) {
      // Handle user cancellation gracefully
    } on FlutterAppAuthPlatformException catch (e) {
      final FlutterAppAuthPlatformErrorDetails details = e.details;
      // Handle exceptions based on error codes from AppAuth
    } catch (e) {
      // Handle other errors
    }
  9. Troubleshoot Azure B2C/AD redirect issues on iOS

    master

    Redirect failing on iOS (Azure B2C/AD)

    If the login request redirects properly on Android but fails on iOS when using Azure B2C or Azure AD, it is likely due to the AppAuth iOS SDK's redirect URL validation logic.

    Fix: Add a trailing slash to the redirect URL specified in your code.