@trustpin/react-native-attest 0.0.0-stage → 7.1.1
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 +59 -0
- package/README.md +281 -2
- package/TrustPinReactNativeAttest.podspec +33 -0
- package/android/build.gradle +121 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/kotlin/cloud/trustpin/reactnative/attest/AttestEventHub.kt +106 -0
- package/android/src/main/kotlin/cloud/trustpin/reactnative/attest/AttestExecution.kt +59 -0
- package/android/src/main/kotlin/cloud/trustpin/reactnative/attest/AttestWire.kt +139 -0
- package/android/src/main/kotlin/cloud/trustpin/reactnative/attest/TrustPinReactNativeAttest.kt +115 -0
- package/android/src/main/kotlin/cloud/trustpin/reactnative/attest/TrustPinReactNativeAttestModule.kt +119 -0
- package/android/src/main/kotlin/cloud/trustpin/reactnative/attest/TrustPinReactNativeAttestPackage.kt +31 -0
- package/app.plugin.js +4 -0
- package/ios/TrustPinAttestBridge.swift +198 -0
- package/ios/TrustPinAttestEventHub.swift +96 -0
- package/ios/TrustPinAttestWire.swift +203 -0
- package/ios/TrustPinReactNativeAttest.swift +92 -0
- package/ios/TrustPinReactNativeAttestModule.h +11 -0
- package/ios/TrustPinReactNativeAttestModule.mm +122 -0
- package/lib/NativeTrustPinReactNativeAttest.d.ts +59 -0
- package/lib/NativeTrustPinReactNativeAttest.js +4 -0
- package/lib/errors.d.ts +48 -0
- package/lib/errors.js +69 -0
- package/lib/index.d.ts +175 -0
- package/lib/index.js +262 -0
- package/package.json +69 -4
- package/plugin/build/afterDangerous.d.ts +8 -0
- package/plugin/build/afterDangerous.js +22 -0
- package/plugin/build/android.d.ts +16 -0
- package/plugin/build/android.js +109 -0
- package/plugin/build/index.d.ts +5 -0
- package/plugin/build/index.js +20 -0
- package/plugin/build/ios.d.ts +16 -0
- package/plugin/build/ios.js +129 -0
- package/plugin/build/types.d.ts +40 -0
- package/plugin/build/types.js +45 -0
- package/src/NativeTrustPinReactNativeAttest.ts +65 -0
- package/src/errors.ts +73 -0
- package/src/index.ts +351 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TrustPin app attestation for React Native.
|
|
3
|
+
*
|
|
4
|
+
* @module @trustpin/react-native-attest
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import NativeTrustPinReactNativeAttest from './NativeTrustPinReactNativeAttest';
|
|
8
|
+
import type { EventPayload, LogEventPayload, Spec, StatePayload } from './NativeTrustPinReactNativeAttest';
|
|
9
|
+
import {
|
|
10
|
+
NOT_INITIALIZED_MESSAGE,
|
|
11
|
+
TrustPinAttestError,
|
|
12
|
+
TrustPinAttestErrorCodes,
|
|
13
|
+
toAttestError,
|
|
14
|
+
} from './errors';
|
|
15
|
+
import type { TrustPinAttestErrorCode } from './errors';
|
|
16
|
+
|
|
17
|
+
/** Log verbosity accepted by {@link TrustPinAttest.setLogLevel}. Off by default. */
|
|
18
|
+
export type TrustPinAttestLogLevel = 'none' | 'error' | 'warning' | 'info' | 'debug';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The attestation state. Observing it is optional: requests get the header
|
|
22
|
+
* whenever a token is ready.
|
|
23
|
+
*
|
|
24
|
+
* After a failure no timer runs: nothing is tried before `retryAtMs`, and the
|
|
25
|
+
* next request, `getToken()` or `prefetch()` after it starts a new attempt. A
|
|
26
|
+
* `null` `retryAtMs` means no new attempt in this launch, until `reset()` or a
|
|
27
|
+
* restart, except `BINDING_REQUIRED`, which resumes once a binding is set.
|
|
28
|
+
*/
|
|
29
|
+
export type TrustPinAttestState =
|
|
30
|
+
| { status: 'unavailable' }
|
|
31
|
+
| { status: 'attesting' }
|
|
32
|
+
| { status: 'ready'; expiresAtMs: number }
|
|
33
|
+
| { status: 'failed'; error: TrustPinAttestError; retryAtMs: number | null };
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* What the native module did, for your monitoring: durations, waits, counts
|
|
37
|
+
* and reason codes, never a token, a key or a device identifier. New event
|
|
38
|
+
* types can appear in a minor release: keep a default case.
|
|
39
|
+
*/
|
|
40
|
+
export type TrustPinAttestEvent =
|
|
41
|
+
| { type: 'keyGenerated' }
|
|
42
|
+
| { type: 'keyReplaced'; count: number }
|
|
43
|
+
| { type: 'attestationSucceeded'; durationMs: number }
|
|
44
|
+
| { type: 'refused'; retryAtMs: number | null }
|
|
45
|
+
| { type: 'attestationFailed'; reasons: readonly string[]; retryAtMs: number | null }
|
|
46
|
+
| { type: 'refreshSucceeded'; durationMs: number }
|
|
47
|
+
| { type: 'refreshFailed'; error: TrustPinAttestError }
|
|
48
|
+
| { type: 'waitEntered'; untilMs: number }
|
|
49
|
+
| { type: 'waitCleared' }
|
|
50
|
+
| { type: 'deviceUnsupported' }
|
|
51
|
+
| { type: 'verifierUnavailable' }
|
|
52
|
+
| { type: 'assertionSucceeded'; durationMs: number }
|
|
53
|
+
| { type: 'assertionFailed'; error: TrustPinAttestError };
|
|
54
|
+
|
|
55
|
+
/** One log message of the native attestation module. */
|
|
56
|
+
export interface TrustPinAttestLogEvent {
|
|
57
|
+
level: Exclude<TrustPinAttestLogLevel, 'none'>;
|
|
58
|
+
message: string;
|
|
59
|
+
timestampMs: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface TrustPinAttestSubscription {
|
|
63
|
+
remove(): void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const LOG_LEVELS: readonly TrustPinAttestLogLevel[] = ['none', 'error', 'warning', 'info', 'debug'];
|
|
67
|
+
|
|
68
|
+
/** Whether each stream has replayed its buffered startup events to a JS listener. */
|
|
69
|
+
const earlyEventsFlushed = { events: false, logs: false };
|
|
70
|
+
|
|
71
|
+
function native(): Spec {
|
|
72
|
+
if (NativeTrustPinReactNativeAttest == null) {
|
|
73
|
+
throw new TrustPinAttestError(TrustPinAttestErrorCodes.NOT_SET_UP, NOT_INITIALIZED_MESSAGE);
|
|
74
|
+
}
|
|
75
|
+
return NativeTrustPinReactNativeAttest;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Runs a native call, rejecting with a TrustPinAttestError. */
|
|
79
|
+
async function call<T>(body: (module: Spec) => Promise<T>): Promise<T> {
|
|
80
|
+
try {
|
|
81
|
+
return await body(native());
|
|
82
|
+
} catch (error) {
|
|
83
|
+
throw toAttestError(error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function errorOf(payload: { code?: string; message?: string; reasons?: string[] }): TrustPinAttestError {
|
|
88
|
+
return new TrustPinAttestError(
|
|
89
|
+
payload.code ?? TrustPinAttestErrorCodes.ATTEST_ERROR,
|
|
90
|
+
payload.message ?? '',
|
|
91
|
+
payload.reasons ?? [],
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function stateOf(payload: StatePayload): TrustPinAttestState {
|
|
96
|
+
switch (payload.state) {
|
|
97
|
+
case 'attesting':
|
|
98
|
+
return { status: 'attesting' };
|
|
99
|
+
case 'ready':
|
|
100
|
+
return { status: 'ready', expiresAtMs: payload.expiresAtMs ?? Date.now() };
|
|
101
|
+
case 'failed':
|
|
102
|
+
return { status: 'failed', error: errorOf(payload), retryAtMs: payload.retryAtMs ?? null };
|
|
103
|
+
default:
|
|
104
|
+
return { status: 'unavailable' };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Decodes a native event, or returns `null` for one this version doesn't know. */
|
|
109
|
+
function eventOf(payload: EventPayload): TrustPinAttestEvent | null {
|
|
110
|
+
switch (payload.event) {
|
|
111
|
+
case 'keyGenerated':
|
|
112
|
+
case 'waitCleared':
|
|
113
|
+
case 'deviceUnsupported':
|
|
114
|
+
case 'verifierUnavailable':
|
|
115
|
+
return { type: payload.event };
|
|
116
|
+
case 'keyReplaced':
|
|
117
|
+
return { type: 'keyReplaced', count: payload.count ?? 0 };
|
|
118
|
+
case 'attestationSucceeded':
|
|
119
|
+
case 'refreshSucceeded':
|
|
120
|
+
case 'assertionSucceeded':
|
|
121
|
+
return { type: payload.event, durationMs: payload.durationMs ?? 0 };
|
|
122
|
+
case 'refused':
|
|
123
|
+
return { type: 'refused', retryAtMs: payload.retryAtMs ?? null };
|
|
124
|
+
case 'attestationFailed':
|
|
125
|
+
return {
|
|
126
|
+
type: 'attestationFailed',
|
|
127
|
+
reasons: payload.reasons ?? [],
|
|
128
|
+
retryAtMs: payload.retryAtMs ?? null,
|
|
129
|
+
};
|
|
130
|
+
case 'refreshFailed':
|
|
131
|
+
case 'assertionFailed':
|
|
132
|
+
return { type: payload.event, error: errorOf(payload) };
|
|
133
|
+
case 'waitEntered':
|
|
134
|
+
return { type: 'waitEntered', untilMs: payload.untilMs ?? Date.now() };
|
|
135
|
+
default:
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function logLevelOf(level: string): TrustPinAttestLogEvent['level'] {
|
|
141
|
+
return level === 'error' || level === 'warning' || level === 'debug' ? level : 'info';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The 32 bytes of a digest as lowercase hex, or `null` for anything else. */
|
|
145
|
+
function digestHex(digest: Uint8Array | ArrayBuffer | string): string | null {
|
|
146
|
+
if (typeof digest === 'string') {
|
|
147
|
+
return /^[0-9a-fA-F]{64}$/.test(digest) ? digest.toLowerCase() : null;
|
|
148
|
+
}
|
|
149
|
+
const bytes = digest instanceof Uint8Array ? digest : new Uint8Array(digest);
|
|
150
|
+
if (bytes.length !== 32) return null;
|
|
151
|
+
return Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* TrustPin app attestation for React Native.
|
|
156
|
+
*
|
|
157
|
+
* Proves to your API that a request comes from your genuine app on a genuine
|
|
158
|
+
* device. The native init helper sets the module up from the same
|
|
159
|
+
* configuration file as pinning, before JavaScript runs. From then on, every
|
|
160
|
+
* `fetch`, `XMLHttpRequest` and axios request to an attested host carries the
|
|
161
|
+
* `TrustPin-Attestation` header, added natively; when no token is ready in a
|
|
162
|
+
* short, bounded time, the request goes out without it and your API decides.
|
|
163
|
+
*
|
|
164
|
+
* Use this API to bind tokens to the user's session, get the token for
|
|
165
|
+
* transports React Native's networking doesn't carry, add an assertion to a
|
|
166
|
+
* sensitive operation, and observe the state, events and logs.
|
|
167
|
+
*/
|
|
168
|
+
export const TrustPinAttest = {
|
|
169
|
+
/** The header that carries the token. */
|
|
170
|
+
headerName: 'TrustPin-Attestation',
|
|
171
|
+
|
|
172
|
+
/** The header that carries the assertion of a sensitive operation. */
|
|
173
|
+
assertionHeaderName: 'TrustPin-Assertion',
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Starts attestation or renewal in the background so the first request
|
|
177
|
+
* doesn't wait, typically after sign-in. Never rejects.
|
|
178
|
+
*/
|
|
179
|
+
async prefetch(): Promise<void> {
|
|
180
|
+
try {
|
|
181
|
+
await native().prefetch();
|
|
182
|
+
} catch {
|
|
183
|
+
// Fire and forget: a failure shows in the state.
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The current token, attesting or renewing first if needed. For transports
|
|
189
|
+
* React Native's networking doesn't carry (gRPC, some WebSocket clients);
|
|
190
|
+
* send it in the {@link TrustPinAttest.headerName} header.
|
|
191
|
+
*/
|
|
192
|
+
getToken(): Promise<string> {
|
|
193
|
+
return call(module => module.getToken());
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The headers to add to a request for `url`: the token header when `url` is
|
|
198
|
+
* HTTPS on an attested host and a token is ready in time, otherwise none.
|
|
199
|
+
* Requests made with `fetch`, `XMLHttpRequest` or axios get them
|
|
200
|
+
* automatically; use this for other clients. `headers` are the request's
|
|
201
|
+
* own headers, used for header binding. Never rejects.
|
|
202
|
+
*/
|
|
203
|
+
async attestationHeaders(
|
|
204
|
+
url: string,
|
|
205
|
+
options: { method?: string; headers?: Record<string, string> } = {},
|
|
206
|
+
): Promise<Record<string, string>> {
|
|
207
|
+
try {
|
|
208
|
+
const result = await native().attestationHeaders(url, options.method ?? 'GET', options.headers ?? {});
|
|
209
|
+
return result as Record<string, string>;
|
|
210
|
+
} catch {
|
|
211
|
+
return {};
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The headers for one sensitive operation: the token and an assertion that
|
|
217
|
+
* binds this device's attested key to `payloadDigest`, the SHA-256 digest of
|
|
218
|
+
* whatever your backend verifies (for example the request body), as 32
|
|
219
|
+
* bytes or 64 hex characters. Send both with that request; your backend
|
|
220
|
+
* passes them and its own digest to your verifier. Each call produces a new
|
|
221
|
+
* assertion.
|
|
222
|
+
*
|
|
223
|
+
* Rejects with `INVALID_DIGEST` for a digest that isn't 32 bytes, and the
|
|
224
|
+
* usual codes when no token is available.
|
|
225
|
+
*/
|
|
226
|
+
assertionHeaders(payloadDigest: Uint8Array | ArrayBuffer | string): Promise<Record<string, string>> {
|
|
227
|
+
const hex = digestHex(payloadDigest);
|
|
228
|
+
if (hex == null) {
|
|
229
|
+
return Promise.reject(
|
|
230
|
+
new TrustPinAttestError(
|
|
231
|
+
TrustPinAttestErrorCodes.INVALID_DIGEST,
|
|
232
|
+
'assertionHeaders: the digest must be 32 bytes (a SHA-256 digest) or 64 hex characters.',
|
|
233
|
+
),
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
return call(module => module.assertionHeaders(hex)) as Promise<Record<string, string>>;
|
|
237
|
+
},
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Binds tokens to a fixed `value`, such as a session id: only its SHA-256
|
|
241
|
+
* travels. `null` removes the fixed binding. A change renews the token with
|
|
242
|
+
* the existing device key.
|
|
243
|
+
*/
|
|
244
|
+
setTokenBinding(value: string | null): Promise<void> {
|
|
245
|
+
return call(module => module.setTokenBinding(value));
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Binds each token to the value of the request header `name`, usually
|
|
250
|
+
* `Authorization`: only its SHA-256 travels. `null` stops header binding.
|
|
251
|
+
*/
|
|
252
|
+
bindToHeader(name: string | null): Promise<void> {
|
|
253
|
+
return call(module => module.bindToHeader(name));
|
|
254
|
+
},
|
|
255
|
+
|
|
256
|
+
/** The current attestation state. */
|
|
257
|
+
async getState(): Promise<TrustPinAttestState> {
|
|
258
|
+
return stateOf(await call(module => module.getState()));
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Subscribes to the state: `listener` gets the current state first, then
|
|
263
|
+
* every change.
|
|
264
|
+
*/
|
|
265
|
+
onStateChange(listener: (state: TrustPinAttestState) => void): TrustPinAttestSubscription {
|
|
266
|
+
const module = native();
|
|
267
|
+
let changed = false;
|
|
268
|
+
let removed = false;
|
|
269
|
+
const subscription = module.onStateChange(payload => {
|
|
270
|
+
changed = true;
|
|
271
|
+
listener(stateOf(payload));
|
|
272
|
+
});
|
|
273
|
+
module.getState().then(
|
|
274
|
+
payload => {
|
|
275
|
+
// A change that arrived first is newer than this read.
|
|
276
|
+
if (!changed && !removed) listener(stateOf(payload));
|
|
277
|
+
},
|
|
278
|
+
() => undefined,
|
|
279
|
+
);
|
|
280
|
+
return {
|
|
281
|
+
remove() {
|
|
282
|
+
removed = true;
|
|
283
|
+
subscription.remove();
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
},
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Subscribes to the module's events. Events from before JavaScript was
|
|
290
|
+
* running replay to the first subscriber.
|
|
291
|
+
*/
|
|
292
|
+
onEvent(listener: (event: TrustPinAttestEvent) => void): TrustPinAttestSubscription {
|
|
293
|
+
const module = native();
|
|
294
|
+
const subscription = module.onEvent(payload => {
|
|
295
|
+
const event = eventOf(payload);
|
|
296
|
+
if (event != null) listener(event);
|
|
297
|
+
});
|
|
298
|
+
if (!earlyEventsFlushed.events) {
|
|
299
|
+
earlyEventsFlushed.events = true;
|
|
300
|
+
module.flushEarlyEvents();
|
|
301
|
+
}
|
|
302
|
+
return subscription;
|
|
303
|
+
},
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Subscribes to the module's log output, already filtered by the log level.
|
|
307
|
+
* Messages never contain tokens, key certificates, bound values or debug
|
|
308
|
+
* tokens. Early messages replay to the first subscriber.
|
|
309
|
+
*/
|
|
310
|
+
onLogEvent(listener: (event: TrustPinAttestLogEvent) => void): TrustPinAttestSubscription {
|
|
311
|
+
const module = native();
|
|
312
|
+
const subscription = module.onLogEvent((payload: LogEventPayload) => {
|
|
313
|
+
listener({
|
|
314
|
+
level: logLevelOf(payload.level),
|
|
315
|
+
message: payload.message,
|
|
316
|
+
timestampMs: payload.timestampMs,
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
if (!earlyEventsFlushed.logs) {
|
|
320
|
+
earlyEventsFlushed.logs = true;
|
|
321
|
+
module.flushEarlyLogEvents();
|
|
322
|
+
}
|
|
323
|
+
return subscription;
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Discards the device key and key certificate; the next request attests a
|
|
328
|
+
* new key, so the token's `sub` changes. For support flows, not routine
|
|
329
|
+
* use.
|
|
330
|
+
*/
|
|
331
|
+
reset(): Promise<void> {
|
|
332
|
+
return call(module => module.reset());
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
/** Sets the module's log verbosity. Use `'debug'` only in development. */
|
|
336
|
+
setLogLevel(level: TrustPinAttestLogLevel): Promise<void> {
|
|
337
|
+
if (!LOG_LEVELS.includes(level)) {
|
|
338
|
+
return Promise.reject(
|
|
339
|
+
new TrustPinAttestError(
|
|
340
|
+
TrustPinAttestErrorCodes.INVALID_ARGUMENTS,
|
|
341
|
+
`setLogLevel: level must be one of ${LOG_LEVELS.join(', ')}; got ${String(level)}.`,
|
|
342
|
+
),
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
return call(module => module.setLogLevel(level));
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export { TrustPinAttestError, TrustPinAttestErrorCodes };
|
|
350
|
+
export type { TrustPinAttestErrorCode };
|
|
351
|
+
export default TrustPinAttest;
|