flutter_appauth 13.2.0
flutter_appauth: ^13.2.0 copied to clipboard
This plugin provides an abstraction around the Android and iOS AppAuth SDKs so it can be used to communicate with OAuth 2.0 and OpenID Connect providers
import 'dart:convert';
import 'dart:io' show Platform;
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_appauth/flutter_appauth.dart';
import 'package:http/http.dart' as http;
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
bool _isBusy = false;
bool _isAuthFlowInProgress = false;
final FlutterAppAuth _appAuth = const FlutterAppAuth();
String? _codeVerifier;
String? _nonce;
String? _authorizationCode;
String? _refreshToken;
String? _accessToken;
String? _idToken;
String? _error;
final TextEditingController _authorizationCodeTextController =
TextEditingController();
final TextEditingController _accessTokenTextController =
TextEditingController();
final TextEditingController _accessTokenExpirationTextController =
TextEditingController();
final TextEditingController _idTokenTextController = TextEditingController();
final TextEditingController _refreshTokenTextController =
TextEditingController();
String? _userInfo;
// For a list of client IDs, go to https://demo.duendesoftware.com
final String _clientId = 'interactive.public';
final String _redirectUrl = 'com.duendesoftware.demo:/oauthredirect';
final String _issuer = 'https://demo.duendesoftware.com';
final String _discoveryUrl =
'https://demo.duendesoftware.com/.well-known/openid-configuration';
final String _postLogoutRedirectUrl = 'com.duendesoftware.demo:/';
final List<String> _scopes = <String>[
'openid',
'profile',
'email',
'offline_access',
'api'
];
final AuthorizationServiceConfiguration _serviceConfiguration =
const AuthorizationServiceConfiguration(
authorizationEndpoint: 'https://demo.duendesoftware.com/connect/authorize',
tokenEndpoint: 'https://demo.duendesoftware.com/connect/token',
endSessionEndpoint: 'https://demo.duendesoftware.com/connect/endsession',
);
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
/*
On Android, the OS can recreate the host Activity (and with it this
Flutter engine/widget tree) while the authorization browser is in the
foreground, e.g. to reclaim memory. When that happens the auth result
arrives natively with nothing on the Dart side awaiting it, so it's
stored and can be retrieved once the app has reinitialized by calling
resumePendingAuthorization().
In these cases, the initState invocation
and the app's resumed state have no reliable order, so
resumePendingAuthorization is called from both events, see
didChangeAppLifecycleState.
To test this on Android, enable "Don't keep activities" in the
developer options, start a sign in flow and then complete it. The
Activity will have been destroyed and recreated in the background.
Always returns null on other platforms, so can be called unconditionally.
*/
_resumePendingAuthorization();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_resumePendingAuthorization();
if (_isAuthFlowInProgress) {
setState(() {
_isAuthFlowInProgress = false;
_clearBusyState();
});
}
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Visibility(
visible: _isBusy,
child: const LinearProgressIndicator(),
),
const SizedBox(height: 8),
ElevatedButton(
child: const Text('Sign in with no code exchange'),
onPressed: () => _signInWithNoCodeExchange(),
),
const SizedBox(height: 8),
ElevatedButton(
child: const Text(
'Sign in with no code exchange and generated nonce'),
onPressed: () => _signInWithNoCodeExchangeAndGeneratedNonce(),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _authorizationCode != null ? _exchangeCode : null,
child: const Text('Exchange code'),
),
const SizedBox(height: 8),
ElevatedButton(
child: const Text('Sign in with auto code exchange'),
onPressed: () => _signInWithAutoCodeExchange(),
),
if (Platform.isAndroid)
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
child: const Text(
'Resume pending authorization',
textAlign: TextAlign.center,
),
onPressed: () => _resumePendingAuthorization(),
),
),
if (Platform.isIOS || Platform.isMacOS)
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
child: const Text(
'Sign in with auto code exchange using ephemeral '
'session',
textAlign: TextAlign.center,
),
onPressed: () => _signInWithAutoCodeExchange(
externalUserAgent: ExternalUserAgent
.ephemeralAsWebAuthenticationSession),
),
),
if (Platform.isIOS) ...[
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
child: const Text(
'Sign in with auto code exchange using '
'SFSafariViewController',
textAlign: TextAlign.center,
),
onPressed: () => _signInWithAutoCodeExchange(
externalUserAgent:
ExternalUserAgent.sfSafariViewController),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
child: const Text(
'Auto code exchange using system browser (iOS only)',
textAlign: TextAlign.center,
),
onPressed: () => _signInWithAutoCodeExchange(
externalUserAgent:
ExternalUserAgent.customBrowserSafari),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
child: const Text(
'Auto code exchange using Chrome (iOS only)',
textAlign: TextAlign.center,
),
onPressed: () => _signInWithAutoCodeExchange(
externalUserAgent:
ExternalUserAgent.customBrowserChrome),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
child: const Text(
'Auto code exchange using Firefox (iOS only)',
textAlign: TextAlign.center,
),
onPressed: () => _signInWithAutoCodeExchange(
externalUserAgent:
ExternalUserAgent.customBrowserFirefox),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
child: const Text(
'Auto code exchange using Opera (iOS only)',
textAlign: TextAlign.center,
),
onPressed: () => _signInWithAutoCodeExchange(
externalUserAgent:
ExternalUserAgent.customBrowserOpera),
),
),
],
ElevatedButton(
onPressed: _refreshToken != null ? _refresh : null,
child: const Text('Refresh token'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _idToken != null
? () async {
await _endSession();
}
: null,
child: const Text('End session'),
),
if (Platform.isIOS || Platform.isMacOS)
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: _idToken != null
? () async {
await _endSession(
externalUserAgent: ExternalUserAgent
.ephemeralAsWebAuthenticationSession);
}
: null,
child:
const Text('End session using ephemeral session'),
)),
if (Platform.isIOS)
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: _idToken != null
? () async {
await _endSession(
externalUserAgent: ExternalUserAgent
.sfSafariViewController);
}
: null,
child: const Text(
'End session using SFSafariViewController'),
)),
const SizedBox(height: 8),
if (_error != null) Text(_error ?? ''),
const SizedBox(height: 8),
const Text('authorization code'),
TextField(
controller: _authorizationCodeTextController,
),
const Text('access token'),
TextField(
controller: _accessTokenTextController,
),
const Text('access token expiration'),
TextField(
controller: _accessTokenExpirationTextController,
),
const Text('id token'),
TextField(
controller: _idTokenTextController,
),
const Text('refresh token'),
TextField(
controller: _refreshTokenTextController,
),
const Text('test api results'),
Text(_userInfo ?? ''),
],
),
),
),
),
);
}
Future<void> _endSession(
{ExternalUserAgent externalUserAgent =
ExternalUserAgent.asWebAuthenticationSession}) async {
try {
_setBusyState();
await _appAuth.endSession(EndSessionRequest(
idTokenHint: _idToken,
postLogoutRedirectUrl: _postLogoutRedirectUrl,
serviceConfiguration: _serviceConfiguration,
externalUserAgent: externalUserAgent));
_clearSessionInfo();
} catch (e) {
_handleError(e);
} finally {
_clearBusyState();
}
}
void _clearSessionInfo() {
setState(() {
_codeVerifier = null;
_nonce = null;
_authorizationCode = null;
_authorizationCodeTextController.clear();
_accessToken = null;
_accessTokenTextController.clear();
_idToken = null;
_idTokenTextController.clear();
_refreshToken = null;
_refreshTokenTextController.clear();
_accessTokenExpirationTextController.clear();
_userInfo = null;
});
}
Future<void> _refresh() async {
try {
_setBusyState();
final TokenResponse result = await _appAuth.token(TokenRequest(
_clientId, _redirectUrl,
refreshToken: _refreshToken, issuer: _issuer, scopes: _scopes));
_processTokenResponse(result);
await _testApi(result);
} catch (e) {
_handleError(e);
} finally {
_clearBusyState();
}
}
Future<void> _exchangeCode() async {
try {
_setBusyState();
final TokenResponse result = await _appAuth.token(TokenRequest(
_clientId, _redirectUrl,
authorizationCode: _authorizationCode,
discoveryUrl: _discoveryUrl,
codeVerifier: _codeVerifier,
nonce: _nonce,
scopes: _scopes));
_processTokenResponse(result);
await _testApi(result);
} catch (e) {
_handleError(e);
} finally {
_clearBusyState();
}
}
Future<void> _signInWithNoCodeExchange() async {
try {
_setBusyState();
/*
The discovery endpoint (_discoveryUrl) is used to find the
configuration. The code challenge generation can be checked here
> https://github.com/MaikuB/flutter_appauth/search?q=challenge.
The code challenge is generated from the code verifier and only the
code verifier is included in the result. This because to get the token
in the method _exchangeCode (see above) we need only the code verifier
and the authorization code.
Code challenge is not used according to the spec
https://www.rfc-editor.org/rfc/rfc7636 page 9 section 4.5.
*/
final AuthorizationResponse result = await _appAuth.authorize(
AuthorizationRequest(_clientId, _redirectUrl,
discoveryUrl: _discoveryUrl, scopes: _scopes, loginHint: 'bob'),
);
/*
or just use the issuer
var result = await _appAuth.authorize(
AuthorizationRequest(
_clientId,
_redirectUrl,
issuer: _issuer,
scopes: _scopes,
),
);
*/
_processAuthResponse(result);
} catch (e) {
_handleError(e);
} finally {
_clearBusyState();
}
}
Future<void> _signInWithNoCodeExchangeAndGeneratedNonce() async {
try {
_setBusyState();
final Random random = Random.secure();
final String nonce =
base64Url.encode(List<int>.generate(16, (_) => random.nextInt(256)));
// use the discovery endpoint to find the configuration
final AuthorizationResponse result = await _appAuth.authorize(
AuthorizationRequest(_clientId, _redirectUrl,
discoveryUrl: _discoveryUrl,
scopes: _scopes,
loginHint: 'bob',
nonce: nonce),
);
_processAuthResponse(result);
} catch (e) {
_handleError(e);
} finally {
_clearBusyState();
}
}
Future<void> _resumePendingAuthorization() async {
// Only clear the busy state if this invocation set it. This is called on
// every app resume, so a null result must not clear the busy state of a
// regular sign in flow (or another resume invocation) that is in progress.
bool didSetBusyState = false;
try {
// If a pending authorizeAndExchangeCode() flow is resumed, the code
// exchange happens as part of this call. Pass the same clientSecret and
// allowInsecureConnections values as for the original request here, if
// your app uses them.
final AuthorizationResumeResponse? result =
await _appAuth.resumePendingAuthorization();
// Returns null when there was nothing pending, e.g. on a normal app
// start rather than a resumption after the Activity was recreated.
if (result == null) {
return;
}
_setBusyState();
didSetBusyState = true;
// The response depends on whether the pending flow was started with
// authorize() or authorizeAndExchangeCode(). The response can be handled
// just like any standard sign in response.
switch (result) {
case AuthorizationResumeResponseAuthorize(:final response):
_processAuthResponse(response);
case AuthorizationResumeResponseToken(:final response):
_processAuthTokenResponse(response);
await _testApi(response);
}
} catch (e) {
_handleError(e);
} finally {
if (didSetBusyState) {
_clearBusyState();
}
}
}
bool _isCustomBrowser(ExternalUserAgent externalUserAgent) {
return externalUserAgent == ExternalUserAgent.customBrowserSafari ||
externalUserAgent == ExternalUserAgent.customBrowserChrome ||
externalUserAgent == ExternalUserAgent.customBrowserFirefox ||
externalUserAgent == ExternalUserAgent.customBrowserOpera;
}
Future<void> _signInWithAutoCodeExchange(
{ExternalUserAgent externalUserAgent =
ExternalUserAgent.asWebAuthenticationSession}) async {
try {
_setBusyState(isAuthFlow: _isCustomBrowser(externalUserAgent));
/*
This shows that we can also explicitly specify the endpoints rather than
getting from the details from the discovery document.
*/
final Future<AuthorizationTokenResponse> authRequest =
_appAuth.authorizeAndExchangeCode(
AuthorizationTokenRequest(_clientId, _redirectUrl,
serviceConfiguration: _serviceConfiguration,
scopes: _scopes,
externalUserAgent: externalUserAgent),
);
// Apply timeout only when using an external browser user agent.
final AuthorizationTokenResponse result =
_isCustomBrowser(externalUserAgent)
? await authRequest.timeout(const Duration(minutes: 2))
: await authRequest;
/*
This code block demonstrates passing in values for the prompt
parameter. In this case it prompts the user login even if they have
already signed in. the list of supported values depends on the
identity provider
```dart
final AuthorizationTokenResponse result = await _appAuth
.authorizeAndExchangeCode(
AuthorizationTokenRequest(_clientId, _redirectUrl,
serviceConfiguration: _serviceConfiguration,
scopes: _scopes,
promptValues: ['login']),
);
```
*/
_processAuthTokenResponse(result);
await _testApi(result);
} catch (e) {
_handleError(e);
} finally {
_clearBusyState();
}
}
void _handleError(Object e) {
if (e is FlutterAppAuthUserCancelledException) {
setState(() {
_error = 'The user cancelled the flow!';
});
} else if (e is FlutterAppAuthPlatformException) {
setState(() {
_error = e.platformErrorDetails.toString();
});
} else if (e is PlatformException) {
setState(() {
_error = 'Error\n\nCode: ${e.code}\nMessage: ${e.message}\n'
'Details: ${e.details}';
});
} else {
setState(() {
_error = 'Error: $e';
});
}
}
void _clearBusyState() {
setState(() {
_isBusy = false;
});
}
void _setBusyState({bool? isAuthFlow}) {
setState(() {
_error = '';
_isBusy = true;
if (isAuthFlow != null) {
_isAuthFlowInProgress = isAuthFlow;
}
});
}
void _processAuthTokenResponse(AuthorizationTokenResponse response) {
setState(() {
_accessToken = _accessTokenTextController.text = response.accessToken!;
_idToken = _idTokenTextController.text = response.idToken!;
_refreshToken = _refreshTokenTextController.text = response.refreshToken!;
_accessTokenExpirationTextController.text =
response.accessTokenExpirationDateTime!.toIso8601String();
});
}
void _processAuthResponse(AuthorizationResponse response) {
setState(() {
/*
Save the code verifier and nonce as it must be used when exchanging the
token.
*/
_codeVerifier = response.codeVerifier;
_nonce = response.nonce;
_authorizationCode =
_authorizationCodeTextController.text = response.authorizationCode!;
_isBusy = false;
});
}
void _processTokenResponse(TokenResponse response) {
setState(() {
_accessToken = _accessTokenTextController.text = response.accessToken!;
_idToken = _idTokenTextController.text = response.idToken!;
_refreshToken = _refreshTokenTextController.text = response.refreshToken!;
_accessTokenExpirationTextController.text =
response.accessTokenExpirationDateTime!.toIso8601String();
});
}
Future<void> _testApi(TokenResponse? response) async {
final http.Response httpResponse = await http.get(
Uri.parse('https://demo.duendesoftware.com/api/test'),
headers: <String, String>{'Authorization': 'Bearer $_accessToken'});
setState(() {
_userInfo = httpResponse.statusCode == 200 ? httpResponse.body : '';
_isBusy = false;
});
}
}