This commit is contained in:
Tobe O 2018-12-09 11:29:15 -05:00
parent e490afe637
commit 8e9c85cb96
7 changed files with 31 additions and 10 deletions

View file

@ -1,3 +1,6 @@
# 2.0.3
* Updates for streaming parse of request bodies.
# 2.0.2 # 2.0.2
* Handle `null` return in `authenticate` + `failureRedirect`. * Handle `null` return in `authenticate` + `failureRedirect`.

View file

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'package:angel_auth/angel_auth.dart'; import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
main() async { main() async {
var app = new Angel(); var app = new Angel();

View file

@ -205,7 +205,7 @@ class AngelAuth<User> {
var jwt = getJwt(req); var jwt = getJwt(req);
if (jwt == null) { if (jwt == null) {
var body = await req.parseBody(); var body = await req.parseBody().then((_) => req.bodyAsMap);
jwt = body['token']?.toString(); jwt = body['token']?.toString();
} }
if (jwt == null) { if (jwt == null) {

View file

@ -64,7 +64,10 @@ class LocalAuthStrategy<User> extends AuthStrategy<User> {
} }
if (verificationResult == null) { if (verificationResult == null) {
var body = await req.parseBody(); var body = await req
.parseBody()
.then((_) => req.bodyAsMap)
.catchError((_) => <String, dynamic>{});
if (_validateString(body[usernameField]?.toString()) && if (_validateString(body[usernameField]?.toString()) &&
_validateString(body[passwordField]?.toString())) { _validateString(body[passwordField]?.toString())) {
verificationResult = await verifier( verificationResult = await verifier(

View file

@ -1,6 +1,6 @@
name: angel_auth name: angel_auth
description: A complete authentication plugin for Angel. Includes support for stateless JWT tokens, Basic Auth, and more. description: A complete authentication plugin for Angel. Includes support for stateless JWT tokens, Basic Auth, and more.
version: 2.0.2 version: 2.0.3
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_auth homepage: https://github.com/angel-dart/angel_auth
environment: environment:

View file

@ -1,6 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'package:angel_auth/angel_auth.dart'; import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'dart:convert'; import 'dart:convert';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:io/ansi.dart'; import 'package:io/ansi.dart';
@ -65,23 +66,23 @@ main() {
}); });
await app await app
.service('users') .findService('users')
.create({'username': 'jdoe1', 'password': 'password'}); .create({'username': 'jdoe1', 'password': 'password'});
auth = new AngelAuth<User>(); auth = new AngelAuth<User>();
auth.serializer = (u) => u.id; auth.serializer = (u) => u.id;
auth.deserializer = auth.deserializer =
(id) async => await app.service('users').read(id) as User; (id) async => await app.findService('users').read(id) as User;
await app.configure(auth.configureServer); await app.configure(auth.configureServer);
app.fallback(auth.decodeJwt); app.fallback(auth.decodeJwt);
auth.strategies['local'] = auth.strategies['local'] =
new LocalAuthStrategy((username, password) async { new LocalAuthStrategy((username, password) async {
var users = (await app var users = await app
.service('users') .findService('users')
.index() .index()
.then((it) => it.map<User>(User.parse).toList())) as Iterable<User>; .then((it) => it.map<User>((m) => User.parse(m as Map)).toList());
return users.firstWhere( return users.firstWhere(
(user) => user.username == username && user.password == password, (user) => user.username == username && user.password == password,
orElse: () => null); orElse: () => null);

View file

@ -1,9 +1,11 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_auth/angel_auth.dart'; import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'dart:convert'; import 'dart:convert';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
final AngelAuth<Map<String, String>> auth = final AngelAuth<Map<String, String>> auth =
@ -50,6 +52,14 @@ main() async {
]); ]);
app.get('/failure', (req, res) => "nope"); app.get('/failure', (req, res) => "nope");
app.logger = new Logger('angel_auth')
..onRecord.listen((rec) {
if (rec.error != null) {
print(rec.error);
print(rec.stackTrace);
}
});
HttpServer server = await angelHttp.startServer('127.0.0.1', 0); HttpServer server = await angelHttp.startServer('127.0.0.1', 0);
url = "http://${server.address.host}:${server.port}"; url = "http://${server.address.host}:${server.port}";
basicAuthUrl = basicAuthUrl =
@ -105,7 +115,10 @@ main() async {
auth.strategies.clear(); auth.strategies.clear();
auth.strategies['local'] = auth.strategies['local'] =
new LocalAuthStrategy(verifier, forceBasic: true, realm: 'test'); new LocalAuthStrategy(verifier, forceBasic: true, realm: 'test');
var response = await client.get("$url/hello", headers: headers); var response = await client.get("$url/hello", headers: {
'accept': 'application/json',
'content-type': 'application/json'
});
print(response.headers); print(response.headers);
print('Body <${response.body}>'); print('Body <${response.body}>');
expect(response.headers['www-authenticate'], equals('Basic realm="test"')); expect(response.headers['www-authenticate'], equals('Basic realm="test"'));