policyTriggerSilently method

Future policyTriggerSilently(
  1. String tag,
  2. String subject,
  3. String policyName,
  4. List<String> scopes,
)

Run user flow silently using stored refresh token.

Once the user finishes with the flow, the stored access-token will be refreshed and stored in the sessionStorage or localStorage respectively as specified in the configuration file.

The tag argument is used to distinguish which invocation of this function generated the return callback. The subject is used to specify the user to authenticate (it corresponds to the

Returns a Future callable from the AzureB2C plugin.

It emits a B2COperationResult from B2COperationSource.POLICY_TRIGGER_SILENTLY with possible results:

Implementation

Future policyTriggerSilently(String tag, String subject, String policyName,
    List<String> scopes) async {
  try {
    var user = _users[subject];
    if (user == null) {
      _emitCallback(B2COperationResult(
          tag,
          B2COperationSource.POLICY_TRIGGER_SILENTLY,
          B2COperationState.CLIENT_ERROR));
      return;
    }
    var result = await _b2cApp!.acquireTokenSilent(SilentRequest()
      ..account = user
      ..scopes = scopes
      ..authority = _getAuthorityFromPolicyName(policyName));

    _accessTokens[subject] = _accessTokenFromAuthResult(result);

    _emitCallback(B2COperationResult(
        tag,
        B2COperationSource.POLICY_TRIGGER_SILENTLY,
        B2COperationState.SUCCESS));
  } on AuthException catch (exception) {
    log("Authentication failed: $exception", name: tag);
    if (exception is ClientAuthException) {
      _emitCallback(B2COperationResult(
          tag,
          B2COperationSource.POLICY_TRIGGER_SILENTLY,
          B2COperationState.CLIENT_ERROR));
    } else if (exception is InteractionRequiredAuthException) {
      /* Tokens expired or no session, retry with interactive */
      _emitCallback(B2COperationResult(
          tag,
          B2COperationSource.POLICY_TRIGGER_SILENTLY,
          B2COperationState.USER_INTERACTION_REQUIRED));
    } else if (exception is ServerException) {
      /* Exception when communicating with the STS, likely config issue */
      _emitCallback(B2COperationResult(
          tag,
          B2COperationSource.POLICY_TRIGGER_SILENTLY,
          B2COperationState.SERVICE_ERROR));
    }
  }
}