authenticate method

  1. @override
Future<Principal> authenticate(
  1. Credential credential, {
  2. required Uint8List challenge,
})
override

Authenticates credential, returning the resolved principal.

Implementation

@override
Future<Principal> authenticate(
  Credential credential, {
  required Uint8List challenge,
}) async {
  final token = credential.token;
  if (token == null) {
    throw const AuthException('Missing token for token auth');
  }

  TokenGrant? matched;
  // Iterate all entries with a constant-time comparison so a wrong token does
  // not short-circuit and reveal information through timing.
  for (final entry in _tokens.entries) {
    if (_constantTimeEquals(entry.key, token)) {
      matched = entry.value;
    }
  }
  if (matched == null) {
    throw const AuthException('Invalid token');
  }
  if (matched.principal.value != credential.principal) {
    throw const AuthException('Token does not match principal');
  }

  return Principal(id: matched.principal, roles: matched.roles);
}