get code tests
This commit is contained in:
parent
2e50f7f71b
commit
7978d8c78a
2 changed files with 153 additions and 1 deletions
|
@ -341,7 +341,9 @@ abstract class AuthorizationServer<Client, User> {
|
||||||
var grantType = await _getParam(req, 'grant_type', state,
|
var grantType = await _getParam(req, 'grant_type', state,
|
||||||
body: true, throwIfEmpty: false);
|
body: true, throwIfEmpty: false);
|
||||||
|
|
||||||
if (grantType != 'authorization_code') {
|
if (grantType != 'authorization_code' &&
|
||||||
|
grantType != 'urn:ietf:params:oauth:grant-type:device_code' &&
|
||||||
|
grantType != null) {
|
||||||
var match =
|
var match =
|
||||||
_rgxBasic.firstMatch(req.headers.value('authorization') ?? '');
|
_rgxBasic.firstMatch(req.headers.value('authorization') ?? '');
|
||||||
|
|
||||||
|
@ -420,11 +422,39 @@ abstract class AuthorizationServer<Client, User> {
|
||||||
response = await extensionGrants[grantType](req, res);
|
response = await extensionGrants[grantType](req, res);
|
||||||
} else if (grantType == null) {
|
} else if (grantType == null) {
|
||||||
// This is a device code grant.
|
// This is a device code grant.
|
||||||
|
var clientId = await _getParam(req, 'client_id', state, body: true);
|
||||||
|
client = await findClient(clientId);
|
||||||
|
|
||||||
|
if (client == null) {
|
||||||
|
throw new AuthorizationException(
|
||||||
|
new ErrorResponse(
|
||||||
|
ErrorResponse.unauthorizedClient,
|
||||||
|
'Invalid "client_id" parameter.',
|
||||||
|
state,
|
||||||
|
),
|
||||||
|
statusCode: 400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
var scopes = await _getScopes(req, body: true);
|
var scopes = await _getScopes(req, body: true);
|
||||||
var deviceCodeResponse =
|
var deviceCodeResponse =
|
||||||
await requestDeviceCode(client, scopes, req, res);
|
await requestDeviceCode(client, scopes, req, res);
|
||||||
return deviceCodeResponse.toJson();
|
return deviceCodeResponse.toJson();
|
||||||
} else if (grantType == 'urn:ietf:params:oauth:grant-type:device_code') {
|
} else if (grantType == 'urn:ietf:params:oauth:grant-type:device_code') {
|
||||||
|
var clientId = await _getParam(req, 'client_id', state, body: true);
|
||||||
|
client = await findClient(clientId);
|
||||||
|
|
||||||
|
if (client == null) {
|
||||||
|
throw new AuthorizationException(
|
||||||
|
new ErrorResponse(
|
||||||
|
ErrorResponse.unauthorizedClient,
|
||||||
|
'Invalid "client_id" parameter.',
|
||||||
|
state,
|
||||||
|
),
|
||||||
|
statusCode: 400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
var deviceCode = await _getParam(req, 'device_code', state, body: true);
|
var deviceCode = await _getParam(req, 'device_code', state, body: true);
|
||||||
response = await exchangeDeviceCodeForToken(
|
response = await exchangeDeviceCodeForToken(
|
||||||
client, deviceCode, state, req, res);
|
client, deviceCode, state, req, res);
|
||||||
|
|
122
test/device_code_test.dart
Normal file
122
test/device_code_test.dart
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:angel_framework/angel_framework.dart';
|
||||||
|
import 'package:angel_test/angel_test.dart';
|
||||||
|
import 'package:angel_oauth2/angel_oauth2.dart';
|
||||||
|
import 'package:angel_validate/angel_validate.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
|
main() {
|
||||||
|
TestClient client;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
var app = new Angel();
|
||||||
|
var oauth2 = new _AuthorizationServer();
|
||||||
|
|
||||||
|
app.group('/oauth2', (router) {
|
||||||
|
router
|
||||||
|
..get('/authorize', oauth2.authorizationEndpoint)
|
||||||
|
..post('/token', oauth2.tokenEndpoint);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.logger = new Logger('angel_oauth2')
|
||||||
|
..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('/oauth2/token', body: {
|
||||||
|
'client_id': 'barr',
|
||||||
|
});
|
||||||
|
print(response.body);
|
||||||
|
expect(response, hasStatus(400));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('valid client id, no scopes', () async {
|
||||||
|
var response = await client.post('/oauth2/token', body: {
|
||||||
|
'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
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('valid client id, with scopes', () async {
|
||||||
|
var response = await client.post('/oauth2/token', body: {
|
||||||
|
'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(
|
||||||
|
queryParameters: {'scopes': 'bar,baz,quux'}).toString(),
|
||||||
|
"expires_in": 3600
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class _AuthorizationServer
|
||||||
|
extends AuthorizationServer<PseudoApplication, PseudoUser> {
|
||||||
|
@override
|
||||||
|
PseudoApplication findClient(String clientId) {
|
||||||
|
return clientId == pseudoApplication.id ? pseudoApplication : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<bool> verifyClient(
|
||||||
|
PseudoApplication client, String clientSecret) async {
|
||||||
|
return client.secret == clientSecret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
FutureOr<DeviceCodeResponse> requestDeviceCode(PseudoApplication client,
|
||||||
|
Iterable<String> scopes, RequestContext req, ResponseContext res) {
|
||||||
|
return new DeviceCodeResponse(
|
||||||
|
'foo',
|
||||||
|
'bar',
|
||||||
|
Uri.parse('https://regiostech.com')
|
||||||
|
.replace(queryParameters: {'scopes': scopes.join(',')}),
|
||||||
|
3600);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<AuthorizationTokenResponse> implicitGrant(
|
||||||
|
PseudoApplication client,
|
||||||
|
String redirectUri,
|
||||||
|
Iterable<String> scopes,
|
||||||
|
String state,
|
||||||
|
RequestContext req,
|
||||||
|
ResponseContext res) async {
|
||||||
|
return new AuthorizationTokenResponse('foo');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue