platform/packages/oauth2/test/device_code_test.dart

172 lines
4.8 KiB
Dart
Raw Normal View History

2018-12-14 07:40:37 +00:00
import 'dart:async';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_test/angel3_test.dart';
import 'package:angel3_oauth2/angel3_oauth2.dart';
2018-12-14 07:40:37 +00:00
import 'package:logging/logging.dart';
import 'package:test/test.dart';
import 'common.dart';
void main() {
late TestClient client;
2018-12-14 07:40:37 +00:00
setUp(() async {
2019-05-02 07:28:38 +00:00
var app = Angel();
var oauth2 = _AuthorizationServer();
2018-12-14 07:40:37 +00:00
app.group('/oauth2', (router) {
router
..get('/authorize', oauth2.authorizationEndpoint)
..post('/token', oauth2.tokenEndpoint);
});
2019-05-02 07:28:38 +00:00
app.logger = Logger('angel_oauth2')
2018-12-14 07:40:37 +00:00
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
app.errorHandler = (e, req, res) async {
res.json(e.toJson());
};
client = await connectTo(app);
});
tearDown(() => client.close());
group('get initial code', () {
test('invalid client id', () async {
var response = await client.post(Uri.parse('/oauth2/token'), body: {
2018-12-14 07:40:37 +00:00
'client_id': 'barr',
});
print(response.body);
expect(response, hasStatus(400));
});
test('valid client id, no scopes', () async {
var response = await client.post(Uri.parse('/oauth2/token'), body: {
2018-12-14 07:40:37 +00:00
'client_id': 'foo',
});
print(response.body);
expect(
response,
allOf(
hasStatus(200),
isJson({
'device_code': 'foo',
'user_code': 'bar',
'verification_uri': 'https://regiostech.com?scopes',
'expires_in': 3600
2018-12-14 07:40:37 +00:00
}),
));
});
test('valid client id, with scopes', () async {
var response = await client.post(Uri.parse('/oauth2/token'), body: {
2018-12-14 07:40:37 +00:00
'client_id': 'foo',
'scope': 'bar baz quux',
});
print(response.body);
expect(
response,
allOf(
hasStatus(200),
isJson({
'device_code': 'foo',
'user_code': 'bar',
'verification_uri': Uri.parse('https://regiostech.com').replace(
2018-12-14 07:40:37 +00:00
queryParameters: {'scopes': 'bar,baz,quux'}).toString(),
'expires_in': 3600
2018-12-14 07:40:37 +00:00
}),
));
});
});
2018-12-15 07:19:35 +00:00
group('get token', () {
test('valid device code + timing', () async {
var response = await client.post(Uri.parse('/oauth2/token'), body: {
2018-12-15 07:19:35 +00:00
'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',
'client_id': 'foo',
'device_code': 'bar',
});
print(response.body);
expect(
response,
allOf(
hasStatus(200),
isJson({'token_type': 'bearer', 'access_token': 'foo'}),
2018-12-15 07:19:35 +00:00
));
});
// The rationale for only testing one possible error response is that
// they all only differ in terms of the `code` string sent down,
// which is chosen by the end user.
//
// The logic for throwing errors and turning them into responses
// has already been tested.
test('failure', () async {
var response = await client.post(Uri.parse('/oauth2/token'), body: {
2018-12-15 07:19:35 +00:00
'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',
'client_id': 'foo',
'device_code': 'brute',
});
print(response.body);
expect(
response,
allOf(
hasStatus(400),
isJson({
'error': 'slow_down',
'error_description':
'Ho, brother! Ho, whoa, whoa, whoa now! You got too much dip on your chip!'
2018-12-15 07:19:35 +00:00
}),
));
});
});
2018-12-14 07:40:37 +00:00
}
class _AuthorizationServer
extends AuthorizationServer<PseudoApplication, PseudoUser> {
@override
PseudoApplication? findClient(String? clientId) {
2018-12-14 07:40:37 +00:00
return clientId == pseudoApplication.id ? pseudoApplication : null;
}
@override
Future<bool> verifyClient(
PseudoApplication client, String? clientSecret) async {
2018-12-14 07:40:37 +00:00
return client.secret == clientSecret;
}
@override
FutureOr<DeviceCodeResponse> requestDeviceCode(PseudoApplication client,
Iterable<String> scopes, RequestContext req, ResponseContext res) {
2019-05-02 07:28:38 +00:00
return DeviceCodeResponse(
2018-12-14 07:40:37 +00:00
'foo',
'bar',
Uri.parse('https://regiostech.com')
.replace(queryParameters: {'scopes': scopes.join(',')}),
3600);
}
@override
2018-12-15 07:19:35 +00:00
FutureOr<AuthorizationTokenResponse> exchangeDeviceCodeForToken(
2018-12-14 07:40:37 +00:00
PseudoApplication client,
String? deviceCode,
2018-12-14 07:40:37 +00:00
String state,
RequestContext req,
2018-12-15 07:19:35 +00:00
ResponseContext res) {
if (deviceCode == 'brute') {
2019-05-02 07:28:38 +00:00
throw AuthorizationException(ErrorResponse(
2018-12-15 07:19:35 +00:00
ErrorResponse.slowDown,
'Ho, brother! Ho, whoa, whoa, whoa now! You got too much dip on your chip!',
2018-12-15 07:19:35 +00:00
state));
}
2019-05-02 07:28:38 +00:00
return AuthorizationTokenResponse('foo');
2018-12-14 07:40:37 +00:00
}
}