2017-09-24 04:32:38 +00:00
|
|
|
import 'dart:async';
|
2016-11-23 20:37:40 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:angel_auth/angel_auth.dart';
|
2018-06-27 16:36:31 +00:00
|
|
|
import 'package:dart2_constant/convert.dart';
|
2016-11-23 20:37:40 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2017-09-24 04:32:38 +00:00
|
|
|
final AngelAuth auth = new AngelAuth();
|
2018-06-27 16:36:31 +00:00
|
|
|
var headers = <String, String>{HttpHeaders.ACCEPT: ContentType.JSON.mimeType};
|
2016-11-23 20:37:40 +00:00
|
|
|
AngelAuthOptions localOpts = new AngelAuthOptions(
|
|
|
|
failureRedirect: '/failure', successRedirect: '/success');
|
|
|
|
Map sampleUser = {'hello': 'world'};
|
|
|
|
|
2017-09-24 04:32:38 +00:00
|
|
|
Future verifier(String username, String password) async {
|
2016-11-23 20:37:40 +00:00
|
|
|
if (username == 'username' && password == 'password') {
|
|
|
|
return sampleUser;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-24 04:32:38 +00:00
|
|
|
Future wireAuth(Angel app) async {
|
|
|
|
auth.serializer = (user) async => 1337;
|
|
|
|
auth.deserializer = (id) async => sampleUser;
|
2016-11-23 20:37:40 +00:00
|
|
|
|
2017-09-24 04:32:38 +00:00
|
|
|
auth.strategies.add(new LocalAuthStrategy(verifier));
|
|
|
|
await app.configure(auth.configureServer);
|
|
|
|
app.use(auth.decodeJwt);
|
2016-11-23 20:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main() async {
|
|
|
|
Angel app;
|
2018-06-27 16:36:31 +00:00
|
|
|
AngelHttp angelHttp;
|
2016-11-23 20:37:40 +00:00
|
|
|
http.Client client;
|
|
|
|
String url;
|
|
|
|
String basicAuthUrl;
|
|
|
|
|
|
|
|
setUp(() async {
|
|
|
|
client = new http.Client();
|
2017-09-24 04:32:38 +00:00
|
|
|
app = new Angel();
|
2018-06-27 16:36:31 +00:00
|
|
|
angelHttp = new AngelHttp(app, useZone: false);
|
2016-11-23 20:37:40 +00:00
|
|
|
await app.configure(wireAuth);
|
2017-09-24 04:32:38 +00:00
|
|
|
app.get('/hello', 'Woo auth', middleware: [auth.authenticate('local')]);
|
2016-11-23 20:37:40 +00:00
|
|
|
app.post('/login', 'This should not be shown',
|
2017-09-24 04:32:38 +00:00
|
|
|
middleware: [auth.authenticate('local', localOpts)]);
|
2016-11-23 20:37:40 +00:00
|
|
|
app.get('/success', "yep", middleware: ['auth']);
|
|
|
|
app.get('/failure', "nope");
|
|
|
|
|
|
|
|
HttpServer server =
|
2018-06-27 16:36:31 +00:00
|
|
|
await angelHttp.startServer('127.0.0.1', 0);
|
2016-11-23 20:37:40 +00:00
|
|
|
url = "http://${server.address.host}:${server.port}";
|
|
|
|
basicAuthUrl =
|
|
|
|
"http://username:password@${server.address.host}:${server.port}";
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
2018-06-27 16:36:31 +00:00
|
|
|
await angelHttp.close();
|
2016-11-23 20:37:40 +00:00
|
|
|
client = null;
|
|
|
|
url = null;
|
|
|
|
basicAuthUrl = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can use "auth" as middleware', () async {
|
|
|
|
var response = await client
|
|
|
|
.get("$url/success", headers: {'Accept': 'application/json'});
|
|
|
|
print(response.body);
|
|
|
|
expect(response.statusCode, equals(403));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('successRedirect', () async {
|
|
|
|
Map postData = {'username': 'username', 'password': 'password'};
|
|
|
|
var response = await client.post("$url/login",
|
2018-06-27 16:36:31 +00:00
|
|
|
body: json.encode(postData),
|
2016-11-23 20:37:40 +00:00
|
|
|
headers: {HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType});
|
|
|
|
expect(response.statusCode, equals(200));
|
|
|
|
expect(response.headers[HttpHeaders.LOCATION], equals('/success'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('failureRedirect', () async {
|
|
|
|
Map postData = {'username': 'password', 'password': 'username'};
|
|
|
|
var response = await client.post("$url/login",
|
2018-06-27 16:36:31 +00:00
|
|
|
body: json.encode(postData),
|
2016-11-23 20:37:40 +00:00
|
|
|
headers: {HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType});
|
|
|
|
print("Login response: ${response.body}");
|
|
|
|
expect(response.headers[HttpHeaders.LOCATION], equals('/failure'));
|
|
|
|
expect(response.statusCode, equals(401));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allow basic', () async {
|
2018-06-27 16:36:31 +00:00
|
|
|
String authString = base64.encode("username:password".runes.toList());
|
2016-11-23 20:37:40 +00:00
|
|
|
var response = await client.get("$url/hello",
|
|
|
|
headers: {HttpHeaders.AUTHORIZATION: 'Basic $authString'});
|
|
|
|
expect(response.body, equals('"Woo auth"'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allow basic via URL encoding', () async {
|
|
|
|
var response = await client.get("$basicAuthUrl/hello");
|
|
|
|
expect(response.body, equals('"Woo auth"'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('force basic', () async {
|
2017-09-24 04:32:38 +00:00
|
|
|
auth.strategies.clear();
|
|
|
|
auth.strategies
|
2016-11-23 20:37:40 +00:00
|
|
|
.add(new LocalAuthStrategy(verifier, forceBasic: true, realm: 'test'));
|
|
|
|
var response = await client.get("$url/hello", headers: headers);
|
|
|
|
print(response.headers);
|
|
|
|
expect(response.headers[HttpHeaders.WWW_AUTHENTICATE],
|
|
|
|
equals('Basic realm="test"'));
|
|
|
|
});
|
|
|
|
}
|