2017-09-24 04:32:38 +00:00
|
|
|
import 'dart:async';
|
2021-05-14 11:09:48 +00:00
|
|
|
import 'package:angel3_auth/angel3_auth.dart';
|
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_framework/http.dart';
|
2018-09-11 22:14:33 +00:00
|
|
|
import 'dart:convert';
|
2016-11-23 20:37:40 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2018-12-09 16:29:15 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2016-11-23 20:37:40 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2021-06-07 00:50:39 +00:00
|
|
|
final AngelAuth<Map<String, String>> auth = AngelAuth<Map<String, String>>(
|
2021-09-29 07:40:27 +00:00
|
|
|
serializer: (user) async => '1337', deserializer: (id) async => sampleUser);
|
2018-07-12 17:11:54 +00:00
|
|
|
var headers = <String, String>{'accept': 'application/json'};
|
2019-04-19 07:50:04 +00:00
|
|
|
var localOpts = AngelAuthOptions<Map<String, String>>(
|
2016-11-23 20:37:40 +00:00
|
|
|
failureRedirect: '/failure', successRedirect: '/success');
|
2021-07-15 07:57:47 +00:00
|
|
|
var localOpts2 =
|
|
|
|
AngelAuthOptions<Map<String, String>>(canRespondWithJson: false);
|
|
|
|
|
2018-08-26 23:11:37 +00:00
|
|
|
Map<String, String> sampleUser = {'hello': 'world'};
|
2016-11-23 20:37:40 +00:00
|
|
|
|
2021-07-15 07:57:47 +00:00
|
|
|
Future<Map<String, String>> verifier(String? username, String? password) async {
|
2016-11-23 20:37:40 +00:00
|
|
|
if (username == 'username' && password == 'password') {
|
|
|
|
return sampleUser;
|
2021-03-07 13:40:25 +00:00
|
|
|
} else {
|
2021-07-15 07:57:47 +00:00
|
|
|
return {};
|
2021-03-07 13:40:25 +00:00
|
|
|
}
|
2016-11-23 20:37:40 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 04:32:38 +00:00
|
|
|
Future wireAuth(Angel app) async {
|
2021-06-07 00:50:39 +00:00
|
|
|
//auth.serializer = (user) async => 1337;
|
|
|
|
//auth.deserializer = (id) async => sampleUser;
|
2016-11-23 20:37:40 +00:00
|
|
|
|
2019-04-19 07:50:04 +00:00
|
|
|
auth.strategies['local'] = LocalAuthStrategy(verifier);
|
2017-09-24 04:32:38 +00:00
|
|
|
await app.configure(auth.configureServer);
|
2016-11-23 20:37:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 13:40:25 +00:00
|
|
|
void main() async {
|
2016-11-23 20:37:40 +00:00
|
|
|
Angel app;
|
2021-03-20 23:51:20 +00:00
|
|
|
late AngelHttp angelHttp;
|
2021-05-29 01:48:33 +00:00
|
|
|
late http.Client client;
|
2021-03-20 23:51:20 +00:00
|
|
|
String? url;
|
|
|
|
String? basicAuthUrl;
|
2016-11-23 20:37:40 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2019-04-19 07:50:04 +00:00
|
|
|
client = http.Client();
|
|
|
|
app = Angel();
|
|
|
|
angelHttp = AngelHttp(app, useZone: false);
|
2016-11-23 20:37:40 +00:00
|
|
|
await app.configure(wireAuth);
|
2021-06-07 00:50:39 +00:00
|
|
|
app.get('/hello', (req, res) {
|
|
|
|
// => 'Woo auth'
|
|
|
|
return 'Woo auth';
|
2021-07-15 07:57:47 +00:00
|
|
|
}, middleware: [auth.authenticate('local', localOpts2)]);
|
2018-08-26 23:11:37 +00:00
|
|
|
app.post('/login', (req, res) => 'This should not be shown',
|
2017-09-24 04:32:38 +00:00
|
|
|
middleware: [auth.authenticate('local', localOpts)]);
|
2021-03-07 13:40:25 +00:00
|
|
|
app.get('/success', (req, res) => 'yep', middleware: [
|
2018-08-26 23:11:37 +00:00
|
|
|
requireAuthentication<Map<String, String>>(),
|
|
|
|
]);
|
2021-03-07 13:40:25 +00:00
|
|
|
app.get('/failure', (req, res) => 'nope');
|
2016-11-23 20:37:40 +00:00
|
|
|
|
2021-07-08 01:20:21 +00:00
|
|
|
app.logger = Logger('local_test')
|
2018-12-09 16:29:15 +00:00
|
|
|
..onRecord.listen((rec) {
|
2021-07-08 01:20:21 +00:00
|
|
|
print(
|
|
|
|
'${rec.time}: ${rec.level.name}: ${rec.loggerName}: ${rec.message}');
|
|
|
|
|
2018-12-09 16:29:15 +00:00
|
|
|
if (rec.error != null) {
|
|
|
|
print(rec.error);
|
|
|
|
print(rec.stackTrace);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-07 13:40:25 +00:00
|
|
|
var server = await angelHttp.startServer('127.0.0.1', 0);
|
|
|
|
url = 'http://${server.address.host}:${server.port}';
|
2016-11-23 20:37:40 +00:00
|
|
|
basicAuthUrl =
|
2021-03-07 13:40:25 +00:00
|
|
|
'http://username:password@${server.address.host}:${server.port}';
|
2016-11-23 20:37:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
2018-06-27 16:36:31 +00:00
|
|
|
await angelHttp.close();
|
2021-05-29 01:48:33 +00:00
|
|
|
//client = null;
|
2016-11-23 20:37:40 +00:00
|
|
|
url = null;
|
|
|
|
basicAuthUrl = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can use "auth" as middleware', () async {
|
2021-05-29 01:48:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/success'),
|
2021-03-07 13:40:25 +00:00
|
|
|
headers: {'Accept': 'application/json'});
|
2016-11-23 20:37:40 +00:00
|
|
|
print(response.body);
|
|
|
|
expect(response.statusCode, equals(403));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('successRedirect', () async {
|
2021-03-07 13:40:25 +00:00
|
|
|
var postData = {'username': 'username', 'password': 'password'};
|
2021-05-29 01:48:33 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/login'),
|
2018-06-27 16:36:31 +00:00
|
|
|
body: json.encode(postData),
|
2018-07-12 17:11:54 +00:00
|
|
|
headers: {'content-type': 'application/json'});
|
2018-12-31 16:50:03 +00:00
|
|
|
expect(response.statusCode, equals(302));
|
2018-07-12 17:11:54 +00:00
|
|
|
expect(response.headers['location'], equals('/success'));
|
2016-11-23 20:37:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('failureRedirect', () async {
|
2021-03-07 13:40:25 +00:00
|
|
|
var postData = {'username': 'password', 'password': 'username'};
|
2021-05-29 01:48:33 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/login'),
|
2018-06-27 16:36:31 +00:00
|
|
|
body: json.encode(postData),
|
2018-07-12 17:11:54 +00:00
|
|
|
headers: {'content-type': 'application/json'});
|
2021-07-08 01:20:21 +00:00
|
|
|
print('Status Code: ${response.statusCode}');
|
|
|
|
print(response.headers);
|
|
|
|
print(response.body);
|
2018-07-12 17:11:54 +00:00
|
|
|
expect(response.headers['location'], equals('/failure'));
|
2016-11-23 20:37:40 +00:00
|
|
|
expect(response.statusCode, equals(401));
|
|
|
|
});
|
|
|
|
|
2021-07-08 01:20:21 +00:00
|
|
|
test('basic auth without authorization', () async {
|
|
|
|
var response = await client.get(Uri.parse('$url/hello'));
|
|
|
|
print('Status Code: ${response.statusCode}');
|
|
|
|
print(response.headers);
|
|
|
|
print(response.body);
|
|
|
|
expect(response.statusCode, equals(401));
|
|
|
|
});
|
|
|
|
|
|
|
|
//test('allow basic', () async {
|
|
|
|
test('basic auth with authorization', () async {
|
2021-03-07 13:40:25 +00:00
|
|
|
var authString = base64.encode('username:password'.runes.toList());
|
2021-05-29 01:48:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/hello'),
|
2021-03-07 13:40:25 +00:00
|
|
|
headers: {'authorization': 'Basic $authString'});
|
2021-06-07 00:50:39 +00:00
|
|
|
print(response.statusCode);
|
|
|
|
print(response.body);
|
2016-11-23 20:37:40 +00:00
|
|
|
expect(response.body, equals('"Woo auth"'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allow basic via URL encoding', () async {
|
2021-05-29 01:48:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$basicAuthUrl/hello'));
|
2016-11-23 20:37:40 +00:00
|
|
|
expect(response.body, equals('"Woo auth"'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('force basic', () async {
|
2017-09-24 04:32:38 +00:00
|
|
|
auth.strategies.clear();
|
2018-09-11 22:03:35 +00:00
|
|
|
auth.strategies['local'] =
|
2019-04-19 07:50:04 +00:00
|
|
|
LocalAuthStrategy(verifier, forceBasic: true, realm: 'test');
|
2021-05-29 01:48:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/hello'), headers: {
|
2018-12-09 16:29:15 +00:00
|
|
|
'accept': 'application/json',
|
|
|
|
'content-type': 'application/json'
|
|
|
|
});
|
2021-05-29 01:48:33 +00:00
|
|
|
print('Header = ${response.headers}');
|
|
|
|
print('Body <${response.body}>');
|
|
|
|
var head = response.headers['www-authenticate'];
|
2021-05-09 11:16:15 +00:00
|
|
|
expect(head, equals('Basic realm="test"'));
|
2016-11-23 20:37:40 +00:00
|
|
|
});
|
|
|
|
}
|