Skip to content

revoke_user_session

revoke_user_session(
    access_token: TokenModelType,
    session_id: SessionIdType,
    *,
    config_overrides: Optional[CognitoIdentityProviderClientConfig] = None,
    revoke_associated_sessions: Optional[RevokeAssociatedSessionsType] = None,
) -> RevokeUserSessionResponse

Revokes a specific session for the currently signed-in user. Amazon Cognito authorizes this operation with the user's access token, revokes all refresh and access tokens that are associated with the session, and deletes the session record. You can't revoke the session that the calling access token belongs to.

Parameters:

  • access_token (TokenModelType) –

    A valid access token that Amazon Cognito issued to the user. Amazon Cognito uses this token to authorize the request and to identify the user.

  • session_id (SessionIdType) –

    The ID of the session to revoke. This is a 28-character string of lowercase letters and digits, prefixed with tkn for a token session or sso for an SSO session. This value can't identify the caller's own current session.

  • revoke_associated_sessions (Optional[RevokeAssociatedSessionsType], default: None ) –

    Specifies whether to also revoke the sessions that are linked to the targeted session. NONE, the default, revokes only the targeted session. LINKED also revokes the directly linked session or sessions, interpreted relative to the type of the targeted session: revoking a token session also revokes the parent SSO session it came through, and revoking an SSO session also revokes the token sessions that came through it. The set of linked sessions is eventually consistent, so associated sessions are typically revoked within a few seconds.

Raises:

  • ForbiddenException –

    This exception is thrown when WAF doesn't allow your request based on a web ACL that's associated with your user pool.

  • InternalErrorException –

    This exception is thrown when Amazon Cognito encounters an internal error.

  • InvalidParameterException –

    This exception is thrown when the Amazon Cognito service encounters an invalid parameter.

  • NotAuthorizedException –

    This exception is thrown when a user isn't authorized.

  • OperationNotEnabledException –

    This exception is thrown when an operation is not available in the current region or for the current user pool configuration. This can occur when attempting to perform operations that are not supported in secondary replica regions.

  • PasswordResetRequiredException –

    This exception is thrown when a password reset is required.

  • ResourceNotFoundException –

    This exception is thrown when the Amazon Cognito service can't find the requested resource.

  • TooManyRequestsException –

    This exception is thrown when the user has made too many requests for a given operation.

  • UserNotConfirmedException –

    This exception is thrown when a user isn't confirmed successfully.

  • UserNotFoundException –

    This exception is thrown when a user isn't found.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

Example revoke one of a user's own sessions The following example revokes a single token session that belongs to the user who owns the provided access token, such as signing out a lost or stolen device. Only the targeted session is revoked.

>>> client.revoke_user_session(access_token='eyJra456defEXAMPLE', session_id='tknm5k9p2q7r4s8t1v6w3x0y5z2b', revoke_associated_sessions='NONE')
Source code in docs_src/capo_cognito_identity_provider/_services/cognito_identity_provider.py
def revoke_user_session(
    self,
    access_token: "capo_cognito_identity_provider.types.token_model_type.TokenModelType",
    session_id: "capo_cognito_identity_provider.types.session_id_type.SessionIdType",
    *,
    config_overrides: Optional[CognitoIdentityProviderClientConfig] = None,
    revoke_associated_sessions: Optional[
        "capo_cognito_identity_provider.types.revoke_associated_sessions_type.RevokeAssociatedSessionsType"
    ] = None,
) -> "capo_cognito_identity_provider.types.revoke_user_session_response.RevokeUserSessionResponse":
    """<p>Revokes a specific session for the currently signed-in user. Amazon Cognito authorizes this operation with the user's access token, revokes all refresh and access tokens that are associated with the session, and deletes the session record. You can't revoke the session that the calling access token belongs to.</p>

    Args:
        access_token: <p>A valid access token that Amazon Cognito issued to the user. Amazon Cognito uses this token to authorize the request and to identify the user.</p>
        session_id: <p>The ID of the session to revoke. This is a 28-character string of lowercase letters and digits, prefixed with <code>tkn</code> for a token session or <code>sso</code> for an SSO session. This value can't identify the caller's own current session.</p>
        revoke_associated_sessions: <p>Specifies whether to also revoke the sessions that are linked to the targeted session. <code>NONE</code>, the default, revokes only the targeted session. <code>LINKED</code> also revokes the directly linked session or sessions, interpreted relative to the type of the targeted session: revoking a token session also revokes the parent SSO session it came through, and revoking an SSO session also revokes the token sessions that came through it. The set of linked sessions is eventually consistent, so associated sessions are typically revoked within a few seconds.</p>

    Raises:
        capo_cognito_identity_provider.errors.forbidden_exception.ForbiddenException: <p>This exception is thrown when WAF doesn't allow your request based on a web ACL that's associated with your user pool.</p>
        capo_cognito_identity_provider.errors.internal_error_exception.InternalErrorException: <p>This exception is thrown when Amazon Cognito encounters an internal error.</p>
        capo_cognito_identity_provider.errors.invalid_parameter_exception.InvalidParameterException: <p>This exception is thrown when the Amazon Cognito service encounters an invalid parameter.</p>
        capo_cognito_identity_provider.errors.not_authorized_exception.NotAuthorizedException: <p>This exception is thrown when a user isn't authorized.</p>
        capo_cognito_identity_provider.errors.operation_not_enabled_exception.OperationNotEnabledException: <p>This exception is thrown when an operation is not available in the current region or for the current user pool configuration. This can occur when attempting to perform operations that are not supported in secondary replica regions.</p>
        capo_cognito_identity_provider.errors.password_reset_required_exception.PasswordResetRequiredException: <p>This exception is thrown when a password reset is required.</p>
        capo_cognito_identity_provider.errors.resource_not_found_exception.ResourceNotFoundException: <p>This exception is thrown when the Amazon Cognito service can't find the requested resource.</p>
        capo_cognito_identity_provider.errors.too_many_requests_exception.TooManyRequestsException: <p>This exception is thrown when the user has made too many requests for a given operation.</p>
        capo_cognito_identity_provider.errors.user_not_confirmed_exception.UserNotConfirmedException: <p>This exception is thrown when a user isn't confirmed successfully.</p>
        capo_cognito_identity_provider.errors.user_not_found_exception.UserNotFoundException: <p>This exception is thrown when a user isn't found.</p>
        capo_cognito_identity_provider.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        Example revoke one of a user's own sessions
        The following example revokes a single token session that belongs to the user who owns the provided access token, such as signing out a lost or stolen device. Only the targeted session is revoked.

        >>> client.revoke_user_session(access_token='eyJra456defEXAMPLE', session_id='tknm5k9p2q7r4s8t1v6w3x0y5z2b', revoke_associated_sessions='NONE')
    """

    def _handler(
        req: "OperationRequest[capo_cognito_identity_provider.types.revoke_user_session_request.RevokeUserSessionRequest]",
    ) -> OperationResponse[
        "capo_cognito_identity_provider.types.revoke_user_session_response.RevokeUserSessionResponse"
    ]:
        import capo_cognito_identity_provider._operations.aws_cognito_identity_provider_service.revoke_user_session

        output, http_response = (
            capo_cognito_identity_provider._operations.aws_cognito_identity_provider_service.revoke_user_session.revoke_user_session(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_cognito_identity_provider.types.revoke_user_session_request.RevokeUserSessionRequest",
    ) -> dict[str, Any]:
        import capo_cognito_identity_provider.types.revoke_user_session_request

        return capo_cognito_identity_provider.types.revoke_user_session_request.redact_sensitive(
            input_
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("RevokeUserSession", redact_input=_redact_input),
    )
    input_: capo_cognito_identity_provider.types.revoke_user_session_request.RevokeUserSessionRequest = {
        "access_token": access_token,
        "session_id": session_id,
    }
    if revoke_associated_sessions is not None:
        input_["revoke_associated_sessions"] = revoke_associated_sessions

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output