getAccessToken static method
Implementation
static Future<String> getAccessToken() async {
String accessToken = _accessToken;
if (_accessToken.isEmpty) {
accessToken = await _secureStorage.read(key: _accessTokenKey) ?? "";
if (accessToken.isEmpty) return "";
_accessToken = accessToken;
}
try {
if (JwtDecoder.getRemainingTime(accessToken).inMinutes > 1) {
try {
if (_heraPublicKey == "") {
throw Exception("missing required Hera public key");
}
JWT.verify(accessToken, RSAPublicKey(_heraPublicKey));
return accessToken; // we have a valid access token with time left
} catch (e) {
print("could not verify token with err" + e.toString());
return "";
}
} else {
String refreshToken = await getRefreshToken();
if (refreshToken.isEmpty) {
return "";
}
// token is expired - refresh
HeraRequest req = HeraRequest()
..namespace = _namespace
..accessToken = accessToken;
Token _token = Token()
..deviceInfo = (await HeraExtenstion.getDeviceInfo())
..refreshToken = _refreshToken;
req.token = _token;
HeraResponse refreshResp = await _heraClient.refreshToken(req);
_setAccessToken(refreshResp.token.accessToken);
_setRefreshToken(refreshResp.token.refreshToken);
return refreshResp.token.accessToken;
}
} catch (e) {
await _secureStorage.delete(key: _refreshTokenKey);
print("could not refresh token with err: " + e.toString());
}
return "";
}