Authorization.parse constructor

Authorization.parse(
  1. String auth
)

Parses an authorization header string into an Authorization object.

The auth parameter should be in the format of 'type value', where 'type' specifies the type of authorization (e.g., 'Basic', 'Bearer') and 'value' is the credential or token.

Example:

var auth = Authorization.parse('Basic dXNlcjpwYXNzd29yZA==');

Implementation

factory Authorization.parse(String auth) {
  final splitedAuth = auth.split(' ');
  if (splitedAuth.length <= 1) {
    return Authorization(type: AuthType.none);
  }

  final typeString = splitedAuth[0].trim().toLowerCase();
  if (typeString == 'basic') {
    return Authorization(
      type: AuthType.basic,
      value: splitedAuth[1].fromBase64(),
    );
  }

  if (typeString == 'bearer') {
    return Authorization(
      type: AuthType.basic,
      value: splitedAuth[1],
    );
  }

  return Authorization();
}