validateToken method

  1. @override
Future<AuthResult> validateToken(
  1. String token, {
  2. List<String>? requiredScopes,
})
override

Validate a bearer token

Implementation

@override
Future<AuthResult> validateToken(String token, {List<String>? requiredScopes}) async {
  try {
    final introspection = await introspectToken(token);

    // Check if token is active
    if (introspection['active'] != true) {
      return const AuthResult.failure(error: 'Token is not active');
    }
    final tokenScopes = (introspection['scope'] as String?)
            ?.split(' ')
            .where((s) => s.isNotEmpty)
            .toList() ??
        <String>[];
    final aud = introspection['aud'];

    return AuthResult.success(
      userInfo: {
        'sub': introspection['sub'],
        'client_id': introspection['client_id'],
        'username': introspection['username'],
        'scope': introspection['scope'],
        'exp': introspection['exp'],
        'iat': introspection['iat'],
      },
      validatedScopes: tokenScopes,
      // No `aud` in the introspection response: nothing shows the token was
      // issued for this server, so it is reported with no audience (which
      // the server refuses) rather than as checked.
      audience: aud is String ? [aud] : aud is List ? [for (final a in aud) '$a'] : const [],
    );

  } catch (e) {
    return AuthResult.failure(error: 'Token validation failed: $e');
  }
}