react-native-app-auth:基于鸿蒙 React Native 生态的 OAuth 2.0 认证适配项目

基于 AppAuth 的 OAuth 2.0 认证桥接库

Branch5Tags3
FilesLast commitLast update
8 hours ago
8 hours ago
8 hours ago
8 hours ago
8 hours ago
8 hours ago
1 month ago
1 month ago
8 hours ago
18 days ago
8 hours ago
1 month ago
8 hours ago
1 month ago

react-native-app-auth

本项目基于 react-native-app-auth开发。如果在使用过程中有任何问题,欢迎在AtomGit提交Issue,会及时跟进。

项目介绍

@hxa-rn/react-native-app-auth 是 react-native-app-auth 的鸿蒙(OpenHarmony)适配版本,当前规范版本号为 8.5.1。

本库包含自有原生 HAR,通过 TurboModule 提供 OAuth 2.0 / OpenID Connect 授权码流程(含 PKCE)、配置预取、令牌刷新、动态客户端注册、令牌吊销与会话登出。当前配套版本为 React Native 0.82.1 / React 19.1.1 / RNOH 0.82.30。

鸿蒙适配包版本 原始库版本 支持 RN 版本 Autolink 编译 API 版本
8.5.1 react-native-app-auth 8.5.1 0.82+(<0.83.0) 是 API 17+

集成指南

npm install @hxa-rn/react-native-app-auth

peerDependencies:react-native >= 0.82.0 且 < 0.83.0;当前配套版本为 0.82.1,React 为 19.1.1。宿主需统一使用 RNOH / Harmony CLI 0.82.30。本分支的 8.5.1 升级包尚未发布到 npm;以上为发布后的业务集成命令。

包内已声明 harmony.alias 为 react-native-app-auth。业务代码在配置 alias 后仍从原库名导入:

import { authorize, prefetchConfiguration, refresh } from 'react-native-app-auth';

不要写成 from '@hxa-rn/react-native-app-auth'。

当前版本支持 Autolink;工程已接入时可跳过手动配置。如需 Manual Link:

  1. 在工程根 oh-package.json5 的 overrides 中统一 @rnoh/react-native-openharmony。两个 Demo 指向 npm @react-native-oh/react-native-harmony@0.82.30 配套的 Release HAR;库自身声明官方 OHPM 0.82.30 依赖。Demo CMake 显式使用 oh_modules 的公开链接作为头文件路径,避免 Windows Ninja 增量依赖记录超过 260 字符。
  2. 在 entry/oh-package.json5 添加依赖:"@hxa-rn/react-native-app-auth": "file:../../node_modules/@hxa-rn/react-native-app-auth/harmony/rnaa.har",并执行 ohpm install。
  3. 在 entry/src/main/cpp/CMakeLists.txt 中 add_subdirectory 本模块 C++ 并 target_link_libraries。
  4. 在 entry/src/main/cpp/RNOHPackagesFactory.h 注册 RNAppAuthPackage(C++)。
  5. 在 entry/src/main/ets/RNOHPackagesFactory.ets 注册 RNAppAuthPackage(ETS)。

授权/登出重定向:在 entry module.json5 的 EntryAbility.skills.uris 中声明自定义 scheme(如 rnaa://oauth2redirect)。

使用说明

在 OAuth 服务端登记客户端与回调地址,并在应用中配置对应的 redirect scheme。下例使用仓库示例的 Duende 配置;接入自己的服务时替换 issuer、clientId、redirectUrl 和 scopes。将导出的 runAuthFlow 绑定到业务按钮,由用户触发;函数捕获预取、授权和刷新错误。服务端未返回刷新令牌时,保留授权结果并跳过刷新。

import { authorize, prefetchConfiguration, refresh } from 'react-native-app-auth';

export async function runAuthFlow() {
  const config = {
    issuer: 'https://demo.duendesoftware.com',
    clientId: 'interactive.public',
    redirectUrl: 'rnaa://oauth2redirect',
    scopes: ['openid', 'profile', 'email', 'offline_access'],
  };

  try {
    await prefetchConfiguration({ ...config, connectionTimeoutSeconds: 10 });

    const result = await authorize({ ...config, connectionTimeoutSeconds: 10 });
    console.log('授权完成');

    if (!result.refreshToken) {
      return result;
    }
    const refreshed = await refresh(config, { refreshToken: result.refreshToken });
    console.log('令牌刷新完成');
    return { ...result, ...refreshed, refreshToken: refreshed.refreshToken || result.refreshToken };
  } catch (error) {
    console.warn('授权或刷新失败', error);
    return null;
  }
}

接口文档

API 描述 参数 返回值 HarmonyOS
prefetchConfiguration 预取并缓存 OIDC 服务发现配置 AuthConfiguration Promise<void> 返回 void(与 Android resolve boolean 不同)
authorize 授权码登录(PKCE、clientAuthMethod、附加参数) AuthConfiguration Promise<AuthorizeResult> 系统浏览器 + 重定向回调
refresh 使用刷新令牌获取新访问令牌 AuthConfiguration, RefreshConfiguration Promise<RefreshResult> 支持
register OIDC 动态客户端注册(DCR) RegistrationConfiguration Promise<RegistrationResponse> 支持
revoke 吊销访问令牌(JS fetch) BaseAuthConfiguration, RevokeConfiguration 声明为 Promise<void>;当前实现成功时 resolve 为 fetch Response HTTP 非 2xx 时抛出 token_revocation_failed
logout RP-Initiated Logout EndSessionConfiguration, LogoutConfiguration Promise<EndSessionResult> 系统浏览器 + 重定向回调
resumePendingAuthorize 尝试恢复挂起授权 可选 ResumePendingAuthorizeConfiguration Promise<AuthorizeResult | null> 恒返回 null

平台差异要点:

  • warmAndPrefetchChrome 在鸿蒙为 no-op。
  • dangerouslyAllowInsecureHttpRequests 被忽略。
  • handleRedirect / cancelPendingFlow 为内部方法,不对业务开放。
  • 上游 androidPrefersEphemeralSession 为 Android 专属选项,鸿蒙端忽略。
  • resumePendingAuthorize() 在鸿蒙端与上游 iOS 保持一致,调用时返回 null,不支持恢复进程被回收前的挂起授权;返回 null 不表示授权恢复成功。
  • additionalParameters 可传递 response_mode;自定义 scheme 回调支持 query / fragment 参数,不支持浏览器 form_post 回调。

快速验证(运行 Example)

前置条件

依赖 版本要求
Node.js >= 20.19.4
React Native / React 0.82.1 / 19.1.1
RNOH / Harmony CLI 0.82.30 / 0.82.30
DevEco Studio 使用与 HarmonyOS 6.1.1 SDK 配套的版本
HarmonyOS SDK 最低兼容 API 17;当前构建使用 HarmonyOS 6.1.1(API 24)

运行步骤

1. 克隆仓库

git clone https://gitcode.com/hxa-rn/react-native-app-auth.git
cd react-native-app-auth
git checkout main

2. 安装依赖并构建

npm install --legacy-peer-deps
npm pack           # 生成 tgz 包

3. 进入 example 目录,安装依赖

cd example          # 或 example_auto
npm install --legacy-peer-deps

4. 生成 JS Bundle

npm run dev

产物:harmony/entry/src/main/resources/rawfile/bundle.harmony.js

5. 用 DevEco Studio 打开鸿蒙工程

  • 打开 DevEco Studio
  • 选择 example/harmony(或 example_auto/harmony)目录
  • 等待 Sync 完成

6. 编译并运行 HAP

在 DevEco Studio 中点击运行按钮,将 HAP 安装到设备/模拟器。

注意:Example 中已预置插件依赖和 Package 注册,无需手动配置 Link。Example 已声明 ohos.permission.INTERNET,并需在 entry module.json5 中配置 OAuth 重定向 scheme。

约束与限制

兼容性

  • 最低应用 API:17;构建 SDK:HarmonyOS 6.1.1(API 24)
  • DevEco Studio:使用与构建 SDK 配套的版本
  • 上游 API 基线:react-native-app-auth 8.5.1(Android/iOS 平台功能仍受鸿蒙平台差异限制)
  • React Native / React:0.82.1 / 19.1.1
  • RNOH / Harmony CLI:0.82.30

权限

  • ohos.permission.INTERNET(entry module.json5 的 requestPermissions)

其它

  • authorize / logout 须声明自定义 redirect scheme,否则浏览器无法回跳。
  • 重定向回调依赖应用存活;进程被回收后挂起请求会丢失。
  • revoke 未配置 revocationEndpoint 时会请求 issuer 的 OIDC discovery(需网络)。

开源license

本项目基于 MIT 协议,详见 LICENSE 文件。

问题反馈渠道

使用问题请在 AtomGit 提交 Issue。也可在 GitCode 仓库反馈:

https://gitcode.com/hxa-rn/react-native-app-auth

https://gitcode.com/hxa-rn/react-native-app-auth/issues

Introduction

基于 AppAuth 的 OAuth 2.0 认证桥接库

Customize your domain