openid_client 0.1.3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 81

openid_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

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:openid_client/openid_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?.claimSet?.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();
}

Command line tool

Install

pub global activate openid_client

Usage

Show a list of known OpenID providers:

openid_client issuers-list

Discover and show the metadata of an OP:

openid_client issusers-discover https://www.example.com

Show a list of known clients:

openid_client clients-list

Add a client:

openid_client clients-add --secret optional_secret https://some.issuer.com client_id
 

Remove a client:

openid_client clients-remove https://some.issuer.com client_id

Authenticate with a client:

openid_client clients-auth --secret optional_secret https://some.issuer.com client_id

Show the content of an id token and validate it:

openid_client tokens-validate eyJhbGciOiJSUzI1NiIsImtpZCI6ImE2YzJjNmQ0ZTZkYTFmOWJjMTdmYzhkMzExMzNiOTJmMD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ATaDh8-aBsNKpqQ

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

0.1.0

  • Initial version

example/main.dart

// Copyright (c) 2017, rbellens. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:openid_client/openid_client.dart';
import 'package:openid_client/src/html.dart';
import 'dart:html' hide Client, Credential;
import 'dart:convert';
import 'package:angular2/platform/browser.dart';
import 'package:angular2/core.dart';

main() async {

  bootstrap(AppComponent);

/*
  var issuer = await Issuer.discover(Uri.parse('https://accounts.google.com'));
  var client = new Client(issuer,
      "581155104943-rpjk0sjvnp1kgadc5t2ni9qoaktdjs21.apps.googleusercontent.com");

  var a = await new Authenticator(client);

  var c = await a.credential;


  if (c==null) {
    document.querySelector("#login")
      ..disabled = false
          ..onClick.listen((e)=>a.authorize());

  } else {
    var info = await c.getUserInfo();
    document.querySelector("#name").text = info.name;
    document.querySelector("#picture").src = info.picture.toString();
  }

*/

}

@Component(selector: 'my-app', templateUrl: 'app_component.html')
class AppComponent {
  List<Uri> issuers = Issuer.knownIssuers.toList();

  Issuer selectedIssuer;

  Map<String,List<String>> allClients = {};

  List<String> clients = [];

  Client selectedClient;

  Authenticator authenticator;

  Credential credential;

  UserInfo userinfo;

  AppComponent() {
    allClients = JSON.decode(window.localStorage["openid_clients"] ?? "{}");

    () async {
      if (window.localStorage.containsKey("issuer")) {
        await select(window.localStorage["issuer"]);
        if (selectedIssuer!=null) {
          if (window.localStorage.containsKey("client_id")) {
            selectClient(window.localStorage["client_id"]);
          }
        }
      }
    }();
    print("clients $allClients");
  }

  select(v) async {
    print("select $v");
    window.localStorage["issuer"] = v;
    this.selectedClient = null;
    this.clients = [];
    this.selectedIssuer = await Issuer.discover(Uri.parse(v));
    this.clients = allClients[selectedIssuer.metadata.issuer.toString()] ??= [];
  }

  selectClient(String v) async {
    print("select client $v");
    if (!clients.contains(v)) {
      clients.add(v);
      window.localStorage["openid_clients"] = JSON.encode(allClients);
    }
    window.localStorage["client_id"] = v;
    selectedClient = new Client(selectedIssuer, v);
    authenticator = new Authenticator(selectedClient);
    credential = null;
    userinfo = null;
    credential = await authenticator.credential;
    print("select client $credential");
    userinfo = await credential.getUserInfo();
    print("userinfo $userinfo");
    print(userinfo.toJson());
  }

  login() {
    authenticator.authorize();
  }

  logout() {
    userinfo = null;
    authenticator.logout();
  }
}

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  openid_client: "^0.1.3"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:openid_client/openid_client.dart';
        
Version Uploaded Documentation Archive
0.1.3 Feb 6, 2017 Go to the documentation of openid_client 0.1.3 Download openid_client 0.1.3 archive
0.1.2 Feb 6, 2017 Go to the documentation of openid_client 0.1.2 Download openid_client 0.1.2 archive
0.1.1 Feb 1, 2017 Go to the documentation of openid_client 0.1.1 Download openid_client 0.1.1 archive
0.1.0 Feb 1, 2017 Go to the documentation of openid_client 0.1.0 Download openid_client 0.1.0 archive

Analysis

We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
72 / 100
Health:
Code health derived from static analysis. [more]
97 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
78 / 100
Overall score:
Weighted score of the above. [more]
81
Learn more about scoring.

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:openid_client/openid_client.dart.

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 error 22 hints.

    Strong-mode analysis of lib/src/id_token.dart failed with the following error:

    line: 36 col: 3
    Invalid override. The type of 'IdToken.validatePayload' ('(JwtValidationContext) → Set<ConstraintViolation>') isn't a subtype of 'JsonWebSignature<MapJwtClaimSet>.validatePayload' ('(JwsValidationContext) → Set<ConstraintViolation>').

    Run dartfmt to format lib/openid_client.dart.

    Similar analysis of the following files failed:

    • lib/src/console.dart (hint)
    • lib/src/html.dart (hint)
    • lib/src/http_util.dart (hint)
    • lib/src/http_util/browser.dart (hint)
    • lib/src/http_util/io.dart (hint)
    • lib/src/model.dart (hint)
    • lib/src/model/claims.dart (hint)
    • lib/src/model/jwk.dart (hint)
    • lib/src/model/metadata.dart (hint)
    • lib/src/model/token_response.dart (hint)
    • lib/src/openid.dart (hint)
  • Fix .analysis_options.

    We were unable to parse .analysis_options.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

  • Enable strong mode analysis.

    Strong mode helps you to detect bugs and potential issues earlier.Start your analysis_options.yaml file with the following:

    analyzer:
      strong-mode: true
    

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.21.0 <2.0.0
dart_jwt >=0.6.0 <0.7.0 0.6.0
http >=0.11.0 <0.12.0 0.11.3+16
source_gen >=0.5.3 <0.6.0 0.5.10+1 0.8.1
unscripted >=0.6.2 <0.7.0 0.6.2
Transitive dependencies
analyzer 0.30.0+4 0.31.1
ansicolor 0.0.9 0.1.0
args 0.13.7 1.4.2
async 2.0.6
bignum 0.0.7 0.1.0
build 0.9.3 0.12.2
charcode 1.1.1
cipher 0.7.1
cli_util 0.1.2+1
collection 1.14.9
convert 2.0.1
crypto 2.0.2+1
csslib 0.14.1
dart_style 1.0.9+1 1.0.10
fixnum 0.9.1+2 0.10.7
front_end 0.1.0-alpha.4.1 0.1.0-alpha.11
glob 1.1.5
html 0.13.3
http_parser 3.1.1
isolate 1.1.0 2.0.0
kernel 0.3.0-alpha.1.1 0.3.0-alpha.11
logging 0.11.3+1
meta 1.1.2
mockable_filesystem 0.0.3
package_config 1.0.3
path 1.5.1
plugin 0.2.0+2
source_span 1.4.0
string_scanner 1.0.2
supports_color 0.1.1
typed_data 1.1.5
utf 0.9.0+4
watcher 0.9.7+7
yaml 2.1.13
Dev dependencies
angular2 >=2.0.0 <4.0.0
browser >=0.10.0 <0.11.0
build_runner >=0.1.1 <0.2.0
test >=0.12.0 <0.13.0