fetchRemoteToken static method

Future<void> fetchRemoteToken({
  1. required String clientId,
  2. required String clientSecret,
  3. required String pkce,
  4. required String code,
  5. required String stateSecret,
  6. String encrypter(
    1. String plaintext
    )?,
  7. 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.

pkce The code verifier that was generated in BridgeDiscoveryRepo.remoteAuthRequest. This is returned and captured from the deep link.

code The code that was returned from the deep link. This is what is being traded for the token.

stateSecret The state secret that was generated in BridgeDiscoveryRepo.remoteAuthRequest. This method can either take the full state value, or just the secret part.

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 state value is read from local storage, it is 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.

Implementation

static Future<void> fetchRemoteToken({
  required String clientId,
  required String clientSecret,
  required String pkce,
  required String code,
  required String stateSecret,
  String Function(String plaintext)? encrypter,
  String Function(String ciphertext)? decrypter,
}) async {
  // Only use the secret part of the state.
  if (stateSecret.contains("-")) {
    stateSecret = stateSecret.substring(0, stateSecret.indexOf("-"));
  } else {
    stateSecret = stateSecret;
  }

  /// The state secret that is stored in local storage.
  String storedStateSecret;

  // Read the state secret from local storage.
  storedStateSecret = await LocalStorageRepo.read(
        folder: Folder.tmp,
        fileName: BridgeDiscoveryRepo.stateSecretFile,
        decrypter: decrypter,
      ) ??
      "";

  // If the state secret does not match, return.
  if (storedStateSecret != stateSecret) return;

  // Call the service.
  Map<String, dynamic>? res = await TokenService.fetchRemoteToken(
    clientId: clientId,
    clientSecret: clientSecret,
    pkce: pkce,
    code: code,
  );

  if (res == null) return;

  await _writeRemoteTokensToLocal(res, encrypter);
}