@hxa-rn/react-native-app-auth
v8.1.0-beta.1
Published
React Native bridge for AppAuth for supporting any OAuth 2 provider
Readme
本项目基于 react-native-app-auth开发。如果在使用过程中有任何问题,欢迎在AtomGit提交Issue,会及时跟进。
项目介绍
@hxa-rn/react-native-app-auth 为 React Native 库 react-native-app-auth 的鸿蒙适配包,提供 OAuth 2.0 / OpenID Connect 授权码流程(含 PKCE)、配置预取、令牌刷新、动态客户端注册、令牌吊销与会话登出。当前版本 8.1.0-beta.1,对应上游 8.1.0,支持 Autolink,编译目标 API 12+。
集成指南
npm install @hxa-rn/react-native-app-authpeerDependencies:react-native >= 0.72。
包内已声明 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:
- 在工程根
oh-package.json5配置@rnoh/react-native-openharmony的 overrides(按宿主工程约定)。 - 在
entry/oh-package.json5添加依赖:"@hxa-rn/react-native-app-auth": "file:../../node_modules/@hxa-rn/react-native-app-auth/harmony/rnaa.har",并执行ohpm install。 - 在
entry/src/main/cpp/CMakeLists.txt中add_subdirectory本模块 C++ 并target_link_libraries。 - 在
PackageProvider.cpp注册RNAppAuthPackage(C++)。 - 在
RNPackagesFactory.ets注册RNAppAuthPackage(ETS)。
授权/登出重定向:在 entry module.json5 的 EntryAbility.skills.uris 中声明自定义 scheme(如 rnaa://oauth2redirect)。
本地验证可参考仓库 example:安装依赖、npm run dev 生成 bundle 后,用 DevEco Studio 打开 example/harmony 编译运行。
使用说明
import { authorize, prefetchConfiguration, refresh } from 'react-native-app-auth';
async function runAuthFlow() {
const config = {
issuer: 'https://demo.duendesoftware.com',
clientId: 'interactive.public',
redirectUrl: 'rnaa://oauth2redirect',
scopes: ['openid', 'profile', 'email', 'offline_access'],
};
await prefetchConfiguration({ ...config, connectionTimeoutSeconds: 10 });
const result = await authorize({ ...config, connectionTimeoutSeconds: 10 });
console.log(result.accessToken, result.idToken);
const refreshed = await refresh(config, { refreshToken: result.refreshToken });
console.log(refreshed.accessToken);
}接口文档
| 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> | 支持 |
| logout | RP-Initiated Logout | EndSessionConfiguration, LogoutConfiguration | Promise<EndSessionResult> | 系统浏览器 + 重定向回调 |
平台差异要点:
warmAndPrefetchChrome在鸿蒙为 no-op。dangerouslyAllowInsecureHttpRequests被忽略。handleRedirect/cancelPendingFlow为内部方法,不对业务开放。
快速验证(运行 Example)
前置条件
| 依赖 | 版本要求 | |------|----------| | Node.js | >= 18 | | DevEco Studio | 5.0+ | | HarmonyOS SDK | API 12+ |
运行步骤
1. 克隆仓库
git clone https://gitcode.com/hxa-rn/react-native-app-auth.git
cd react-native-app-auth
git checkout br_rnoh0.722. 安装仓库开发依赖
npm install --legacy-peer-depsExample 已改为从 npm 公仓安装 @hxa-rn/[email protected],不再使用本地 file:../xxx.tgz,运行 Example 不必再执行 npm pack。
3. 进入 example 目录,安装依赖
cd example # 或 example_auto
npm install --legacy-peer-deps4. 生成 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,并需在 entrymodule.json5中配置 OAuth 重定向 scheme。
约束与限制
兼容性
- 鸿蒙 SDK:API 12+
- DevEco Studio:5.0+
- 上游 RN 库:react-native-app-auth
8.1.0 - React Native / RNOH:0.72+
权限
ohos.permission.INTERNET(entrymodule.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
