aad_oauth 套件是一個 Flutter 庫,旨在透過 OAuth2 v2.0 協定簡化與 Azure Active Directory 的使用者驗證。它為需要透過 Microsoft 雲端平台進行身分管理的應用程式提供安全的登入流程。該套件負責處理權杖取得、更新與儲存,支援與受 Azure AD 保護的 API 無縫整合。專為針對企業環境的開發者設計,可簡化在 Flutter 應用中實作現代驗證標準的過程。
適用情境
在企業 Flutter 應用中整合 Azure AD 登錄
使用 OAuth2 權杖保護 API 存取
透過 Microsoft 身份驗證啟用單一登入 (SSO)
處理權杖刷新和持久化
為受監管行業建構合規的身份工作流程
主要特色
Azure AD 的 OAuth2 v2.0 支援
安全的令牌處理和存儲
內建的權杖刷新邏輯
Azure AD 應用的簡單設定
跨平台相容性(iOS、Android、Web)
支援 PKCE 流以增強安全性
適合對象
企業移動應用開發人員
需要 Microsoft 身分驗證整合的 Flutter 應用
需要符合企業認證策略的應用程式
使用 Azure AD 作為身分提供者的開發人員
需要安全、可擴展驗證且程式碼量最少的專案
使用建議
需要正確的 Azure AD 應用程式註冊
需要正確設置重定向 URI
僅限 Azure AD OAuth2 v2.0 終結點
無內建使用者介面——需要自訂或第三方登入畫面
針對 iOS/Android/Web 可能需要特定於平台的設定
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
Azure Active Directory OAuth
A Flutter OAuth package for performing user authentication against Azure Active Directory OAuth2 v2.0 endpoint. Forked from hitherejoe.FlutterOAuth.
Your minSdkVersion must be >= 20 in android/app/build.gradle section android / defaultConfig to support webview_flutter. Version 19 may build but will likely fail at runtime.
If your app does not have the android.permission.INTERNET permission you must add it to the AndroidManifest
<uses-permission android:name="android.permission.INTERNET"/>
Afterwards you must create a navigatorKey and initialize the library as follow:
final navigatorKey = GlobalKey<NavigatorState>();
// ... staticfinalConfig config = newConfig(
tenant: "YOUR_TENANT_ID",
clientId: "YOUR_CLIENT_ID",
scope: "openid profile offline_access",
// redirectUri is Optional as a default is calculated based on app type/web location
redirectUri: "your redirect url available in azure portal",
navigatorKey: navigatorKey,
webUseRedirect: true, // default is false - on web only, forces a redirect flow instead of popup auth//Optional parameter: Centered CircularProgressIndicator while rendering web page in WebView
loader: Center(child: CircularProgressIndicator()),
postLogoutRedirectUri: 'http://your_base_url/logout', //optional
);
finalAadOAuth oauth = newAadOAuth(config);
This allows you to pass in an tenant ID, client ID, scope and redirect url.
The same navigatorKey must be provided to the top-level MaterialApp.
// ...// Material App must be built with the same navigatorKey// to support navigation to the login route for interactive// authentication.// ...Widgetbuild(BuildContext context) {
returnMaterialApp(
// ...
navigatorKey: navigatorKey,
// ...
);
}
Then once you have an OAuth instance, you can call login() and afterwards getAccessToken() to retrieve an access token:
final result = await oauth.login();
result.fold(
(failure) => showError(failure.toString()),
(token) => showMessage('Logged in successfully, your access token: $token'),
);
String accessToken = await oauth.getAccessToken();
Tokens are stored in Keychain for iOS or Keystore for Android. To destroy the tokens you can call logout():
await oauth.logout();
Web Usage
For web you also have to add some lines to your index.html (see the index.html in the example applications):
Note that when using redirect flow on web, the login() call will not return if the user has not logged in yet because
the page is redirected and the app is destroyed until login is complete. Your application must take care of calling
login() again once reloaded to complete the login process within the flutter application - if login was successful,
this second call will be fast, and will not cause another redirection.
When using redirecting logins with the example application, you will need to click on the login button again following
a successful login to see the token details.
Add your Azure tenant ID, tenantName, client ID (ID of App), client Secret (Secret of App) and redirectUrl in the main.dart source-code:
staticfinalConfig configB2Ca = newConfig(
tenant: "YOUR_TENANT_NAME",
clientId: "YOUR_CLIENT_ID",
scope: "YOUR_CLIENT_ID offline_access",
// redirectUri: "https://login.live.com/oauth20_desktop.srf", // Note: this is the default for Mobile// clientSecret: "YOUR_CLIENT_SECRET", // Note: do not include secret in publicly available applications
isB2C: true,
policy: "YOUR_USER_FLOW___USER_FLOW_A",
tokenIdentifier: "UNIQUE IDENTIFIER A",
navigatorKey: navigatorKey,
);
Afterwards you can login and get an access token for accessing other resources. You can also use multiple configs at the same time.
ADFS Usage
This library only suports ADFS authentication for Flutter mobile applications, not web builds.
Add the following to your pubspec.yaml dependencies:
dependencies:aad_oauth:"^1.0.1"
Contribution
Contributions can be submitted as pull requests and are highly welcomed. Changes will be bundled together into a release. You can find the next release date and past releases in the CHANGELOG file.