get_token function

Future<String?> get_token(
  1. String username, {
  2. dynamic retry_count = 0,
})

Implementation

Future<String?> get_token(String username, {retry_count = 0}) async {
  // use authorization code flow (native app) to get token
  //try to get token from database
  if (retry_count > 3) {
    throw Exception('too many retries');
  }
  var token_from_store = await tokenStore.get("token_$username");
  var token_validity = await dateStore.get("token_validity_$username");
  if (token_from_store != null &&
      token_validity != null &&
      DateTime.now().isBefore(token_validity)) {
    Show.success("USING TOKEN", "FROM STORE");
    return token_from_store;
  }
  try {
    final microsoftConfiguration = OAuth2Configuration(
      authorizationEndpoint: authorization_endpoint,
      tokenEndpoint: auth_token_endpoint,
      clientID: APP_ID,
      scopes: scopes,
    );
    final credentials = await OAuth2.login(
      microsoftConfiguration,
      redirect: (u) async {
        final string_url = auth_token_endpoint;
      },
      redirectPage: authorizationSuccessHtml,
    );
    var token = credentials.accessToken;
    var validity = await get_token_validity(token);
    await tokenStore.set("token_$username", token);
    await dateStore.set("token_validity_$username", validity);
    return token;
  } catch (e) {
    print(e);
    return await get_token(username, retry_count: retry_count + 1);
  }
}