@smartcrab/react-native 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/app.plugin.js +9 -0
- package/dist/adapters/expo-crypto-port.d.ts +3 -0
- package/dist/adapters/expo-crypto-port.js +8 -0
- package/dist/adapters/expo-linking-port.d.ts +3 -0
- package/dist/adapters/expo-linking-port.js +21 -0
- package/dist/adapters/expo-secure-store-port.d.ts +11 -0
- package/dist/adapters/expo-secure-store-port.js +15 -0
- package/dist/adapters/expo-web-browser-port.d.ts +7 -0
- package/dist/adapters/expo-web-browser-port.js +9 -0
- package/dist/adapters/react-native-app-state-port.d.ts +8 -0
- package/dist/adapters/react-native-app-state-port.js +13 -0
- package/dist/adapters/react-native-passkeys-adapter.d.ts +2 -0
- package/dist/adapters/react-native-passkeys-adapter.js +112 -0
- package/dist/adapters/react-native-passkeys-adapter.test.d.ts +1 -0
- package/dist/adapters/react-native-passkeys-adapter.test.js +76 -0
- package/dist/base64url.d.ts +20 -0
- package/dist/base64url.js +89 -0
- package/dist/base64url.test.d.ts +1 -0
- package/dist/base64url.test.js +36 -0
- package/dist/browser-port.d.ts +17 -0
- package/dist/browser-port.js +1 -0
- package/dist/client.d.ts +43 -0
- package/dist/client.js +871 -0
- package/dist/client.test.d.ts +1 -0
- package/dist/client.test.js +995 -0
- package/dist/context.d.ts +8 -0
- package/dist/context.js +17 -0
- package/dist/crypto-port.d.ts +16 -0
- package/dist/crypto-port.js +1 -0
- package/dist/errors.d.ts +113 -0
- package/dist/errors.js +82 -0
- package/dist/errors.test.d.ts +1 -0
- package/dist/errors.test.js +81 -0
- package/dist/hooks.d.ts +93 -0
- package/dist/hooks.js +191 -0
- package/dist/http.d.ts +41 -0
- package/dist/http.js +102 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +29 -0
- package/dist/jwt.d.ts +1 -0
- package/dist/jwt.js +23 -0
- package/dist/lifecycle-ports.d.ts +29 -0
- package/dist/lifecycle-ports.js +1 -0
- package/dist/linking-port.d.ts +15 -0
- package/dist/linking-port.js +1 -0
- package/dist/passkey-adapter.d.ts +20 -0
- package/dist/passkey-adapter.js +1 -0
- package/dist/pkce.d.ts +32 -0
- package/dist/pkce.js +41 -0
- package/dist/pkce.test.d.ts +1 -0
- package/dist/pkce.test.js +66 -0
- package/dist/plugin.d.ts +89 -0
- package/dist/plugin.js +242 -0
- package/dist/plugin.test.d.ts +1 -0
- package/dist/plugin.test.js +210 -0
- package/dist/provider.d.ts +14 -0
- package/dist/provider.js +5 -0
- package/dist/secure-store.d.ts +54 -0
- package/dist/secure-store.js +55 -0
- package/dist/secure-store.test.d.ts +1 -0
- package/dist/secure-store.test.js +69 -0
- package/dist/types.d.ts +177 -0
- package/dist/types.js +1 -0
- package/package.json +69 -0
- package/src/adapters/expo-crypto-port.ts +11 -0
- package/src/adapters/expo-linking-port.ts +23 -0
- package/src/adapters/expo-secure-store-port.ts +20 -0
- package/src/adapters/expo-web-browser-port.ts +16 -0
- package/src/adapters/react-native-app-state-port.ts +15 -0
- package/src/adapters/react-native-passkeys-adapter.test.ts +87 -0
- package/src/adapters/react-native-passkeys-adapter.ts +138 -0
- package/src/base64url.test.ts +47 -0
- package/src/base64url.ts +92 -0
- package/src/browser-port.ts +19 -0
- package/src/client.test.ts +1275 -0
- package/src/client.ts +1329 -0
- package/src/context.ts +20 -0
- package/src/crypto-port.ts +16 -0
- package/src/errors.test.ts +94 -0
- package/src/errors.ts +216 -0
- package/src/hooks.ts +374 -0
- package/src/http.ts +159 -0
- package/src/index.ts +101 -0
- package/src/jwt.ts +24 -0
- package/src/lifecycle-ports.ts +32 -0
- package/src/linking-port.ts +16 -0
- package/src/passkey-adapter.ts +20 -0
- package/src/pkce.test.ts +84 -0
- package/src/pkce.ts +62 -0
- package/src/plugin.test.ts +303 -0
- package/src/plugin.ts +387 -0
- package/src/provider.tsx +20 -0
- package/src/secure-store.test.ts +91 -0
- package/src/secure-store.ts +102 -0
- package/src/types.ts +181 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/app.plugin.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Expo config-plugin root shim (design.md §21.5). Expo's own tooling looks
|
|
2
|
+
// for `app.plugin.js` at a package's root by convention; consumers of this
|
|
3
|
+
// package reference the `/plugin` subpath directly (see package.json
|
|
4
|
+
// `exports`), but this file is kept for tools that resolve the bare package
|
|
5
|
+
// name. It re-exports the built plugin (`pnpm --filter @smartcrab/react-native build`
|
|
6
|
+
// emits `dist/plugin.js`) since Expo's config-plugin loader executes plain
|
|
7
|
+
// JS during `expo prebuild`, not TypeScript. ESM (not CJS) because this
|
|
8
|
+
// package's `package.json` declares `"type": "module"`.
|
|
9
|
+
export { default } from "./dist/plugin.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as Crypto from "expo-crypto";
|
|
2
|
+
/** Default `CryptoPort`: `expo-crypto` (design.md §21.1 peer dependency). */
|
|
3
|
+
export const expoCryptoPort = {
|
|
4
|
+
getRandomBytes: (byteCount) => Crypto.getRandomBytes(byteCount),
|
|
5
|
+
sha256Base64: (data) => Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA256, data, {
|
|
6
|
+
encoding: Crypto.CryptoEncoding.BASE64,
|
|
7
|
+
}),
|
|
8
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as Linking from "expo-linking";
|
|
2
|
+
/** Default `LinkingPort`: `expo-linking` (design.md §21.1, §20.6). */
|
|
3
|
+
export const expoLinkingPort = {
|
|
4
|
+
parse: (url) => {
|
|
5
|
+
const parsed = Linking.parse(url);
|
|
6
|
+
const queryParams = {};
|
|
7
|
+
if (parsed.queryParams !== null) {
|
|
8
|
+
for (const [key, value] of Object.entries(parsed.queryParams)) {
|
|
9
|
+
if (typeof value === "string") {
|
|
10
|
+
queryParams[key] = value;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return { queryParams };
|
|
15
|
+
},
|
|
16
|
+
getInitialURL: () => Linking.getInitialURL(),
|
|
17
|
+
addEventListener: (handler) => {
|
|
18
|
+
const subscription = Linking.addEventListener("url", (event) => handler(event.url));
|
|
19
|
+
return () => subscription.remove();
|
|
20
|
+
},
|
|
21
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SecureStorePort } from "../secure-store.js";
|
|
2
|
+
/**
|
|
3
|
+
* Default `SecureStorePort`: `expo-secure-store` (design.md §21.1, §21.6).
|
|
4
|
+
*
|
|
5
|
+
* `AFTER_FIRST_UNLOCK` keeps the refresh token readable for background token
|
|
6
|
+
* refresh (e.g. a network-recovery-triggered refresh, design.md §21.7) while
|
|
7
|
+
* still requiring the device to have been unlocked at least once since boot;
|
|
8
|
+
* it is never `ALWAYS`/`ALWAYS_THIS_DEVICE_ONLY` (both deprecated by Expo in
|
|
9
|
+
* favor of protections that survive a stolen, still-unlocked device less).
|
|
10
|
+
*/
|
|
11
|
+
export declare const expoSecureStorePort: SecureStorePort;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as SecureStore from "expo-secure-store";
|
|
2
|
+
/**
|
|
3
|
+
* Default `SecureStorePort`: `expo-secure-store` (design.md §21.1, §21.6).
|
|
4
|
+
*
|
|
5
|
+
* `AFTER_FIRST_UNLOCK` keeps the refresh token readable for background token
|
|
6
|
+
* refresh (e.g. a network-recovery-triggered refresh, design.md §21.7) while
|
|
7
|
+
* still requiring the device to have been unlocked at least once since boot;
|
|
8
|
+
* it is never `ALWAYS`/`ALWAYS_THIS_DEVICE_ONLY` (both deprecated by Expo in
|
|
9
|
+
* favor of protections that survive a stolen, still-unlocked device less).
|
|
10
|
+
*/
|
|
11
|
+
export const expoSecureStorePort = {
|
|
12
|
+
getItemAsync: (key) => SecureStore.getItemAsync(key, { keychainAccessible: SecureStore.AFTER_FIRST_UNLOCK }),
|
|
13
|
+
setItemAsync: (key, value) => SecureStore.setItemAsync(key, value, { keychainAccessible: SecureStore.AFTER_FIRST_UNLOCK }),
|
|
14
|
+
deleteItemAsync: (key) => SecureStore.deleteItemAsync(key, { keychainAccessible: SecureStore.AFTER_FIRST_UNLOCK }),
|
|
15
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { WebBrowserPort } from "../browser-port.js";
|
|
2
|
+
/**
|
|
3
|
+
* Default `WebBrowserPort`: `expo-web-browser`'s auth session API (design.md
|
|
4
|
+
* §20.6, §21.1). `preferEphemeralSession` avoids sharing cookies with the
|
|
5
|
+
* user's normal browsing session for the duration of the sign-in ceremony.
|
|
6
|
+
*/
|
|
7
|
+
export declare const expoWebBrowserPort: WebBrowserPort;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as WebBrowser from "expo-web-browser";
|
|
2
|
+
/**
|
|
3
|
+
* Default `WebBrowserPort`: `expo-web-browser`'s auth session API (design.md
|
|
4
|
+
* §20.6, §21.1). `preferEphemeralSession` avoids sharing cookies with the
|
|
5
|
+
* user's normal browsing session for the duration of the sign-in ceremony.
|
|
6
|
+
*/
|
|
7
|
+
export const expoWebBrowserPort = {
|
|
8
|
+
openAuthSessionAsync: (url, redirectUrl) => WebBrowser.openAuthSessionAsync(url, redirectUrl, { preferEphemeralSession: true }).then((result) => result.type === "success" ? { type: "success", url: result.url } : { type: result.type }),
|
|
9
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AppStatePort } from "../lifecycle-ports.js";
|
|
2
|
+
/**
|
|
3
|
+
* Default `AppStatePort`: React Native's own `AppState` (design.md §21.1,
|
|
4
|
+
* §21.7). RN's `AppStateStatus` union is structurally identical to this
|
|
5
|
+
* package's own (`active`/`background`/`inactive`/`unknown`/`extension`), so
|
|
6
|
+
* this passes through without any cast.
|
|
7
|
+
*/
|
|
8
|
+
export declare const reactNativeAppStatePort: AppStatePort;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AppState } from "react-native";
|
|
2
|
+
/**
|
|
3
|
+
* Default `AppStatePort`: React Native's own `AppState` (design.md §21.1,
|
|
4
|
+
* §21.7). RN's `AppStateStatus` union is structurally identical to this
|
|
5
|
+
* package's own (`active`/`background`/`inactive`/`unknown`/`extension`), so
|
|
6
|
+
* this passes through without any cast.
|
|
7
|
+
*/
|
|
8
|
+
export const reactNativeAppStatePort = {
|
|
9
|
+
get currentState() {
|
|
10
|
+
return AppState.currentState;
|
|
11
|
+
},
|
|
12
|
+
addEventListener: (type, handler) => AppState.addEventListener(type, handler),
|
|
13
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { passkeyAuthenticationOptionsResponseSchema, passkeyRegistrationOptionsResponseSchema, } from "@smartcrab/contracts-public";
|
|
2
|
+
import * as Passkeys from "react-native-passkeys";
|
|
3
|
+
/**
|
|
4
|
+
* Default `PasskeyNativeAdapter`: `react-native-passkeys` (design.md §21.1,
|
|
5
|
+
* §21.2). Converts between this package's JSON-string adapter boundary and
|
|
6
|
+
* `react-native-passkeys`' object API; its types never escape this file.
|
|
7
|
+
*
|
|
8
|
+
* The `optionsJson` input is always JSON this same SDK produced moments
|
|
9
|
+
* earlier from an already schema-validated server response (design.md §16.2
|
|
10
|
+
* `.../passkeys/registration/options` / `.../authentication/options`), so
|
|
11
|
+
* re-validating it here (via the same contracts-public response schemas,
|
|
12
|
+
* since the wire shape and the native module's request shape are identical)
|
|
13
|
+
* replaces an unvalidated `unknown -> object` cast with a real check, per
|
|
14
|
+
* IMPL_NOTES §1.
|
|
15
|
+
*/
|
|
16
|
+
const parseJson = (json) => JSON.parse(json);
|
|
17
|
+
const isPresent = (value) => value !== null && value !== undefined;
|
|
18
|
+
export const reactNativePasskeysAdapter = {
|
|
19
|
+
isSupported: () => Promise.resolve(Passkeys.isSupported()),
|
|
20
|
+
create: (optionsJson) => Promise.resolve()
|
|
21
|
+
.then(() => parseJson(optionsJson))
|
|
22
|
+
.then((parsed) => {
|
|
23
|
+
const result = passkeyRegistrationOptionsResponseSchema.safeParse(parsed);
|
|
24
|
+
if (!result.success) {
|
|
25
|
+
throw new Error("PasskeyNativeAdapter.create received malformed registration options JSON");
|
|
26
|
+
}
|
|
27
|
+
return toCreationRequest(result.data);
|
|
28
|
+
})
|
|
29
|
+
.then((request) => Passkeys.create(request))
|
|
30
|
+
.then((response) => {
|
|
31
|
+
if (response === null) {
|
|
32
|
+
throw new Error("react-native-passkeys.create() returned null (ceremony cancelled)");
|
|
33
|
+
}
|
|
34
|
+
return JSON.stringify({
|
|
35
|
+
id: response.id,
|
|
36
|
+
rawId: response.rawId,
|
|
37
|
+
type: response.type,
|
|
38
|
+
response: {
|
|
39
|
+
clientDataJSON: response.response.clientDataJSON,
|
|
40
|
+
attestationObject: response.response.attestationObject,
|
|
41
|
+
...(isPresent(response.response.transports)
|
|
42
|
+
? { transports: response.response.transports }
|
|
43
|
+
: {}),
|
|
44
|
+
},
|
|
45
|
+
...(isPresent(response.authenticatorAttachment)
|
|
46
|
+
? { authenticatorAttachment: response.authenticatorAttachment }
|
|
47
|
+
: {}),
|
|
48
|
+
...(isPresent(response.clientExtensionResults)
|
|
49
|
+
? { clientExtensionResults: response.clientExtensionResults }
|
|
50
|
+
: {}),
|
|
51
|
+
});
|
|
52
|
+
}),
|
|
53
|
+
get: (optionsJson) => Promise.resolve()
|
|
54
|
+
.then(() => parseJson(optionsJson))
|
|
55
|
+
.then((parsed) => {
|
|
56
|
+
const result = passkeyAuthenticationOptionsResponseSchema.safeParse(parsed);
|
|
57
|
+
if (!result.success) {
|
|
58
|
+
throw new Error("PasskeyNativeAdapter.get received malformed authentication options JSON");
|
|
59
|
+
}
|
|
60
|
+
return toRequestOptions(result.data);
|
|
61
|
+
})
|
|
62
|
+
.then((request) => Passkeys.get(request))
|
|
63
|
+
.then((response) => {
|
|
64
|
+
if (response === null) {
|
|
65
|
+
throw new Error("react-native-passkeys.get() returned null (ceremony cancelled)");
|
|
66
|
+
}
|
|
67
|
+
return JSON.stringify({
|
|
68
|
+
id: response.id,
|
|
69
|
+
rawId: response.rawId,
|
|
70
|
+
type: response.type,
|
|
71
|
+
response: {
|
|
72
|
+
clientDataJSON: response.response.clientDataJSON,
|
|
73
|
+
authenticatorData: response.response.authenticatorData,
|
|
74
|
+
signature: response.response.signature,
|
|
75
|
+
...(isPresent(response.response.userHandle)
|
|
76
|
+
? { userHandle: response.response.userHandle }
|
|
77
|
+
: {}),
|
|
78
|
+
},
|
|
79
|
+
...(isPresent(response.authenticatorAttachment)
|
|
80
|
+
? { authenticatorAttachment: response.authenticatorAttachment }
|
|
81
|
+
: {}),
|
|
82
|
+
...(isPresent(response.clientExtensionResults)
|
|
83
|
+
? { clientExtensionResults: response.clientExtensionResults }
|
|
84
|
+
: {}),
|
|
85
|
+
});
|
|
86
|
+
}),
|
|
87
|
+
};
|
|
88
|
+
const toCreationRequest = (options) => ({
|
|
89
|
+
rp: options.rp,
|
|
90
|
+
user: options.user,
|
|
91
|
+
challenge: options.challenge,
|
|
92
|
+
pubKeyCredParams: options.pubKeyCredParams,
|
|
93
|
+
...(options.timeout !== undefined ? { timeout: options.timeout } : {}),
|
|
94
|
+
...(options.excludeCredentials !== undefined
|
|
95
|
+
? { excludeCredentials: options.excludeCredentials }
|
|
96
|
+
: {}),
|
|
97
|
+
authenticatorSelection: {
|
|
98
|
+
residentKey: options.authenticatorSelection.residentKey,
|
|
99
|
+
userVerification: options.authenticatorSelection.userVerification,
|
|
100
|
+
...(options.authenticatorSelection.authenticatorAttachment !== undefined
|
|
101
|
+
? { authenticatorAttachment: options.authenticatorSelection.authenticatorAttachment }
|
|
102
|
+
: {}),
|
|
103
|
+
},
|
|
104
|
+
attestation: options.attestation,
|
|
105
|
+
});
|
|
106
|
+
const toRequestOptions = (options) => ({
|
|
107
|
+
challenge: options.challenge,
|
|
108
|
+
...(options.timeout !== undefined ? { timeout: options.timeout } : {}),
|
|
109
|
+
rpId: options.rpId,
|
|
110
|
+
...(options.allowCredentials !== undefined ? { allowCredentials: options.allowCredentials } : {}),
|
|
111
|
+
userVerification: options.userVerification,
|
|
112
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from "vitest";
|
|
2
|
+
const mocks = vi.hoisted(() => ({
|
|
3
|
+
create: vi.fn(),
|
|
4
|
+
get: vi.fn(),
|
|
5
|
+
isSupported: vi.fn(() => true),
|
|
6
|
+
}));
|
|
7
|
+
vi.mock("react-native-passkeys", () => mocks);
|
|
8
|
+
import { reactNativePasskeysAdapter } from "./react-native-passkeys-adapter.js";
|
|
9
|
+
const registrationOptions = {
|
|
10
|
+
rp: { id: "auth.example", name: "Auth Example" },
|
|
11
|
+
user: { id: "dXNlcg", name: "user@example.com", displayName: "User" },
|
|
12
|
+
challenge: "Y2hhbGxlbmdl",
|
|
13
|
+
pubKeyCredParams: [{ type: "public-key", alg: -7 }],
|
|
14
|
+
authenticatorSelection: {
|
|
15
|
+
residentKey: "required",
|
|
16
|
+
userVerification: "required",
|
|
17
|
+
},
|
|
18
|
+
attestation: "none",
|
|
19
|
+
};
|
|
20
|
+
const authenticationOptions = {
|
|
21
|
+
challenge: "Y2hhbGxlbmdl",
|
|
22
|
+
rpId: "auth.example",
|
|
23
|
+
userVerification: "required",
|
|
24
|
+
};
|
|
25
|
+
describe("reactNativePasskeysAdapter", () => {
|
|
26
|
+
test("omits nullable optional fields from native registration responses", async () => {
|
|
27
|
+
mocks.create.mockResolvedValueOnce({
|
|
28
|
+
id: "Y3JlZGVudGlhbA",
|
|
29
|
+
rawId: "Y3JlZGVudGlhbA",
|
|
30
|
+
type: "public-key",
|
|
31
|
+
response: {
|
|
32
|
+
clientDataJSON: "Y2xpZW50LWRhdGE",
|
|
33
|
+
attestationObject: "YXR0ZXN0YXRpb24",
|
|
34
|
+
transports: null,
|
|
35
|
+
},
|
|
36
|
+
authenticatorAttachment: null,
|
|
37
|
+
clientExtensionResults: null,
|
|
38
|
+
});
|
|
39
|
+
const result = await reactNativePasskeysAdapter.create(JSON.stringify(registrationOptions));
|
|
40
|
+
expect(JSON.parse(result)).toEqual({
|
|
41
|
+
id: "Y3JlZGVudGlhbA",
|
|
42
|
+
rawId: "Y3JlZGVudGlhbA",
|
|
43
|
+
type: "public-key",
|
|
44
|
+
response: {
|
|
45
|
+
clientDataJSON: "Y2xpZW50LWRhdGE",
|
|
46
|
+
attestationObject: "YXR0ZXN0YXRpb24",
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
test("omits nullable optional fields from native authentication responses", async () => {
|
|
51
|
+
mocks.get.mockResolvedValueOnce({
|
|
52
|
+
id: "Y3JlZGVudGlhbA",
|
|
53
|
+
rawId: "Y3JlZGVudGlhbA",
|
|
54
|
+
type: "public-key",
|
|
55
|
+
response: {
|
|
56
|
+
clientDataJSON: "Y2xpZW50LWRhdGE",
|
|
57
|
+
authenticatorData: "YXV0aGVudGljYXRvcg",
|
|
58
|
+
signature: "c2lnbmF0dXJl",
|
|
59
|
+
userHandle: null,
|
|
60
|
+
},
|
|
61
|
+
authenticatorAttachment: null,
|
|
62
|
+
clientExtensionResults: null,
|
|
63
|
+
});
|
|
64
|
+
const result = await reactNativePasskeysAdapter.get(JSON.stringify(authenticationOptions));
|
|
65
|
+
expect(JSON.parse(result)).toEqual({
|
|
66
|
+
id: "Y3JlZGVudGlhbA",
|
|
67
|
+
rawId: "Y3JlZGVudGlhbA",
|
|
68
|
+
type: "public-key",
|
|
69
|
+
response: {
|
|
70
|
+
clientDataJSON: "Y2xpZW50LWRhdGE",
|
|
71
|
+
authenticatorData: "YXV0aGVudGljYXRvcg",
|
|
72
|
+
signature: "c2lnbmF0dXJl",
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* base64url (RFC 4648 §5, no padding) helpers for the React Native SDK.
|
|
3
|
+
*
|
|
4
|
+
* Hermes (RN's JS engine) has no `atob`/`btoa`/`Buffer` guarantees for
|
|
5
|
+
* arbitrary bytes, so encoding is implemented directly over `Uint8Array`
|
|
6
|
+
* (same approach as `@smartcrab/identifiers`, which this package does not
|
|
7
|
+
* depend on).
|
|
8
|
+
*/
|
|
9
|
+
/** base64url-encodes raw bytes without padding. Suitable for PKCE/state values. */
|
|
10
|
+
export declare const base64UrlEncodeBytes: (bytes: Uint8Array) => string;
|
|
11
|
+
/** Reverses `base64UrlEncodeBytes`. Throws on invalid input (see callers: always wrapped in a promise chain, never a bare `try`). */
|
|
12
|
+
export declare const base64UrlDecodeToBytes: (encoded: string) => Uint8Array;
|
|
13
|
+
export declare const base64UrlDecodeToUtf8String: (encoded: string) => string;
|
|
14
|
+
/**
|
|
15
|
+
* Converts a standard base64 string (as returned by
|
|
16
|
+
* `expo-crypto`'s `digestStringAsync` with `CryptoEncoding.BASE64`) into
|
|
17
|
+
* unpadded base64url. Returns `null` when the input is not valid base64 —
|
|
18
|
+
* the digest source is a native module and is never trusted blindly.
|
|
19
|
+
*/
|
|
20
|
+
export declare const standardBase64ToBase64Url: (value: string) => string | null;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* base64url (RFC 4648 §5, no padding) helpers for the React Native SDK.
|
|
3
|
+
*
|
|
4
|
+
* Hermes (RN's JS engine) has no `atob`/`btoa`/`Buffer` guarantees for
|
|
5
|
+
* arbitrary bytes, so encoding is implemented directly over `Uint8Array`
|
|
6
|
+
* (same approach as `@smartcrab/identifiers`, which this package does not
|
|
7
|
+
* depend on).
|
|
8
|
+
*/
|
|
9
|
+
const BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
10
|
+
/** base64url-encodes raw bytes without padding. Suitable for PKCE/state values. */
|
|
11
|
+
export const base64UrlEncodeBytes = (bytes) => {
|
|
12
|
+
let out = "";
|
|
13
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
14
|
+
const b0 = bytes[i] ?? 0;
|
|
15
|
+
const b1 = bytes[i + 1];
|
|
16
|
+
const b2 = bytes[i + 2];
|
|
17
|
+
out += BASE64URL_ALPHABET[b0 >> 2] ?? "";
|
|
18
|
+
out += BASE64URL_ALPHABET[((b0 & 0x03) << 4) | ((b1 ?? 0) >> 4)] ?? "";
|
|
19
|
+
if (b1 !== undefined)
|
|
20
|
+
out += BASE64URL_ALPHABET[((b1 & 0x0f) << 2) | ((b2 ?? 0) >> 6)] ?? "";
|
|
21
|
+
if (b2 !== undefined)
|
|
22
|
+
out += BASE64URL_ALPHABET[b2 & 0x3f] ?? "";
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
};
|
|
26
|
+
/** Reverses `base64UrlEncodeBytes`. Throws on invalid input (see callers: always wrapped in a promise chain, never a bare `try`). */
|
|
27
|
+
export const base64UrlDecodeToBytes = (encoded) => {
|
|
28
|
+
const out = [];
|
|
29
|
+
let buffer = 0;
|
|
30
|
+
let bits = 0;
|
|
31
|
+
for (const char of encoded) {
|
|
32
|
+
const index = BASE64URL_ALPHABET.indexOf(char);
|
|
33
|
+
if (index < 0) {
|
|
34
|
+
throw new Error(`Invalid base64url character: ${char}`);
|
|
35
|
+
}
|
|
36
|
+
buffer = (buffer << 6) | index;
|
|
37
|
+
bits += 6;
|
|
38
|
+
if (bits >= 8) {
|
|
39
|
+
bits -= 8;
|
|
40
|
+
out.push((buffer >>> bits) & 0xff);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return new Uint8Array(out);
|
|
44
|
+
};
|
|
45
|
+
/** Minimal UTF-8 byte decoder (React Native's ambient types do not include `TextDecoder`). */
|
|
46
|
+
const decodeUtf8 = (bytes) => {
|
|
47
|
+
let out = "";
|
|
48
|
+
let i = 0;
|
|
49
|
+
while (i < bytes.length) {
|
|
50
|
+
const byte0 = bytes[i] ?? 0;
|
|
51
|
+
if (byte0 < 0x80) {
|
|
52
|
+
out += String.fromCharCode(byte0);
|
|
53
|
+
i += 1;
|
|
54
|
+
}
|
|
55
|
+
else if ((byte0 & 0xe0) === 0xc0) {
|
|
56
|
+
const byte1 = bytes[i + 1] ?? 0;
|
|
57
|
+
out += String.fromCharCode(((byte0 & 0x1f) << 6) | (byte1 & 0x3f));
|
|
58
|
+
i += 2;
|
|
59
|
+
}
|
|
60
|
+
else if ((byte0 & 0xf0) === 0xe0) {
|
|
61
|
+
const byte1 = bytes[i + 1] ?? 0;
|
|
62
|
+
const byte2 = bytes[i + 2] ?? 0;
|
|
63
|
+
out += String.fromCharCode(((byte0 & 0x0f) << 12) | ((byte1 & 0x3f) << 6) | (byte2 & 0x3f));
|
|
64
|
+
i += 3;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const byte1 = bytes[i + 1] ?? 0;
|
|
68
|
+
const byte2 = bytes[i + 2] ?? 0;
|
|
69
|
+
const byte3 = bytes[i + 3] ?? 0;
|
|
70
|
+
const codepoint = ((byte0 & 0x07) << 18) | ((byte1 & 0x3f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f);
|
|
71
|
+
out += String.fromCodePoint(codepoint);
|
|
72
|
+
i += 4;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
};
|
|
77
|
+
export const base64UrlDecodeToUtf8String = (encoded) => decodeUtf8(base64UrlDecodeToBytes(encoded));
|
|
78
|
+
const STANDARD_BASE64_PATTERN = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
79
|
+
/**
|
|
80
|
+
* Converts a standard base64 string (as returned by
|
|
81
|
+
* `expo-crypto`'s `digestStringAsync` with `CryptoEncoding.BASE64`) into
|
|
82
|
+
* unpadded base64url. Returns `null` when the input is not valid base64 —
|
|
83
|
+
* the digest source is a native module and is never trusted blindly.
|
|
84
|
+
*/
|
|
85
|
+
export const standardBase64ToBase64Url = (value) => {
|
|
86
|
+
if (!STANDARD_BASE64_PATTERN.test(value))
|
|
87
|
+
return null;
|
|
88
|
+
return value.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
89
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|