This document describes the integration process for the Device Risk SDK (HarmonyOS).
Usage notes
The Device Risk SDK requires HarmonyOS NEXT 5.0 or later (API 12 or later).
The HarmonyOS SDK has the following limits:
-
Supports mobile smart devices (phones or tablets) running HarmonyOS NEXT 5.0 or later.
-
Supports the arm64-v8a architecture.
Prerequisites
To fulfill the privacy compliance obligations of integrating third-party SDKs, reduce privacy violation risks, and achieve compliant operation, use the latest version published on the Alibaba Cloud Documentation Center. Before using Device Risk Detection, review the personal information processing rules and the Fraud Detection SDK Privacy Policy, and integrate following the SDK compliance guidelines.
Permissions
To enhance fraud detection accuracy, the SDK requires the following permissions:
|
Permission |
Required |
Description |
|
|
Yes |
Internet access. The SDK requires a network connection to function. |
|
|
Yes |
Network status verification. The SDK uses network status to provide better service. |
|
|
No (Recommended) |
Allow the app to store persistent data. The SDK uses this permission to enhance device fingerprint stability. |
|
|
No (Recommended) |
Multi-device collaboration. The SDK detects the status of multiple devices to enhance security. |
|
|
No (Recommended) |
Obtain the advertising identifier. The SDK uses IDFA information to improve device ID stability. |
Dependency configuration
-
Download the HarmonyOS SDK and extract the archive. The SDK is provided as a standard HarmonyOS .har package.
-
A single-architecture SO file is approximately 3.0 MB.
-
The Device Risk Detection SDK has built-in code protection and data encryption mechanisms, so the package size is relatively large.
-
In the oh-package.json5 file of your project, add the following dependency to the dependencies section:
{
"dependencies": {
"aliyundevice": "file:../libs/HarmonyOS-AliyunDevice-xxx.har"
}
}
Interface obfuscation configuration (important)
To prevent functional errors caused by interface obfuscation, check the configuration in the obfuscation.txt file within the .har package and do not remove that file. If the obfuscation configuration cannot be merged in certain compiler versions and integration fails, add the obfuscation-rules.txt file from the .har package to the main project of your app and include the obfuscation configuration there.
Call the SDK
After completing the preceding configuration, integrate the SDK client in the following three steps:
-
Initialize (initWithOptions)
-
Obtain the client token (getDeviceToken)
-
Send the token to your business server
1. Initialize (initWithOptions)
This function initializes the SDK and collects device information. Call it as early as possible during app startup while meeting compliance requirements. Call it only once per app launch.
-
Function prototype
export class SecurityInitListener {
// code indicates the API call status code
onInitFinish(code: number): void {}
}
public initWithOptions(ctx: Context,
userAppKey: string,
options: Map<string, string>,
securityInitListener: SecurityInitListener): void;
-
Parameters
-
ctx: The Context of the current Ability.
-
userAppKey: Identifies your application. You can apply for it under Device App Management in the Alibaba Cloud Management Console.
-
securityInitListener: The initialization callback. You can use the callback to check whether initialization succeeds. Default value: null. For valid values of the code field, see Status return values.
-
options: Optional information collection parameters. Default value: null. The available options are as follows.
Field
Description
Example
IPv6Whether to use an IPv6 domain name to report device information. 0 (default): Use an IPv4 domain name. 1: Use an IPv6 domain name.
"0"CustomUrlThe domain name of the data reporting server. Used for site-specific reporting. No configuration required by default.
"https://cloudauth-device.aliyuncs.com"CustomHostThe host of the data reporting server. Must be used together with CustomUrl. Neither needs to be set by default.
"cloudauth-device.aliyuncs.com"CustomUrl and CustomHost parameter description
For site-specific reporting, set CustomUrl and CustomHost to the designated endpoint for your region. No configuration required by default.
Region
Address
Singapore (default)
CustomUrl: https://cloudauth-device.ap-southeast-1.aliyuncs.com
CustomHost: cloudauth-device.ap-southeast-1.aliyuncs.comChina (Hong Kong)
CustomUrl: https://cloudauth-device.cn-hongkong.aliyuncs.com
CustomHost: cloudauth-device.cn-hongkong.aliyuncs.comGermany
CustomUrl: https://cloudauth-device.eu-central-1.aliyuncs.com
CustomHost: cloudauth-device.eu-central-1.aliyuncs.comUS
CustomUrl: https://cloudauth-device.us-west-1.aliyuncs.com
CustomHost: cloudauth-device.us-west-1.aliyuncs.com
-
-
Example
@State USER_PRODUCT_KEY: string = "123e4567e89b12d3a45642661417****";
let options: Map<string, string> = new Map<string, string>();
options.set("IPv6", "0"); // Set to IPv4
// Set a custom data reporting endpoint
// options.set("CustomUrl", "xxx"); Set the reporting site URL
// options.set("CustomHost", "xxx"); Set the reporting site host
SecurityDevice.getInstance().initWithOptions(getContext(),
this.USER_PRODUCT_KEY, options, null);
2. Obtain the client token (getDeviceToken)
Obtain the client token and report it to your business server. You can then use the server-side Server-side API integration to retrieve device risk information.
Ensure that the interval between calling initWithOptions and getDeviceToken is at least 3 seconds, or call getDeviceToken in the initialization success callback.
When calling getDeviceToken, we recommend passing a bizId to bind the token to a unique business ID. When querying results on the server side, pass the same ID to verify the risk of token tampering.
-
Function prototype
export class SecurityToken {
// Result code
public code:number = 0;
// deviceToken returned by the SDK
public token:string = "";
}
// Recommended: pass bizId
public getDeviceToken(bizId?: string): SecurityToken
Under good network conditions, the token is approximately 600 bytes. Under poor network conditions, the token is approximately 2 KB.
If a large number of long tokens appear in your business:
First, ensure that the client network is stable.
Second, ensure that the interval between the SDK initWithOptions and getDeviceToken calls is at least 3 seconds, or call getDeviceToken in the initialization success callback.
-
Example
// Example with bizId. bizId is the client's business ID; passing it is optional.
let bizId = "1234567890abcdef1234567890ab****";
let tokenObj: SecurityToken = SecurityDevice.getInstance().getDeviceToken(bizId);
if (tokenObj.code == SecurityCode.SC_SUCCESS) {
console.log("Aliyun Token: " + tokenObj.token);
} else {
console.log("Aliyun Code: " + tokenObj.code);
}
3. Send the token to your business server
After obtaining the deviceToken, pass it as a parameter to your business server. The server then calls the Alibaba Cloud Device Risk Detection API with the deviceToken to query and verify device risk information.
Status return values
|
SecurityCode |
Code |
Description |
|
SC_SUCCESS |
10000 |
SDK information collection succeeded. |
|
SC_NOT_INIT |
10001 |
SDK has not completed information collection. |
|
SC_NOT_PERMISSION |
10002 |
The required HarmonyOS basic permissions are not fully granted. |
|
SC_UNKNOWN_ERROR |
10003 |
Unknown system error. |
|
SC_NETWORK_ERROR |
10004 |
Network error. |
|
SC_NETWORK_ERROR_EMPTY |
10005 |
Network error. The response is empty. |
|
SC_NETWORK_ERROR_INVALID |
10006 |
The network response format is invalid. |
|
SC_PARSE_SRV_CFG_ERROR |
10007 |
Failed to parse the server configuration. |
|
SC_NETWORK_RET_CODE_ERROR |
10008 |
The gateway returned a failure. |
|
SC_APPKEY_EMPTY |
10009 |
AppKey is empty. |
|
SC_PARAMS_ERROR |
10010 |
Other parameter errors. |
|
SC_FGKEY_ERROR |
10011 |
Key calculation error. |
|
SC_APPKEY_ERROR |
10012 |
The SDK version and AppKey version do not match. |
Complete code example
import { SecurityCode, SecurityToken, SecurityDevice } from 'aliyundevice';
@Entry
@Component
struct Index {
@State message: string = 'Aliyun Device';
@State ALIYUN_APPKEY: string = "XXX";
build() {
Row() {
Column() {
Button(this.message)
.fontSize(18)
.fontWeight(FontWeight.Bold)
.onClick((event: ClickEvent) => {
// Initialize the SDK. Call only once during the app lifecycle.
SecurityDevice.getInstance().initWithOptions(getContext(), this.ALIYUN_APPKEY, null, null);
// Do not call getDeviceToken synchronously right after initialization. If initialization has not finished, a degraded deviceToken is returned.
setTimeout(() => {
let tokenObj: SecurityToken = SecurityDevice.getInstance().getDeviceToken();
if (tokenObj.code == SecurityCode.SC_SUCCESS) {
console.log("Aliyun Token: " + tokenObj.token);
} else {
console.log("Aliyun Code: " + tokenObj.code);
}
}, 2000);
})
.margin({ top: 10 })
}
.width('100%')
}
.height('100%')
}
}
Call the Device Risk Detection API
Pass the deviceToken and other parameters, and refer to Server-side API integration to call the Fraud Detection API for detection.
FAQ
For frequently asked questions about Device Risk SDK integration, see FAQ.