refreshRemoteToken static method

Future<void> refreshRemoteToken({
  1. required String clientId,
  2. required String clientSecret,
  3. String encrypter(
    1. String plaintext
    )?,
  4. String decrypter(
    1. String ciphertext
    )?,
})

This method fetches the token that grants temporary access to the bridge.

This is step 2. Step 1 is BridgeDiscoveryRepo.remoteAuthRequest.

clientId Identifies the client that is making the request. The value passed in this parameter must exactly match the value you receive from hue.

clientSecret The client secret you have received from Hue when registering for the Hue Remote API.

encrypter When the token is stored locally, it is encrypted. This parameter allows you to provide your own encryption method. This will be used in addition to the default encryption method. This will be performed before the default encryption method.

decrypter When the old tokens are read from local storage, they are decrypted. This parameter allows you to provide your own decryption method. This will be used in addition to the default decryption method. This will be performed after the default decryption method.

May throw ExpiredRefreshTokenException if the refresh token is expired. If this happens, get the user to grand access to the app again by using BridgeDiscoveryRepo.remoteAuthRequest.

Implementation

static Future<void> refreshRemoteToken({
  required String clientId,
  required String clientSecret,
  String Function(String plaintext)? encrypter,
  String Function(String ciphertext)? decrypter,
}) async {
  /// The state token data that is stored in local storage.
  String storedTokenDataStr;

  // Read the state secret from local storage.
  storedTokenDataStr = await LocalStorageRepo.read(
        folder: Folder.remoteTokens,
        fileName: remoteTokenFile,
        decrypter: decrypter,
      ) ??
      "";

  Map<String, dynamic> storedTokenData =
      JsonTool.readJson(storedTokenDataStr);

  String? oldRefreshToken = storedTokenData[ApiFields.refreshToken];

  if (oldRefreshToken == null) return;

  // Call the service.
  Map<String, dynamic>? res = await TokenService.refreshRemoteToken(
    clientId: clientId,
    clientSecret: clientSecret,
    refreshToken: oldRefreshToken,
  );

  if (res == null) return;

  await _writeRemoteTokensToLocal(res, encrypter);
}