dart2.auth.http_bearer_auth.mustache Maven / Gradle / Ivy
{{>header}}
{{>part_of}}
typedef HttpBearerAuthProvider = String Function();
class HttpBearerAuth implements Authentication {
HttpBearerAuth();
dynamic _accessToken;
dynamic get accessToken => _accessToken;
set accessToken(dynamic accessToken) {
if (accessToken is! String && accessToken is! HttpBearerAuthProvider) {
throw ArgumentError('accessToken value must be either a String or a String Function().');
}
_accessToken = accessToken;
}
@override
Future applyToParams(List queryParams, Map headerParams,) async {
if (_accessToken == null) {
return;
}
String accessToken;
if (_accessToken is String) {
accessToken = _accessToken;
} else if (_accessToken is HttpBearerAuthProvider) {
accessToken = _accessToken!();
} else {
return;
}
if (accessToken.isNotEmpty) {
headerParams['Authorization'] = 'Bearer $accessToken';
}
}
}