Fixed Basic Auth issue

This commit is contained in:
thomashii 2023-06-08 00:42:34 +08:00
parent a806d7c6db
commit f59b309327
9 changed files with 53 additions and 8 deletions

View file

@ -13,7 +13,7 @@
* Updated: angel3_auth * Updated: angel3_auth
* Updated: angel3_configuration * Updated: angel3_configuration
* Updated: angel3_validate * Updated: angel3_validate
* Updated: angel3_client (1 failed test case) * Updated: angel3_client
* Updated: angel3_websocket (1 failed test case) * Updated: angel3_websocket (1 failed test case)
* Updated: angel3_test * Updated: angel3_test
* Updated: jael3 * Updated: jael3

View file

@ -7,7 +7,7 @@
* Fixed failed `successRedirect` test case * Fixed failed `successRedirect` test case
* Fixed failed `failureRedirect` test case * Fixed failed `failureRedirect` test case
* Fixed failed `login` test case * Fixed failed `login` test case
* Fixed failed 'force basic` test case * Fixed failed `force basic` test case
* Added `example1` and `example2` * Added `example1` and `example2`
## 7.0.1 ## 7.0.1

View file

@ -20,7 +20,7 @@ Ensure you have read the [User Guide](https://angel3-docs.dukefirehawk.com/guide
configureServer(Angel app) async { configureServer(Angel app) async {
var auth = AngelAuth<User>( var auth = AngelAuth<User>(
serializer: (user) => user.id ?? '', serializer: (user) => user.id ?? '',
deserializer: (id) => fetchAUserByIdSomehow(id) deserializer: (id) => fetchAUserByIdSomehow(id
); );
auth.strategies['local'] = LocalAuthStrategy(...); auth.strategies['local'] = LocalAuthStrategy(...);

View file

@ -84,7 +84,7 @@ void main() async {
(user) => user.username == username && user.password == password); (user) => user.username == username && user.password == password);
return Future.value(result); return Future.value(result);
}); }, allowBasic: true);
app.post( app.post(
'/login', '/login',

View file

@ -28,7 +28,7 @@ class LocalAuthStrategy<User> extends AuthStrategy<User> {
{this.usernameField = 'username', {this.usernameField = 'username',
this.passwordField = 'password', this.passwordField = 'password',
this.invalidMessage = 'Please provide a valid username and password.', this.invalidMessage = 'Please provide a valid username and password.',
this.allowBasic = true, this.allowBasic = false,
this.forceBasic = false, this.forceBasic = false,
this.realm = 'Authentication is required.'}) { this.realm = 'Authentication is required.'}) {
_log.info('Using LocalAuthStrategy'); _log.info('Using LocalAuthStrategy');

View file

@ -93,7 +93,7 @@ void main() {
(user) => user.username == username && user.password == password); (user) => user.username == username && user.password == password);
return Future.value(result); return Future.value(result);
}); }, allowBasic: true);
app.post( app.post(
'/login', '/login',

View file

@ -30,7 +30,7 @@ Future wireAuth(Angel app) async {
//auth.serializer = (user) async => 1337; //auth.serializer = (user) async => 1337;
//auth.deserializer = (id) async => sampleUser; //auth.deserializer = (id) async => sampleUser;
auth.strategies['local'] = LocalAuthStrategy(verifier); auth.strategies['local'] = LocalAuthStrategy(verifier, allowBasic: true);
await app.configure(auth.configureServer); await app.configure(auth.configureServer);
} }
@ -104,7 +104,6 @@ void main() async {
var encodedAuth = base64.encode(utf8.encode('password:username')); var encodedAuth = base64.encode(utf8.encode('password:username'));
var response = await client.post(Uri.parse('$url/login'), var response = await client.post(Uri.parse('$url/login'),
//body: json.encode(postData),
headers: {'Authorization': 'Basic $encodedAuth'}); headers: {'Authorization': 'Basic $encodedAuth'});
print('Status Code: ${response.statusCode}'); print('Status Code: ${response.statusCode}');
print(response.headers); print(response.headers);

View file

@ -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<String, String> user = {'username': 'foo', 'password': 'bar'};
var localAuth = await client.authenticate(type: 'local', credentials: user);
print('JWT: ${localAuth.token}');
print('Data: ${localAuth.data}');
}

View file

@ -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<String, String> user = {'username': 'foo', 'password': 'bar'};
var localOpts =
AngelAuthOptions<Map<String, String>>(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);
}