Flutter AppAuth
flutter_appauth plugin wraps the native iOS and Android AppAuth libraries. As a standard OpenID Connect client, it seamlessly integrates with Choreo Identity & Access Management via discovery. Once configured, the underlying AppAuth library natively manages the complex authentication flows, including browser hand-offs, PKCE, and token requests. A ready-made sample app is also provided to demonstrate this implementation in action.
flutter_appauth- Plugin by the flutter_appauth authors
Get started
Works with a new Flutter application or one you already have. Requires a running Choreo Identity & Access Management instance; the steps use https://localhost:8090, the default for a local install. The scheme com.example.appauth is a placeholder: use your own, in lowercase, everywhere it appears. To start from working code instead, download the sample app above, which runs every step of this guide; its README covers setting it up.
1. Create the application in Choreo Identity & Access Management
A Flutter application is a public client: it has no client secret, and Choreo Identity & Access Management requires PKCE for it. The authorization code and refresh token grants are already enabled.
In the Console: Open the Console at https://localhost:8090/console and navigate to Applications › Click Add Application and select Flutter › Enter a name, select the sign-in options, and select a theme › Under Sign-In Approach, select Redirect to Choreo Identity & Access Management Gate › Under Deep Link / Universal Link, enter com.example.appauth://callback › Click Create, then copy the Client ID
Select the redirect approach, not Bring Your Own UI. flutter_appauth signs users in through the system browser, so it needs the Choreo Identity & Access Management-hosted sign-in pages.
2. Allow sign-out and user claims
Two settings on the new application. The post-logout redirect URI must match the one the application sends exactly. The attributes decide which claims reach the ID token: with none selected, only sub is returned, even when the profile and email scopes are granted.
In the Console: On the Advanced tab, add com.example.appauth://logout under Post-Logout Redirect URIs and save › On the Token tab, open User, then ID Token, and add the attributes you need (for example email and name) › Add the same attributes under User Info Endpoint if you call the userinfo endpoint
3. Add flutter_appauth
Create a project first with flutter create, or skip that and add the plugin to the project you already have. The sample stores the refresh token with flutter_secure_storage; use any secure store you prefer.
flutter create --org com.example my_app
cd my_app
flutter pub add flutter_appauth flutter_secure_storage
4. Register the redirect scheme on Android
AppAuth's redirect activity receives the browser's callback through this scheme. It covers both the sign-in and the post-logout redirect, since they share it.
android {
defaultConfig {
manifestPlaceholders.putAll(
mapOf("appAuthRedirectScheme" to "com.example.appauth")
)
}
}
Keep the scheme in lowercase; the plugin's authors report failed redirects otherwise. On a Groovy build.gradle, use manifestPlaceholders += ['appAuthRedirectScheme': 'com.example.appauth'].
5. Let the redirect return to the application on Android
Recent flutter create templates set android:taskAffinity="" on MainActivity. That puts AppAuth's redirect handling in a separate task, so the browser returns to the application but sign-in never completes, and Logcat shows "No stored state - unable to handle response". Remove the attribute.
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
...>
<!-- No android:taskAffinity="" here. -->
</activity>
6. Register the redirect scheme on iOS
Add the same scheme as a URL type of the Runner target.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.example.appauth</string>
</array>
</dict>
</array>
7. Point the application at your Choreo Identity & Access Management issuer
AppAuth reads every endpoint from the discovery document, so the issuer, the client ID, and the two redirect URIs are all the configuration there is. A refresh token is issued without the offline_access scope, so the scopes stay at the OpenID Connect basics.
class AuthConfig {
static const issuer = 'https://localhost:8090';
static const clientId = '<your-client-id>';
static const discoveryUrl = '$issuer/.well-known/openid-configuration';
static const redirectUrl = 'com.example.appauth://callback';
static const postLogoutRedirectUrl = 'com.example.appauth://logout';
static const scopes = ['openid', 'profile', 'email'];
}
8. Sign in
One call opens the Choreo Identity & Access Management sign-in page in the system browser (ASWebAuthenticationSession on iOS, a Custom Tab on Android), then exchanges the authorization code for tokens. AppAuth generates the PKCE verifier and S256 challenge itself; Choreo Identity & Access Management supports only S256 and rejects requests without a challenge.
import 'package:flutter_appauth/flutter_appauth.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'auth_config.dart';
const appAuth = FlutterAppAuth();
const storage = FlutterSecureStorage();
Future<TokenResponse?> signIn() async {
try {
final response = await appAuth.authorizeAndExchangeCode(
AuthorizationTokenRequest(
AuthConfig.clientId,
AuthConfig.redirectUrl,
discoveryUrl: AuthConfig.discoveryUrl,
scopes: AuthConfig.scopes,
),
);
await storage.write(key: 'refresh_token', value: response.refreshToken);
return response;
} on FlutterAppAuthUserCancelledException {
return null; // The user closed the browser.
}
}
The response carries the access token, its expiry, the ID token, and the refresh token. AppAuth validates the ID token's issuer, audience, and nonce before returning it.
9. Refresh the tokens
Exchange the refresh token for new tokens before the access token expires. Choreo Identity & Access Management rotates refresh tokens: each refresh revokes the one it used, and reusing a revoked token revokes the whole chain. Always store the token from the latest response.
Future<TokenResponse> refresh() async {
final response = await appAuth.token(
TokenRequest(
AuthConfig.clientId,
AuthConfig.redirectUrl,
discoveryUrl: AuthConfig.discoveryUrl,
refreshToken: await storage.read(key: 'refresh_token'),
scopes: AuthConfig.scopes,
),
);
await storage.write(key: 'refresh_token', value: response.refreshToken);
return response;
}
Rotation does not extend the session: a new refresh token keeps the original expiry, 24 hours by default. When a refresh fails, sign the user in again.
10. Sign out
Ending the session in the browser signs the user out of Choreo Identity & Access Management as well as the application, so the next sign-in asks for credentials again. Passing the ID token as a hint lets Choreo Identity & Access Management skip the confirmation prompt.
Future<void> signOut(String idToken) async {
try {
await appAuth.endSession(
EndSessionRequest(
idTokenHint: idToken,
postLogoutRedirectUrl: AuthConfig.postLogoutRedirectUrl,
discoveryUrl: AuthConfig.discoveryUrl,
),
);
} finally {
await storage.delete(key: 'refresh_token');
}
}
11. Trust the local certificate
A local Choreo Identity & Access Management serves a self-signed certificate from <THUNDERID_HOME>/config/certs/server.cert. Both the system browser and AppAuth's own HTTP client reject it until the device trusts it, and flutter_appauth's allowInsecureConnections option does not help, since it only permits plain http. Run these once per emulator or simulator.
Android
# Forward the emulator's localhost:8090 to your machine.
adb reverse tcp:8090 tcp:8090
# Copy the certificate, then install it on the emulator: Settings > Security & privacy >
# More security & privacy > Encryption & credentials > Install a certificate > CA certificate,
# and select thunderid-dev.crt from Downloads. Older Android versions list
# Encryption & credentials directly under Settings > Security.
adb push <THUNDERID_HOME>/config/certs/server.cert /sdcard/Download/thunderid-dev.crt
Android debug config
<?xml version="1.0" encoding="utf-8"?>
<!-- Reference it from android/app/src/debug/AndroidManifest.xml:
<application android:networkSecurityConfig="@xml/network_security_config"/> -->
<network-security-config>
<debug-overrides>
<trust-anchors>
<certificates src="user" />
</trust-anchors>
</debug-overrides>
</network-security-config>
iOS Simulator
xcrun simctl keychain booted add-root-cert <THUNDERID_HOME>/config/certs/server.cert
Development only. Android apps ignore user-installed certificates unless the debug configuration above opts in, and keeping it in the debug source set leaves release builds untouched. In production, serve Choreo Identity & Access Management with a certificate from a public certificate authority and skip this step.
Endpoint mapping
Where each flutter_appauth call lands on Choreo Identity & Access Management. AppAuth resolves every endpoint from the discovery document, so none of these paths appear in your code.
| flutter_appauth call | Choreo Identity & Access Management endpoint | Notes |
|---|---|---|
| discoveryUrl | /.well-known/openid-configuration | Fetched before every call. The issuer is the server URL, with no path. |
| authorizeAndExchangeCode() | /oauth2/authorize | Opened in the system browser. A code_challenge (S256) is required for public clients. |
| authorizeAndExchangeCode() | /oauth2/token | Code exchange with the code_verifier. No client secret is sent. |
| token() | /oauth2/token | refresh_token grant. Returns a new refresh token and, with openid, a new ID token. |
| endSession() | /oauth2/logout | Opened in the system browser. post_logout_redirect_uri must be registered. |
Requirements
| | |
|---|---|
| flutter_appauth | >= 12.0 (supported) |
| Flutter | >= 3.38.1 (supported) |
| Android | API 24+ (supported) |
| iOS | 13+ (supported) |
Where to get help
Two projects, two places to ask.
Something wrong in this guide?
The guide and the Choreo Identity & Access Management configuration it documents are ours, along with the sample application that goes with it.
Choreo Identity & Access Management issues · Ask the community
Something wrong in flutter_appauth?
The plugin itself is maintained by its own authors. Bugs and feature requests belong on their tracker, not ours.
Was this page helpful?