angel_auth_oauth2 1.0.1

  • README.md
  • Installing
  • Versions
  • 27

auth_oauth2

version 1.0.1

package:angel_auth strategy for OAuth2 login, i.e. Facebook or Github.

Usage

First, create an options object:

configureServer(Angel app) async {
  // Load from a Map, i.e. app config:
  var opts = new AngelOAuth2Options.fromJson(map);
  
  // Create in-place:
  const AngelAuthOAuth2Options OAUTH2_CONFIG = const AngelAuthOAuth2Options(
      callback: '<callback-url>',
      key: '<client-id>',
      secret: '<client-secret>',
      authorizationEndpoint: '<authorization-endpoint>',
      tokenEndpoint: '<access-token-endpoint>');
}

After getting authenticated against the remote server, we need to be able to identify users within our own application. Use an OAuth2Verifier to associate remote users with local users.

/// You might use a pure function to create a verifier that queries a
/// given service.
OAuth2Verifier oauth2verifier(Service userService) {
  return (oauth2.Client client) async {
     var response = await client.get('https://api.github.com/user');
     var ghUser = JSON.decode(response.body);
     var id = ghUser['id'];
 
     Iterable<Map> matchingUsers = await userService.index({
       'query': {'githubId': id}
     });
 
     if (matchingUsers.isNotEmpty) {
       // Return the corresponding user, if it exists
       return User.parse(matchingUsers.firstWhere((u) => u['githubId'] == id));
     } else {
       // Otherwise,create a user
       return await userService.create({'githubId': id}).then(User.parse);
     }
   };
}

Now, initialize an OAuth2Strategy, using the options and verifier. You'll also need to provide a name for this instance of the strategy. Consider using the name of the remote authentication provider (ex. facebook).

configureServer(Angel app) {
  // ...
  var oauthStrategy =
    new OAuth2Strategy('github', OAUTH2_CONFIG, oauth2Verifier(app.service('users')));
}

Lastly, connect it to an AngelAuth instance, and wire it up to an Angel server. Set up two routes:

  1. Redirect users to the external provider
  2. Acts as a callback and handles an access code

In the case of the callback route, you may want to display an HTML page that closes a popup window. In this case, use confirmPopupAuthentication, which is bundled with package:angel_auth, as a callback function:

configureServer(Angel app) async {
  // ...
  var auth = new AngelAuth();
  auth.strategies.add(oauth2Strategy);
  
  // Redirect
  app.get('/auth/github', auth.authenticate('github'));
  
  // Callback
  app.get('/auth/github/callback', auth.authenticate(
    'github',
    new AngelAuthOptions(callback: confirmPopupAuthentication())
  ));
  
  // Connect the plug-in!!!
  await app.configure(auth);
}

Custom Scope Delimiter

This package should work out-of-the-box for most OAuth2 providers, such as Github or Dropbox. However, if your OAuth2 scopes are separated by a delimiter other than the default (' '), you can add it in the AngelOAuth2Options constructor:

configureServer(Angel app) async {
  const AngelOAuth2Options OPTS = const AngelOAuth2Options(
    // ...
    delimiter: ','
  );
}

1. Depend on it

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


dependencies:
  angel_auth_oauth2: "^1.0.1"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:angel_auth_oauth2/angel_auth_oauth2.dart';
        
Version Uploaded Documentation Archive
1.0.1 Jun 3, 2017 Go to the documentation of angel_auth_oauth2 1.0.1 Download angel_auth_oauth2 1.0.1 archive
1.0.0 Feb 23, 2017 Go to the documentation of angel_auth_oauth2 1.0.0 Download angel_auth_oauth2 1.0.0 archive
0.0.0 Jan 12, 2017 Go to the documentation of angel_auth_oauth2 0.0.0 Download angel_auth_oauth2 0.0.0 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Feb 14, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
30 / 100
Health:
Code health derived from static analysis. [more]
0 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
61 / 100
Overall score:
Weighted score of the above. [more]
27

Platforms

Detected platforms: unsure

Error(s) prevent platform classification.

Suggestions

  • Fix lib/angel_auth_oauth2.dart.

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

    line: 5 col: 8
    Target of URI doesn't exist: 'package:angel_framework/src/http/response_context.dart'.

  • Fix platform conflicts.

    Make sure none of the libraries use mutually exclusive dependendencies.

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • Fix issues reported by dartanalyzer.

    dartanalyzer reported 1 error(s) and 0 warning(s).

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use angel_auth_oauth2.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0
angel_auth ^1.0.0-dev 1.1.0+2
angel_validate ^1.0.0-beta 1.0.3
oauth2 ^1.0.0 1.1.0
Transitive dependencies
angel_framework 1.1.1
angel_http_exception 1.0.0
angel_model 1.0.0
angel_route 2.0.5
async 2.0.3 2.0.4
body_parser 1.0.3
charcode 1.1.1
collection 1.14.5
combinator 1.0.0-beta+7
container 0.1.2
convert 2.0.1
crypto 2.0.2+1
http 0.11.3+16
http_parser 3.1.1
http_server 0.9.7
json_god 2.0.0-beta+1
logging 0.11.3+1
matcher 0.12.1+4
merge_map 1.0.0
meta 1.1.2
mime 0.9.6
path 1.5.1
pool 1.3.4
quiver_hashcode 1.0.0
random_string 0.0.1
source_span 1.4.0
stack_trace 1.9.2
string_scanner 1.0.2
tuple 1.0.1
typed_data 1.1.5
Dev dependencies
angel_diagnostics ^1.0.0-dev