Skip to content

get_derived_token

get_derived_token(
    client_id: ClientIdType,
    subject_token: GetDerivedTokenTokenModelType,
    subject_token_type: TokenTypeIdentifierType,
    *,
    config_overrides: Optional[CognitoIdentityProviderClientConfig] = None,
    client_secret: Optional[ClientSecretType] = None,
    subject_token_provider_name: Optional[ProviderNameType] = None,
    actor_token: Optional[GetDerivedTokenTokenModelType] = None,
    actor_token_type: Optional[TokenTypeIdentifierType] = None,
    actor_token_provider_name: Optional[ProviderNameType] = None,
    scopes: Optional[ScopeListType] = None,
    audiences: Optional[AudienceListType] = None,
    resources: Optional[ResourceListType] = None,
    requested_token_type: Optional[IssuedTokenTypeIdentifierType] = None,
) -> GetDerivedTokenResponse

Exchanges an existing token for a new token that is scoped and audience-bound for a specific downstream service, following the OAuth 2.0 Token Exchange grant type. For more information about the OAuth 2.0 Token Exchange grant type, see RFC 8693 - OAuth 2.0 Token Exchange on the IETF website. Present a SubjectToken, the credential that you already hold, to receive a new token appropriate for calling a specific resource server or audience.

If you also present an ActorToken that identifies an intermediary service, the issued token carries an act claim that identifies the intermediary, and downstream services can distinguish a user acting directly from an intermediary acting on the user's behalf. If you don't present an ActorToken, the issued token appears as if the user acted directly.

To use this operation, you must have IAM credentials, or you must present the requesting app client's ClientId and, if the client has a secret, ClientSecret, in the request.

The requesting app client must have ALLOW_TOKEN_EXCHANGE_IMPERSONATION or ALLOW_TOKEN_EXCHANGE_DELEGATION in its ExplicitAuthFlows (depending on whether you include an ActorToken), or the request fails.

Parameters:

  • client_id (ClientIdType) –

    The ID of the app client that requests the token exchange.

  • client_secret (Optional[ClientSecretType], default: None ) –

    The client secret of the requested app client, if the client has a secret.

  • subject_token (GetDerivedTokenTokenModelType) –

    The token that you want to exchange for a new token. This is the credential that represents the identity of the party on behalf of whom the request is being made.

  • subject_token_type (TokenTypeIdentifierType) –

    The type of the SubjectToken that you want to exchange. Valid values:

    • ACCESS_TOKEN – The subject token is an access token.

    • ID_TOKEN – The subject token is an ID token.

    • SAML2 – The subject token is a SAML 2.0 assertion.

  • subject_token_provider_name (Optional[ProviderNameType], default: None ) –

    The name of the third-party identity provider that issued the SubjectToken, if the subject token isn't a Amazon Cognito token. Provide this value when your SubjectToken came from a SAML or OIDC provider that you registered with your user pool, so that Amazon Cognito can validate it against that provider's configuration. Omit this parameter for a Amazon Cognito ID or access token.

  • actor_token (Optional[GetDerivedTokenTokenModelType], default: None ) –

    A token that identifies the intermediary service that is exchanging the token on behalf of the user. When you provide an ActorToken, the token that Amazon Cognito issues carries an act claim that identifies the intermediary, so that downstream services can distinguish a user acting directly from an intermediary acting on the user's behalf.

  • actor_token_type (Optional[TokenTypeIdentifierType], default: None ) –

    The type of the ActorToken that you provide. Required when you provide an ActorToken. Valid values:

    • ACCESS_TOKEN – The actor token is an access token.

    • ID_TOKEN – The actor token is an ID token.

    • SAML2 – The actor token is a SAML 2.0 assertion.

  • actor_token_provider_name (Optional[ProviderNameType], default: None ) –

    The name of the third-party identity provider that issued the ActorToken, if the actor token isn't a Amazon Cognito token.

  • scopes (Optional[ScopeListType], default: None ) –

    The scopes that you want the new token to have. Amazon Cognito issues a token with the intersection of these requested scopes, the scopes of your SubjectToken, and the scopes that are allowed for the requesting app client. If you don't specify any scopes, Amazon Cognito issues a token with the same scopes as your SubjectToken.

  • audiences (Optional[AudienceListType], default: None ) –

    The logical name of the target service where you intend to use the issued token. You can't request both Audiences and Resources in the same request.

  • resources (Optional[ResourceListType], default: None ) –

    The identifier of a resource server in your user pool where you intend to use the issued token. You can't request both Resources and Audiences in the same request.

  • requested_token_type (Optional[IssuedTokenTypeIdentifierType], default: None ) –

    The type of token that you want Amazon Cognito to issue. Valid values:

    • ACCESS_TOKEN – Issues an access token.

    • ID_TOKEN – Issues an ID token.

    Amazon Cognito can't issue a SAML assertion, so SAML2 isn't a valid value for this parameter.

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.

  • InvalidLambdaResponseException –

    This exception is thrown when Amazon Cognito encounters an invalid Lambda response.

  • 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.

  • 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.

  • UnexpectedLambdaException –

    This exception is thrown when Amazon Cognito encounters an unexpected exception with Lambda.

  • UserLambdaValidationException –

    This exception is thrown when the Amazon Cognito service encounters a user validation exception with the Lambda service.

  • 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 exchange a subject token for an audience-bound access token The following example exchanges an access token for a new access token that is scoped and audience-bound for the solar-system-data resource server.

>>> client.get_derived_token(client_id='1example23456789', client_secret='13ka4h7u28d9oo44tqpq9djqsfvhvu8rk4d2ighvpu0k8fj1c2r9', subject_token='eyJra456defEXAMPLE', subject_token_type='ACCESS_TOKEN', audiences=['solar-system-data'], scopes=['solar-system-data/asteroids.read'])
Source code in docs_src/capo_cognito_identity_provider/_services/cognito_identity_provider.py
def get_derived_token(
    self,
    client_id: "capo_cognito_identity_provider.types.client_id_type.ClientIdType",
    subject_token: "capo_cognito_identity_provider.types.get_derived_token_token_model_type.GetDerivedTokenTokenModelType",
    subject_token_type: "capo_cognito_identity_provider.types.token_type_identifier_type.TokenTypeIdentifierType",
    *,
    config_overrides: Optional[CognitoIdentityProviderClientConfig] = None,
    client_secret: Optional[
        "capo_cognito_identity_provider.types.client_secret_type.ClientSecretType"
    ] = None,
    subject_token_provider_name: Optional[
        "capo_cognito_identity_provider.types.provider_name_type.ProviderNameType"
    ] = None,
    actor_token: Optional[
        "capo_cognito_identity_provider.types.get_derived_token_token_model_type.GetDerivedTokenTokenModelType"
    ] = None,
    actor_token_type: Optional[
        "capo_cognito_identity_provider.types.token_type_identifier_type.TokenTypeIdentifierType"
    ] = None,
    actor_token_provider_name: Optional[
        "capo_cognito_identity_provider.types.provider_name_type.ProviderNameType"
    ] = None,
    scopes: Optional[
        "capo_cognito_identity_provider.types.scope_list_type.ScopeListType"
    ] = None,
    audiences: Optional[
        "capo_cognito_identity_provider.types.audience_list_type.AudienceListType"
    ] = None,
    resources: Optional[
        "capo_cognito_identity_provider.types.resource_list_type.ResourceListType"
    ] = None,
    requested_token_type: Optional[
        "capo_cognito_identity_provider.types.issued_token_type_identifier_type.IssuedTokenTypeIdentifierType"
    ] = None,
) -> "capo_cognito_identity_provider.types.get_derived_token_response.GetDerivedTokenResponse":
    """<p>Exchanges an existing token for a new token that is scoped and audience-bound for a specific downstream service, following the OAuth 2.0 Token Exchange grant type. For more information about the OAuth 2.0 Token Exchange grant type, see <a href="https://tools.ietf.org/html/rfc8693">RFC 8693 - OAuth 2.0 Token Exchange</a> on the IETF website. Present a <code>SubjectToken</code>, the credential that you already hold, to receive a new token appropriate for calling a specific resource server or audience.</p> <p>If you also present an <code>ActorToken</code> that identifies an intermediary service, the issued token carries an <code>act</code> claim that identifies the intermediary, and downstream services can distinguish a user acting directly from an intermediary acting on the user's behalf. If you don't present an <code>ActorToken</code>, the issued token appears as if the user acted directly.</p> <p>To use this operation, you must have IAM credentials, or you must present the requesting app client's <code>ClientId</code> and, if the client has a secret, <code>ClientSecret</code>, in the request.</p> <p>The requesting app client must have <code>ALLOW_TOKEN_EXCHANGE_IMPERSONATION</code> or <code>ALLOW_TOKEN_EXCHANGE_DELEGATION</code> in its <code>ExplicitAuthFlows</code> (depending on whether you include an <code>ActorToken</code>), or the request fails.</p>

    Args:
        client_id: <p>The ID of the app client that requests the token exchange.</p>
        client_secret: <p>The client secret of the requested app client, if the client has a secret.</p>
        subject_token: <p>The token that you want to exchange for a new token. This is the credential that represents the identity of the party on behalf of whom the request is being made.</p>
        subject_token_type: <p>The type of the <code>SubjectToken</code> that you want to exchange. Valid values:</p> <ul> <li> <p> <code>ACCESS_TOKEN</code> – The subject token is an access token.</p> </li> <li> <p> <code>ID_TOKEN</code> – The subject token is an ID token.</p> </li> <li> <p> <code>SAML2</code> – The subject token is a SAML 2.0 assertion.</p> </li> </ul>
        subject_token_provider_name: <p>The name of the third-party identity provider that issued the <code>SubjectToken</code>, if the subject token isn't a Amazon Cognito token. Provide this value when your <code>SubjectToken</code> came from a SAML or OIDC provider that you registered with your user pool, so that Amazon Cognito can validate it against that provider's configuration. Omit this parameter for a Amazon Cognito ID or access token.</p>
        actor_token: <p>A token that identifies the intermediary service that is exchanging the token on behalf of the user. When you provide an <code>ActorToken</code>, the token that Amazon Cognito issues carries an <code>act</code> claim that identifies the intermediary, so that downstream services can distinguish a user acting directly from an intermediary acting on the user's behalf.</p>
        actor_token_type: <p>The type of the <code>ActorToken</code> that you provide. Required when you provide an <code>ActorToken</code>. Valid values:</p> <ul> <li> <p> <code>ACCESS_TOKEN</code> – The actor token is an access token.</p> </li> <li> <p> <code>ID_TOKEN</code> – The actor token is an ID token.</p> </li> <li> <p> <code>SAML2</code> – The actor token is a SAML 2.0 assertion.</p> </li> </ul>
        actor_token_provider_name: <p>The name of the third-party identity provider that issued the <code>ActorToken</code>, if the actor token isn't a Amazon Cognito token.</p>
        scopes: <p>The scopes that you want the new token to have. Amazon Cognito issues a token with the intersection of these requested scopes, the scopes of your <code>SubjectToken</code>, and the scopes that are allowed for the requesting app client. If you don't specify any scopes, Amazon Cognito issues a token with the same scopes as your <code>SubjectToken</code>.</p>
        audiences: <p>The logical name of the target service where you intend to use the issued token. You can't request both <code>Audiences</code> and <code>Resources</code> in the same request.</p>
        resources: <p>The identifier of a resource server in your user pool where you intend to use the issued token. You can't request both <code>Resources</code> and <code>Audiences</code> in the same request.</p>
        requested_token_type: <p>The type of token that you want Amazon Cognito to issue. Valid values:</p> <ul> <li> <p> <code>ACCESS_TOKEN</code> – Issues an access token.</p> </li> <li> <p> <code>ID_TOKEN</code> – Issues an ID token.</p> </li> </ul> <p>Amazon Cognito can't issue a SAML assertion, so <code>SAML2</code> isn't a valid value for this parameter.</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_lambda_response_exception.InvalidLambdaResponseException: <p>This exception is thrown when Amazon Cognito encounters an invalid Lambda response.</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.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.unexpected_lambda_exception.UnexpectedLambdaException: <p>This exception is thrown when Amazon Cognito encounters an unexpected exception with Lambda.</p>
        capo_cognito_identity_provider.errors.user_lambda_validation_exception.UserLambdaValidationException: <p>This exception is thrown when the Amazon Cognito service encounters a user validation exception with the Lambda service.</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 exchange a subject token for an audience-bound access token
        The following example exchanges an access token for a new access token that is scoped and audience-bound for the solar-system-data resource server.

        >>> client.get_derived_token(client_id='1example23456789', client_secret='13ka4h7u28d9oo44tqpq9djqsfvhvu8rk4d2ighvpu0k8fj1c2r9', subject_token='eyJra456defEXAMPLE', subject_token_type='ACCESS_TOKEN', audiences=['solar-system-data'], scopes=['solar-system-data/asteroids.read'])
    """

    def _handler(
        req: "OperationRequest[capo_cognito_identity_provider.types.get_derived_token_request.GetDerivedTokenRequest]",
    ) -> OperationResponse[
        "capo_cognito_identity_provider.types.get_derived_token_response.GetDerivedTokenResponse"
    ]:
        import capo_cognito_identity_provider._operations.aws_cognito_identity_provider_service.get_derived_token

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

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

        return capo_cognito_identity_provider.types.get_derived_token_request.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_cognito_identity_provider.types.get_derived_token_response.GetDerivedTokenResponse",
    ) -> dict[str, Any]:
        import capo_cognito_identity_provider.types.get_derived_token_response

        return capo_cognito_identity_provider.types.get_derived_token_response.redact_sensitive(
            output
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "GetDerivedToken",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_cognito_identity_provider.types.get_derived_token_request.GetDerivedTokenRequest = {
        "client_id": client_id,
        "subject_token": subject_token,
        "subject_token_type": subject_token_type,
    }
    if client_secret is not None:
        input_["client_secret"] = client_secret
    if subject_token_provider_name is not None:
        input_["subject_token_provider_name"] = subject_token_provider_name
    if actor_token is not None:
        input_["actor_token"] = actor_token
    if actor_token_type is not None:
        input_["actor_token_type"] = actor_token_type
    if actor_token_provider_name is not None:
        input_["actor_token_provider_name"] = actor_token_provider_name
    if scopes is not None:
        input_["scopes"] = scopes
    if audiences is not None:
        input_["audiences"] = audiences
    if resources is not None:
        input_["resources"] = resources
    if requested_token_type is not None:
        input_["requested_token_type"] = requested_token_type

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