authenticate method
Implementation
@override
Future<void> authenticate(Map<String, String> metadata, String uri) async {
_accessToken = await token;
if (_accessToken == null || uri != _lastUri) {
try {
await obtainAccessCredentials(uri);
_lastUri = uri;
_accessToken = await token;
} catch (e) {
throw $grpc.GrpcError.unauthenticated('Require Authentication.');
}
}
if (_accessToken == null) {
throw $grpc.GrpcError.unauthenticated('Require Authentication.');
}
final tokenHeaders = _tokenHeader != null
? _tokenHeader!(_accessToken!)
: const <String, String>{};
metadata.addAll(tokenHeaders);
if (_shouldRefresh(null, _accessToken!)) {
// Token is about to expire. Extend it prematurely.
final authClient = http.Client();
unawaited(_refreshToken(_accessToken, _lastUri, authClient)
.catchError((_) {})
.then((refreshedToken) async {
unawaited(setToken(refreshedToken));
}).whenComplete(() => authClient.close()));
}
}