import 'dart:async'; import 'package:protevus_container/mirrors.dart'; import 'package:protevus_framework/protevus_framework.dart'; import 'package:protevus_oauth2/protevus_oauth2.dart'; import 'package:logging/logging.dart'; import '../test/common.dart'; void main() { Logger.root.level = Level.ALL; Logger.root.onRecord.listen((record) { print( '${record.time} ${record.level.name.padLeft(6, ' ')} [${record.loggerName}] : ${record.message}'); if (record.error != null) print(record.error); if (record.stackTrace != null) print(record.stackTrace); }); // Declae the function void setUp() async { var app = Protevus(reflector: MirrorsReflector()); var oauth2 = _AuthorizationServer(); app.group('/oauth2', (router) { router ..get('/authorize', oauth2.authorizationEndpoint) ..post('/token', oauth2.tokenEndpoint); }); //app.logger.level = Level.ALL; app.logger = Logger("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()); }; } setUp(); } class _AuthorizationServer extends AuthorizationServer { var logger = Logger('AuthorizationServer'); @override PseudoApplication? findClient(String? clientId) { return clientId == pseudoApplication.id ? pseudoApplication : null; } @override Future verifyClient( PseudoApplication client, String? clientSecret) async { return client.secret == clientSecret; } @override FutureOr requestDeviceCode(PseudoApplication client, Iterable scopes, RequestContext req, ResponseContext res) { return DeviceCodeResponse( 'foo', 'bar', Uri.parse('https://regiostech.com') .replace(queryParameters: {'scopes': scopes.join(',')}), 3600); } @override FutureOr exchangeDeviceCodeForToken( PseudoApplication client, String? deviceCode, String state, RequestContext req, ResponseContext res) { print("[Server] exchangeDeviceCodeForToken"); print("[Server] $deviceCode"); print("[Server] $client"); if (deviceCode == 'brute') { print("[Server] Throws AuthorizationException"); throw AuthorizationException(ErrorResponse( ErrorResponse.slowDown, 'Ho, brother! Ho, whoa, whoa, whoa now! You got too much dip on your chip!', state)); } return AuthorizationTokenResponse('foo'); } }