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_framework/http.dart';
|
|
|
|
import 'package:protevus_oauth2/protevus_oauth2.dart';
|
2021-05-30 00:46:13 +00:00
|
|
|
import 'package:collection/collection.dart' show IterableExtension;
|
2017-10-16 16:46:01 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:oauth2/oauth2.dart' as oauth2;
|
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() {
|
2024-10-12 10:35:14 +00:00
|
|
|
late Protevus app;
|
2021-05-30 00:46:13 +00:00
|
|
|
late Uri tokenEndpoint;
|
2017-10-16 06:38:46 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2024-10-12 10:35:14 +00:00
|
|
|
app = Protevus();
|
2019-05-02 07:28:38 +00:00
|
|
|
var auth = _AuthorizationServer();
|
2017-10-16 06:38:46 +00:00
|
|
|
|
|
|
|
app.group('/oauth2', (router) {
|
|
|
|
router
|
2017-10-16 16:46:01 +00:00
|
|
|
..get('/authorize', auth.authorizationEndpoint)
|
|
|
|
..post('/token', auth.tokenEndpoint);
|
2017-10-16 06:38:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.errorHandler = (e, req, res) async {
|
|
|
|
res.json(e.toJson());
|
|
|
|
};
|
|
|
|
|
2019-05-02 07:28:38 +00:00
|
|
|
app.logger = Logger('password_test')..onRecord.listen(print);
|
2017-10-16 16:46:01 +00:00
|
|
|
|
2024-10-12 10:35:14 +00:00
|
|
|
var http = ProtevusHttp(app);
|
2018-07-09 15:38:29 +00:00
|
|
|
var server = await http.startServer();
|
2017-10-16 16:46:01 +00:00
|
|
|
var url = 'http://${server.address.address}:${server.port}';
|
|
|
|
tokenEndpoint = Uri.parse('$url/oauth2/token');
|
2017-10-16 06:38:46 +00:00
|
|
|
});
|
|
|
|
|
2017-10-16 16:46:01 +00:00
|
|
|
tearDown(() => app.close());
|
2017-10-16 06:38:46 +00:00
|
|
|
|
|
|
|
test('authenticate via username+password', () async {
|
2017-10-16 16:46:01 +00:00
|
|
|
var client = await oauth2.resourceOwnerPasswordGrant(
|
|
|
|
tokenEndpoint,
|
|
|
|
'michael',
|
|
|
|
'jackson',
|
|
|
|
identifier: 'foo',
|
|
|
|
secret: 'bar',
|
2017-10-16 06:38:46 +00:00
|
|
|
);
|
2017-10-16 16:46:01 +00:00
|
|
|
print(client.credentials.toJson());
|
|
|
|
client.close();
|
|
|
|
expect(client.credentials.accessToken, 'foo');
|
|
|
|
expect(client.credentials.refreshToken, 'bar');
|
|
|
|
});
|
2017-10-16 06:38:46 +00:00
|
|
|
|
2017-10-16 16:46:01 +00:00
|
|
|
test('force correct username+password', () async {
|
2021-05-30 00:46:13 +00:00
|
|
|
oauth2.Client? client;
|
2017-10-16 16:46:01 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
client = await oauth2.resourceOwnerPasswordGrant(
|
|
|
|
tokenEndpoint,
|
|
|
|
'michael',
|
|
|
|
'jordan',
|
|
|
|
identifier: 'foo',
|
|
|
|
secret: 'bar',
|
|
|
|
);
|
2017-10-16 06:38:46 +00:00
|
|
|
|
2019-05-02 07:28:38 +00:00
|
|
|
throw StateError('should fail');
|
2017-10-16 16:46:01 +00:00
|
|
|
} on oauth2.AuthorizationException catch (e) {
|
|
|
|
expect(e.error, ErrorResponse.accessDenied);
|
|
|
|
} finally {
|
|
|
|
client?.close();
|
|
|
|
}
|
2017-10-16 06:38:46 +00:00
|
|
|
});
|
|
|
|
|
2017-10-16 16:46:01 +00:00
|
|
|
test('can refresh token', () async {
|
|
|
|
var client = await oauth2.resourceOwnerPasswordGrant(
|
|
|
|
tokenEndpoint,
|
|
|
|
'michael',
|
|
|
|
'jackson',
|
|
|
|
identifier: 'foo',
|
|
|
|
secret: 'bar',
|
2017-10-16 06:38:46 +00:00
|
|
|
);
|
2017-10-16 16:46:01 +00:00
|
|
|
client = await client.refreshCredentials();
|
|
|
|
print(client.credentials.toJson());
|
|
|
|
client.close();
|
|
|
|
expect(client.credentials.accessToken, 'baz');
|
|
|
|
expect(client.credentials.refreshToken, '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;
|
|
|
|
}
|
|
|
|
|
2017-10-16 16:46:01 +00:00
|
|
|
@override
|
|
|
|
Future<AuthorizationTokenResponse> refreshAuthorizationToken(
|
2021-05-30 00:46:13 +00:00
|
|
|
PseudoApplication? client,
|
|
|
|
String? refreshToken,
|
2017-10-16 16:46:01 +00:00
|
|
|
Iterable<String> scopes,
|
|
|
|
RequestContext req,
|
|
|
|
ResponseContext res) async {
|
2019-05-02 07:28:38 +00:00
|
|
|
return AuthorizationTokenResponse('baz', refreshToken: 'bar');
|
2017-10-16 16:46:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 06:38:46 +00:00
|
|
|
@override
|
|
|
|
Future<AuthorizationTokenResponse> resourceOwnerPasswordCredentialsGrant(
|
2021-05-30 00:46:13 +00:00
|
|
|
PseudoApplication? client,
|
|
|
|
String? username,
|
|
|
|
String? password,
|
2017-10-16 06:38:46 +00:00
|
|
|
Iterable<String> scopes,
|
|
|
|
RequestContext req,
|
|
|
|
ResponseContext res) async {
|
2021-05-30 00:46:13 +00:00
|
|
|
var user = pseudoUsers.firstWhereOrNull(
|
|
|
|
(u) => u.username == username && u.password == password);
|
2017-10-16 06:38:46 +00:00
|
|
|
|
|
|
|
if (user == null) {
|
2018-12-14 06:55:36 +00:00
|
|
|
var body = await req.parseBody().then((_) => req.bodyAsMap);
|
2019-05-02 07:28:38 +00:00
|
|
|
throw AuthorizationException(
|
|
|
|
ErrorResponse(
|
2017-10-16 06:38:46 +00:00
|
|
|
ErrorResponse.accessDenied,
|
|
|
|
'Invalid username or password.',
|
2018-11-08 15:32:36 +00:00
|
|
|
body['state']?.toString() ?? '',
|
2017-10-16 06:38:46 +00:00
|
|
|
),
|
|
|
|
statusCode: 401,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:28:38 +00:00
|
|
|
return AuthorizationTokenResponse('foo', refreshToken: 'bar');
|
2017-10-16 06:38:46 +00:00
|
|
|
}
|
|
|
|
}
|