policyTriggerSilently method
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:
- B2COperationState.SUCCESS if successful,
- B2COperationState.CLIENT_ERROR if an error occurred,
- B2COperationState.USER_INTERACTION_REQUIRED if it the policy trigger cannot be completed without user intervention (e.g. refresh token expired).
- B2COperationState.SERVICE_ERROR if there is a configuration error with respect to the authority setting or if the authentication provider is down for some reasons.
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 ||
exception.message.contains(_B2C_INTERACTION_REQUIRED)) {
/* 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));
}
}
}