openharmony 鸿蒙 js-apis-useriam-useraccessctrl-sys

2025-06-12 浏览 (1)

@ohos.userIAM.userAccessCtrl (User Access Control) (System API)

The userAccessCtrl module provides APIs for setting and obtaining user identity authentication policies and verifying user identity authentication results.

NOTE

The initial APIs of this module are supported since API version 18. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { userAccessCtrl } from '@kit.UserAuthenticationKit';

AuthTokenType

Enumerates the authentication token types.

System capability: SystemCapability.UserIAM.UserAuth.Core

System API: This is a system API.

NameValueDescription
TOKEN_TYPE_LOCAL_AUTH0Authentication token issued based on the local authentication result.
TOKEN_TYPE_LOCAL_RESIGN1Authentication token issued based on the reused identity authentication result.
TOKEN_TYPE_COAUTH2Authentication token issued based on a collaborative authentication result of multiple devices.

AuthToken

Represents the AuthToken data returned after a successful verification.

System capability: SystemCapability.UserIAM.UserAuth.Core

System API: This is a system API.

NameTypeRead OnlyOptionalDescription
challengeUint8ArrayNoNoRandom challenge for the authentication.
authTrustLeveluserAuth.AuthTrustLevelNoNoAuthentication trust level.
authTypeuserAuth.UserAuthTypeNoNoCredential type for the identity authentication.
tokenTypeAuthTokenTypeNoNoAuthentication token type.
userIdnumberNoNoUser ID.
timeIntervalbigintNoNoTime elapsed since the issuance of the authentication token, in milliseconds.
secureUidbigintNoYesSecure user ID.
enrolledIdbigintNoYesCredential enrollment ID.
credentialIdbigintNoYesCredential ID.

userAccessCtrl.verifyAuthToken

verifyAuthToken(authToken: Uint8Array, allowableDuration: number): Promise<AuthToken>

Verifies an authentication token.

Required permissions: ohos.permission.USE_USER_ACCESS_MANAGER

System capability: SystemCapability.UserIAM.UserAuth.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
authTokenUint8ArrayYesAuthentication token to verify, which cannot exceed 1024.
allowableDurationnumberYesTime allowed for the authentication token to be used after being issued, in milliseconds. The value must be greater than 0 and less than or equal to 86,400,000.

Return value

TypeDescription
Promise<AuthToken>Promise used to return the parsed authentication token.

Error codes

For details about the error codes, see User Authentication Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401Incorrect parameters. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.
12500002General operation error.
12500015AuthToken integrity check failed.
12500016AuthToken has expired.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { userAccessCtrl } from '@kit.UserAuthenticationKit';
import { userAuth } from '@kit.UserAuthenticationKit';

try {
  const rand = cryptoFramework.createRandom();
  const allowableDuration: number = 5000;
  const len: number = 16;
  const randData: Uint8Array = rand?.generateRandomSync(len)?.data;
  const authParam: userAuth.AuthParam = {
    challenge: randData,
    authType: [userAuth.UserAuthType.PIN],
    authTrustLevel: userAuth.AuthTrustLevel.ATL3,
  };
  const widgetParam: userAuth.WidgetParam = {
    title: 'Enter password',
  };

  const userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam);
  console.info('get userAuth instance success');
  // The authentication result is returned by onResult() only after the authentication is started by start() of UserAuthInstance.
  userAuthInstance.on('result', {
    onResult (result) {
        if (!result.token) {
            console.error('userAuthInstance callback result.token is null');
            return;
        }
        // Initiate a request for verifying the AuthToken.
        userAccessCtrl.verifyAuthToken(result.token, allowableDuration)
            .then((retAuthToken: userAccessCtrl.AuthToken) => {
                Object.keys(retAuthToken).forEach((key) => {
                    console.info(`retAuthToken key:${key}, value:${retAuthToken[key]}`);
                })
            }).catch ((error: BusinessError) => {
                console.error(`verify authToken error. Code is ${error?.code}, message is ${error?.message}`);
            })
    }
  });
  console.info('auth on success');
  // Start authentication.
  userAuthInstance.start();
  console.info('auth start success');
} catch (error) {
  const err: BusinessError = error as BusinessError;
  console.error(`auth catch error. Code is ${err?.code}, message is ${err?.message}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙User Authentication Kit (User Authentication Service)

harmony 鸿蒙User Authentication Error Codes

harmony 鸿蒙@ohos.userIAM.faceAuth (Facial Authentication) (System API)

harmony 鸿蒙@ohos.userIAM.userAuth (User Authentication) (System API)

harmony 鸿蒙@ohos.userIAM.userAuth (User Authentication)

harmony 鸿蒙@ohos.userIAM.userAuthIcon (Embedded User Authentication Widget)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/IyaEPb