diff --git a/lib/angel_auth.dart b/lib/angel_auth.dart index e063da36..f6f0d025 100644 --- a/lib/angel_auth.dart +++ b/lib/angel_auth.dart @@ -33,11 +33,14 @@ class Auth { strategies.firstWhere((AuthStrategy x) => x.name == type); var result = await strategy.authenticate( req, res, options: options ?? {}); - if (result is bool) + print("${req.path} -> $result"); + if (result == true) return result; - else { + else if(result != false) { req.session['userId'] = await serializer(result); return true; + } else { + throw new AngelHttpException.NotAuthenticated(); } }; } diff --git a/test/local.dart b/test/local.dart index 95ddec61..2eb3f0a0 100644 --- a/test/local.dart +++ b/test/local.dart @@ -30,6 +30,7 @@ main() async { Angel app; http.Client client; String url; + String basicAuthUrl; setUp(() async { client = new http.Client(); @@ -39,22 +40,26 @@ main() async { app.get('/hello', 'Woo auth', middleware: [Auth.authenticate('local')]); app.post('/login', 'This should not be shown', middleware: [Auth.authenticate('local', localOpts)]); - app.get('/success', "yep", middleware: []); + app.get('/success', "yep", middleware: ['auth']); app.get('/failure', "nope"); HttpServer server = await app.startServer( InternetAddress.LOOPBACK_IP_V4, 0); url = "http://${server.address.host}:${server.port}"; + basicAuthUrl = + "http://username:password@${server.address.host}:${server.port}"; }); tearDown(() async { await app.httpServer.close(force: true); client = null; url = null; + basicAuthUrl = null; }); test('can use login as middleware', () async { - var response = await client.get("$url/success"); + var response = await client.get("$url/success", headers: {'Accept': 'application/json'}); + print(response.body); expect(response.statusCode, equals(401)); }); @@ -90,6 +95,12 @@ main() async { expect(response.body, equals('"Woo auth"')); }); + test('allow basic via URL encoding', () async { + var response = await client.get( + basicAuthUrl, headers: headers); + expect(response.body, equals('"Woo auth"')); + }); + test('force basic', () async { Auth.strategies.clear(); Auth.strategies.add(new LocalAuthStrategy(