सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
एक फ्लटर प्लगइन जो उपयोगकर्ताओं को सुरक्षित, एप में ब्राउज़र के माध्यम से वेब सेवाओं के साथ प्रमाणीकरण करने की अनुमति देता है। यह ओएथ 2 और समान प्रमाणीकरण प्रवाह को सरल बनाता है, जो लॉगिन के लिए एक वेब व्यू खोलता है और रीडायरेक्ट को सुरक्षित ढंग से संभालता है। यह प्लगइन एंड्रॉइड, आईओएस, वेब और मैकओएस प्लेटफॉर्म पर काम करने के लिए डिज़ाइन किया गया है, जिससे एक समान उपयोगकर्ता अनुभव सुनिश्चित होता है। इसमें डीप लिंकिंग और कॉलबैक हैंडलिंग समर्थन है, जो गूगल, फेसबुक या कस्टम ओएथ 2 सर्वर जैसे तीसरे पक्ष के पहचान प्रदाताओं को एकीकृत करने के लिए आदर्श है।
उपयोग
OAuth2 प्रमाणीकरण
सोशल प्रदाता के साथ लॉगिन करें
सुरक्षित वेब-आधारित लॉगिन
कस्टम पहचान सर्वर के साथ एकीकरण
एकल प्रवेश वातावरण
मुख्य विशेषताएँ
प्लेटफॉर्म के अनुकूल समर्थन
एप में ब्राउज़र एकीकरण
पुनर्निर्देश URL का प्रबंधन
गहरा लिंक समर्थन
सुरक्षित τोकन प्राप्ति
इनके लिए उपयुक्त
OAuth2 लॉगिन की आवश्यकता वाले ऐप्स
तीसरे पक्ष की प्रमाणीकरण सेवाओं का उपयोग करने वाले विकासकर्मी
कई प्लेटफॉर्म के लिए प्रोजेक्ट्स
सुरक्षित वेब प्रमाणीकरण की आवश्यकता वाले अनुप्रयोग
SSO फीचर्स बनाने वाली टीमें
विचार योग्य बातें
सही रीडायरेक्ट URI सेटअप की आवश्यकता है
मोबाइल पर कस्टम URL स्कीम्स की आवश्यकता हो सकती है
प्लेटफॉर्म-विशिष्ट ब्राउज़र व्यवहार पर निर्भर करता है
टोकन के सुरक्षित निपटान की आवश्यकता है
वेब-आधारित प्रमाणीकरण प्रवाहों तक सीमित
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Web Auth for Flutter
A Flutter plugin for authenticating a user with a web service, even if the web service is run by a third party. Most commonly used with OAuth2, but can be used with any web flow that can redirect to a custom scheme.
In the background, this plugin uses ASWebAuthenticationSession on iOS 12+ and macOS 10.15+, SFAuthenticationSession on iOS 11, Chrome Custom Tabs on Android and opens a new window on Web. You can build it with iOS 8+, but it is currently only supported by iOS 11 or higher.
iOS
Android
macOS
Usage
To authenticate against your own custom site:
import'package:flutter_web_auth/flutter_web_auth.dart';
// Present the dialog to the userfinal result = awaitFlutterWebAuth.authenticate(url: "https://my-custom-app.com/connect", callbackUrlScheme: "my-custom-app");
// Extract token from resulting urlfinal token = Uri.parse(result).queryParameters['token']
To authenticate the user using Google's OAuth2:
import'package:flutter_web_auth/flutter_web_auth.dart';
import'dart:convert'show jsonDecode;
import'package:http/http.dart'as http;
// App specific variablesfinal googleClientId = 'XXXXXXXXXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
final callbackUrlScheme = 'com.googleusercontent.apps.XXXXXXXXXXXX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Construct the urlfinal url = Uri.https('accounts.google.com', '/o/oauth2/v2/auth', {
'response_type': 'code',
'client_id': googleClientId,
'redirect_uri': '$callbackUrlScheme:/',
'scope': 'email',
});
// Present the dialog to the userfinal result = awaitFlutterWebAuth.authenticate(url: url.toString(), callbackUrlScheme: callbackUrlScheme);
// Extract code from resulting urlfinal code = Uri.parse(result).queryParameters['code'];
// Construct an Uri to Google's oauth2 endpointfinal url = Uri.https('www.googleapis.com', 'oauth2/v4/token');
// Use this code to get an access tokenfinal response = await http.post(url, body: {
'client_id': googleClientId,
'redirect_uri': '$callbackUrlScheme:/',
'grant_type': 'authorization_code',
'code': code,
});
// Get the access token from the responsefinal accessToken = jsonDecode(response.body)['access_token'] asString;
Note: To use multiple scopes with Google, you need to encode them as a single string, separated by spaces. For example, scope: 'email https://www.googleapis.com/auth/userinfo.profile'. Here is a list of all supported scopes.
Setup
Setup works as for any Flutter plugin, expect the Android and Web caveats outlined below:
Android
In order to capture the callback url, the following activity needs to be added to your AndroidManifest.xml. Be sure to relpace YOUR_CALLBACK_URL_SCHEME_HERE with your actual callback url scheme.
On the Web platform an endpoint needs to be created that captures the callback URL and sends it to the application using the JavaScript postMessage() method. In the ./web folder of the project, create an HTML file with the name e.g. auth.html with content:
<!DOCTYPE html><title>Authentication complete</title><p>Authentication is complete. If this does not happen automatically, please
close the window.
<script>window.opener.postMessage({
'flutter-web-auth': window.location.href
}, window.location.origin);
window.close();
</script>
Redirection URL passed to the authentication service must be the same as the URL on which the application is running (schema, host, port if necessary) and the path must point to created HTML file, /auth.html in this case. The callbackUrlScheme parameter of the authenticate() method does not take into account, so it is possible to use a schema for native platforms in the code.
For the Sign in with Apple in web_message response mode, postMessage from https://appleid.apple.com is also captured, and the authorization object is returned as a URL fragment encoded as a query string (for compatibility with other providers).
Troubleshooting
When you use this package for the first time, there are some problems you may have. These are some of the common solutions
Troubleshooting callbackUrlScheme
callbackUrlScheme must be a valid schema string or else this wont work.
A valid RFC 3986 URL scheme must consist of "a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-")."
This means you can not use underscore "_", space " " or uppercase "ABCDEF...". You can not also start with a number. See RFC3986#page-17
examples of VALID callbackUrlScheme are callback-scheme, another.scheme, examplescheme
examples of INVALID callbackUrlScheme are callback_scheme,1another.scheme, exampleScheme
Troubleshooting Flutter App
You have to tell the FlutterWebAuth.authenticate function what your callbackUrlScheme is.
Example if your callbackUrlScheme is valid-callback-scheme, your dart code will look like
import'package:flutter_web_auth/flutter_web_auth.dart';
// Present the dialog to the userfinal result = awaitFlutterWebAuth.authenticate(url: "https://my-custom-app.com/connect", callbackUrlScheme: "valid-callback-scheme");
Troubleshooting Android
You are required to update your AndroidManifest.xml to include the com.linusu.flutter_web_auth.CallbackActivity activity something like
<manifest><application><!-- add the com.linusu.flutter_web_auth.CallbackActivity activity --><activityandroid:name="com.linusu.flutter_web_auth.CallbackActivity"android:exported="true"><intent-filterandroid:label="flutter_web_auth"><actionandroid:name="android.intent.action.VIEW" /><categoryandroid:name="android.intent.category.DEFAULT" /><categoryandroid:name="android.intent.category.BROWSABLE" /><dataandroid:scheme="YOUR_CALLBACK_URL_SCHEME_HERE" /></intent-filter></activity></application></manifest>
Example of valid AndroidManifest.xml with VALID callbackUrlScheme. in the example below valid-callback-scheme is our callbackUrlScheme
If you are targeting S+ (version 31 and above) you need to provide an explicit value for android:exported. If you followed earlier installation instructions this was not included. Make sure that you add android:exported="true" to the com.linusu.flutter_web_auth.CallbackActivity activity in your AndroidManifest.xml file.
Your OAuth Provider must redirect to the valid callbackUrlScheme + ://. This mean if your callbackUrlScheme is validscheme, your OAuth Provider must redirect to validscheme://
If you are using HTML hyperlinks, it must be a valid callbackUrlScheme + ://. This mean if your callbackUrlScheme is customappname, your html hyperlink should be customappname://
example with HTML
<ahref="customappname://?data1=value1&data2=value2">Go Back to App</a>
Troubleshooting passing data to app
You can pass data back to your app by adding GET query parameters. This means by adding name=value type of data after your callbackUrlScheme + :// + ?
example to pass access-token to your app a valid url for that could be
my-callback-schema://?access-token=jdu9292s
You can pass multipe data by concatenating with &
my-callback-schema://?data1=value1&data2=value2
example to pass access-token and user_id to your app a valid url for that could be