@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/lib/errors.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NOT_INITIALIZED_MESSAGE = exports.TrustPinAttestError = exports.TrustPinAttestErrorCodes = void 0;
|
|
4
|
+
exports.toAttestError = toAttestError;
|
|
5
|
+
/**
|
|
6
|
+
* Stable error codes of the attestation module. Native rejections carry one
|
|
7
|
+
* of these as the rejection `code`; new codes can appear in a minor release,
|
|
8
|
+
* so handle unknown ones.
|
|
9
|
+
*/
|
|
10
|
+
exports.TrustPinAttestErrorCodes = {
|
|
11
|
+
/** A configuration value was rejected, or the configuration file is missing. */
|
|
12
|
+
INVALID_CONFIGURATION: 'INVALID_CONFIGURATION',
|
|
13
|
+
/** Setup ran twice. */
|
|
14
|
+
ALREADY_INITIALIZED: 'ALREADY_INITIALIZED',
|
|
15
|
+
/** Called before the native init helper set the module up. */
|
|
16
|
+
NOT_SET_UP: 'NOT_SET_UP',
|
|
17
|
+
/** App Attest is unsupported on this device (iOS). */
|
|
18
|
+
UNSUPPORTED_DEVICE: 'UNSUPPORTED_DEVICE',
|
|
19
|
+
/** The device can't produce the evidence the policy requires. */
|
|
20
|
+
EVIDENCE_UNAVAILABLE: 'EVIDENCE_UNAVAILABLE',
|
|
21
|
+
/** The policy requires a token binding and none is set yet. */
|
|
22
|
+
BINDING_REQUIRED: 'BINDING_REQUIRED',
|
|
23
|
+
/** The verifier refused; see `reasons`. */
|
|
24
|
+
ATTESTATION_FAILED: 'ATTESTATION_FAILED',
|
|
25
|
+
/** The verifier couldn't be reached, including when strict pinning refuses its host. */
|
|
26
|
+
VERIFIER_UNAVAILABLE: 'VERIFIER_UNAVAILABLE',
|
|
27
|
+
/** The verifier is too old for this SDK: update it. */
|
|
28
|
+
VERIFIER_INCOMPATIBLE: 'VERIFIER_INCOMPATIBLE',
|
|
29
|
+
/** `assertionHeaders` got a digest that isn't 32 bytes. */
|
|
30
|
+
INVALID_DIGEST: 'INVALID_DIGEST',
|
|
31
|
+
/** An argument was rejected before it reached the native module. */
|
|
32
|
+
INVALID_ARGUMENTS: 'INVALID_ARGUMENTS',
|
|
33
|
+
/** The operation was cancelled. */
|
|
34
|
+
CANCELLED: 'CANCELLED',
|
|
35
|
+
/** Any other native failure. */
|
|
36
|
+
ATTEST_ERROR: 'ATTEST_ERROR',
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The error of a rejected call, of a failed state and of failure events.
|
|
40
|
+
* `code` is one of {@link TrustPinAttestErrorCodes} or a newer one.
|
|
41
|
+
*/
|
|
42
|
+
class TrustPinAttestError extends Error {
|
|
43
|
+
constructor(code, message, reasons = []) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = 'TrustPinAttestError';
|
|
46
|
+
this.code = code;
|
|
47
|
+
this.reasons = reasons;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.TrustPinAttestError = TrustPinAttestError;
|
|
51
|
+
/** The message must name the missing native init step. */
|
|
52
|
+
exports.NOT_INITIALIZED_MESSAGE = 'TrustPin attestation native module is unavailable. Call ' +
|
|
53
|
+
'TrustPinReactNativeAttest.start(...) right after TrustPinReactNative.start(...) ' +
|
|
54
|
+
'in AppDelegate.application(_:didFinishLaunchingWithOptions:) on iOS and in ' +
|
|
55
|
+
'Application.onCreate() on Android, then rebuild the app (pod install / gradle sync). ' +
|
|
56
|
+
'With Expo, add the @trustpin/react-native-attest config plugin and run prebuild.';
|
|
57
|
+
/** Converts a native rejection (or anything thrown) into a TrustPinAttestError. */
|
|
58
|
+
function toAttestError(error) {
|
|
59
|
+
if (error instanceof TrustPinAttestError)
|
|
60
|
+
return error;
|
|
61
|
+
const candidate = error;
|
|
62
|
+
const code = typeof candidate?.code === 'string' ? candidate.code : exports.TrustPinAttestErrorCodes.ATTEST_ERROR;
|
|
63
|
+
const message = typeof candidate?.message === 'string' ? candidate.message : String(error);
|
|
64
|
+
const info = candidate?.userInfo;
|
|
65
|
+
const reasons = Array.isArray(info?.reasons)
|
|
66
|
+
? info.reasons.filter((reason) => typeof reason === 'string')
|
|
67
|
+
: [];
|
|
68
|
+
return new TrustPinAttestError(code, message, reasons);
|
|
69
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TrustPin app attestation for React Native.
|
|
3
|
+
*
|
|
4
|
+
* @module @trustpin/react-native-attest
|
|
5
|
+
*/
|
|
6
|
+
import { TrustPinAttestError, TrustPinAttestErrorCodes } from './errors';
|
|
7
|
+
import type { TrustPinAttestErrorCode } from './errors';
|
|
8
|
+
/** Log verbosity accepted by {@link TrustPinAttest.setLogLevel}. Off by default. */
|
|
9
|
+
export type TrustPinAttestLogLevel = 'none' | 'error' | 'warning' | 'info' | 'debug';
|
|
10
|
+
/**
|
|
11
|
+
* The attestation state. Observing it is optional: requests get the header
|
|
12
|
+
* whenever a token is ready.
|
|
13
|
+
*
|
|
14
|
+
* After a failure no timer runs: nothing is tried before `retryAtMs`, and the
|
|
15
|
+
* next request, `getToken()` or `prefetch()` after it starts a new attempt. A
|
|
16
|
+
* `null` `retryAtMs` means no new attempt in this launch, until `reset()` or a
|
|
17
|
+
* restart, except `BINDING_REQUIRED`, which resumes once a binding is set.
|
|
18
|
+
*/
|
|
19
|
+
export type TrustPinAttestState = {
|
|
20
|
+
status: 'unavailable';
|
|
21
|
+
} | {
|
|
22
|
+
status: 'attesting';
|
|
23
|
+
} | {
|
|
24
|
+
status: 'ready';
|
|
25
|
+
expiresAtMs: number;
|
|
26
|
+
} | {
|
|
27
|
+
status: 'failed';
|
|
28
|
+
error: TrustPinAttestError;
|
|
29
|
+
retryAtMs: number | null;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* What the native module did, for your monitoring: durations, waits, counts
|
|
33
|
+
* and reason codes, never a token, a key or a device identifier. New event
|
|
34
|
+
* types can appear in a minor release: keep a default case.
|
|
35
|
+
*/
|
|
36
|
+
export type TrustPinAttestEvent = {
|
|
37
|
+
type: 'keyGenerated';
|
|
38
|
+
} | {
|
|
39
|
+
type: 'keyReplaced';
|
|
40
|
+
count: number;
|
|
41
|
+
} | {
|
|
42
|
+
type: 'attestationSucceeded';
|
|
43
|
+
durationMs: number;
|
|
44
|
+
} | {
|
|
45
|
+
type: 'refused';
|
|
46
|
+
retryAtMs: number | null;
|
|
47
|
+
} | {
|
|
48
|
+
type: 'attestationFailed';
|
|
49
|
+
reasons: readonly string[];
|
|
50
|
+
retryAtMs: number | null;
|
|
51
|
+
} | {
|
|
52
|
+
type: 'refreshSucceeded';
|
|
53
|
+
durationMs: number;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'refreshFailed';
|
|
56
|
+
error: TrustPinAttestError;
|
|
57
|
+
} | {
|
|
58
|
+
type: 'waitEntered';
|
|
59
|
+
untilMs: number;
|
|
60
|
+
} | {
|
|
61
|
+
type: 'waitCleared';
|
|
62
|
+
} | {
|
|
63
|
+
type: 'deviceUnsupported';
|
|
64
|
+
} | {
|
|
65
|
+
type: 'verifierUnavailable';
|
|
66
|
+
} | {
|
|
67
|
+
type: 'assertionSucceeded';
|
|
68
|
+
durationMs: number;
|
|
69
|
+
} | {
|
|
70
|
+
type: 'assertionFailed';
|
|
71
|
+
error: TrustPinAttestError;
|
|
72
|
+
};
|
|
73
|
+
/** One log message of the native attestation module. */
|
|
74
|
+
export interface TrustPinAttestLogEvent {
|
|
75
|
+
level: Exclude<TrustPinAttestLogLevel, 'none'>;
|
|
76
|
+
message: string;
|
|
77
|
+
timestampMs: number;
|
|
78
|
+
}
|
|
79
|
+
export interface TrustPinAttestSubscription {
|
|
80
|
+
remove(): void;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* TrustPin app attestation for React Native.
|
|
84
|
+
*
|
|
85
|
+
* Proves to your API that a request comes from your genuine app on a genuine
|
|
86
|
+
* device. The native init helper sets the module up from the same
|
|
87
|
+
* configuration file as pinning, before JavaScript runs. From then on, every
|
|
88
|
+
* `fetch`, `XMLHttpRequest` and axios request to an attested host carries the
|
|
89
|
+
* `TrustPin-Attestation` header, added natively; when no token is ready in a
|
|
90
|
+
* short, bounded time, the request goes out without it and your API decides.
|
|
91
|
+
*
|
|
92
|
+
* Use this API to bind tokens to the user's session, get the token for
|
|
93
|
+
* transports React Native's networking doesn't carry, add an assertion to a
|
|
94
|
+
* sensitive operation, and observe the state, events and logs.
|
|
95
|
+
*/
|
|
96
|
+
export declare const TrustPinAttest: {
|
|
97
|
+
/** The header that carries the token. */
|
|
98
|
+
headerName: string;
|
|
99
|
+
/** The header that carries the assertion of a sensitive operation. */
|
|
100
|
+
assertionHeaderName: string;
|
|
101
|
+
/**
|
|
102
|
+
* Starts attestation or renewal in the background so the first request
|
|
103
|
+
* doesn't wait, typically after sign-in. Never rejects.
|
|
104
|
+
*/
|
|
105
|
+
prefetch(): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* The current token, attesting or renewing first if needed. For transports
|
|
108
|
+
* React Native's networking doesn't carry (gRPC, some WebSocket clients);
|
|
109
|
+
* send it in the {@link TrustPinAttest.headerName} header.
|
|
110
|
+
*/
|
|
111
|
+
getToken(): Promise<string>;
|
|
112
|
+
/**
|
|
113
|
+
* The headers to add to a request for `url`: the token header when `url` is
|
|
114
|
+
* HTTPS on an attested host and a token is ready in time, otherwise none.
|
|
115
|
+
* Requests made with `fetch`, `XMLHttpRequest` or axios get them
|
|
116
|
+
* automatically; use this for other clients. `headers` are the request's
|
|
117
|
+
* own headers, used for header binding. Never rejects.
|
|
118
|
+
*/
|
|
119
|
+
attestationHeaders(url: string, options?: {
|
|
120
|
+
method?: string;
|
|
121
|
+
headers?: Record<string, string>;
|
|
122
|
+
}): Promise<Record<string, string>>;
|
|
123
|
+
/**
|
|
124
|
+
* The headers for one sensitive operation: the token and an assertion that
|
|
125
|
+
* binds this device's attested key to `payloadDigest`, the SHA-256 digest of
|
|
126
|
+
* whatever your backend verifies (for example the request body), as 32
|
|
127
|
+
* bytes or 64 hex characters. Send both with that request; your backend
|
|
128
|
+
* passes them and its own digest to your verifier. Each call produces a new
|
|
129
|
+
* assertion.
|
|
130
|
+
*
|
|
131
|
+
* Rejects with `INVALID_DIGEST` for a digest that isn't 32 bytes, and the
|
|
132
|
+
* usual codes when no token is available.
|
|
133
|
+
*/
|
|
134
|
+
assertionHeaders(payloadDigest: Uint8Array | ArrayBuffer | string): Promise<Record<string, string>>;
|
|
135
|
+
/**
|
|
136
|
+
* Binds tokens to a fixed `value`, such as a session id: only its SHA-256
|
|
137
|
+
* travels. `null` removes the fixed binding. A change renews the token with
|
|
138
|
+
* the existing device key.
|
|
139
|
+
*/
|
|
140
|
+
setTokenBinding(value: string | null): Promise<void>;
|
|
141
|
+
/**
|
|
142
|
+
* Binds each token to the value of the request header `name`, usually
|
|
143
|
+
* `Authorization`: only its SHA-256 travels. `null` stops header binding.
|
|
144
|
+
*/
|
|
145
|
+
bindToHeader(name: string | null): Promise<void>;
|
|
146
|
+
/** The current attestation state. */
|
|
147
|
+
getState(): Promise<TrustPinAttestState>;
|
|
148
|
+
/**
|
|
149
|
+
* Subscribes to the state: `listener` gets the current state first, then
|
|
150
|
+
* every change.
|
|
151
|
+
*/
|
|
152
|
+
onStateChange(listener: (state: TrustPinAttestState) => void): TrustPinAttestSubscription;
|
|
153
|
+
/**
|
|
154
|
+
* Subscribes to the module's events. Events from before JavaScript was
|
|
155
|
+
* running replay to the first subscriber.
|
|
156
|
+
*/
|
|
157
|
+
onEvent(listener: (event: TrustPinAttestEvent) => void): TrustPinAttestSubscription;
|
|
158
|
+
/**
|
|
159
|
+
* Subscribes to the module's log output, already filtered by the log level.
|
|
160
|
+
* Messages never contain tokens, key certificates, bound values or debug
|
|
161
|
+
* tokens. Early messages replay to the first subscriber.
|
|
162
|
+
*/
|
|
163
|
+
onLogEvent(listener: (event: TrustPinAttestLogEvent) => void): TrustPinAttestSubscription;
|
|
164
|
+
/**
|
|
165
|
+
* Discards the device key and key certificate; the next request attests a
|
|
166
|
+
* new key, so the token's `sub` changes. For support flows, not routine
|
|
167
|
+
* use.
|
|
168
|
+
*/
|
|
169
|
+
reset(): Promise<void>;
|
|
170
|
+
/** Sets the module's log verbosity. Use `'debug'` only in development. */
|
|
171
|
+
setLogLevel(level: TrustPinAttestLogLevel): Promise<void>;
|
|
172
|
+
};
|
|
173
|
+
export { TrustPinAttestError, TrustPinAttestErrorCodes };
|
|
174
|
+
export type { TrustPinAttestErrorCode };
|
|
175
|
+
export default TrustPinAttest;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* TrustPin app attestation for React Native.
|
|
4
|
+
*
|
|
5
|
+
* @module @trustpin/react-native-attest
|
|
6
|
+
*/
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.TrustPinAttestErrorCodes = exports.TrustPinAttestError = exports.TrustPinAttest = void 0;
|
|
12
|
+
const NativeTrustPinReactNativeAttest_1 = __importDefault(require("./NativeTrustPinReactNativeAttest"));
|
|
13
|
+
const errors_1 = require("./errors");
|
|
14
|
+
Object.defineProperty(exports, "TrustPinAttestError", { enumerable: true, get: function () { return errors_1.TrustPinAttestError; } });
|
|
15
|
+
Object.defineProperty(exports, "TrustPinAttestErrorCodes", { enumerable: true, get: function () { return errors_1.TrustPinAttestErrorCodes; } });
|
|
16
|
+
const LOG_LEVELS = ['none', 'error', 'warning', 'info', 'debug'];
|
|
17
|
+
/** Whether each stream has replayed its buffered startup events to a JS listener. */
|
|
18
|
+
const earlyEventsFlushed = { events: false, logs: false };
|
|
19
|
+
function native() {
|
|
20
|
+
if (NativeTrustPinReactNativeAttest_1.default == null) {
|
|
21
|
+
throw new errors_1.TrustPinAttestError(errors_1.TrustPinAttestErrorCodes.NOT_SET_UP, errors_1.NOT_INITIALIZED_MESSAGE);
|
|
22
|
+
}
|
|
23
|
+
return NativeTrustPinReactNativeAttest_1.default;
|
|
24
|
+
}
|
|
25
|
+
/** Runs a native call, rejecting with a TrustPinAttestError. */
|
|
26
|
+
async function call(body) {
|
|
27
|
+
try {
|
|
28
|
+
return await body(native());
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
throw (0, errors_1.toAttestError)(error);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function errorOf(payload) {
|
|
35
|
+
return new errors_1.TrustPinAttestError(payload.code ?? errors_1.TrustPinAttestErrorCodes.ATTEST_ERROR, payload.message ?? '', payload.reasons ?? []);
|
|
36
|
+
}
|
|
37
|
+
function stateOf(payload) {
|
|
38
|
+
switch (payload.state) {
|
|
39
|
+
case 'attesting':
|
|
40
|
+
return { status: 'attesting' };
|
|
41
|
+
case 'ready':
|
|
42
|
+
return { status: 'ready', expiresAtMs: payload.expiresAtMs ?? Date.now() };
|
|
43
|
+
case 'failed':
|
|
44
|
+
return { status: 'failed', error: errorOf(payload), retryAtMs: payload.retryAtMs ?? null };
|
|
45
|
+
default:
|
|
46
|
+
return { status: 'unavailable' };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** Decodes a native event, or returns `null` for one this version doesn't know. */
|
|
50
|
+
function eventOf(payload) {
|
|
51
|
+
switch (payload.event) {
|
|
52
|
+
case 'keyGenerated':
|
|
53
|
+
case 'waitCleared':
|
|
54
|
+
case 'deviceUnsupported':
|
|
55
|
+
case 'verifierUnavailable':
|
|
56
|
+
return { type: payload.event };
|
|
57
|
+
case 'keyReplaced':
|
|
58
|
+
return { type: 'keyReplaced', count: payload.count ?? 0 };
|
|
59
|
+
case 'attestationSucceeded':
|
|
60
|
+
case 'refreshSucceeded':
|
|
61
|
+
case 'assertionSucceeded':
|
|
62
|
+
return { type: payload.event, durationMs: payload.durationMs ?? 0 };
|
|
63
|
+
case 'refused':
|
|
64
|
+
return { type: 'refused', retryAtMs: payload.retryAtMs ?? null };
|
|
65
|
+
case 'attestationFailed':
|
|
66
|
+
return {
|
|
67
|
+
type: 'attestationFailed',
|
|
68
|
+
reasons: payload.reasons ?? [],
|
|
69
|
+
retryAtMs: payload.retryAtMs ?? null,
|
|
70
|
+
};
|
|
71
|
+
case 'refreshFailed':
|
|
72
|
+
case 'assertionFailed':
|
|
73
|
+
return { type: payload.event, error: errorOf(payload) };
|
|
74
|
+
case 'waitEntered':
|
|
75
|
+
return { type: 'waitEntered', untilMs: payload.untilMs ?? Date.now() };
|
|
76
|
+
default:
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function logLevelOf(level) {
|
|
81
|
+
return level === 'error' || level === 'warning' || level === 'debug' ? level : 'info';
|
|
82
|
+
}
|
|
83
|
+
/** The 32 bytes of a digest as lowercase hex, or `null` for anything else. */
|
|
84
|
+
function digestHex(digest) {
|
|
85
|
+
if (typeof digest === 'string') {
|
|
86
|
+
return /^[0-9a-fA-F]{64}$/.test(digest) ? digest.toLowerCase() : null;
|
|
87
|
+
}
|
|
88
|
+
const bytes = digest instanceof Uint8Array ? digest : new Uint8Array(digest);
|
|
89
|
+
if (bytes.length !== 32)
|
|
90
|
+
return null;
|
|
91
|
+
return Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* TrustPin app attestation for React Native.
|
|
95
|
+
*
|
|
96
|
+
* Proves to your API that a request comes from your genuine app on a genuine
|
|
97
|
+
* device. The native init helper sets the module up from the same
|
|
98
|
+
* configuration file as pinning, before JavaScript runs. From then on, every
|
|
99
|
+
* `fetch`, `XMLHttpRequest` and axios request to an attested host carries the
|
|
100
|
+
* `TrustPin-Attestation` header, added natively; when no token is ready in a
|
|
101
|
+
* short, bounded time, the request goes out without it and your API decides.
|
|
102
|
+
*
|
|
103
|
+
* Use this API to bind tokens to the user's session, get the token for
|
|
104
|
+
* transports React Native's networking doesn't carry, add an assertion to a
|
|
105
|
+
* sensitive operation, and observe the state, events and logs.
|
|
106
|
+
*/
|
|
107
|
+
exports.TrustPinAttest = {
|
|
108
|
+
/** The header that carries the token. */
|
|
109
|
+
headerName: 'TrustPin-Attestation',
|
|
110
|
+
/** The header that carries the assertion of a sensitive operation. */
|
|
111
|
+
assertionHeaderName: 'TrustPin-Assertion',
|
|
112
|
+
/**
|
|
113
|
+
* Starts attestation or renewal in the background so the first request
|
|
114
|
+
* doesn't wait, typically after sign-in. Never rejects.
|
|
115
|
+
*/
|
|
116
|
+
async prefetch() {
|
|
117
|
+
try {
|
|
118
|
+
await native().prefetch();
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
// Fire and forget: a failure shows in the state.
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
/**
|
|
125
|
+
* The current token, attesting or renewing first if needed. For transports
|
|
126
|
+
* React Native's networking doesn't carry (gRPC, some WebSocket clients);
|
|
127
|
+
* send it in the {@link TrustPinAttest.headerName} header.
|
|
128
|
+
*/
|
|
129
|
+
getToken() {
|
|
130
|
+
return call(module => module.getToken());
|
|
131
|
+
},
|
|
132
|
+
/**
|
|
133
|
+
* The headers to add to a request for `url`: the token header when `url` is
|
|
134
|
+
* HTTPS on an attested host and a token is ready in time, otherwise none.
|
|
135
|
+
* Requests made with `fetch`, `XMLHttpRequest` or axios get them
|
|
136
|
+
* automatically; use this for other clients. `headers` are the request's
|
|
137
|
+
* own headers, used for header binding. Never rejects.
|
|
138
|
+
*/
|
|
139
|
+
async attestationHeaders(url, options = {}) {
|
|
140
|
+
try {
|
|
141
|
+
const result = await native().attestationHeaders(url, options.method ?? 'GET', options.headers ?? {});
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return {};
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
/**
|
|
149
|
+
* The headers for one sensitive operation: the token and an assertion that
|
|
150
|
+
* binds this device's attested key to `payloadDigest`, the SHA-256 digest of
|
|
151
|
+
* whatever your backend verifies (for example the request body), as 32
|
|
152
|
+
* bytes or 64 hex characters. Send both with that request; your backend
|
|
153
|
+
* passes them and its own digest to your verifier. Each call produces a new
|
|
154
|
+
* assertion.
|
|
155
|
+
*
|
|
156
|
+
* Rejects with `INVALID_DIGEST` for a digest that isn't 32 bytes, and the
|
|
157
|
+
* usual codes when no token is available.
|
|
158
|
+
*/
|
|
159
|
+
assertionHeaders(payloadDigest) {
|
|
160
|
+
const hex = digestHex(payloadDigest);
|
|
161
|
+
if (hex == null) {
|
|
162
|
+
return Promise.reject(new errors_1.TrustPinAttestError(errors_1.TrustPinAttestErrorCodes.INVALID_DIGEST, 'assertionHeaders: the digest must be 32 bytes (a SHA-256 digest) or 64 hex characters.'));
|
|
163
|
+
}
|
|
164
|
+
return call(module => module.assertionHeaders(hex));
|
|
165
|
+
},
|
|
166
|
+
/**
|
|
167
|
+
* Binds tokens to a fixed `value`, such as a session id: only its SHA-256
|
|
168
|
+
* travels. `null` removes the fixed binding. A change renews the token with
|
|
169
|
+
* the existing device key.
|
|
170
|
+
*/
|
|
171
|
+
setTokenBinding(value) {
|
|
172
|
+
return call(module => module.setTokenBinding(value));
|
|
173
|
+
},
|
|
174
|
+
/**
|
|
175
|
+
* Binds each token to the value of the request header `name`, usually
|
|
176
|
+
* `Authorization`: only its SHA-256 travels. `null` stops header binding.
|
|
177
|
+
*/
|
|
178
|
+
bindToHeader(name) {
|
|
179
|
+
return call(module => module.bindToHeader(name));
|
|
180
|
+
},
|
|
181
|
+
/** The current attestation state. */
|
|
182
|
+
async getState() {
|
|
183
|
+
return stateOf(await call(module => module.getState()));
|
|
184
|
+
},
|
|
185
|
+
/**
|
|
186
|
+
* Subscribes to the state: `listener` gets the current state first, then
|
|
187
|
+
* every change.
|
|
188
|
+
*/
|
|
189
|
+
onStateChange(listener) {
|
|
190
|
+
const module = native();
|
|
191
|
+
let changed = false;
|
|
192
|
+
let removed = false;
|
|
193
|
+
const subscription = module.onStateChange(payload => {
|
|
194
|
+
changed = true;
|
|
195
|
+
listener(stateOf(payload));
|
|
196
|
+
});
|
|
197
|
+
module.getState().then(payload => {
|
|
198
|
+
// A change that arrived first is newer than this read.
|
|
199
|
+
if (!changed && !removed)
|
|
200
|
+
listener(stateOf(payload));
|
|
201
|
+
}, () => undefined);
|
|
202
|
+
return {
|
|
203
|
+
remove() {
|
|
204
|
+
removed = true;
|
|
205
|
+
subscription.remove();
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
},
|
|
209
|
+
/**
|
|
210
|
+
* Subscribes to the module's events. Events from before JavaScript was
|
|
211
|
+
* running replay to the first subscriber.
|
|
212
|
+
*/
|
|
213
|
+
onEvent(listener) {
|
|
214
|
+
const module = native();
|
|
215
|
+
const subscription = module.onEvent(payload => {
|
|
216
|
+
const event = eventOf(payload);
|
|
217
|
+
if (event != null)
|
|
218
|
+
listener(event);
|
|
219
|
+
});
|
|
220
|
+
if (!earlyEventsFlushed.events) {
|
|
221
|
+
earlyEventsFlushed.events = true;
|
|
222
|
+
module.flushEarlyEvents();
|
|
223
|
+
}
|
|
224
|
+
return subscription;
|
|
225
|
+
},
|
|
226
|
+
/**
|
|
227
|
+
* Subscribes to the module's log output, already filtered by the log level.
|
|
228
|
+
* Messages never contain tokens, key certificates, bound values or debug
|
|
229
|
+
* tokens. Early messages replay to the first subscriber.
|
|
230
|
+
*/
|
|
231
|
+
onLogEvent(listener) {
|
|
232
|
+
const module = native();
|
|
233
|
+
const subscription = module.onLogEvent((payload) => {
|
|
234
|
+
listener({
|
|
235
|
+
level: logLevelOf(payload.level),
|
|
236
|
+
message: payload.message,
|
|
237
|
+
timestampMs: payload.timestampMs,
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
if (!earlyEventsFlushed.logs) {
|
|
241
|
+
earlyEventsFlushed.logs = true;
|
|
242
|
+
module.flushEarlyLogEvents();
|
|
243
|
+
}
|
|
244
|
+
return subscription;
|
|
245
|
+
},
|
|
246
|
+
/**
|
|
247
|
+
* Discards the device key and key certificate; the next request attests a
|
|
248
|
+
* new key, so the token's `sub` changes. For support flows, not routine
|
|
249
|
+
* use.
|
|
250
|
+
*/
|
|
251
|
+
reset() {
|
|
252
|
+
return call(module => module.reset());
|
|
253
|
+
},
|
|
254
|
+
/** Sets the module's log verbosity. Use `'debug'` only in development. */
|
|
255
|
+
setLogLevel(level) {
|
|
256
|
+
if (!LOG_LEVELS.includes(level)) {
|
|
257
|
+
return Promise.reject(new errors_1.TrustPinAttestError(errors_1.TrustPinAttestErrorCodes.INVALID_ARGUMENTS, `setLogLevel: level must be one of ${LOG_LEVELS.join(', ')}; got ${String(level)}.`));
|
|
258
|
+
}
|
|
259
|
+
return call(module => module.setLogLevel(level));
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
exports.default = exports.TrustPinAttest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,71 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trustpin/react-native-attest",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "7.1.1",
|
|
4
|
+
"description": "TrustPin app attestation for React Native — proves to your API that requests come from your genuine app on a genuine device, checked by your own TrustPin verifier",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"types": "./lib/index.d.ts",
|
|
7
|
+
"react-native": "./src/index.ts",
|
|
8
|
+
"source": "./src/index.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"src",
|
|
11
|
+
"lib",
|
|
12
|
+
"android",
|
|
13
|
+
"ios",
|
|
14
|
+
"TrustPinReactNativeAttest.podspec",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.md",
|
|
17
|
+
"app.plugin.js",
|
|
18
|
+
"plugin/build",
|
|
19
|
+
"!**/__tests__",
|
|
20
|
+
"!android/build",
|
|
21
|
+
"!android/.gradle",
|
|
22
|
+
"!android/local.properties",
|
|
23
|
+
"!ios/build",
|
|
24
|
+
"!android/src/test",
|
|
25
|
+
"!plugin/build/__tests__"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p tsconfig.build.json && tsc -p plugin/tsconfig.json",
|
|
29
|
+
"typecheck": "tsc --noEmit && tsc -p plugin/tsconfig.json --noEmit",
|
|
30
|
+
"test": "jest",
|
|
31
|
+
"prepack": "npm run build"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"react-native",
|
|
35
|
+
"app-attestation",
|
|
36
|
+
"app-attest",
|
|
37
|
+
"play-integrity",
|
|
38
|
+
"security",
|
|
39
|
+
"trustpin"
|
|
40
|
+
],
|
|
41
|
+
"author": "TrustPin (https://trustpin.cloud)",
|
|
42
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
43
|
+
"homepage": "https://github.com/trustpin-cloud/react-native.sdk/tree/main/attest",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/trustpin-cloud/react-native.sdk.git",
|
|
47
|
+
"directory": "attest"
|
|
48
|
+
},
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/trustpin-cloud/react-native.sdk/issues"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@trustpin/react-native": "~7.1.0",
|
|
54
|
+
"expo": "*",
|
|
55
|
+
"react": "*",
|
|
56
|
+
"react-native": ">=0.85.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"expo": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"codegenConfig": {
|
|
64
|
+
"name": "TrustPinReactNativeAttestSpec",
|
|
65
|
+
"type": "modules",
|
|
66
|
+
"jsSrcsDir": "src",
|
|
67
|
+
"android": {
|
|
68
|
+
"javaPackageName": "cloud.trustpin.reactnative.attest"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ConfigPlugin, ExportedConfigWithProps } from '@expo/config-plugins';
|
|
2
|
+
/**
|
|
3
|
+
* A dangerous mod whose action runs after every other dangerous mod of the
|
|
4
|
+
* platform, whatever the order the plugins are listed in: it runs the rest of
|
|
5
|
+
* the chain first. The `@trustpin/react-native` plugin writes the
|
|
6
|
+
* configuration files in its own dangerous mod; this one then adds to them.
|
|
7
|
+
*/
|
|
8
|
+
export declare function withDangerousModAfterOthers(platform: 'ios' | 'android', action: (config: ExportedConfigWithProps<unknown>) => Promise<void>): ConfigPlugin;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withDangerousModAfterOthers = withDangerousModAfterOthers;
|
|
4
|
+
const config_plugins_1 = require("@expo/config-plugins");
|
|
5
|
+
/**
|
|
6
|
+
* A dangerous mod whose action runs after every other dangerous mod of the
|
|
7
|
+
* platform, whatever the order the plugins are listed in: it runs the rest of
|
|
8
|
+
* the chain first. The `@trustpin/react-native` plugin writes the
|
|
9
|
+
* configuration files in its own dangerous mod; this one then adds to them.
|
|
10
|
+
*/
|
|
11
|
+
function withDangerousModAfterOthers(platform, action) {
|
|
12
|
+
return config => (0, config_plugins_1.withBaseMod)(config, {
|
|
13
|
+
platform,
|
|
14
|
+
mod: 'dangerous',
|
|
15
|
+
isProvider: false,
|
|
16
|
+
async action({ modRequest: { nextMod, ...modRequest }, ...rest }) {
|
|
17
|
+
const results = await nextMod({ ...rest, modRequest });
|
|
18
|
+
await action(results);
|
|
19
|
+
return results;
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ConfigPlugin } from '@expo/config-plugins';
|
|
2
|
+
import { TrustPinAttestPluginProps } from './types';
|
|
3
|
+
export declare const ASSET_FILE_NAME = "trustpin.json";
|
|
4
|
+
/**
|
|
5
|
+
* Adds `attest_verifier_url`, `attested_hosts` and, when set,
|
|
6
|
+
* `google_cloud_project_number` to the asset the pinning plugin wrote. Keys
|
|
7
|
+
* already present are left as they are.
|
|
8
|
+
*/
|
|
9
|
+
export declare function addAttestKeys(json: string, props: TrustPinAttestPluginProps): string;
|
|
10
|
+
/**
|
|
11
|
+
* Inserts the native init call right after the pinning one, or as the first
|
|
12
|
+
* statement of `onCreate()` while the pinning plugin hasn't run yet (it then
|
|
13
|
+
* puts its own call first). Idempotent.
|
|
14
|
+
*/
|
|
15
|
+
export declare function addStartCall(contents: string, logLevel?: string): string;
|
|
16
|
+
export declare const withTrustPinAttestAndroid: ConfigPlugin<TrustPinAttestPluginProps>;
|