accessToken method

Future<AccessToken> accessToken()

Retrieves and validates the current access token.

This method handles access token management including retrieval from local storage, validation, and automatic refresh through device integrity verification when needed.

Returns

A Future that resolves to a valid AccessToken

Throws

  • Exception if unable to retrieve or validate the access token
  • Exception if the token is expired or invalid

Example

try {
  final token = await calljmp.service.accessToken();
  print('Access token expires at: ${token.expiresAt}');
} catch (e) {
  print('Failed to get access token: $e');
}

Implementation

Future<AccessToken> accessToken() async {
  var token = await CalljmpStore.instance.get(CalljmpStoreKey.accessToken);
  if (token != null) {
    final result = AccessToken.tryParse(token);
    if (result.data != null) {
      return result.data!;
    }
    await CalljmpStore.instance.delete(CalljmpStoreKey.accessToken);
  }

  await _integrity.access();

  token = await CalljmpStore.instance.get(CalljmpStoreKey.accessToken);
  if (token == null) {
    throw Exception("Failed to retrieve access token after attestation");
  }

  final result = AccessToken.tryParse(token);
  if (result.data == null || result.error != null) {
    throw Exception("Failed to parse access token: ${result.error}");
  }

  final accessToken = result.data!;
  if (accessToken.isExpired) {
    throw Exception("Access token is expired");
  }

  return accessToken;
}