查看: 93|回复: 1

简析OpenHarmony用户鉴权

[复制链接]

1

主题

3

回帖

13

积分

新手上路

积分
13
发表于 2025-3-30 16:37:14 | 显示全部楼层 |阅读模式
用户凭证管理:
    pin_auth(口令认证):模块支持用户口令的设置,删除和认证功能。与用户IAM子系统基础框架配合,也可以支持用户口令修改的功能;口令认证作为OpenHarmony最基础的用户身份认证执行器,按照协同认证定义的资源注册接口,将口令认证相关资源信息注册到协同认证框架,并根据协同认证框架的调度,完成口令的设置,删除和认证功能;face_auth(人脸认证):支持用户人脸的录入,删除和认证功能;人脸认证是OpenHarmony支持的一种生物认证执行器,按照协同认证定义的资源注册接口,将人脸认证相关资源信息注册到协同认证框架,并根据协同认证框架的调度,调用人脸认证HDI,完成人脸的录入,认证,删除等功能。user_auth_framework:主要包括三个模块,用户认证、凭据管理和执行器管理:
      用户认证模块对外提供统一用户身份认证功能,并且开放生物特征认证API给三方应用调用。凭据管理模块提供系统内统一的用户凭据管理(增删改查)接口,向下通过执行器管理模块,调用系统内的执行器资源,完成用户凭据的生命周期管理和安全存储。执行器管理模块提供系统内执行器资源的统一管理和协同调度能力,当前支持口令执行器和人脸执行器的管理。

使用:

  • 接口
    1. import osAccount from '@ohos.account.osAccount'
    复制代码

    • 凭证管理
      1. //UserIdentityManager
      2. /**
      3. * Provides the abilities for managing user identity.
      4. * @name UserIdentityManager
      5. * @syscap SystemCapability.Account.OsAccount
      6. * @since 8
      7. */
      8. class UserIdentityManager {
      9.    /**
      10.    * Constructor to get the UserIdentityManager class instance.
      11.    * @returns Returns the UserIdentityManager class instance.
      12.    * @systemapi Hide this for inner system use.
      13.    * @since 8
      14.    */
      15.    constructor();
      16.    /**
      17.    * Opens session.
      18.    *
      19.    * Start an IDM operation to obtain challenge value.
      20.    * A challenge value of 0 indicates that opensession failed.
      21.    * @permission ohos.permission.MANAGE_USER_IDM
      22.    * @returns Returns a challenge value.
      23.    * @throws {BusinessError} 201 - permission denied.
      24.    * @throws {BusinessError} 401 - the parameter check failed.
      25.    * @throws {BusinessError} 12300001 - system service exception.
      26.    * @systemapi Hide this for inner system use.
      27.    * @since 8
      28.    */
      29.    openSession(callback: AsyncCallback): void;
      30.    openSession(): Promise;
      31.    /**
      32.    * Adds credential.
      33.    *
      34.    * Add user credential information, pass in credential addition method and credential information
      35.    * (credential type, subclass, if adding user's non password credentials, pass in password authentication token),
      36.    * and get the result / acquireinfo callback.
      37.    * @permission ohos.permission.MANAGE_USER_IDM
      38.    * @param credentialInfo Indicates the credential information.
      39.    * @param callback Indicates the callback to get results and acquireInfo.
      40.    * @throws {BusinessError} 201 - permission denied.
      41.    * @throws {BusinessError} 401 - the parameter check failed.
      42.    * @throws {BusinessError} 12300001 - system service exception.
      43.    * @throws {BusinessError} 12300002 - invalid credentialInfo.
      44.    * @systemapi Hide this for inner system use.
      45.    * @since 8
      46.    */
      47.    addCredential(credentialInfo: CredentialInfo, callback: IIdmCallback): void;
      48.    /**
      49.    * Updates credential.
      50.    * @permission ohos.permission.MANAGE_USER_IDM
      51.    * @param credentialInfo Indicates the credential information.
      52.    * @param callback Indicates the callback to get results and acquireInfo.
      53.    * @throws {BusinessError} 201 - permission denied.
      54.    * @throws {BusinessError} 401 - the parameter check failed.
      55.    * @throws {BusinessError} 12300001 - system service exception.
      56.    * @throws {BusinessError} 12300002 - invalid credentialInfo.
      57.    * @systemapi Hide this for inner system use.
      58.    * @since 8
      59.    */
      60.    updateCredential(credentialInfo: CredentialInfo, callback: IIdmCallback): void;
      61.    /**
      62.    * Closes session.
      63.    *
      64.    * End an IDM operation.  
      65.    * @permission ohos.permission.MANAGE_USER_IDM
      66.    * @systemapi Hide this for inner system use.
      67.    * @since 8
      68.    */
      69.    closeSession(): void;
      70.    /**
      71.    * Cancels entry with a challenge value.
      72.    * @permission ohos.permission.MANAGE_USER_IDM
      73.    * @param challenge Indicates the challenge value.
      74.    * @throws {BusinessError} 201 - permission denied.
      75.    * @throws {BusinessError} 401 - the parameter check failed.
      76.    * @throws {BusinessError} 12300001 - system service exception.
      77.    * @throws {BusinessError} 12300002 - invalid challenge.
      78.    * @systemapi Hide this for inner system use.
      79.    * @since 8
      80.    */
      81.    cancel(challenge: Uint8Array): void;
      82.    /**
      83.    * Deletes the user with the authentication token.
      84.    * @permission ohos.permission.MANAGE_USER_IDM
      85.    * @param token Indicates the authentication token.
      86.    * @param callback Indicates the callback to get the deletion result.
      87.    * @throws {BusinessError} 201 - permission denied.
      88.    * @throws {BusinessError} 401 - the parameter check failed.
      89.    * @throws {BusinessError} 12300001 - system service exception.
      90.    * @throws {BusinessError} 12300002 - invalid token.
      91.    * @systemapi Hide this for inner system use.
      92.    * @since 8
      93.    */
      94.    delUser(token: Uint8Array, callback: IIdmCallback): void;
      95.    /**
      96.    * Deletes the user credential information.
      97.    * @permission ohos.permission.MANAGE_USER_IDM
      98.    * @param credentialId Indicates the credential index.
      99.    * @param token Indicates the authentication token.
      100.    * @param callback Indicates the callback to get the deletion result.
      101.    * @throws {BusinessError} 201 - permission denied.
      102.    * @throws {BusinessError} 401 - the parameter check failed.
      103.    * @throws {BusinessError} 12300001 - system service exception.
      104.    * @throws {BusinessError} 12300002 - invalid credentialId or token.
      105.    * @systemapi Hide this for inner system use.
      106.    * @since 8
      107.    */
      108.    delCred(credentialId: Uint8Array, token: Uint8Array, callback: IIdmCallback): void;
      109.    /**
      110.    * Gets authentication information.
      111.    * @permission ohos.permission.USE_USER_IDM
      112.    * @param authType Indicates the authentication type.
      113.    * @param callback Indicates the callback to get all registered credential information of
      114.    * the specified type for the current user.
      115.    * @throws {BusinessError} 201 - permission denied.
      116.    * @throws {BusinessError} 401 - the parameter check failed.
      117.    * @throws {BusinessError} 12300001 - system service exception.
      118.    * @throws {BusinessError} 12300002 - invalid authType.
      119.    * @throws {BusinessError} 12300015 - the authType is not supported on current device.
      120.    * @throws {BusinessError} 12300016 - authentication timeout.
      121.    * @throws {BusinessError} 12300017 - authentication service is busy.
      122.    * @throws {BusinessError} 12300018 - authentication service is locked.
      123.    * @throws {BusinessError} 12300019 - the credential does not exist.
      124.    * @systemapi Hide this for inner system use.
      125.    * @since 8
      126.    */
      127.    getAuthInfo(callback: AsyncCallback): void;
      128.    getAuthInfo(authType: AuthType, callback: AsyncCallback): void;
      129.    getAuthInfo(authType?: AuthType): Promise;
      130. }
      复制代码
    • 用户管理
      1. /**
      2.     * Provides the abilities for user authentication.
      3.     * @name UserAuth
      4.     * @syscap SystemCapability.Account.OsAccount
      5.     * @since 8
      6.     */
      7.    class UserAuth {
      8.        /**
      9.         * Constructor to get the UserAuth class instance.
      10.         * @returns Returns the UserAuth class instance.
      11.         * @systemapi Hide this for inner system use.
      12.         * @since 8
      13.         */
      14.        constructor();
      15.        /**
      16.         * Gets version information.
      17.         * @returns Returns the version information.
      18.         * @systemapi Hide this for inner system use.
      19.         * @since 8
      20.         */
      21.        getVersion(): number;
      22.        /**
      23.         * Checks whether the authentication capability is available.
      24.         * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
      25.         * @param authType Indicates the credential type for authentication.
      26.         * @param authTrustLevel Indicates the trust level of authentication result.
      27.         * @returns Returns a status result.
      28.         * @throws {BusinessError} 201 - permission denied.
      29.         * @throws {BusinessError} 401 - the parameter check failed.
      30.         * @throws {BusinessError} 12300001 - system service exception.
      31.         * @throws {BusinessError} 12300002 - invalid authType or authTrustLevel.
      32.         * @throws {BusinessError} 12300014 - the authTrustLevel is not supported on current device
      33.         * @throws {BusinessError} 12300015 - the authType is not supported on current device.
      34.         * @systemapi Hide this for inner system use.
      35.         * @since 8
      36.         */
      37.        getAvailableStatus(authType: AuthType, authTrustLevel: AuthTrustLevel): number;
      38.        /**
      39.         * Gets the property based on the specified request information.
      40.         * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
      41.         * @param request Indicates the request information, including authentication type, and property type list.
      42.         * @returns Returns an executor property.
      43.         * @throws {BusinessError} 201 - permission denied.
      44.         * @throws {BusinessError} 401 - the parameter check failed.
      45.         * @throws {BusinessError} 12300001 - system service exception.
      46.         * @throws {BusinessError} 12300002 - invalid request.
      47.         * @systemapi Hide this for inner system use.
      48.         * @since 8
      49.         */
      50.        getProperty(request: GetPropertyRequest, callback: AsyncCallback): void;
      51.        getProperty(request: GetPropertyRequest): Promise;
      52.        /**
      53.         * Sets property that can be used to initialize algorithms.
      54.         * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
      55.         * @param request Indicates the request information, including authentication type and the key-value to be set.
      56.         * @returns Returns a number value indicating whether the property setting was successful.
      57.         * @throws {BusinessError} 201 - permission denied.
      58.         * @throws {BusinessError} 401 - the parameter check failed.
      59.         * @throws {BusinessError} 12300001 - system service exception.
      60.         * @throws {BusinessError} 12300002 - invalid request.
      61.         * @systemapi Hide this for inner system use.
      62.         * @since 8
      63.         */
      64.        setProperty(request: SetPropertyRequest, callback: AsyncCallback): void;
      65.        setProperty(request: SetPropertyRequest): Promise;
      66.        /**
      67.         * Executes authentication.
      68.         * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
      69.         * @param challenge Indicates the challenge value.
      70.         * @param authType Indicates the authentication type.
      71.         * @param authTrustLevel Indicates the trust level of authentication result.
      72.         * @param callback Indicates the callback to get result and acquireInfo.
      73.         * @returns Returns a context ID for cancellation.
      74.         * @throws {BusinessError} 201 - permission denied.
      75.         * @throws {BusinessError} 401 - the parameter check failed.
      76.         * @throws {BusinessError} 12300001 - system service exception.
      77.         * @throws {BusinessError} 12300002 - invalid challenge, authType or authTrustLevel.
      78.         * @throws {BusinessError} 12300014 - the authTrustLevel is not supported on current device
      79.         * @throws {BusinessError} 12300015 - the authType is not supported on current device.
      80.         * @throws {BusinessError} 12300016 - authentication timeout.
      81.         * @throws {BusinessError} 12300017 - authentication service is busy.
      82.         * @throws {BusinessError} 12300018 - authentication service is locked.
      83.         * @throws {BusinessError} 12300019 - the credential does not exist.
      84.         * @systemapi Hide this for inner system use.
      85.         * @since 8
      86.         */
      87.        auth(challenge: Uint8Array, authType: AuthType, authTrustLevel: AuthTrustLevel, callback: IUserAuthCallback): Uint8Array;
      88.        /**
      89.         * Executes user authentication.
      90.         * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
      91.         * @param userId Indicates the user identification.
      92.         * @param challenge Indicates the challenge value.
      93.         * @param authType Indicates the authentication type.
      94.         * @param authTrustLevel Indicates the trust level of authentication result.
      95.         * @param callback Indicates the callback to get result and acquireInfo.
      96.         * @returns Returns a context ID for cancellation.
      97.         * @throws {BusinessError} 201 - permission denied.
      98.         * @throws {BusinessError} 401 - the parameter check failed.
      99.         * @throws {BusinessError} 12300001 - system service exception.
      100.         * @throws {BusinessError} 12300002 - invalid userId, challenge, authType or authTrustLevel.
      101.         * @throws {BusinessError} 12300003 - the account indicated by userId dose not exist.
      102.         * @throws {BusinessError} 12300014 - the authTrustLevel is not supported on current device
      103.         * @throws {BusinessError} 12300015 - the authType is not supported on current device.
      104.         * @throws {BusinessError} 12300016 - authentication timeout.
      105.         * @throws {BusinessError} 12300017 - authentication service is busy.
      106.         * @throws {BusinessError} 12300018 - authentication service is locked.
      107.         * @throws {BusinessError} 12300019 - the credential does not exist.
      108.         * @systemapi Hide this for inner system use.
      109.         * @since 8
      110.         */
      111.        authUser(userId: number, challenge: Uint8Array, authType: AuthType, authTrustLevel: AuthTrustLevel, callback: IUserAuthCallback): Uint8Array;
      112.        /**
      113.         * Cancels authentication with context ID.
      114.         * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
      115.         * @param contextID Indicates the authentication context ID.
      116.         * @throws {BusinessError} 201 - permission denied.
      117.         * @throws {BusinessError} 401 - the parameter check failed.
      118.         * @throws {BusinessError} 12300001 - system service exception.
      119.         * @throws {BusinessError} 12300002 - invalid contexId.
      120.         * @systemapi Hide this for inner system use.
      121.         * @since 8
      122.         */
      123.        cancelAuth(contextID: Uint8Array): void;
      124.    }
      复制代码
    • 口令管理
      1. /**
      2.     * Provides the abilities for Pin code authentication.
      3.     * @name PINAuth
      4.     * @syscap SystemCapability.Account.OsAccount
      5.     * @since 8
      6.     */
      7. class PINAuth {
      8.     /**
      9.     * Constructor to get the PINAuth class instance.
      10.     * @returns Returns the PINAuth class instance.
      11.     * @systemapi Hide this for inner system use.
      12.     * @since 8
      13.     */
      14.    constructor();
      15.    /**
      16.     * Register inputer.
      17.    * @permission ohos.permission.ACCESS_PIN_AUTH
      18.    * @param inputer Indicates the password input box callback
      19.    * @throws {BusinessError} 201 - permission denied.
      20.    * @throws {BusinessError} 401 - the parameter check failed.
      21.    * @throws {BusinessError} 12300001 - system service exception.
      22.    * @throws {BusinessError} 12300007 - PIN inputer has been registered.
      23.    * @systemapi Hide this for inner system use.
      24.    * @since 8
      25.    */
      26.    registerInputer(inputer: IInputer): void;
      27.    /**
      28.    * Unregister inputer.
      29.    * @permission ohos.permission.ACCESS_PIN_AUTH
      30.    * @systemapi Hide this for inner system use.
      31.    * @since 8
      32.    */
      33.    unregisterInputer(): void;
      34. }
      复制代码
    • 回调:IInputData,Inputer回调时带的参数,用来输入口令
      1. /**
      2.     * Password data callback.
      3.     *
      4.     * @name IInputData
      5.     * @syscap SystemCapability.Account.OsAccount
      6.     * @systemapi Hide this for inner system use.
      7.     * @since 8
      8.     */
      9.    interface IInputData {
      10.        /**
      11.         * Notifies to set data.
      12.         * @param pinSubType Indicates the credential subtype for authentication.
      13.         * @param data Indicates the data to set.
      14.         * @throws {BusinessError} 401 - the parameter check failed.
      15.         * @throws {BusinessError} 12300002 - invalid pinSubType.
      16.         * @systemapi Hide this for inner system use.
      17.         * @since 8
      18.         */
      19.        onSetData: (pinSubType: AuthSubType, data: Uint8Array) => void;
      20.    }
      复制代码
    • 回调:IInputer,regitsterInputer是传入的回调,在需要输密码时被调用
      1. /**
      2.     * Password input box callback.
      3.     * @name IInputer
      4.     * @syscap SystemCapability.Account.OsAccount
      5.     * @systemapi Hide this for inner system use.
      6.     * @since 8
      7.     */
      8.    interface IInputer {
      9.        /**
      10.         * Notifies to get data.
      11.         * @param pinSubType Indicates the credential subtype for authentication.
      12.         * @param callback Indicates the password data callback.
      13.         * @systemapi Hide this for inner system use.
      14.         * @since 8
      15.         */
      16.        onGetData: (pinSubType: AuthSubType, callback: IInputData) => void;
      17.    }
      复制代码
    • 回调:IUserAuthCallback,auth,authUser的回调,用来接收auth的结果
      1. /**
      2.     * User authentication callback.
      3.     * @name IUserAuthCallback
      4.     * @syscap SystemCapability.Account.OsAccount
      5.     * @systemapi Hide this for inner system use.
      6.     * @since 8
      7.     */
      8.    interface IUserAuthCallback {
      9.        /**
      10.         * The authentication result code is returned through the callback.
      11.         * @param result Indicates the authentication result code.
      12.         * @param extraInfo Indicates the specific information for different situation.
      13.         * If the authentication is passed, the authentication token is returned in extrainfo,
      14.         * If the authentication fails, the remaining authentication times are returned in extrainfo,
      15.         * If the authentication executor is locked, the freezing time is returned in extrainfo.
      16.         * @systemapi Hide this for inner system use.
      17.         * @since 8
      18.         */
      19.        onResult: (result: number, extraInfo: AuthResult) => void;
      20.        /**
      21.         * During an authentication, the TipsCode is returned through the callback.
      22.         * @param module Indicates the executor type for authentication.
      23.         * @param acquire Indicates the tip code for different authentication executor.
      24.         * @param extraInfo reserved parameter.
      25.         * @systemapi Hide this for inner system use.
      26.         * @since 8
      27.         */
      28.        onAcquireInfo?: (module: number, acquire: number, extraInfo: any) => void;
      29.    }
      复制代码
    • 回调:IIdmCallback,addCredential,updateCredential,delUser,delCred的回调,用来收听onResult是否成功
      1. /**
      2.     * Identity manager callback.
      3.     * @name IIdmCallback
      4.     * @syscap SystemCapability.Account.OsAccount
      5.     * @systemapi Hide this for inner system use.
      6.     * @since 8
      7.     */
      8.    interface IIdmCallback {
      9.        /**
      10.         * The authentication result code is returned through the callback.
      11.         * @param result Indicates the authentication result code.
      12.         * @param extraInfo pass the specific information for different situation.
      13.         * @systemapi Hide this for inner system use.
      14.         * @since 8
      15.         */
      16.        onResult: (result: number, extraInfo: RequestResult) => void;
      17.        /**
      18.         * During an authentication, the TipsCode is returned through the callback.
      19.         * @param module Indicates the executor type for authentication.
      20.         * @param acquire Indicates the tip code for different authentication executor.
      21.         * @param extraInfo reserved parameter.
      22.         * @systemapi Hide this for inner system use.
      23.         * @since 8
      24.         */
      25.        onAcquireInfo?: (module: number, acquire: number, extraInfo: any) => void;
      26.    }
      复制代码
    流程:
    havePasswordnoPassworduserIdentityManager::openSessionpinAuth::registerInputeruserIdentityManager::getAuthInfouserAuth::getPropertyuserIdentityManager::addCredential

    • 构建对象
      1. //三个对象
      2. this.userIdentityManager = new osAccount.UserIdentityManager();
      3. this.pinAuth = new osAccount.PINAuth();
      4. this.userAuth = new osAccount.UserAuth();
      复制代码
    • opensession
      1. /**
      2.   * Open Session
      3.   * A challenge value of 0 indicates that opensession failed
      4.   *
      5.   * @returns challenge value
      6.   */
      7.  openSession(callback: (challenge: string) => void): void {
      8.    LogUtil.debug(`${this.TAG}openSession in.`);
      9.    try {
      10.      this.userIdentityManager.openSession()
      11.                              .then((data) =>{
      12.                                callback(this.u8AToStr(data));
      13.                                LogUtil.info(`${this.TAG} openSession success`);
      14.                              })
      15.                              .catch((err) => {
      16.                                LogUtil.error(`${this.TAG} openSession failed` + JSON.stringify(err));
      17.                              })
      18.    } catch {
      19.      LogUtil.error(`${this.TAG}openSession failed`);
      20.      callback('0');
      21.    }
      22.    LogUtil.debug(`${this.TAG}openSession out.`);
      23.  }
      复制代码
    • 注册inputer
      1. /**
      2.   * Register Inputer
      3.   */
      4.  registerInputer(): boolean {
      5.    LogUtil.debug(`${this.TAG}registerInputer in.`);
      6.    let result = false;
      7.    try {
      8.      result = this.pinAuth.registerInputer({
      9.        onGetData: (authSubType, inputData) => {
      10.          let u8aPwd = this.encodeToU8A(this.password);
      11.          LogUtil.info(`${this.TAG} before set data, type: ${this.pinSubType}.`);
      12.          inputData.onSetData(this.pinSubType, u8aPwd);
      13.        }
      14.      });
      15.      if(!result){
      16.        this.unregisterInputer();
      17.        result = this.pinAuth.registerInputer({
      18.          onGetData: (authSubType, inputData) => {
      19.            let u8aPwd = this.encodeToU8A(this.password);
      20.            inputData.onSetData(this.pinSubType, u8aPwd);
      21.          }
      22.        });
      23.      }
      24.    } catch {
      25.      LogUtil.error(`${this.TAG}registerInputer failed`);
      26.    }
      27.    LogUtil.info(`${this.TAG}registerInputer out.`);
      28.    return result;
      29.   }
      复制代码
    • createPassword
      1. /**
      2.   * Call api to create password
      3.   */
      4.  createPassword() {
      5.    PasswordModel.addPinCredential(this.passwordType, this.password, (result) => {
      6.      if (result === ResultCode.SUCCESS) {
      7.        LogUtil.info(`${this.TAG}create password success`);
      8.        this.goBackCorrect();
      9.      } else {
      10.        LogUtil.info(`${this.TAG}create password failed`);
      11.        //TODO show api message to view
      12.        this.checkMessage = 'create failed.';
      13.      }
      14.    });
      15.   }
      复制代码
    • getAuthInfo
      1. /**
      2.   * Get AuthInfo
      3.   *
      4.   * @param authType Credential type.
      5.   * @returns Returns all registered credential information of this type for the current user
      6.   */
      7.  getPinAuthInfo(callback: (data: Array) => void): void {
      8.    LogUtil.debug(`${this.TAG}getPinAuthInfo in.`);
      9.    try {
      10.      this.userIdentityManager.getAuthInfo(AuthType.PIN)
      11.                              .then((data) => {
      12.                                LogUtil.info(`${this.TAG} get pin auth info data.`);
      13.                                let arrCredInfo = [];
      14.                                try {
      15.                                  for(let i = 0; i < data.length; i++) {
      16.                                    let credInfo = {
      17.                                      'authType': data[i].authType,
      18.                                      'authSubType': data[i].authSubType
      19.                                    };
      20.                                    if (credInfo.authType == AuthType.PIN) {
      21.                                      this.pinSubType = credInfo.authSubType;
      22.                                    }
      23.                                    arrCredInfo.push(credInfo);
      24.                                  }
      25.                                } catch(e) {
      26.                                  LogUtil.info('faceDemo pin.getAuthInfo error = ' + e);
      27.                                }
      28.                                callback(arrCredInfo);
      29.                                LogUtil.info(`${this.TAG} getAuthInfo success.`);
      30.                              })
      31.                              .catch((err) => {
      32.                                LogUtil.error(`${this.TAG} getAuthInfo failed.` + JSON.stringify(err));
      33.                              })
      34.    } catch (e) {
      35.      LogUtil.error(`${this.TAG}getPinAuthInfo failed:` + e);
      36.    }
      37.    LogUtil.debug(`${this.TAG}getPinAuthInfo out.`);
      38.  }
      复制代码
    • autPin
      1. /**
      2.   * Auth
      3.   *
      4.   * @param challenge pass in challenge value. challenge是从openSession的回调得到
      5.   * @param password password
      6.   * @param onResult Return results through callback.
      7.   */
      8.  authPin(challenge: string, password: string, onResult: (result: number, extraInfo: {
      9.    token?: string;
      10.    remainTimes?: number;
      11.    freezingTime?: number;
      12.   }) => void): void {
      13.    LogUtil.debug(`${this.TAG}authPin in.`);
      14.    this.password = password;
      15.    try {
      16.      LogUtil.info(`${this.TAG} before userAuth auth pin`);
      17.      this.userAuth.auth(this.strToU8A(challenge), AuthType.PIN, AuthTrustLevel.ATL4, {
      18.        onResult: (result, extraInfo) => {
      19.          try{
      20.            if (result === ResultCode.SUCCESS) {
      21.              LogUtil.debug(`${this.TAG}userAuth.auth onResult: result = success`);
      22.            } else {
      23.              LogUtil.debug(`${this.TAG}userAuth.auth failed onResult: result =  ${result}`);
      24.            }
      25.            let info = {
      26.              'token':  this.u8AToStr(extraInfo?.token),
      27.              'remainTimes': extraInfo.remainTimes,
      28.              'freezingTime': extraInfo.freezingTime
      29.            }
      30.            onResult(result, info)
      31.          }
      32.          catch(e) {
      33.            LogUtil.debug(`${this.TAG}userAuth.auth onResult error = ${JSON.stringify(e)}`);
      34.          }
      35.        },
      36.        onAcquireInfo: (acquireModule, acquire, extraInfo) => {
      37.          try{
      38.            LogUtil.debug(this.TAG + 'faceDemo pin.auth onAcquireInfo acquireModule = ' + acquireModule);
      39.            LogUtil.debug(this.TAG + 'faceDemo pin.auth onAcquireInfo acquire = ' + acquire);
      40.          }
      41.          catch(e) {
      42.            LogUtil.error(this.TAG + 'faceDemo pin.auth onAcquireInfo error = ' + e);
      43.          }
      44.        }
      45.      })
      46.    } catch (e) {
      47.      LogUtil.error(`${this.TAG}AuthPin failed:` + e);
      48.    }
      49.    LogUtil.debug(`${this.TAG}authPin out.`);
      50.  }
      复制代码

概述

小结

    用户鉴权(包括屏保)是以accountmgr服务为入口为应用层提供功能,以useridm为实现,完成具体的口令管理和人脸管理(人脸管理目前还有欠缺)用户鉴权是系统级服务,要求ohos.permission.MANAGE_USER_IDM, ohos.permission.USE_USER_IDM,ohos.permission.MANAGE_LOCAL_ACCOUNTS,ohos.permission.ACCESS_USER_AUTH_INTERNAL,ohos.permission.ACCESS_PIN_AUTH权限和selinux权限(富设备支持)

0

主题

2

回帖

10

积分

新手上路

积分
10
发表于 2025-3-30 16:37:34 | 显示全部楼层
我在签名中配置了
"apl":"system_core",
"app-feature":"hos_system_app"
目前遇到了这样的问题(不是系统hap,没有selinux权限,没打包设置)
请问如何才能让应用拥有selinux权限
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表