From a806d7c6dbbf52e94e23cebe833e0f7ef1640043 Mon Sep 17 00:00:00 2001 From: thomashii Date: Mon, 5 Jun 2023 19:00:42 +0800 Subject: [PATCH] Fixed auth failed test cases --- .../auth/example/client/example_client.http | 9 +++++++-- packages/auth/example/example2.dart | 6 +++++- packages/auth/lib/src/strategies/local.dart | 17 ++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/auth/example/client/example_client.http b/packages/auth/example/client/example_client.http index 7a5f2b73..330cae03 100644 --- a/packages/auth/example/client/example_client.http +++ b/packages/auth/example/client/example_client.http @@ -1,12 +1,17 @@ ### Load landing page GET http://localhost:3000/ HTTP/1.1 -### Success redirect +### login (call_back) POST http://localhost:3000/login HTTP/1.1 Content-Type: application/json Authorization: Basic jdoe1:password -### Failure redirect +### Success redirect (local) +POST http://localhost:3000/login HTTP/1.1 +Content-Type: application/json +Authorization: Basic username:password + +### Failure redirect (local) POST http://localhost:3000/login HTTP/1.1 Content-Type: application/json Authorization: Basic password:username diff --git a/packages/auth/example/example2.dart b/packages/auth/example/example2.dart index 12210e64..77830377 100644 --- a/packages/auth/example/example2.dart +++ b/packages/auth/example/example2.dart @@ -25,10 +25,14 @@ Future> verifier(String? username, String? password) async { Future wireAuth(Angel app) async { //auth.strategies['local'] = LocalAuthStrategy(verifier); - auth.strategies['local'] = LocalAuthStrategy(verifier, forceBasic: true, realm: 'test'); + auth.strategies['local'] = + LocalAuthStrategy(verifier, forceBasic: true, realm: 'test'); await app.configure(auth.configureServer); } +/* + * Backend for local test cases + */ void main() async { Angel app = Angel(reflector: MirrorsReflector()); AngelHttp angelHttp = AngelHttp(app, useZone: false); diff --git a/packages/auth/lib/src/strategies/local.dart b/packages/auth/lib/src/strategies/local.dart index d2d58739..b89d2b6f 100644 --- a/packages/auth/lib/src/strategies/local.dart +++ b/packages/auth/lib/src/strategies/local.dart @@ -81,15 +81,22 @@ class LocalAuthStrategy extends AuthStrategy { } } - // User authentication succeeded - if (verificationResult == true || - (verificationResult is Map && verificationResult.isNotEmpty)) { - return verificationResult; + // User authentication succeeded can return Map(one element), User(non null) or true + if (verificationResult != null && verificationResult != false) { + if (verificationResult is Map && verificationResult.isNotEmpty) { + return verificationResult; + } else if (verificationResult is! Map) { + return verificationResult; + } } // Force basic if set if (forceBasic) { - res.headers['www-authenticate'] = 'Basic realm="$realm"'; + //res.headers['www-authenticate'] = 'Basic realm="$realm"'; + res + ..statusCode = 401 + ..headers['www-authenticate'] = 'Basic realm="$realm"'; + await res.close(); return null; }