Last updated:
0 purchases
flutter aad oauth
Azure Active Directory OAuth #
Forked from Earlybyte.aad_oauth.
A Flutter OAuth package for performing user authentication against Azure Active Directory OAuth2 v2.0 endpoint. Forked from hitherejoe.FlutterOAuth.
Supported Flows:
Authorization code flow (including refresh token flow)
Usage #
For using this library you have to create an azure app at the Azure App registration portal. Use native app as platform type (with callback URL: https://login.live.com/oauth20_desktop.srf).
Afterwards you have to initialize the library as follow:
final Config config = Config(
azureTenantId: "$TENANT_ID",
clientId: "$CLIENT_ID",
scope: "$scope",
redirectUri: "$redirectUri",
responseType: "$responseType");
final AadOAuth oauth = AadOAuth(config);
copied to clipboard
This allows you to pass in an tenant ID, client ID, scope and redirect url.
Four important things: #
ResponseType for mobile is usually "code", and web is usually "id_token + token";
Redirect URI to Flutter Web is explained below.
nonce is an optional parm to Web flow
To save multiple tokens it is necessary to pass tokenIdentifier in the FlutterAadOauth constructor. The parameter is used to define different names in storage, avoiding overwriting data.
Then once you have an OAuth instance, you can call login() and afterwards getAccessToken() or getIdToken() or the getRefreshToken() to retrieve an access token or the id token or the refresh token:
await oauth.login();
String accessToken = await oauth.getAccessToken();
String idToken = await oauth.getIdToken();
String refreshToken = await oauth.getRefreshToken();
String tokenType = await oauth.getTokenType();
DateTime issueTimeStamp = await oauth.getIssueTimeStamp();
DateTime refreshToken = await oauth.getExpireTimeStamp();
int refreshToken = await oauth.getExpiresIn();
copied to clipboard
You can also call getAccessToken() directly. It will automatically login and retrieve an access token.
You can also call getIdToken() directly. It will automatically login and retrieve an id token.
You can also call getRefreshToken() directly. It will automatically login and retrieve a refresh token.
You can also call getTokenType() directly. It will retrieve the token type.
You can also call getIssueTimeStamp() directly. It retrieve the Issue Time Stamp.
You can also call getExpireTimeStamp() directly. It retrieve the Expire Time Stamp.
You can also call getExpiresIn() directly. It retrieve the value of when the token expires in.
Tokens are stored in Keychain for iOS or Keystore for Android. To destroy the tokens you can call logout():
await oauth.logout();
copied to clipboard
Flutter Web #
There are a few steps to make the authentication flow in Flutter Web:
Your project must have a web folder at the root of the directory (Flutter 2.0);
Inside this folder it is necessary to create the file authRedirect.html, to be the redirect page, when requesting the token. This html file needs to have a script with the post method, like this:
<script>
window.opener.postMessage(window.location.href, '*');
</script>
copied to clipboard
The redirect URL must be previously registered in Azure, so the application must always run on the same port. To do this just use the --web-port=3000 flag in flutter run, or add the args to the launch.json file (VsCode) as shown below
"args": ["--web-port", "3000"],
copied to clipboard
In the repository of this package, there is an example folder, with a flutter project that uses this lib with mobile and web authentication.
Example To Get Redirect URI #
final currentUri = Uri.base;
redirectUri = Uri(
host: currentUri.host,
scheme: currentUri.scheme,
port: currentUri.port,
path: '/authRedirect.html',
);
copied to clipboard
Flutter Web support was based on this article
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.