Skip to content

create_o_auth2_token_with_token_exchange async

create_o_auth2_token_with_token_exchange(
    grant_type: TokenExchangeGrantType,
    *,
    config_overrides: Optional[AsyncSigninClientConfig] = None,
    subject_token: Optional[SubjectToken] = None,
    subject_token_type: Optional[SubjectTokenType] = None,
    assertion: Optional[Assertion] = None,
    client_id: Optional[JwtBearerClientId] = None,
    resource: Optional[Resource] = None,
    scope: Optional[Scope] = None,
) -> CreateOAuth2TokenWithTokenExchangeResponse

Exchanges a supported token for an OAuth 2.0 access token scoped to a resource, which you can use to access AWS services from applications.

Parameters:

  • grant_type (TokenExchangeGrantType) –

    The OAuth 2.0 grant type. Currently supports "urn:ietf:params:oauth:grant-type:token-exchange" and "urn:ietf:params:oauth:grant-type:jwt-bearer".

  • subject_token (Optional[SubjectToken], default: None ) –

    The subject token that represents the identity of the party on whose behalf the request is made. Required when "grant_type=urn:ietf:params:oauth:grant-type:token-exchange".

  • subject_token_type (Optional[SubjectTokenType], default: None ) –

    The token type of the subject token. Currently only supports "urn:ietf:params:oauth:token-type:jwt". Required when "grant_type=urn:ietf:params:oauth:grant-type:token-exchange".

  • assertion (Optional[Assertion], default: None ) –

    The JWT authorization grant, as defined in RFC 7523. Required when "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer".

  • client_id (Optional[JwtBearerClientId], default: None ) –

    The OAuth 2.0 client identifier. If provided with "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer", must match the assertion's "client_id" claim.

  • resource (Optional[Resource], default: None ) –

    The OAuth resource for which the access token is requested: - "grant_type=urn:ietf:params:oauth:grant-type:token-exchange": Required. Must be a service principal. Example: "glue.amazonaws.com". - "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer": Required when the assertion has no "resource" claim, otherwise must match it. Must be a URI. Example: "https://aws-mcp.us-east-1.api.aws/mcp".

  • scope (Optional[Scope], default: None ) –

    The IAM managed policy Amazon Resource Names (ARNs) to apply as session policies that further scope down the permissions of the issued access token, provided as a space-delimited list. Example: "arn:aws:iam::444455556666:policy/MyPolicy". If provided with "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer", must match the assertion's "scope" claim.

Raises:

Source code in docs_src/capo_signin/_services/async_signin.py
async def create_o_auth2_token_with_token_exchange(
    self,
    grant_type: "capo_signin.types.token_exchange_grant_type.TokenExchangeGrantType",
    *,
    config_overrides: Optional[AsyncSigninClientConfig] = None,
    subject_token: Optional["capo_signin.types.subject_token.SubjectToken"] = None,
    subject_token_type: Optional[
        "capo_signin.types.subject_token_type.SubjectTokenType"
    ] = None,
    assertion: Optional["capo_signin.types.assertion.Assertion"] = None,
    client_id: Optional[
        "capo_signin.types.jwt_bearer_client_id.JwtBearerClientId"
    ] = None,
    resource: Optional["capo_signin.types.resource.Resource"] = None,
    scope: Optional["capo_signin.types.scope.Scope"] = None,
) -> "capo_signin.types.create_o_auth2_token_with_token_exchange_response.CreateOAuth2TokenWithTokenExchangeResponse":
    """Exchanges a supported token for an OAuth 2.0 access token scoped to a resource, which you can use to access AWS services from applications.

    Args:
        grant_type: The OAuth 2.0 grant type. Currently supports "urn:ietf:params:oauth:grant-type:token-exchange" and "urn:ietf:params:oauth:grant-type:jwt-bearer".
        subject_token: The subject token that represents the identity of the party on whose behalf the request is made. Required when "grant_type=urn:ietf:params:oauth:grant-type:token-exchange".
        subject_token_type: The token type of the subject token. Currently only supports "urn:ietf:params:oauth:token-type:jwt". Required when "grant_type=urn:ietf:params:oauth:grant-type:token-exchange".
        assertion: The JWT authorization grant, as defined in RFC 7523. Required when "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer".
        client_id: The OAuth 2.0 client identifier. If provided with "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer", must match the assertion's "client_id" claim.
        resource: The OAuth resource for which the access token is requested: - "grant_type=urn:ietf:params:oauth:grant-type:token-exchange": Required. Must be a service principal. Example: "glue.amazonaws.com". - "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer": Required when the assertion has no "resource" claim, otherwise must match it. Must be a URI. Example: "https://aws-mcp.us-east-1.api.aws/mcp".
        scope: The IAM managed policy Amazon Resource Names (ARNs) to apply as session policies that further scope down the permissions of the issued access token, provided as a space-delimited list. Example: "arn:aws:iam::444455556666:policy/MyPolicy". If provided with "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer", must match the assertion's "scope" claim.

    Raises:
        capo_signin.errors.internal_server_exception.InternalServerException: The request failed because of an internal server error.
        capo_signin.errors.invalid_grant_exception.InvalidGrantException: The request failed because the provided grant is invalid or expired.
        capo_signin.errors.invalid_scope_exception.InvalidScopeException: The request failed because the requested scope is invalid or exceeds the scope that can be granted.
        capo_signin.errors.service_unavailable_exception.ServiceUnavailableException: The request failed because a dependency is temporarily unavailable. You can retry the request.
        capo_signin.errors.too_many_requests_error.TooManyRequestsError: The request failed because you exceeded the request rate limit for this operation.
        capo_signin.errors.validation_exception.ValidationException: The request failed because it contains a malformed, missing, or invalid parameter.
        capo_signin.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_signin.types.create_o_auth2_token_with_token_exchange_request.CreateOAuth2TokenWithTokenExchangeRequest]",
    ) -> AsyncOperationResponse[
        "capo_signin.types.create_o_auth2_token_with_token_exchange_response.CreateOAuth2TokenWithTokenExchangeResponse"
    ]:
        import capo_signin._operations.signin.create_o_auth2_token_with_token_exchange

        (
            output,
            http_response,
        ) = await capo_signin._operations.signin.create_o_auth2_token_with_token_exchange.async_create_o_auth2_token_with_token_exchange(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_signin.types.create_o_auth2_token_with_token_exchange_request.CreateOAuth2TokenWithTokenExchangeRequest",
    ) -> dict[str, Any]:
        import capo_signin.types.create_o_auth2_token_with_token_exchange_request

        return capo_signin.types.create_o_auth2_token_with_token_exchange_request.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_signin.types.create_o_auth2_token_with_token_exchange_response.CreateOAuth2TokenWithTokenExchangeResponse",
    ) -> dict[str, Any]:
        import capo_signin.types.create_o_auth2_token_with_token_exchange_response

        return capo_signin.types.create_o_auth2_token_with_token_exchange_response.redact_sensitive(
            output
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "CreateOAuth2TokenWithTokenExchange",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_signin.types.create_o_auth2_token_with_token_exchange_request.CreateOAuth2TokenWithTokenExchangeRequest = {
        "grant_type": grant_type
    }
    if subject_token is not None:
        input_["subject_token"] = subject_token
    if subject_token_type is not None:
        input_["subject_token_type"] = subject_token_type
    if assertion is not None:
        input_["assertion"] = assertion
    if client_id is not None:
        input_["client_id"] = client_id
    if resource is not None:
        input_["resource"] = resource
    if scope is not None:
        input_["scope"] = scope

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output