2017-09-29 02:16:44 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:angel_oauth2/angel_oauth2.dart';
|
|
|
|
import 'package:angel_test/angel_test.dart';
|
2018-07-09 15:38:29 +00:00
|
|
|
import 'package:dart2_constant/convert.dart';
|
2017-09-29 02:16:44 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:oauth2/oauth2.dart' as oauth2;
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import 'common.dart';
|
|
|
|
|
|
|
|
main() {
|
|
|
|
Angel app;
|
|
|
|
Uri authorizationEndpoint, tokenEndpoint, redirectUri;
|
|
|
|
TestClient testClient;
|
|
|
|
|
|
|
|
setUp(() async {
|
|
|
|
app = new Angel()..lazyParseBodies = true;
|
|
|
|
app.configuration['properties'] = app.configuration;
|
|
|
|
app.inject('authCodes', <String, String>{});
|
|
|
|
|
|
|
|
var server = new _Server();
|
|
|
|
|
|
|
|
app.group('/oauth2', (router) {
|
|
|
|
router
|
|
|
|
..get('/authorize', server.authorizationEndpoint)
|
|
|
|
..post('/token', server.tokenEndpoint);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.logger = new Logger('angel')
|
|
|
|
..onRecord.listen((rec) {
|
|
|
|
print(rec);
|
|
|
|
if (rec.error != null) print(rec.error);
|
|
|
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
|
|
|
});
|
|
|
|
|
2018-07-09 15:38:29 +00:00
|
|
|
var http = new AngelHttp(app);
|
|
|
|
var s = await http.startServer();
|
|
|
|
var url = 'http://${s.address.address}:${s.port}';
|
2017-09-29 02:16:44 +00:00
|
|
|
authorizationEndpoint = Uri.parse('$url/oauth2/authorize');
|
|
|
|
tokenEndpoint = Uri.parse('$url/oauth2/token');
|
|
|
|
redirectUri = Uri.parse('http://foo.bar/baz');
|
|
|
|
|
|
|
|
testClient = await connectTo(app);
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
await testClient.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
group('auth code', () {
|
|
|
|
oauth2.AuthorizationCodeGrant createGrant() =>
|
|
|
|
new oauth2.AuthorizationCodeGrant(
|
|
|
|
pseudoApplication.id,
|
|
|
|
authorizationEndpoint,
|
|
|
|
tokenEndpoint,
|
|
|
|
secret: pseudoApplication.secret,
|
|
|
|
);
|
|
|
|
|
|
|
|
test('show authorization form', () async {
|
|
|
|
var grant = createGrant();
|
|
|
|
var url = grant.getAuthorizationUrl(redirectUri, state: 'hello');
|
|
|
|
var response = await testClient.client.get(url);
|
|
|
|
print('Body: ${response.body}');
|
|
|
|
expect(
|
|
|
|
response.body,
|
2018-07-09 15:38:29 +00:00
|
|
|
json.encode(
|
2017-09-29 02:16:44 +00:00
|
|
|
'Hello ${pseudoApplication.id}:${pseudoApplication.secret}'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('preserves state', () async {
|
|
|
|
var grant = createGrant();
|
|
|
|
var url = grant.getAuthorizationUrl(redirectUri, state: 'goodbye');
|
|
|
|
var response = await testClient.client.get(url);
|
|
|
|
print('Body: ${response.body}');
|
2018-07-09 15:38:29 +00:00
|
|
|
expect(json.decode(response.body)['state'], 'goodbye');
|
2017-09-29 02:16:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('sends auth code', () async {
|
|
|
|
var grant = createGrant();
|
|
|
|
var url = grant.getAuthorizationUrl(redirectUri);
|
|
|
|
var response = await testClient.client.get(url);
|
|
|
|
print('Body: ${response.body}');
|
|
|
|
expect(
|
2018-07-09 15:38:29 +00:00
|
|
|
json.decode(response.body),
|
2017-09-29 02:16:44 +00:00
|
|
|
allOf(
|
|
|
|
isMap,
|
|
|
|
predicate((Map m) => m.containsKey('code'), 'contains "code"'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('exchange code for token', () async {
|
|
|
|
var grant = createGrant();
|
|
|
|
var url = grant.getAuthorizationUrl(redirectUri);
|
|
|
|
var response = await testClient.client.get(url);
|
|
|
|
print('Body: ${response.body}');
|
|
|
|
|
2018-07-09 15:38:29 +00:00
|
|
|
var authCode = json.decode(response.body)['code'];
|
2017-09-29 02:16:44 +00:00
|
|
|
var client = await grant.handleAuthorizationCode(authCode);
|
|
|
|
expect(client.credentials.accessToken, authCode + '_access');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can send refresh token', () async {
|
|
|
|
var grant = createGrant();
|
|
|
|
var url = grant.getAuthorizationUrl(redirectUri, state: 'can_refresh');
|
|
|
|
var response = await testClient.client.get(url);
|
|
|
|
print('Body: ${response.body}');
|
|
|
|
|
2018-07-09 15:38:29 +00:00
|
|
|
var authCode = json.decode(response.body)['code'];
|
2017-09-29 02:16:44 +00:00
|
|
|
var client = await grant.handleAuthorizationCode(authCode);
|
|
|
|
expect(client.credentials.accessToken, authCode + '_access');
|
|
|
|
expect(client.credentials.canRefresh, isTrue);
|
|
|
|
expect(client.credentials.refreshToken, authCode + '_refresh');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-16 06:38:46 +00:00
|
|
|
class _Server extends AuthorizationServer<PseudoApplication, Map> {
|
2017-09-29 02:16:44 +00:00
|
|
|
final Uuid _uuid = new Uuid();
|
|
|
|
|
|
|
|
@override
|
|
|
|
FutureOr<PseudoApplication> findClient(String clientId) {
|
|
|
|
return clientId == pseudoApplication.id ? pseudoApplication : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> verifyClient(
|
|
|
|
PseudoApplication client, String clientSecret) async {
|
|
|
|
return client.secret == clientSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2017-10-16 06:38:46 +00:00
|
|
|
Future requestAuthorizationCode(
|
2017-09-29 02:16:44 +00:00
|
|
|
PseudoApplication client,
|
|
|
|
String redirectUri,
|
|
|
|
Iterable<String> scopes,
|
|
|
|
String state,
|
|
|
|
RequestContext req,
|
|
|
|
ResponseContext res) async {
|
|
|
|
if (state == 'hello')
|
|
|
|
return 'Hello ${pseudoApplication.id}:${pseudoApplication.secret}';
|
|
|
|
|
|
|
|
var authCode = _uuid.v4();
|
|
|
|
var authCodes = req.grab<Map<String, String>>('authCodes');
|
|
|
|
authCodes[authCode] = state;
|
|
|
|
|
|
|
|
res.headers['content-type'] = 'application/json';
|
|
|
|
var result = {'code': authCode};
|
|
|
|
if (state?.isNotEmpty == true) result['state'] = state;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2017-10-16 06:38:46 +00:00
|
|
|
Future<AuthorizationTokenResponse> exchangeAuthorizationCodeForToken(
|
2017-09-29 02:16:44 +00:00
|
|
|
String authCode,
|
|
|
|
String redirectUri,
|
|
|
|
RequestContext req,
|
|
|
|
ResponseContext res) async {
|
|
|
|
var authCodes = req.grab<Map<String, String>>('authCodes');
|
|
|
|
var state = authCodes[authCode];
|
|
|
|
var refreshToken = state == 'can_refresh' ? '${authCode}_refresh' : null;
|
2017-10-16 06:38:46 +00:00
|
|
|
return new AuthorizationTokenResponse('${authCode}_access',
|
2017-09-29 02:16:44 +00:00
|
|
|
refreshToken: refreshToken);
|
|
|
|
}
|
|
|
|
}
|