You are here

class BearerTokenInfos in Backup and Migrate Dropbox 7.3

Class BearerTokenInfos manages the bearer tokens.

Each Dropbox destination has its own authorization (as it may be for a different account) and therefore its own active/valid bearer token. Storing the bearer token in the settings of the destination is not wat the settings are meant for, so we store all bearer tokens in a variable of our own.

Hierarchy

Expanded class hierarchy of BearerTokenInfos

File

./backup_migrate_dropbox.dropbox_api.inc, line 861

View source
class BearerTokenInfos {

  /**
   * Returns a bearer token for the current Dropbox destination
   *
   * @param string $id
   *   The id (machine name) of the current Dropbox destination.
   *
   * @return string|null
   *   A bearer token for the current Dropbox destination, if stored and still
   *   valid, null otherwise
   */
  public static function get($id) {
    $bearer_tokens = BearerTokenInfos::get_all();
    return isset($bearer_tokens[$id]) ? $bearer_tokens[$id]->access_token : NULL;
  }

  /**
   * Stores a bearer token that was obtained from Dropbox.
   *
   * @param string $id
   *   The id (machine name) of the current Dropbox destination.
   * @param object $response
   *  The (successful) response as received from Dropbox.
   *
   * @return string
   *   The newly received (and thus valid) bearer token for the current Dropbox
   *   destination.
   */
  public static function set($id, $response) {
    $bearer_tokens = BearerTokenInfos::get_all();
    $bearer_tokens[$id] = (object) [
      'access_token' => $response->access_token,
      'expires' => time() + (int) $response->expires_in,
    ];
    variable_set('backup_migrate_dropbox_bearer_tokens', json_encode($bearer_tokens));
    return $response->access_token;
  }

  /**
   * Removes the bearer token for the current Dropbox destination.
   *
   * @param string $id
   *   The id (machine name) for the current Dropbox destination.
   */
  public static function clear($id) {
    $bearer_tokens = BearerTokenInfos::get_all();
    unset($bearer_tokens[$id]);
    variable_set('backup_migrate_dropbox_bearer_tokens', json_encode($bearer_tokens));
  }

  /**
   * Returns all still valid bearer tokens.
   *
   * @return object[]
   *   All still valid bearer tokens.
   */
  private static function get_all() {

    // Convert to keyed array on the upper level only.
    $bearer_tokens = (array) json_decode(variable_get('backup_migrate_dropbox_bearer_tokens', '{}'));
    $now = time();
    return array_filter($bearer_tokens, function ($bearer_token) use ($now) {
      return $bearer_token->expires >= $now;
    });
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BearerTokenInfos::clear public static function Removes the bearer token for the current Dropbox destination.
BearerTokenInfos::get public static function Returns a bearer token for the current Dropbox destination
BearerTokenInfos::get_all private static function Returns all still valid bearer tokens.
BearerTokenInfos::set public static function Stores a bearer token that was obtained from Dropbox.