This commit is contained in:
thosakwe 2017-02-22 20:13:23 -05:00
parent 0a43161b86
commit 056d6809ed
4 changed files with 19 additions and 17 deletions

View file

@ -1,4 +1,5 @@
# auth_oauth2 # auth_oauth2
[![version 0.0.0](https://img.shields.io/badge/pub-v0.0.0-red.svg)](https://pub.dartlang.org/packages/angel_auth_oauth2)
[![version 1.0.0](https://img.shields.io/badge/pub-1.0.0-brightgreen.svg)](https://pub.dartlang.org/packages/angel_auth_oauth2)
angel_auth strategy for OAuth2 login, i.e. Facebook. angel_auth strategy for OAuth2 login, i.e. Facebook.

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:angel_auth/angel_auth.dart'; import 'package:angel_auth/angel_auth.dart';
import 'package:angel_diagnostics/angel_diagnostics.dart'; import 'package:angel_diagnostics/angel_diagnostics.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/common.dart';
import 'package:angel_auth_oauth2/angel_auth_oauth2.dart'; import 'package:angel_auth_oauth2/angel_auth_oauth2.dart';
import 'package:oauth2/oauth2.dart' as oauth2; import 'package:oauth2/oauth2.dart' as oauth2;
@ -15,7 +16,7 @@ const Map<String, String> OAUTH2_CONFIG = const {
}; };
main() async { main() async {
var app = new Angel()..use('/users', new MemoryService<User>()); var app = new Angel()..use('/users', new TypedService<User>(new MapService()));
var auth = new AngelAuth(jwtKey: 'oauth2 example secret', allowCookie: false); var auth = new AngelAuth(jwtKey: 'oauth2 example secret', allowCookie: false);
auth.deserializer = auth.deserializer =
@ -45,10 +46,10 @@ main() async {
print('Listening on http://${server.address.address}:${server.port}'); print('Listening on http://${server.address.address}:${server.port}');
} }
class User extends MemoryModel { class User extends Model {
String example_siteId; String example_siteId;
User({int id, this.example_siteId}) { User({String id, this.example_siteId}) {
this.id = id; this.id = id;
} }
} }

View file

@ -8,7 +8,7 @@ import 'package:angel_validate/angel_validate.dart';
import 'package:oauth2/oauth2.dart' as oauth2; import 'package:oauth2/oauth2.dart' as oauth2;
/// Loads a user profile via OAuth2. /// Loads a user profile via OAuth2.
typedef Future ProfileLoader(oauth2.Client client); typedef Future OAuth2Verifier(oauth2.Client client);
final Validator OAUTH2_OPTIONS_SCHEMA = new Validator({ final Validator OAUTH2_OPTIONS_SCHEMA = new Validator({
'key*': isString, 'key*': isString,
@ -23,11 +23,11 @@ final Validator OAUTH2_OPTIONS_SCHEMA = new Validator({
'scopes': "'scopes' must be an Iterable of strings. You provided: {{value}}" 'scopes': "'scopes' must be an Iterable of strings. You provided: {{value}}"
}); });
class AngelAuthOauth2Options { class AngelAuthOAuth2Options {
String key, secret, authorizationEndpoint, tokenEndpoint, callback; String key, secret, authorizationEndpoint, tokenEndpoint, callback;
Iterable<String> scopes; Iterable<String> scopes;
AngelAuthOauth2Options( AngelAuthOAuth2Options(
{this.key, {this.key,
this.secret, this.secret,
this.authorizationEndpoint, this.authorizationEndpoint,
@ -37,8 +37,8 @@ class AngelAuthOauth2Options {
this.scopes = scopes ?? []; this.scopes = scopes ?? [];
} }
factory AngelAuthOauth2Options.fromJson(Map json) => factory AngelAuthOAuth2Options.fromJson(Map json) =>
new AngelAuthOauth2Options( new AngelAuthOAuth2Options(
key: json['key'], key: json['key'],
secret: json['secret'], secret: json['secret'],
authorizationEndpoint: json['authorizationEndpoint'], authorizationEndpoint: json['authorizationEndpoint'],
@ -60,8 +60,8 @@ class AngelAuthOauth2Options {
class OAuth2Strategy implements AuthStrategy { class OAuth2Strategy implements AuthStrategy {
String _name; String _name;
AngelAuthOauth2Options _options; AngelAuthOAuth2Options _options;
final ProfileLoader profileLoader; final OAuth2Verifier verifier;
@override @override
String get name => _name; String get name => _name;
@ -69,12 +69,12 @@ class OAuth2Strategy implements AuthStrategy {
@override @override
set name(String value) => _name = name; set name(String value) => _name = name;
/// [options] can be either a `Map` or an instance of [AngelAuthOauth2Options]. /// [options] can be either a `Map` or an instance of [AngelAuthOAuth2Options].
OAuth2Strategy(this._name, options, this.profileLoader) { OAuth2Strategy(this._name, options, this.verifier) {
if (options is AngelAuthOauth2Options) if (options is AngelAuthOAuth2Options)
_options = options; _options = options;
else if (options is Map) else if (options is Map)
_options = new AngelAuthOauth2Options.fromJson( _options = new AngelAuthOAuth2Options.fromJson(
OAUTH2_OPTIONS_SCHEMA.enforce(options)); OAUTH2_OPTIONS_SCHEMA.enforce(options));
else else
throw new ArgumentError('Invalid OAuth2 options: $options'); throw new ArgumentError('Invalid OAuth2 options: $options');
@ -106,7 +106,7 @@ class OAuth2Strategy implements AuthStrategy {
await grant.getAuthorizationUrl(Uri.parse(_options.callback), await grant.getAuthorizationUrl(Uri.parse(_options.callback),
scopes: _options.scopes); scopes: _options.scopes);
var client = await grant.handleAuthorizationResponse(req.query); var client = await grant.handleAuthorizationResponse(req.query);
return await profileLoader(client); return await verifier(client);
} }
@override @override

View file

@ -1,6 +1,6 @@
name: angel_auth_oauth2 name: angel_auth_oauth2
description: angel_auth strategy for OAuth2 login, i.e. Facebook. description: angel_auth strategy for OAuth2 login, i.e. Facebook.
version: 0.0.0 version: 1.0.0
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
environment: environment:
sdk: ">=1.19.0" sdk: ">=1.19.0"