flutter_oauth library

A drop-in OAuth implementation for Flutter apps

This is a generic OAuth 2.0 implementation that should work with compliant backends such as Azure B2C or Okta.

Features include:

  • Support for OAuth 2.0 auth code grant flow
  • Automatically handles showing a browser view for users to log-in
  • Catches access tokens when the browser redirects back to the app (assuming redirect schemes are set up correctly as detailed below)
  • Also supports persistence to avoid logging in every time the app starts

Classes

AppAuthClient
Auth client that uses FlutterAppAuth for handling authentication.
AuthClient<T extends Token>
Client that handles the actual logging in / out and token refresh.
AuthManager<T extends Token>
Manages stateful authentication information such as the currently authenticated user and relevant authentication headers.
AuthPersistence<T extends Token>
Handles persistence of authenticated user information.
AuthUser<T extends Token>
Authenticated user data
FakeAuthClient
Fake auth client implementation that can be controlled using the various controller fields.
FakeAuthPersistence
Fake auth persistence implementation that can be controlled using the currentUserSubject
OAuthToken
Token with OAuth2-related information
SharedPrefOAuthPersistence
Persistence for OAuth token users using shared preferences for storage
Token
Authentication token, with the values to be put in the header

Enums

TokenErrorAction
Action to be taken during an error while obtaining a token, as determined by a token error handler.

Functions

createFakeUser({String? id, String? email, String? idToken, Map<String, dynamic>? idTokenJson, String? accessToken, String? refreshToken, DateTime? accesTokenExpiration}) AuthUser<OAuthToken>
Returns a fake auth user for test purposes
jsonToOAuthUser(Map<String, dynamic> json) AuthUser<OAuthToken>
oAuthUserToJson(AuthUser<OAuthToken> user) Map<String, dynamic>
parseJwt(String token) Map<String, dynamic>
Parsing of JWT id tokens taken from https://www.detroitdave.dev/2020/04/simple-azure-b2c-flutter.html

Typedefs

LogoutUseCase<T extends Token> = Future<void> Function(AuthUser<T> user)
TokenErrorHandler = FutureOr<TokenErrorAction?> Function(Object e)
UpdateTokenCallback = Future<AuthUser<OAuthToken>?> Function(AuthUser<OAuthToken> user)