1 Star 1 Fork 0

ScenarioSamples/RememberPWD

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
6个月前
Loading...
README

免密登录

介绍

本示例基于关键资产存储服务API实现了免密登录功能。

效果预览

使用说明

注意,设备需设置锁屏密码后才可使用免密登录功能。

输入账号和密码,勾选“记住密码”,底部会出现提示弹窗,点击“登录”按钮后,点击“重新加载该页面”,会出现输入锁屏密码的弹窗,输入成功,应用自动填充账号和密码。

需要的权限

  1. ohos.permission.ACCESS_BIOMETRIC

实现思路

保存账户信息

通过asset.add接口保存用户信息,此处设置了访问该信息需要通过用户认证,仅在设置锁屏密码且解锁的情况下可访问关键资产。通过promptAction.showToast接口实现记住密码的文字弹窗。核心代码如下,源码参考

Index.ets

getAccountLoginInfo() {
    const accountInfo: IAccountInfo = {
      name: this.accountName,
      password: this.accountPassword
    }
    const attrInfo: asset.AssetMap = new Map()
    // 需要存储的关键资产数据
    attrInfo.set(asset.Tag.SECRET, this.stringToBuffer(JSON.stringify(accountInfo)))
    // 数据别名
    attrInfo.set(asset.Tag.ALIAS, this.stringToBuffer(this.accountAlias))
    // 解锁状态时可访问
    attrInfo.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_UNLOCKED)
    // 仅在设置锁屏密码的情况下,可访问关键资产
    attrInfo.set(asset.Tag.REQUIRE_PASSWORD_SET, true)
    // 需要开启通过用户认证后,才访问关键资产
    attrInfo.set(asset.Tag.AUTH_TYPE, asset.AuthType.ANY)
    // 新增关键资产时已存在数据,则覆盖
    attrInfo.set(asset.Tag.CONFLICT_RESOLUTION, asset.ConflictResolution.OVERWRITE)
    return attrInfo
  }

查询账户信息

查询账户信息需要通过用户认证,分为以下两个步骤进行。

  1. 调用asset.preQuery接口获取challenge。
async preAccountInfo() {
    const queryInfo: asset.AssetMap = new Map()
    queryInfo.set(asset.Tag.ALIAS, this.stringToBuffer(this.accountAlias))
    // 用户认证token有效期30s
    queryInfo.set(asset.Tag.AUTH_VALIDITY_PERIOD, 30)
    try {
      // step1
      const challenge = await asset.preQuery(queryInfo)
      // step2
      this.startUserAuth(challenge)
    } catch (e) {
      console.error(TAG, `查询账号登录信息失败 ${e.code} ${e.message}`)
    }
  }
  1. 调用用户认证接口,认证通过调用asset.query查询数据,最后调用asset.postQuery接口结束查询。 a)拉起用户认证
startUserAuth(challenge: Uint8Array) {
    const authParam: userAuth.AuthParam = {
      challenge,
      authType: [userAuth.UserAuthType.PIN, userAuth.UserAuthType.FINGERPRINT],
      authTrustLevel: userAuth.AuthTrustLevel.ATL1
    }
    const widgeParam: userAuth.WidgetParam = {
      title: '请输入密码'
    }
    const userAuthInstance = userAuth.getUserAuthInstance(authParam, widgeParam)
    userAuthInstance.on('result', {
      onResult: async (data) => {
        // 认证成功
        if (data.result === userAuth.UserAuthResultCode.SUCCESS) {
          // step3
          await this.queryAccountInfo(data.token, challenge)
          // step4
          const handle: asset.AssetMap = new Map()
          handle.set(asset.Tag.AUTH_CHALLENGE, challenge)
          await asset.postQuery(handle)
          promptAction.showToast({
            message: '获取账户信息成功'
          })
        }
      }
    })
    userAuthInstance.start()
  }

b)查询数据

async queryAccountInfo(token: Uint8Array, challenge: Uint8Array) {
    const query: asset.AssetMap = new Map()
    query.set(asset.Tag.ALIAS, this.stringToBuffer(this.accountAlias))
    query.set(asset.Tag.AUTH_TOKEN, token)
    query.set(asset.Tag.AUTH_CHALLENGE, challenge)
    query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL)
    try {
      const data: Array<asset.AssetMap> = await asset.query(query)
      if (data.length) {
        const map = data.shift()! as asset.AssetMap
        const secret = map.get(asset.Tag.SECRET) as Uint8Array
        const accountInfo: IAccountInfo = JSON.parse(this.bufferToString(secret))
        this.accountName = accountInfo.name
        this.accountPassword = accountInfo.password
      } else {
        console.error(TAG, `没有查询到数据`)
      }
    } catch (e) {
      console.error(TAG, `查询${this.accountAlias}数据失败,错误码:${e.code}${e.message}`)
    }
  }

工程目录

entry/src/main/ets/
|---entryability
|   |---EntryAbility.ets
|---pages
|   |---Index.ets              // 登录页面

一份简单的问卷反馈

亲爱的Harmony Next开发者,您好!
为了协助您高效开发,提高鸿蒙场景化示例的质量,希望您在浏览或使用后抽空填写一份简单的问卷,我们将会收集您的宝贵意见进行优化

点击此处填写问卷

空文件

简介

【鸿蒙 Harmony Next 示例 代码】免密登录 展开 收起
取消

发行版

暂无发行版

贡献者 (4)

全部

近期动态

Pushed new commit to master branch 2 months ago, 7e8da4b...c4b92f8
Pushed new commit to master branch 2 months ago, dbe5f01...7e8da4b
Pushed new commit to master branch 2 months ago, 56229bf...dbe5f01
Pushed new commit to master branch 4 months ago, b865c6f...56229bf
Pushed new commit to master branch 6 months ago, 70ffab8...b865c6f
加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/scenario-samples/remember-pwd.git
git@gitee.com:scenario-samples/remember-pwd.git
scenario-samples
remember-pwd
RememberPWD
master

搜索帮助