From 18ebc9d55490d5d1d0321db64c6a46bc0519c995 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 10 Jul 2018 19:31:50 -0400 Subject: [PATCH] Patch basic auth --- CHANGELOG.md | 5 +++++ lib/src/strategies/local.dart | 9 ++++----- pubspec.yaml | 4 +++- test/callback_test.dart | 33 ++++++++++++++++++++++++++++++--- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9ee4338..d8de60c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.1.1+6 +* Fix a small logic bug that prevented `LocalAuthStrategy` +from correctly propagating the authenticated user when +using `Basic` auth. + # 1.1.1+5 * Prevent duplication of cookies. * Regenerate the JWT if `tokenCallback` is called. diff --git a/lib/src/strategies/local.dart b/lib/src/strategies/local.dart index e6b5a7ac..9f516025 100644 --- a/lib/src/strategies/local.dart +++ b/lib/src/strategies/local.dart @@ -61,13 +61,12 @@ class LocalAuthStrategy extends AuthStrategy { if (verificationResult == false || verificationResult == null) { res ..statusCode = 401 - ..headers[HttpHeaders.WWW_AUTHENTICATE] = 'Basic realm="$realm"' + ..headers['www-authenticate'] = 'Basic realm="$realm"' ..end(); return false; } - res.properties['user'] = verificationResult; - return true; + return verificationResult; } } @@ -75,8 +74,8 @@ class LocalAuthStrategy extends AuthStrategy { await req.parse(); if (_validateString(req.body[usernameField]?.toString()) && _validateString(req.body[passwordField]?.toString())) { - verificationResult = - await verifier(req.body[usernameField]?.toString(), req.body[passwordField]?.toString()); + verificationResult = await verifier(req.body[usernameField]?.toString(), + req.body[passwordField]?.toString()); } } diff --git a/pubspec.yaml b/pubspec.yaml index 98e22501..b9170bba 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: angel_auth description: A complete authentication plugin for Angel. -version: 1.1.1+5 +version: 1.1.1+6 author: Tobe O homepage: https://github.com/angel-dart/angel_auth environment: @@ -11,4 +11,6 @@ dependencies: dart2_constant: ^1.0.0 dev_dependencies: http: ^0.11.0 + io: ^0.3.2 + logging: ^0.11.0 test: ^0.12.0 diff --git a/test/callback_test.dart b/test/callback_test.dart index 28dbcad6..c1bdf3c2 100644 --- a/test/callback_test.dart +++ b/test/callback_test.dart @@ -4,6 +4,8 @@ import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/common.dart'; import 'package:dart2_constant/convert.dart'; import 'package:http/http.dart' as http; +import 'package:io/ansi.dart'; +import 'package:logging/logging.dart'; import 'package:test/test.dart'; class User extends Model { @@ -21,17 +23,39 @@ main() { String url; setUp(() async { + hierarchicalLoggingEnabled = true; app = new Angel(); - angelHttp = new AngelHttp(app, useZone: false); + angelHttp = new AngelHttp(app); app.use('/users', new TypedService(new MapService())); + var oldErrorHandler = app.errorHandler; + app.errorHandler = (e, req, res) { + app.logger.severe(e.message, e, e.stackTrace ?? StackTrace.current); + return oldErrorHandler(e, req, res); + }; + + app.logger = new Logger('angel_auth') + ..level = Level.FINEST + ..onRecord.listen((rec) { + print(rec); + + if (rec.error != null) { + print(yellow.wrap(rec.error.toString())); + } + + if (rec.stackTrace != null) { + print(yellow.wrap(rec.stackTrace.toString())); + } + }); + await app .service('users') .create({'username': 'jdoe1', 'password': 'password'}); auth = new AngelAuth(); auth.serializer = (u) => u.id; - auth.deserializer = app.service('users').read; + auth.deserializer = + (id) async => await app.service('users').read(id) as User; await app.configure(auth.configureServer); app.use(auth.decodeJwt); @@ -78,7 +102,10 @@ main() { body: {'username': 'jdoe1', 'password': 'password'}); print('Response: ${response.body}'); expect(response.body, equals('Hello!')); - }); + }, + skip: Platform.version.contains('2.0.0-dev') + ? 'Blocked on https://github.com/dart-lang/sdk/issues/33594' + : null); test('preserve existing user', () async { final response = await client.post('$url/existing/foo',