Access control

Last Updated: Nov 27, 2017

A mobile device is an untrusted environment. Therefore, the SDK provides two authentication modes based on your servers: STS authentication mode and self-signed mode.

STS authentication mode

Introduction

OSS can temporarily grant access authorizations using the Alibaba Cloud STS service. Alibaba Cloud STS (Security Token Service) is a web service providing temporary access tokens for cloud computing users.

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:

auth-sts

The solution is as follows:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

STS User Manual: Click to view details

OSS authentication policy configuration: Click to view details

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.

The initialization code is:

  1. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  2. OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");
  3. OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);

When you notice that the token is about to expire, you can rebuild a new OSSClient or update the CredentialProvider by the following method:

  1. oss.updateCredentialProvider(new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>"));

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:

  1. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  2. OSSCredentialProvider credentialProvider = new OSSFederationCredentialProvider() {
  3. @Override
  4. public OSSFederationToken getFederationToken() {
  5. // Here you must implement a method to get a FederationToken and construct it into the OSSFederationToken object to return it
  6. // If the token is not obtained, "nil" is returned
  7. OSSFederationToken * token;
  8. // The following are some codes that can be used to obtain the token, for example from your server
  9. ...
  10. return token;
  11. }
  12. };
  13. OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);

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.

Example:

Suppose the URL of the server you set up is http://localhost:8080/distribute-token.json. If you access the URL, the returned data is shown as follows:

  1. {
  2. "StatusCode": 200,
  3. "AccessKeyId":"STS.iA645eTOXEqP3cg3VeHf",
  4. "AccessKeySecret":"rV3VQrpFQ4BsyHSAvi5NVLpPIVffDJv4LojUBZCf",
  5. "Expiration":"2015-11-03T09:52:59Z",
  6. "SecurityToken":"CAES7QIIARKAAZPlqaN9ILiQZPS+JDkS/GSZN45RLx4YS/p3OgaUC+oJl3XSlbJ7StKpQ...."}

You can use the following codes to implement the OSSFederationCredentialProvider instance:

  1. OSSCredentialProvider credetialProvider = new OSSFederationCredentialProvider() {
  2. @Override
  3. public OSSFederationToken getFederationToken() {
  4. try {
  5. URL stsUrl = new URL("http://localhost:8080/distribute-token.json");
  6. HttpURLConnection conn = (HttpURLConnection) stsUrl.openConnection();
  7. InputStream input = conn.getInputStream();
  8. String jsonText = IOUtils.readStreamAsString(input, OSSConstants.DEFAULT_CHARSET_NAME);
  9. JSONObject jsonObjs = new JSONObject(jsonText);
  10. String ak = jsonObjs.getString("AccessKeyId");
  11. String sk = jsonObjs.getString("AccessKeySecret");
  12. String token = jsonObjs.getString("SecurityToken");
  13. String expiration = jsonObjs.getString("Expiration");
  14. return new OSSFederationToken(ak, sk, token, expiration);
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. return null;
  19. }
  20. };

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.

Signature algorithm reference: Click to view details

The content is a string merged according to various request parameters. The algorithm is:

  1. signature = "OSS " + AccessKeyId + ":" + base64(hmac-sha1(AccessKeySecret, content))

As follows:

  1. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  2. credentialProvider = new OSSCustomSignerCredentialProvider() {
  3. @Override
  4. public String signContent(String content) {
  5. // Here you must sign a string using the OSS-specified signature algorithm, merge the AccessKeyID with the signed string, and return the string
  6. // Generally, post the string to your business server and then return the signature
  7. // If the signing fails, "nil" is returned after the error is described
  8. // The following is a demo using a local algorithm for signing
  9. return "OSS " + AccessKeyId + ":" + base64(hmac-sha1(AccessKeySecret, content));
  10. }
  11. };
  12. OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);

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.

Thank you! We've received your feedback.