From f59b309327bf7c64308f5849beab331d7dc46d5c Mon Sep 17 00:00:00 2001 From: thomashii Date: Thu, 8 Jun 2023 00:42:34 +0800 Subject: [PATCH] Fixed Basic Auth issue --- CHANGELOG.md | 2 +- packages/auth/CHANGELOG.md | 2 +- packages/auth/README.md | 2 +- packages/auth/example/example1.dart | 2 +- packages/auth/lib/src/strategies/local.dart | 2 +- packages/auth/test/callback_test.dart | 2 +- packages/auth/test/local_test.dart | 3 +- .../client/example/client/example_client.dart | 11 ++++++ packages/client/example/example1.dart | 35 +++++++++++++++++++ 9 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 packages/client/example/client/example_client.dart create mode 100644 packages/client/example/example1.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fec1520..3b2cd098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ * Updated: angel3_auth * Updated: angel3_configuration * Updated: angel3_validate -* Updated: angel3_client (1 failed test case) +* Updated: angel3_client * Updated: angel3_websocket (1 failed test case) * Updated: angel3_test * Updated: jael3 diff --git a/packages/auth/CHANGELOG.md b/packages/auth/CHANGELOG.md index cf656385..0ec94e79 100644 --- a/packages/auth/CHANGELOG.md +++ b/packages/auth/CHANGELOG.md @@ -7,7 +7,7 @@ * Fixed failed `successRedirect` test case * Fixed failed `failureRedirect` test case * Fixed failed `login` test case -* Fixed failed 'force basic` test case +* Fixed failed `force basic` test case * Added `example1` and `example2` ## 7.0.1 diff --git a/packages/auth/README.md b/packages/auth/README.md index 10082783..a009a5f3 100644 --- a/packages/auth/README.md +++ b/packages/auth/README.md @@ -20,7 +20,7 @@ Ensure you have read the [User Guide](https://angel3-docs.dukefirehawk.com/guide configureServer(Angel app) async { var auth = AngelAuth( serializer: (user) => user.id ?? '', - deserializer: (id) => fetchAUserByIdSomehow(id) + deserializer: (id) => fetchAUserByIdSomehow(id ); auth.strategies['local'] = LocalAuthStrategy(...); diff --git a/packages/auth/example/example1.dart b/packages/auth/example/example1.dart index a39cd25b..7f8d901d 100644 --- a/packages/auth/example/example1.dart +++ b/packages/auth/example/example1.dart @@ -84,7 +84,7 @@ void main() async { (user) => user.username == username && user.password == password); return Future.value(result); - }); + }, allowBasic: true); app.post( '/login', diff --git a/packages/auth/lib/src/strategies/local.dart b/packages/auth/lib/src/strategies/local.dart index b89d2b6f..ac21ac1d 100644 --- a/packages/auth/lib/src/strategies/local.dart +++ b/packages/auth/lib/src/strategies/local.dart @@ -28,7 +28,7 @@ class LocalAuthStrategy extends AuthStrategy { {this.usernameField = 'username', this.passwordField = 'password', this.invalidMessage = 'Please provide a valid username and password.', - this.allowBasic = true, + this.allowBasic = false, this.forceBasic = false, this.realm = 'Authentication is required.'}) { _log.info('Using LocalAuthStrategy'); diff --git a/packages/auth/test/callback_test.dart b/packages/auth/test/callback_test.dart index 1030a2bf..f1b8622b 100644 --- a/packages/auth/test/callback_test.dart +++ b/packages/auth/test/callback_test.dart @@ -93,7 +93,7 @@ void main() { (user) => user.username == username && user.password == password); return Future.value(result); - }); + }, allowBasic: true); app.post( '/login', diff --git a/packages/auth/test/local_test.dart b/packages/auth/test/local_test.dart index 07fab1f3..f7b9f48a 100644 --- a/packages/auth/test/local_test.dart +++ b/packages/auth/test/local_test.dart @@ -30,7 +30,7 @@ Future wireAuth(Angel app) async { //auth.serializer = (user) async => 1337; //auth.deserializer = (id) async => sampleUser; - auth.strategies['local'] = LocalAuthStrategy(verifier); + auth.strategies['local'] = LocalAuthStrategy(verifier, allowBasic: true); await app.configure(auth.configureServer); } @@ -104,7 +104,6 @@ void main() async { var encodedAuth = base64.encode(utf8.encode('password:username')); var response = await client.post(Uri.parse('$url/login'), - //body: json.encode(postData), headers: {'Authorization': 'Basic $encodedAuth'}); print('Status Code: ${response.statusCode}'); print(response.headers); diff --git a/packages/client/example/client/example_client.dart b/packages/client/example/client/example_client.dart new file mode 100644 index 00000000..2266d154 --- /dev/null +++ b/packages/client/example/client/example_client.dart @@ -0,0 +1,11 @@ +import 'package:angel3_client/io.dart' as c; + +void main() async { + c.Angel client = c.Rest('http://localhost:3000'); + + const Map user = {'username': 'foo', 'password': 'bar'}; + + var localAuth = await client.authenticate(type: 'local', credentials: user); + print('JWT: ${localAuth.token}'); + print('Data: ${localAuth.data}'); +} diff --git a/packages/client/example/example1.dart b/packages/client/example/example1.dart new file mode 100644 index 00000000..d63af47c --- /dev/null +++ b/packages/client/example/example1.dart @@ -0,0 +1,35 @@ +import 'package:angel3_auth/angel3_auth.dart'; +import 'package:angel3_framework/angel3_framework.dart'; +import 'package:angel3_framework/http.dart'; +import 'package:logging/logging.dart'; + +void main() async { + const Map user = {'username': 'foo', 'password': 'bar'}; + var localOpts = + AngelAuthOptions>(canRespondWithJson: true); + + Angel app = Angel(); + AngelHttp http = AngelHttp(app, useZone: false); + var auth = AngelAuth( + serializer: (_) async => 'baz', deserializer: (_) async => user); + + auth.strategies['local'] = LocalAuthStrategy((username, password) async { + if (username == 'foo' && password == 'bar') { + return user; + } + + return {}; + }, allowBasic: false); + + app.post('/auth/local', auth.authenticate('local', localOpts)); + + await app.configure(auth.configureServer); + + app.logger = Logger('auth_test') + ..onRecord.listen((rec) { + print( + '${rec.time}: ${rec.level.name}: ${rec.loggerName}: ${rec.message}'); + }); + + await http.startServer('127.0.0.1', 3000); +}