inferAuthorizationHeaderValue function

String inferAuthorizationHeaderValue(
  1. String authorization
)

Infers and adds an authorization scheme to an untyped authorization token.

Existing Basic, Bearer, and Digest values are returned unchanged. Otherwise, structurally valid JWTs are prefixed with Bearer, and valid Base64-encoded username:password credentials are prefixed with Basic. Values that do not match either format are returned unchanged.

This method only infers the header format; it does not verify a JWT signature or authenticate the credentials.

Implementation

String inferAuthorizationHeaderValue(String authorization) {
  if (authorization.isEmpty) return authorization;
  if (authorization.startsWith('Basic ')) return authorization;
  if (authorization.startsWith('Bearer ')) return authorization;
  if (authorization.startsWith('Digest ')) return authorization;

  if (isJwt(authorization)) return 'Bearer $authorization';
  if (isBase64Credentials(authorization)) return 'Basic $authorization';

  return authorization;
}