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
[![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.

View file

@ -3,6 +3,7 @@ import 'dart:io';
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_diagnostics/angel_diagnostics.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:oauth2/oauth2.dart' as oauth2;
@ -15,7 +16,7 @@ const Map<String, String> OAUTH2_CONFIG = const {
};
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);
auth.deserializer =
@ -45,10 +46,10 @@ main() async {
print('Listening on http://${server.address.address}:${server.port}');
}
class User extends MemoryModel {
class User extends Model {
String example_siteId;
User({int id, this.example_siteId}) {
User({String id, this.example_siteId}) {
this.id = id;
}
}

View file

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

View file

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