2017-10-16 06:38:46 +00:00
|
|
|
import 'dart:async';
|
2024-10-13 01:45:27 +00:00
|
|
|
import 'package:protevus_framework/protevus_framework.dart';
|
|
|
|
import 'package:protevus_test/protevus_test.dart';
|
|
|
|
import 'package:protevus_oauth2/protevus_oauth2.dart';
|
2017-10-16 06:38:46 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'common.dart';
|
|
|
|
|
2021-05-30 00:46:13 +00:00
|
|
|
void main() {
|
|
|
|
late TestClient client;
|
2017-10-16 06:38:46 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2024-10-12 10:35:14 +00:00
|
|
|
var app = Protevus();
|
2019-05-02 07:28:38 +00:00
|
|
|
var oauth2 = _AuthorizationServer();
|
2017-10-16 06:38:46 +00:00
|
|
|
|
|
|
|
app.group('/oauth2', (router) {
|
|
|
|
router
|
|
|
|
..get('/authorize', oauth2.authorizationEndpoint)
|
|
|
|
..post('/token', oauth2.tokenEndpoint);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.errorHandler = (e, req, res) async {
|
|
|
|
res.json(e.toJson());
|
|
|
|
};
|
|
|
|
|
|
|
|
client = await connectTo(app);
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() => client.close());
|
|
|
|
|
|
|
|
test('authenticate via implicit grant', () async {
|
|
|
|
var response = await client.get(
|
2021-05-30 00:46:13 +00:00
|
|
|
Uri.parse(
|
|
|
|
'/oauth2/authorize?response_type=token&client_id=foo&redirect_uri=http://foo.com&state=bar'),
|
2017-10-16 06:38:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
print('Headers: ${response.headers}');
|
|
|
|
expect(
|
|
|
|
response,
|
|
|
|
allOf(
|
|
|
|
hasStatus(302),
|
2018-11-08 15:32:36 +00:00
|
|
|
hasHeader('location',
|
|
|
|
'http://foo.com#access_token=foo&token_type=bearer&state=bar'),
|
2017-10-16 06:38:46 +00:00
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AuthorizationServer
|
|
|
|
extends AuthorizationServer<PseudoApplication, PseudoUser> {
|
|
|
|
@override
|
2021-05-30 00:46:13 +00:00
|
|
|
PseudoApplication? findClient(String? clientId) {
|
2017-10-16 06:38:46 +00:00
|
|
|
return clientId == pseudoApplication.id ? pseudoApplication : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> verifyClient(
|
2021-05-30 00:46:13 +00:00
|
|
|
PseudoApplication client, String? clientSecret) async {
|
2017-10-16 06:38:46 +00:00
|
|
|
return client.secret == clientSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2019-05-03 07:24:24 +00:00
|
|
|
Future<void> requestAuthorizationCode(
|
2017-10-16 06:38:46 +00:00
|
|
|
PseudoApplication client,
|
2021-05-30 00:46:13 +00:00
|
|
|
String? redirectUri,
|
2017-10-16 06:38:46 +00:00
|
|
|
Iterable<String> scopes,
|
|
|
|
String state,
|
|
|
|
RequestContext req,
|
2019-05-03 07:24:24 +00:00
|
|
|
ResponseContext res,
|
|
|
|
bool implicit) async {
|
|
|
|
var tok = AuthorizationTokenResponse('foo');
|
2021-05-30 00:46:13 +00:00
|
|
|
var uri = completeImplicitGrant(tok, Uri.parse(redirectUri!), state: state);
|
2019-05-03 07:24:24 +00:00
|
|
|
return res.redirect(uri);
|
2017-10-16 06:38:46 +00:00
|
|
|
}
|
|
|
|
}
|