verifyAuthorization function

Future<String> verifyAuthorization(
  1. RequestContext context,
  2. TokenVerifier tokenVerifier
)

Implementation

Future<String> verifyAuthorization(
  RequestContext context,
  TokenVerifier tokenVerifier,
) async {
  final authHeader = context.rawRequest.headers['Authorization'];
  if (authHeader == null) {
    throw BadRequestException(401, 'No Authorization header present');
  }
  final authHeaderParts = authHeader.split(' ');
  if (authHeaderParts.length != 2) {
    throw BadRequestException(
      401,
      // ignore: lines_longer_than_80_chars
      'Authorization must have exactly one space between `Bearer ` and the token',
    );
  }
  if (authHeaderParts.first != 'Bearer') {
    throw BadRequestException(
      401,
      'Authorization must start with `Bearer `',
    );
  }

  final token = authHeaderParts.last;

  return await tokenVerifier(token, context);
}