StsClient
extends AbstractApi
in package
Base class all API clients are inheriting.
Table of Contents
Properties
- $awsErrorFactory : AwsErrorFactoryInterface
- $configuration : Configuration
- $credentialProvider : CredentialProvider
- $endpointCache : EndpointCache
- $httpClient : HttpClientInterface
- $logger : LoggerInterface
- $signers : array<string, Signer>
Methods
- __construct() : mixed
- assumeRole() : AssumeRoleResponse
- Returns a set of temporary security credentials that you can use to access Amazon Web Services resources. These temporary credentials consist of an access key ID, a secret access key, and a security token. Typically, you use `AssumeRole` within your account or for cross-account access. For a comparison of `AssumeRole` with other API operations that produce temporary credentials, see Requesting Temporary Security Credentials [^1] and Comparing the Amazon Web Services STS API operations [^2] in the *IAM User Guide*.
- assumeRoleWithWebIdentity() : AssumeRoleWithWebIdentityResponse
- Returns a set of temporary security credentials for users who have been authenticated in a mobile or web application with a web identity provider. Example providers include the OAuth 2.0 providers Login with Amazon and Facebook, or any OpenID Connect-compatible identity provider such as Google or Amazon Cognito federated identities [^1].
- getCallerIdentity() : GetCallerIdentityResponse
- Returns details about the IAM user or role whose credentials are used to call the operation.
- getConfiguration() : Configuration
- presign() : string
- discoverEndpoints() : array<string|int, EndpointInterface>
- getAwsErrorFactory() : AwsErrorFactoryInterface
- getEndpoint() : string
- Build the endpoint full uri.
- getEndpointMetadata() : array{endpoint: string, signRegion: string, signService: string, signVersions: string[]}
- Returns the AWS endpoint metadata for the given region.
- getResponse() : Response
- getServiceCode() : string
- getSignatureScopeName() : string
- getSignatureVersion() : string
- getSignerFactories() : array<string, callable(string, string): Signer>
- getDiscoveredEndpoint() : string
- getSigner() : Signer
Properties
$awsErrorFactory
private
AwsErrorFactoryInterface
$awsErrorFactory
$configuration
private
Configuration
$configuration
$credentialProvider
private
CredentialProvider
$credentialProvider
$endpointCache
private
EndpointCache
$endpointCache
$httpClient
private
HttpClientInterface
$httpClient
$logger
private
LoggerInterface
$logger
$signers
private
array<string, Signer>
$signers
Methods
__construct()
public
__construct([mixed $configuration = [] ][, CredentialProvider|null $credentialProvider = null ][, HttpClientInterface|null $httpClient = null ][, LoggerInterface|null $logger = null ]) : mixed
Parameters
- $configuration : mixed = []
- $credentialProvider : CredentialProvider|null = null
- $httpClient : HttpClientInterface|null = null
- $logger : LoggerInterface|null = null
assumeRole()
Returns a set of temporary security credentials that you can use to access Amazon Web Services resources. These temporary credentials consist of an access key ID, a secret access key, and a security token. Typically, you use `AssumeRole` within your account or for cross-account access. For a comparison of `AssumeRole` with other API operations that produce temporary credentials, see Requesting Temporary Security Credentials [^1] and Comparing the Amazon Web Services STS API operations [^2] in the *IAM User Guide*.
public
assumeRole(ProvidedContext|array)[], '@region'?: string|null}|AssumeRoleRequest $input) : AssumeRoleResponse
Permissions
The temporary security credentials created by AssumeRole
can be used to make API calls to any Amazon Web Services
service with the following exception: You cannot call the Amazon Web Services STS GetFederationToken
or
GetSessionToken
API operations.
(Optional) You can pass inline or managed session policies ^3 to this operation. You can pass a single JSON policy document to use as an inline session policy. You can also specify up to 10 managed policy Amazon Resource Names (ARNs) to use as managed session policies. The plaintext that you use for both inline and managed session policies can't exceed 2,048 characters. Passing policies to this operation returns new temporary credentials. The resulting session's permissions are the intersection of the role's identity-based policy and the session policies. You can use the role's temporary credentials in subsequent Amazon Web Services API calls to access resources in the account that owns the role. You cannot use session policies to grant more permissions than those allowed by the identity-based policy of the role that is being assumed. For more information, see Session Policies ^4 in the IAM User Guide.
When you create a role, you create two policies: a role trust policy that specifies who can assume the role, and a permissions policy that specifies what can be done with the role. You specify the trusted principal that is allowed to assume the role in the role trust policy.
To assume a role from a different account, your Amazon Web Services account must be trusted by the role. The trust relationship is defined in the role's trust policy when the role is created. That trust policy states which accounts are allowed to delegate that access to users in the account.
A user who wants to access a role in a different account must also have permissions that are delegated from the
account administrator. The administrator must attach a policy that allows the user to call AssumeRole
for the ARN
of the role in the other account.
To allow a user to assume a role in the same account, you can do either of the following:
- Attach a policy to the user that allows the user to call
AssumeRole
(as long as the role's trust policy trusts the account). - Add the user as a principal directly in the role's trust policy.
You can do either because the role’s trust policy acts as an IAM resource-based policy. When a resource-based policy grants access to a principal in the same account, no additional identity-based policy is required. For more information about trust policies and resource-based policies, see IAM Policies ^5 in the IAM User Guide.
Tags
(Optional) You can pass tag key-value pairs to your session. These tags are called session tags. For more information about session tags, see Passing Session Tags in STS ^6 in the IAM User Guide.
An administrator must grant you the permissions necessary to pass session tags. The administrator can also create granular permissions to allow you to pass only specific session tags. For more information, see Tutorial: Using Tags for Attribute-Based Access Control ^7 in the IAM User Guide.
You can set the session tags as transitive. Transitive tags persist during role chaining. For more information, see Chaining Roles with Session Tags ^8 in the IAM User Guide.
Using MFA with AssumeRole
(Optional) You can include multi-factor authentication (MFA) information when you call AssumeRole
. This is useful
for cross-account scenarios to ensure that the user that assumes the role has been authenticated with an Amazon Web
Services MFA device. In that scenario, the trust policy of the role being assumed includes a condition that tests for
MFA authentication. If the caller does not include valid MFA information, the request to assume the role is denied.
The condition in a trust policy that tests for MFA authentication might look like the following example.
"Condition": {"Bool": {"aws:MultiFactorAuthPresent": true}}
For more information, see Configuring MFA-Protected API Access ^9 in the IAM User Guide guide.
To use MFA with AssumeRole
, you pass values for the SerialNumber
and TokenCode
parameters. The SerialNumber
value identifies the user's hardware or virtual MFA device. The TokenCode
is the time-based one-time password
(TOTP) that the MFA device produces.
Parameters
- $input : ProvidedContext|array)[], '@region'?: string|null}|AssumeRoleRequest
Tags
Return values
AssumeRoleResponseassumeRoleWithWebIdentity()
Returns a set of temporary security credentials for users who have been authenticated in a mobile or web application with a web identity provider. Example providers include the OAuth 2.0 providers Login with Amazon and Facebook, or any OpenID Connect-compatible identity provider such as Google or Amazon Cognito federated identities [^1].
public
assumeRoleWithWebIdentity(PolicyDescriptorType|array)[], Policy?: null|string, DurationSeconds?: null|int, '@region'?: string|null}|AssumeRoleWithWebIdentityRequest $input) : AssumeRoleWithWebIdentityResponse
For mobile applications, we recommend that you use Amazon Cognito. You can use Amazon Cognito with the Amazon Web Services SDK for iOS Developer Guide ^2 and the Amazon Web Services SDK for Android Developer Guide ^3 to uniquely identify a user. You can also supply the user with a consistent identity throughout the lifetime of an application.
To learn more about Amazon Cognito, see Amazon Cognito identity pools ^4 in Amazon Cognito Developer Guide.
Calling AssumeRoleWithWebIdentity
does not require the use of Amazon Web Services security credentials. Therefore,
you can distribute an application (for example, on mobile devices) that requests temporary security credentials
without including long-term Amazon Web Services credentials in the application. You also don't need to deploy
server-based proxy services that use long-term Amazon Web Services credentials. Instead, the identity of the caller
is validated by using a token from the web identity provider. For a comparison of AssumeRoleWithWebIdentity
with
the other API operations that produce temporary credentials, see Requesting Temporary Security Credentials ^5 and
Comparing the Amazon Web Services STS API operations ^6 in the IAM User Guide.
The temporary security credentials returned by this API consist of an access key ID, a secret access key, and a security token. Applications can use these temporary security credentials to sign calls to Amazon Web Services service API operations.
Session Duration
By default, the temporary security credentials created by AssumeRoleWithWebIdentity
last for one hour. However, you
can use the optional DurationSeconds
parameter to specify the duration of your session. You can provide a value
from 900 seconds (15 minutes) up to the maximum session duration setting for the role. This setting can have a value
from 1 hour to 12 hours. To learn how to view the maximum value for your role, see View the Maximum Session Duration
Setting for a Role ^7 in the IAM User Guide. The maximum session duration limit applies when you use the
AssumeRole*
API operations or the assume-role*
CLI commands. However the limit does not apply when you use those
operations to create a console URL. For more information, see Using IAM Roles ^8 in the IAM User Guide.
Permissions
The temporary security credentials created by AssumeRoleWithWebIdentity
can be used to make API calls to any Amazon
Web Services service with the following exception: you cannot call the STS GetFederationToken
or GetSessionToken
API operations.
(Optional) You can pass inline or managed session policies ^9 to this operation. You can pass a single JSON policy document to use as an inline session policy. You can also specify up to 10 managed policy Amazon Resource Names (ARNs) to use as managed session policies. The plaintext that you use for both inline and managed session policies can't exceed 2,048 characters. Passing policies to this operation returns new temporary credentials. The resulting session's permissions are the intersection of the role's identity-based policy and the session policies. You can use the role's temporary credentials in subsequent Amazon Web Services API calls to access resources in the account that owns the role. You cannot use session policies to grant more permissions than those allowed by the identity-based policy of the role that is being assumed. For more information, see Session Policies ^10 in the IAM User Guide.
Tags
(Optional) You can configure your IdP to pass attributes into your web identity token as session tags. Each session tag consists of a key name and an associated value. For more information about session tags, see Passing Session Tags in STS ^11 in the IAM User Guide.
You can pass up to 50 session tags. The plaintext session tag keys can’t exceed 128 characters and the values can’t exceed 256 characters. For these and additional limits, see IAM and STS Character Limits ^12 in the IAM User Guide.
An Amazon Web Services conversion compresses the passed inline session policy, managed policy ARNs, and session tags into a packed binary format that has a separate limit. Your request can fail for this limit even if your plaintext meets the other requirements. The
PackedPolicySize
response element indicates by percentage how close the policies and tags for your request are to the upper size limit.
You can pass a session tag with the same key as a tag that is attached to the role. When you do, the session tag overrides the role tag with the same key.
An administrator must grant you the permissions necessary to pass session tags. The administrator can also create granular permissions to allow you to pass only specific session tags. For more information, see Tutorial: Using Tags for Attribute-Based Access Control ^13 in the IAM User Guide.
You can set the session tags as transitive. Transitive tags persist during role chaining. For more information, see Chaining Roles with Session Tags ^14 in the IAM User Guide.
Identities
Before your application can call AssumeRoleWithWebIdentity
, you must have an identity token from a supported
identity provider and create a role that the application can assume. The role that your application assumes must
trust the identity provider that is associated with the identity token. In other words, the identity provider must be
specified in the role's trust policy.
! Calling AssumeRoleWithWebIdentity
can result in an entry in your CloudTrail logs. The entry includes the Subject
! ^15 of the provided web identity token. We recommend that you avoid using any personally identifiable information
! (PII) in this field. For example, you could instead use a GUID or a pairwise identifier, as suggested in the OIDC
! specification ^16.
For more information about how to use web identity federation and the AssumeRoleWithWebIdentity
API, see the
following resources:
- Using Web Identity Federation API Operations for Mobile Apps ^17 and Federation Through a Web-based Identity Provider ^18.
- Web Identity Federation Playground ^19. Walk through the process of authenticating through Login with Amazon, Facebook, or Google, getting temporary security credentials, and then using those credentials to make a request to Amazon Web Services.
- Amazon Web Services SDK for iOS Developer Guide ^20 and Amazon Web Services SDK for Android Developer Guide ^21. These toolkits contain sample apps that show how to invoke the identity providers. The toolkits then show how to use the information from these providers to get and use temporary security credentials.
- Web Identity Federation with Mobile Applications ^22. This article discusses web identity federation and shows an example of how to use web identity federation to get access to content in Amazon S3.
Parameters
- $input : PolicyDescriptorType|array)[], Policy?: null|string, DurationSeconds?: null|int, '@region'?: string|null}|AssumeRoleWithWebIdentityRequest
Tags
Return values
AssumeRoleWithWebIdentityResponsegetCallerIdentity()
Returns details about the IAM user or role whose credentials are used to call the operation.
public
getCallerIdentity([array{'@region'?: string|null}|GetCallerIdentityRequest $input = [] ]) : GetCallerIdentityResponse
No permissions are required to perform this operation. If an administrator attaches a policy to your identity that explicitly denies access to the
sts:GetCallerIdentity
action, you can still perform this operation. Permissions are not required because the same information is returned when access is denied. To view an example response, see I Am Not Authorized to Perform: iam:DeleteVirtualMFADevice ^1 in the IAM User Guide.
Parameters
- $input : array{'@region'?: string|null}|GetCallerIdentityRequest = []
Tags
Return values
GetCallerIdentityResponsegetConfiguration()
public
final getConfiguration() : Configuration
Return values
Configurationpresign()
public
final presign(Input $input[, DateTimeImmutable|null $expires = null ]) : string
Parameters
- $input : Input
- $expires : DateTimeImmutable|null = null
Return values
stringdiscoverEndpoints()
protected
discoverEndpoints(string|null $region) : array<string|int, EndpointInterface>
Parameters
- $region : string|null
Return values
array<string|int, EndpointInterface>getAwsErrorFactory()
protected
getAwsErrorFactory() : AwsErrorFactoryInterface
Return values
AwsErrorFactoryInterfacegetEndpoint()
Build the endpoint full uri.
protected
getEndpoint(string $uri, array<string, string> $query, string|null $region) : string
Parameters
- $uri : string
-
or path
- $query : array<string, string>
-
parameters that should go in the query string
- $region : string|null
-
region provided by the user in the
@region
parameter of the Input
Return values
stringgetEndpointMetadata()
Returns the AWS endpoint metadata for the given region.
protected
getEndpointMetadata(string|null $region) : array{endpoint: string, signRegion: string, signService: string, signVersions: string[]}
When user did not provide a region, the client have to either return a global endpoint or fallback to the Configuration::DEFAULT_REGION constant.
This implementation is a BC layer for client that does not require core:^1.2.
Parameters
- $region : string|null
-
region provided by the user (without fallback to a default region)
Return values
array{endpoint: string, signRegion: string, signService: string, signVersions: string[]}getResponse()
protected
final getResponse(Request $request[, RequestContext|null $context = null ]) : Response
Parameters
- $request : Request
- $context : RequestContext|null = null
Return values
ResponsegetServiceCode()
protected
getServiceCode() : string
Return values
stringgetSignatureScopeName()
protected
getSignatureScopeName() : string
Return values
stringgetSignatureVersion()
protected
getSignatureVersion() : string
Return values
stringgetSignerFactories()
protected
getSignerFactories() : array<string, callable(string, string): Signer>
Return values
array<string, callable(string, string): Signer>getDiscoveredEndpoint()
private
getDiscoveredEndpoint(string $uri, array<string, string> $query, string|null $region, bool $usesEndpointDiscovery, bool $requiresEndpointDiscovery) : string
Parameters
- $uri : string
- $query : array<string, string>
- $region : string|null
- $usesEndpointDiscovery : bool
- $requiresEndpointDiscovery : bool
Return values
stringgetSigner()
private
getSigner(string|null $region) : Signer
Parameters
- $region : string|null
-
region provided by the user in the
@region
parameter of the Input