oauth_client
Library for working with OpenID Connect and implementing clients.
It currently supports these features:
- discover OpenID Provider metadata
- parsing and validating id tokens
- basic tools for implementing implicit and authorization code flow
- authentication for command line tools
About
Hi everyone, we created this repository due to not updating an excellent library, see here openid_client, we need several impovements in this package and we made some.
Besides authentication providers that support OpenID Connect, this library can also work with other authentication providers supporting oauth2, like Facebook. For these providers, some features (e.g. discovery and id tokens) will not work. You should define the metadata for those providers manually, except for Facebook, which is predefined in the library.
📋 Usage
A simple usage example:
import 'package:oauth_client/oauth_client.dart';
main() async {
// print a list of known issuers
print(Issuer.knownIssuers);
// discover the metadata of the google OP
var issuer = await Issuer.discover(Issuer.google);
// create a client
var client = new Client(issuer, "client_id", "client_secret");
// create a credential object from authorization code
var c = client.createCredential(code: "some received authorization code");
// or from an access token
c = client.createCredential(accessToken: "some received access token");
// or from an id token
c = client.createCredential(idToken: "some id token");
// get userinfo
var info = await c.getUserInfo();
print(info.name);
// get claims from id token if present
print(c.idToken?.claims?.name);
// create an implicit authentication flow
var f = new Flow.implicit(client);
// or an explicit flow
f = new Flow.authorizationCode(client);
// set the redirect uri
f.redirectUri = Uri.parse("http://localhost");
// do something with the authentication url
print(f.authenticationUrl);
// handle the result and get a credential object
c = await f.callback({
"code": "some code",
});
// validate an id token
var violations = await c.validateToken();
}
📋 Usage example on flutter
import 'package:oauth_client/oauth_client_io.dart';
import 'package:url_launcher/url_launcher.dart';
authenticate(Uri uri, String clientId, List<String> scopes) async {
// create the client
var issuer = await Issuer.discover(uri);
var client = new Client(issuer, clientId);
// create a function to open a browser with an url
urlLauncher(String url) async {
if (await canLaunch(url)) {
await launch(url, forceWebView: true);
} else {
throw 'Could not launch $url';
}
}
// create an authenticator
var authenticator = new Authenticator(client,
scopes: scopes,
port: 4000, urlLancher: urlLauncher);
// starts the authentication
var c = await authenticator.authorize();
// close the webview when finished
closeWebView();
// return the user info
return await c.getUserInfo();
}
📋 Usage example on command line
// import the io version
import 'package:oauth_client/oauth_client_io.dart';
authenticate(Uri uri, String clientId, List<String> scopes) async {
// create the client
var issuer = await Issuer.discover(uri);
var client = new Client(issuer, clientId);
// create an authenticator
var authenticator = new Authenticator(client,
scopes: scopes,
port: 4000);
// starts the authentication
var c = await authenticator.authorize(); // this will open a browser
// return the user info
return await c.getUserInfo();
}
📋 Usage example in browser
import 'package:oauth_client/oauth_client_browser.dart';
authenticate(Uri uri, String clientId, List<String> scopes) async {
// create the client
var issuer = await Issuer.discover(uri);
var client = new Client(issuer, clientId);
// create an authenticator
var authenticator = new Authenticator(client, scopes: scopes);
// get the credential
var c = await authenticator.credential;
if (c==null) {
// starts the authentication
authenticator.authorize(); // this will redirect the browser
} else {
// return the user info
return await c.getUserInfo();
}
}
📋 Usage example in webview in modal
import 'package:oauth_client/oauth_client_io.dart';
import 'package:webview_flutter/webview_flutter.dart';
Future _openWebViewLoginAsync(String url, context, Function(AuthModel) onLogin) async {
try {
var uri = Uri.tryParse(url.trimLeft())!;
if (!await canLaunchUrl(uri)) {
return false;
}
showModal(
context: context,
isScrollControlled: true,
height: 0.90,
child: Expanded(
child: WebView(
initialUrl: 'about:blank',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
webViewController.loadUrl(url.trimLeft());
},
),
),
).whenComplete(() async {
var auth = await _getAuthModelAsync(null);
if (auth == AuthModel.empty()) {
await _deleteStorageAsync();
await _deleteCacheAsync();
}
onLogin(auth);
});
} on Exception catch (_, e) {
Groveman.error(_.toString(), error: e);
}
}