With the STS, you can assign a third-party application or federated user (you can manage the user ID) an access credential with a custom validity period and permissions. The access credential in the application is called FederationToken.

Third-party applications or federated users can use these access credentials to directly call the Alibaba Cloud APIs or use the SDKs provided by Alibaba Cloud to access the APIs of cloud products.

You do not need to disclose your long-term key (AccessKey) to a third-party application. You only need to generate an access token and send the access token to the third-party application. You can customize the access permissions and the validity of this token.

You do not need to revoke permissions. The access token becomes invalid automatically when it expires.

For example, the interaction process in an application is shown in the following figure:

The solution is as follows:

Log on to the application. You can manage your application ID. You can customize the ID management system or use an external web account or OpenID. For each valid app user, the AppServer can precisely define the minimum access permission.

The AppServer requests a security token (SecurityToken) from the STS. Before the STS is called, the AppServer needs to determine the minimum access permission (in the policy syntax) of the application user and the expiration time of the authorization.Then, you can obtain the security token by calling the STS AssumeRole API. For more information about the roles and usage, see Role Management in the RAM User Guide.

The STS returns a valid access credential (FederationToken in the application) to the AppServer. The access credential contains a SecurityToken, a temporary pair of access keys (an AccessKeyID and an AccessKeySecret), and the expiration time.

The AppServer returns the FederationToken to the ClientApp. The ClientApp can cache this credential. When the credential becomes invalid, the ClientApp needs to request a new valid access credential from the AppServer. For example, if the access credential is valid for one hour, the ClientApp can request the AppServer to update the access credential every 30 minutes.

The ClientApp uses the locally cached FederationToken to request Alibaba Cloud service APIs. The cloud service detects the STS access credential, verifies the credential with the STS service, and responds to the user requests accordingly.

For more information about STS security tokens, see Role Management in the RAM User Guide. The key is to call the STS’s AssumeRole API to obtain the valid access credential. You can also directly use STS SDKs to call this method. Click to view details.

To use this authentication mode, you must first activate the Alibaba Cloud RAM service.

Set the StsToken directly

You can get a pair of the StsToken in the application by a certain method (such as network requests from your business server) and then use it to initialize the SDK. If this method is adopted, you must follow the expiration time of the STSToken. When the STSToken is about to expire, you must take the initiative to update the new STSToken for the SDK.

Implement the callback to get the StsToken

If you want the SDK to automatically help you manage Token updates, you must configure the SDK to get the token. In the application of the SDK, you must implement a callback, which gets a FederationToken (STSToken) and returns the token. The SDK uses the token for signing. When the token needs to be updated, the SDK calls the callback to get the token, as shown in the following figure:

Additionally, if you have obtained all the fields required by the token by other means, you can use the callback to directly return the token. In this case, you must manually update the token, and then reset the OSSCredentialProvider of the OSSClient instance.

Self-signed mode

You can store the AccessKeyID and AccessKeySecret to your business server and then obtain them by implementing the callback in the SDK. Post the merged signature string to be signed to the server, sign the string using the OSS-specified signature algorithm on the business server, and transfer the signed string to the callback, which returns the string.

Note: For the STS authentication mode and self-signed mode, make sure the results are returned when you call the implemented callback function. Therefore, if you must obtain the token and signature from the business server with a network request, we recommend that you call the synchronous APIs of the network library. The callback is run in the subthread of the request from the SDKs, which does not block the main thread.