Identity Authentication SDK

Updated at:

This SDK provides user identity authentication based on IoT Tokens. It integrates the Account and User SDK with the API Channel SDK to generate and manage user identity credentials. The SDK also authenticates the identity of users who initiate API requests.

Dependent SDKsOverview
API channelProvides API channel features.
Account and user SDKProvides account features.

Initialization

For more information, see SDK initialization.

Usage

When you use the API channel to execute business requests that require IoT identity authentication, the Identity Authentication SDK provides the `IoTCredentialProviderImpl` module. This module automatically populates the authentication information for the request. Use it as follows.

  • Build a business request that requires authentication

    When you build an `IoTRequest`, add the `AuthType` parameter. This parameter is registered as `iotAuth` by default during initialization. The following code provides an example.

    IoTRequest request = new IoTRequestBuilder()
        .setPath("/kit/debug/ping") // Set the path. Refer to the business API documentation.
        .setApiVersion("1.0.0")  // Set the API Version. Refer to the business API documentation.
        .addParam("request", paramMap)
        .setAuthType("iotAuth")   // This must be set to iotAuth.
        .build();             
  • Handle authentication failures

    The server-side logic does not support logging on to the same account from multiple devices. If an account is used to log on from multiple devices, only the last device to log on can access the IoT service. The other devices receive an authentication error. The SDK provides an interface to listen for this error. The following code shows how to use the interface.

    IoTCredentialManageImpl.getInstance(app).setIotTokenInvalidListener(IoTTokenInvalidListener listener)

    The `IoTTokenInvalidListener` is defined as follows.

    public interface IoTTokenInvalidListener {
        void onIoTTokenInvalid();
    }
    Note This API can be set only once globally. Call this API after initialization to listen for an invalid IoT Token. When `onIotTokenInvalid` is triggered, notify the user that the current session has expired and that they must log on again.

Get or refresh user credentials

You can use the following code to retrieve user authentication data. If NULL is returned, you can call the asynchronous refresh interface to try again.

IoTCredentialManage ioTCredentialManage = IoTCredentialManageImpl.getInstance(app);
if(ioTCredentialManage!=null){
    ioTCredentialManage.getIoTCredential();
}

The common fields of `IotCredentialData` are described as follows.

public class IoTCredentialData {
    /**
     * A temporary token for request identity verification.
     */
    public String iotToken;

    /**
     * The time when the iotToken was created. This is a standard UNIX timestamp.
     */
    public long iotTokenCreateTime;

    /**
     * The time when the iotToken expires. The time is synchronized with the server. Unit: milliseconds.
     */
    public long iotTokenExpireTime;

    /**
     * The refreshToken, used to refresh the iotToken.
     */
    public String refreshToken;

    /**
     * The time when the refreshToken was created. This is a standard UNIX timestamp.
     */
    public long refreshTokenCreateTime;

    /**
     * The time when the refreshToken expires. The time is synchronized with the server. Unit: milliseconds.
     */
    public long refreshTokenExpireTime;

    /**
     * The unique identity of the IoT user, identityId.
     */
    public String identity;
}

You can use the following code to refresh user authentication data.

IoTCredentialManage ioTCredentialManage = IoTCredentialManageImpl.getInstance(app);
if (ioTCredentialManage != null) {
    ioTCredentialManage.asyncRefreshIoTCredential(new IoTCredentialListener() {
        @Override        
        public void onRefreshIoTCredentialSuccess(IoTCredentialData ioTCredentialData) {
            Log.i(TAG, "Successfully refreshed IoTCredentialData: " + ioTCredentialData.toString());
        }

        @Override
        public void onRefreshIoTCredentialFailed(IoTCredentialManageError ioTCredentialManageError) {
            Log.i(TAG, "Failed to refresh IoTCredentialData.");
            if (ioTCredentialManageError != null) {
                Log.i(TAG, "Error code: " + ioTCredentialManageError.errorCode);
            }
        }
    });
}

Obfuscation configuration

In the proguard-rules.pro file, add the following code to exclude classes and methods from obfuscation.

-keep public class com.aliyun.iot.aep.sdk.credential** {
    public <methods>;
    public <fields>;
}

Common error codes

Error codeDescription
0The account is not logged on.
1Incorrect account type.
2Invalid account AuthCode. The session has expired.
3The refreshToken has expired. You must log on again.
4Incorrect message format from the server. Submit a ticket with the request and response.
5Account AuthCode verification failed.
-1Other errors. Check the specific business error returned by the APIClient or view the `detail` field.