getAuthorizationHeaderValue method

Future<String> getAuthorizationHeaderValue()

Returns future that resolves "Authorization" header value used for authenticating.

Throws DataException if credential is not valid.

Implementation

// This method returns future to make sure in future we could use the
// [Credential] interface for OAuth2.0 authentication too - which requires
// token rotation (refresh) that's async job.
Future<String> getAuthorizationHeaderValue() {
  if (!isValid()) {
    throw DataException(
      'Saved credential for "$url" pub repository is not supported by '
      'current version of Dart SDK.',
    );
  }

  final String tokenValue;
  final environment = env;
  if (environment != null) {
    final value = Platform.environment[environment];
    if (value == null) {
      throw DataException(
        'Saved credential for "$url" pub repository requires environment '
        'variable named "$env" but not defined.',
      );
    }
    tokenValue = value;
  } else {
    tokenValue = token!;
  }
  if (!isValidBearerToken(tokenValue)) {
    throw DataException(
        'Credential token for $url is not a valid Bearer token. '
        r'It should match `^[a-zA-Z0-9._~+/=-]+$` '
        'Found: $tokenValue');
  }

  return Future.value('Bearer $tokenValue');
}