@trustpin/react-native-attest 0.0.0-stage → 7.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 +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
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.withTrustPinAttestAndroid = exports.ASSET_FILE_NAME = void 0;
|
|
37
|
+
exports.addAttestKeys = addAttestKeys;
|
|
38
|
+
exports.addStartCall = addStartCall;
|
|
39
|
+
const config_plugins_1 = require("@expo/config-plugins");
|
|
40
|
+
const fs = __importStar(require("fs"));
|
|
41
|
+
const path = __importStar(require("path"));
|
|
42
|
+
const afterDangerous_1 = require("./afterDangerous");
|
|
43
|
+
const types_1 = require("./types");
|
|
44
|
+
exports.ASSET_FILE_NAME = 'trustpin.json';
|
|
45
|
+
/**
|
|
46
|
+
* Adds `attest_verifier_url`, `attested_hosts` and, when set,
|
|
47
|
+
* `google_cloud_project_number` to the asset the pinning plugin wrote. Keys
|
|
48
|
+
* already present are left as they are.
|
|
49
|
+
*/
|
|
50
|
+
function addAttestKeys(json, props) {
|
|
51
|
+
let config;
|
|
52
|
+
try {
|
|
53
|
+
config = JSON.parse(json);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
throw new types_1.TrustPinAttestPluginError(`${exports.ASSET_FILE_NAME} is not valid JSON.`);
|
|
57
|
+
}
|
|
58
|
+
config.attest_verifier_url ?? (config.attest_verifier_url = props.verifierUrl);
|
|
59
|
+
config.attested_hosts ?? (config.attested_hosts = props.attestedHosts);
|
|
60
|
+
if (props.googleCloudProjectNumber != null) {
|
|
61
|
+
config.google_cloud_project_number ?? (config.google_cloud_project_number = String(props.googleCloudProjectNumber));
|
|
62
|
+
}
|
|
63
|
+
return `${JSON.stringify(config, null, 2)}\n`;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Inserts the native init call right after the pinning one, or as the first
|
|
67
|
+
* statement of `onCreate()` while the pinning plugin hasn't run yet (it then
|
|
68
|
+
* puts its own call first). Idempotent.
|
|
69
|
+
*/
|
|
70
|
+
function addStartCall(contents, logLevel) {
|
|
71
|
+
if (contents.includes('TrustPinReactNativeAttest.start(')) {
|
|
72
|
+
return contents;
|
|
73
|
+
}
|
|
74
|
+
const call = logLevel
|
|
75
|
+
? `TrustPinReactNativeAttest.start(this, "${logLevel}", null)`
|
|
76
|
+
: 'TrustPinReactNativeAttest.start(this)';
|
|
77
|
+
const line = ` // App attestation: tokens for the attested hosts, from native code.\n ${call}\n`;
|
|
78
|
+
const withImport = contents.includes('import cloud.trustpin.reactnative.attest.TrustPinReactNativeAttest')
|
|
79
|
+
? contents
|
|
80
|
+
: contents.replace(/(^import [^\n]+\n)(?![\s\S]*^import )/m, `$1import cloud.trustpin.reactnative.attest.TrustPinReactNativeAttest\n`);
|
|
81
|
+
const afterPinning = /(^[ \t]*TrustPinReactNative\.start\([^\n]*\)\n)/m;
|
|
82
|
+
if (afterPinning.test(withImport)) {
|
|
83
|
+
return withImport.replace(afterPinning, `$1${line}`);
|
|
84
|
+
}
|
|
85
|
+
const onCreate = /(override fun onCreate\(\) \{\n)/;
|
|
86
|
+
if (!onCreate.test(withImport)) {
|
|
87
|
+
throw new types_1.TrustPinAttestPluginError('could not find onCreate() in MainApplication. ' +
|
|
88
|
+
`Add "${call}" after TrustPinReactNative.start(this) manually.`);
|
|
89
|
+
}
|
|
90
|
+
return withImport.replace(onCreate, `$1${line}\n`);
|
|
91
|
+
}
|
|
92
|
+
const withAttestKeys = (config, props) => (0, afterDangerous_1.withDangerousModAfterOthers)('android', async (modConfig) => {
|
|
93
|
+
if (!(0, types_1.writesKeys)(props))
|
|
94
|
+
return;
|
|
95
|
+
const file = path.join(modConfig.modRequest.platformProjectRoot, 'app/src/main/assets', exports.ASSET_FILE_NAME);
|
|
96
|
+
if (!fs.existsSync(file)) {
|
|
97
|
+
throw new types_1.TrustPinAttestPluginError(`${file} not found. List the @trustpin/react-native config plugin too: it writes this file.`);
|
|
98
|
+
}
|
|
99
|
+
fs.writeFileSync(file, addAttestKeys(fs.readFileSync(file, 'utf8'), props));
|
|
100
|
+
})(config);
|
|
101
|
+
const withInitCall = (config, props) => (0, config_plugins_1.withMainApplication)(config, modConfig => {
|
|
102
|
+
if (modConfig.modResults.language !== 'kt') {
|
|
103
|
+
throw new types_1.TrustPinAttestPluginError(`expected a Kotlin MainApplication, found "${modConfig.modResults.language}".`);
|
|
104
|
+
}
|
|
105
|
+
modConfig.modResults.contents = addStartCall(modConfig.modResults.contents, props.logLevel);
|
|
106
|
+
return modConfig;
|
|
107
|
+
});
|
|
108
|
+
const withTrustPinAttestAndroid = (config, props) => withInitCall(withAttestKeys(config, props), props);
|
|
109
|
+
exports.withTrustPinAttestAndroid = withTrustPinAttestAndroid;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const config_plugins_1 = require("@expo/config-plugins");
|
|
4
|
+
const android_1 = require("./android");
|
|
5
|
+
const ios_1 = require("./ios");
|
|
6
|
+
const types_1 = require("./types");
|
|
7
|
+
const pkg = require('../../package.json');
|
|
8
|
+
/**
|
|
9
|
+
* Wires TrustPin app attestation into an Expo app at prebuild time: adds the
|
|
10
|
+
* attestation keys to the configuration files the `@trustpin/react-native`
|
|
11
|
+
* plugin writes, adds the App Attest entitlement on iOS, and calls the native
|
|
12
|
+
* init helper right after the pinning one on both platforms.
|
|
13
|
+
*
|
|
14
|
+
* Expo Go cannot run this — attestation is native code. Use a development build.
|
|
15
|
+
*/
|
|
16
|
+
const withTrustPinAttest = (config, props) => {
|
|
17
|
+
const resolved = (0, types_1.resolveProps)(props);
|
|
18
|
+
return (0, android_1.withTrustPinAttestAndroid)((0, ios_1.withTrustPinAttestIos)(config, resolved), resolved);
|
|
19
|
+
};
|
|
20
|
+
exports.default = (0, config_plugins_1.createRunOncePlugin)(withTrustPinAttest, pkg.name, pkg.version);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ConfigPlugin } from '@expo/config-plugins';
|
|
2
|
+
import { TrustPinAttestPluginProps } from './types';
|
|
3
|
+
export declare const PLIST_FILE_NAME = "TrustPin-Info.plist";
|
|
4
|
+
export declare const APP_ATTEST_ENTITLEMENT = "com.apple.developer.devicecheck.appattest-environment";
|
|
5
|
+
/**
|
|
6
|
+
* Adds `AttestVerifierURL` and `AttestedHosts` to the plist the pinning plugin
|
|
7
|
+
* wrote. Keys already present are left as they are.
|
|
8
|
+
*/
|
|
9
|
+
export declare function addAttestKeys(plist: string, props: TrustPinAttestPluginProps): string;
|
|
10
|
+
/**
|
|
11
|
+
* Inserts the native init call right after the pinning one, or as the first
|
|
12
|
+
* statement of `didFinishLaunchingWithOptions` while the pinning plugin hasn't
|
|
13
|
+
* run yet (it then puts its own call first). Idempotent.
|
|
14
|
+
*/
|
|
15
|
+
export declare function addStartCall(contents: string, logLevel?: string): string;
|
|
16
|
+
export declare const withTrustPinAttestIos: ConfigPlugin<TrustPinAttestPluginProps>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.withTrustPinAttestIos = exports.APP_ATTEST_ENTITLEMENT = exports.PLIST_FILE_NAME = void 0;
|
|
37
|
+
exports.addAttestKeys = addAttestKeys;
|
|
38
|
+
exports.addStartCall = addStartCall;
|
|
39
|
+
const config_plugins_1 = require("@expo/config-plugins");
|
|
40
|
+
const fs = __importStar(require("fs"));
|
|
41
|
+
const path = __importStar(require("path"));
|
|
42
|
+
const afterDangerous_1 = require("./afterDangerous");
|
|
43
|
+
const types_1 = require("./types");
|
|
44
|
+
exports.PLIST_FILE_NAME = 'TrustPin-Info.plist';
|
|
45
|
+
exports.APP_ATTEST_ENTITLEMENT = 'com.apple.developer.devicecheck.appattest-environment';
|
|
46
|
+
/**
|
|
47
|
+
* Adds `AttestVerifierURL` and `AttestedHosts` to the plist the pinning plugin
|
|
48
|
+
* wrote. Keys already present are left as they are.
|
|
49
|
+
*/
|
|
50
|
+
function addAttestKeys(plist, props) {
|
|
51
|
+
if (!plist.includes('</dict>')) {
|
|
52
|
+
throw new types_1.TrustPinAttestPluginError(`${exports.PLIST_FILE_NAME} has no top-level <dict>.`);
|
|
53
|
+
}
|
|
54
|
+
const entries = [];
|
|
55
|
+
if (!plist.includes('<key>AttestVerifierURL</key>')) {
|
|
56
|
+
entries.push(`\t<key>AttestVerifierURL</key>\n\t<string>${escapeXml(props.verifierUrl)}</string>`);
|
|
57
|
+
}
|
|
58
|
+
if (!plist.includes('<key>AttestedHosts</key>')) {
|
|
59
|
+
const hosts = props.attestedHosts.map(host => `\t\t<string>${escapeXml(host)}</string>`).join('\n');
|
|
60
|
+
entries.push(`\t<key>AttestedHosts</key>\n\t<array>\n${hosts}\n\t</array>`);
|
|
61
|
+
}
|
|
62
|
+
if (entries.length === 0)
|
|
63
|
+
return plist;
|
|
64
|
+
const index = plist.lastIndexOf('</dict>');
|
|
65
|
+
return `${plist.slice(0, index)}${entries.join('\n')}\n${plist.slice(index)}`;
|
|
66
|
+
}
|
|
67
|
+
function escapeXml(value) {
|
|
68
|
+
return value
|
|
69
|
+
.replace(/&/g, '&')
|
|
70
|
+
.replace(/</g, '<')
|
|
71
|
+
.replace(/>/g, '>')
|
|
72
|
+
.replace(/"/g, '"');
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Inserts the native init call right after the pinning one, or as the first
|
|
76
|
+
* statement of `didFinishLaunchingWithOptions` while the pinning plugin hasn't
|
|
77
|
+
* run yet (it then puts its own call first). Idempotent.
|
|
78
|
+
*/
|
|
79
|
+
function addStartCall(contents, logLevel) {
|
|
80
|
+
if (contents.includes('TrustPinReactNativeAttest.start(')) {
|
|
81
|
+
return contents;
|
|
82
|
+
}
|
|
83
|
+
const call = logLevel
|
|
84
|
+
? `TrustPinReactNativeAttest.start(logLevel: .${logLevel})`
|
|
85
|
+
: 'TrustPinReactNativeAttest.start()';
|
|
86
|
+
const line = ` // App attestation: tokens for the attested hosts, from native code.\n ${call}\n`;
|
|
87
|
+
const withImport = contents.includes('import TrustPinReactNativeAttest')
|
|
88
|
+
? contents
|
|
89
|
+
: contents.replace(/(^import [^\n]+\n)(?![\s\S]*^import )/m, `$1import TrustPinReactNativeAttest\n`);
|
|
90
|
+
const afterPinning = /(^[ \t]*TrustPinReactNative\.start\([^\n]*\)\n)/m;
|
|
91
|
+
if (afterPinning.test(withImport)) {
|
|
92
|
+
return withImport.replace(afterPinning, `$1${line}`);
|
|
93
|
+
}
|
|
94
|
+
const launchSignature = /(func application\([\s\S]*?didFinishLaunchingWithOptions[\s\S]*?\) -> Bool \{\n)/;
|
|
95
|
+
if (!launchSignature.test(withImport)) {
|
|
96
|
+
throw new types_1.TrustPinAttestPluginError('could not find application(_:didFinishLaunchingWithOptions:) in AppDelegate.swift. ' +
|
|
97
|
+
`Add "${call}" after TrustPinReactNative.start() manually.`);
|
|
98
|
+
}
|
|
99
|
+
return withImport.replace(launchSignature, `$1${line}\n`);
|
|
100
|
+
}
|
|
101
|
+
const withAttestKeys = (config, props) => (0, afterDangerous_1.withDangerousModAfterOthers)('ios', async (modConfig) => {
|
|
102
|
+
if (!(0, types_1.writesKeys)(props))
|
|
103
|
+
return;
|
|
104
|
+
const projectRoot = modConfig.modRequest.projectRoot;
|
|
105
|
+
const appName = config_plugins_1.IOSConfig.XcodeUtils.getProjectName(projectRoot);
|
|
106
|
+
const file = path.join(modConfig.modRequest.platformProjectRoot, appName, exports.PLIST_FILE_NAME);
|
|
107
|
+
if (!fs.existsSync(file)) {
|
|
108
|
+
throw new types_1.TrustPinAttestPluginError(`${file} not found. List the @trustpin/react-native config plugin too: it writes this file.`);
|
|
109
|
+
}
|
|
110
|
+
fs.writeFileSync(file, addAttestKeys(fs.readFileSync(file, 'utf8'), props));
|
|
111
|
+
})(config);
|
|
112
|
+
const withAppAttestEntitlement = (config, props) => (0, config_plugins_1.withEntitlementsPlist)(config, modConfig => {
|
|
113
|
+
modConfig.modResults[exports.APP_ATTEST_ENTITLEMENT] = props.appAttestEnvironment ?? 'production';
|
|
114
|
+
return modConfig;
|
|
115
|
+
});
|
|
116
|
+
const withInitCall = (config, props) => (0, config_plugins_1.withAppDelegate)(config, modConfig => {
|
|
117
|
+
if (modConfig.modResults.language !== 'swift') {
|
|
118
|
+
throw new types_1.TrustPinAttestPluginError(`expected a Swift AppDelegate, found "${modConfig.modResults.language}". ` +
|
|
119
|
+
'Add [TrustPinReactNativeAttest startWithLogLevelName:nil debugToken:nil] manually.');
|
|
120
|
+
}
|
|
121
|
+
modConfig.modResults.contents = addStartCall(modConfig.modResults.contents, props.logLevel);
|
|
122
|
+
return modConfig;
|
|
123
|
+
});
|
|
124
|
+
const withTrustPinAttestIos = (config, props) => {
|
|
125
|
+
let next = withAttestKeys(config, props);
|
|
126
|
+
next = withAppAttestEntitlement(next, props);
|
|
127
|
+
return withInitCall(next, props);
|
|
128
|
+
};
|
|
129
|
+
exports.withTrustPinAttestIos = withTrustPinAttestIos;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Props accepted by the `@trustpin/react-native-attest` config plugin. List it
|
|
3
|
+
* with the `@trustpin/react-native` plugin, which writes the configuration
|
|
4
|
+
* files this one adds the attestation keys to.
|
|
5
|
+
*/
|
|
6
|
+
export interface TrustPinAttestPluginProps {
|
|
7
|
+
/**
|
|
8
|
+
* The base URL of your TrustPin verifier: `https://` on a host name, without
|
|
9
|
+
* credentials, query or fragment. Leave it out, with `attestedHosts`, when
|
|
10
|
+
* the pinning plugin's `configFile`s already carry the attestation keys.
|
|
11
|
+
*/
|
|
12
|
+
verifierUrl?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The hosts that receive the `TrustPin-Attestation` header: exact names, or
|
|
15
|
+
* `*.example.com` for exactly one label.
|
|
16
|
+
*/
|
|
17
|
+
attestedHosts?: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Android only: the number of the Google Cloud project linked to the app in
|
|
20
|
+
* Play Console, when the policy uses Play Integrity.
|
|
21
|
+
*/
|
|
22
|
+
googleCloudProjectNumber?: string;
|
|
23
|
+
/**
|
|
24
|
+
* iOS App Attest environment of the entitlement: `development` for builds
|
|
25
|
+
* run from Xcode or a dev client, `production` (the default) for TestFlight
|
|
26
|
+
* and the App Store.
|
|
27
|
+
*/
|
|
28
|
+
appAttestEnvironment?: 'development' | 'production';
|
|
29
|
+
/** Verbosity passed to the native init helper. */
|
|
30
|
+
logLevel?: 'none' | 'error' | 'warning' | 'info' | 'debug';
|
|
31
|
+
}
|
|
32
|
+
export declare const LOG_LEVELS: readonly ["none", "error", "warning", "info", "debug"];
|
|
33
|
+
export declare const APP_ATTEST_ENVIRONMENTS: readonly ["development", "production"];
|
|
34
|
+
export declare class TrustPinAttestPluginError extends Error {
|
|
35
|
+
constructor(message: string);
|
|
36
|
+
}
|
|
37
|
+
/** Validates the props; the native SDK validates the values themselves at setup. */
|
|
38
|
+
export declare function resolveProps(props: TrustPinAttestPluginProps | undefined): TrustPinAttestPluginProps;
|
|
39
|
+
/** Whether the plugin writes the attestation keys into the configuration files. */
|
|
40
|
+
export declare function writesKeys(props: TrustPinAttestPluginProps): boolean;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TrustPinAttestPluginError = exports.APP_ATTEST_ENVIRONMENTS = exports.LOG_LEVELS = void 0;
|
|
4
|
+
exports.resolveProps = resolveProps;
|
|
5
|
+
exports.writesKeys = writesKeys;
|
|
6
|
+
exports.LOG_LEVELS = ['none', 'error', 'warning', 'info', 'debug'];
|
|
7
|
+
exports.APP_ATTEST_ENVIRONMENTS = ['development', 'production'];
|
|
8
|
+
class TrustPinAttestPluginError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(`@trustpin/react-native-attest config plugin: ${message}`);
|
|
11
|
+
this.name = 'TrustPinAttestPluginError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.TrustPinAttestPluginError = TrustPinAttestPluginError;
|
|
15
|
+
/** Validates the props; the native SDK validates the values themselves at setup. */
|
|
16
|
+
function resolveProps(props) {
|
|
17
|
+
const resolved = props ?? {};
|
|
18
|
+
const hasUrl = resolved.verifierUrl != null && resolved.verifierUrl !== '';
|
|
19
|
+
const hasHosts = resolved.attestedHosts != null;
|
|
20
|
+
if (hasUrl !== hasHosts) {
|
|
21
|
+
throw new TrustPinAttestPluginError('set verifierUrl and attestedHosts together, or neither (when your configuration files already carry them).');
|
|
22
|
+
}
|
|
23
|
+
if (hasUrl && !resolved.verifierUrl.startsWith('https://')) {
|
|
24
|
+
throw new TrustPinAttestPluginError('verifierUrl must be an https:// URL.');
|
|
25
|
+
}
|
|
26
|
+
if (hasHosts && (!Array.isArray(resolved.attestedHosts) || resolved.attestedHosts.length === 0)) {
|
|
27
|
+
throw new TrustPinAttestPluginError('attestedHosts must be a non-empty array of host names.');
|
|
28
|
+
}
|
|
29
|
+
if (resolved.googleCloudProjectNumber != null &&
|
|
30
|
+
!/^[0-9]+$/.test(String(resolved.googleCloudProjectNumber))) {
|
|
31
|
+
throw new TrustPinAttestPluginError('googleCloudProjectNumber must be a string of digits.');
|
|
32
|
+
}
|
|
33
|
+
if (resolved.logLevel != null && !exports.LOG_LEVELS.includes(resolved.logLevel)) {
|
|
34
|
+
throw new TrustPinAttestPluginError(`logLevel must be one of ${exports.LOG_LEVELS.join(', ')}.`);
|
|
35
|
+
}
|
|
36
|
+
if (resolved.appAttestEnvironment != null &&
|
|
37
|
+
!exports.APP_ATTEST_ENVIRONMENTS.includes(resolved.appAttestEnvironment)) {
|
|
38
|
+
throw new TrustPinAttestPluginError('appAttestEnvironment must be development or production.');
|
|
39
|
+
}
|
|
40
|
+
return resolved;
|
|
41
|
+
}
|
|
42
|
+
/** Whether the plugin writes the attestation keys into the configuration files. */
|
|
43
|
+
function writesKeys(props) {
|
|
44
|
+
return props.verifierUrl != null && props.verifierUrl !== '';
|
|
45
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { TurboModule } from 'react-native';
|
|
2
|
+
import { TurboModuleRegistry } from 'react-native';
|
|
3
|
+
import type { EventEmitter, UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Codegen surface of the attestation module. Setup happens natively, from the
|
|
7
|
+
* init helper, before JavaScript runs; this module exposes the calls an app
|
|
8
|
+
* makes during a session and the module's state, events and logs.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** The state of the native instance. Times are epoch milliseconds. */
|
|
12
|
+
export type StatePayload = {
|
|
13
|
+
/** 'unavailable' | 'attesting' | 'ready' | 'failed' */
|
|
14
|
+
state: string;
|
|
15
|
+
expiresAtMs?: number;
|
|
16
|
+
/** Absent on a failed state with no new attempt in this launch. */
|
|
17
|
+
retryAtMs?: number;
|
|
18
|
+
code?: string;
|
|
19
|
+
message?: string;
|
|
20
|
+
reasons?: string[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** One native event. Only the fields of that event are present. */
|
|
24
|
+
export type EventPayload = {
|
|
25
|
+
event: string;
|
|
26
|
+
count?: number;
|
|
27
|
+
durationMs?: number;
|
|
28
|
+
/** Absent when no new attempt happens in this launch. */
|
|
29
|
+
retryAtMs?: number;
|
|
30
|
+
untilMs?: number;
|
|
31
|
+
reasons?: string[];
|
|
32
|
+
code?: string;
|
|
33
|
+
message?: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type LogEventPayload = {
|
|
37
|
+
/** 'error' | 'warning' | 'info' | 'debug' */
|
|
38
|
+
level: string;
|
|
39
|
+
message: string;
|
|
40
|
+
timestampMs: number;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export interface Spec extends TurboModule {
|
|
44
|
+
prefetch(): Promise<void>;
|
|
45
|
+
getToken(): Promise<string>;
|
|
46
|
+
attestationHeaders(url: string, method: string, headers: UnsafeObject): Promise<UnsafeObject>;
|
|
47
|
+
/** `payloadDigestHex`: the 32-byte digest as lowercase hex. */
|
|
48
|
+
assertionHeaders(payloadDigestHex: string): Promise<UnsafeObject>;
|
|
49
|
+
setTokenBinding(value: string | null): Promise<void>;
|
|
50
|
+
bindToHeader(name: string | null): Promise<void>;
|
|
51
|
+
getState(): Promise<StatePayload>;
|
|
52
|
+
reset(): Promise<void>;
|
|
53
|
+
setLogLevel(level: string): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Flushes the buffered early native events of one stream once a listener
|
|
56
|
+
* has been attached.
|
|
57
|
+
*/
|
|
58
|
+
flushEarlyEvents(): void;
|
|
59
|
+
flushEarlyLogEvents(): void;
|
|
60
|
+
readonly onStateChange: EventEmitter<StatePayload>;
|
|
61
|
+
readonly onEvent: EventEmitter<EventPayload>;
|
|
62
|
+
readonly onLogEvent: EventEmitter<LogEventPayload>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default TurboModuleRegistry.get<Spec>('TrustPinReactNativeAttest');
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable error codes of the attestation module. Native rejections carry one
|
|
3
|
+
* of these as the rejection `code`; new codes can appear in a minor release,
|
|
4
|
+
* so handle unknown ones.
|
|
5
|
+
*/
|
|
6
|
+
export const TrustPinAttestErrorCodes = {
|
|
7
|
+
/** A configuration value was rejected, or the configuration file is missing. */
|
|
8
|
+
INVALID_CONFIGURATION: 'INVALID_CONFIGURATION',
|
|
9
|
+
/** Setup ran twice. */
|
|
10
|
+
ALREADY_INITIALIZED: 'ALREADY_INITIALIZED',
|
|
11
|
+
/** Called before the native init helper set the module up. */
|
|
12
|
+
NOT_SET_UP: 'NOT_SET_UP',
|
|
13
|
+
/** App Attest is unsupported on this device (iOS). */
|
|
14
|
+
UNSUPPORTED_DEVICE: 'UNSUPPORTED_DEVICE',
|
|
15
|
+
/** The device can't produce the evidence the policy requires. */
|
|
16
|
+
EVIDENCE_UNAVAILABLE: 'EVIDENCE_UNAVAILABLE',
|
|
17
|
+
/** The policy requires a token binding and none is set yet. */
|
|
18
|
+
BINDING_REQUIRED: 'BINDING_REQUIRED',
|
|
19
|
+
/** The verifier refused; see `reasons`. */
|
|
20
|
+
ATTESTATION_FAILED: 'ATTESTATION_FAILED',
|
|
21
|
+
/** The verifier couldn't be reached, including when strict pinning refuses its host. */
|
|
22
|
+
VERIFIER_UNAVAILABLE: 'VERIFIER_UNAVAILABLE',
|
|
23
|
+
/** The verifier is too old for this SDK: update it. */
|
|
24
|
+
VERIFIER_INCOMPATIBLE: 'VERIFIER_INCOMPATIBLE',
|
|
25
|
+
/** `assertionHeaders` got a digest that isn't 32 bytes. */
|
|
26
|
+
INVALID_DIGEST: 'INVALID_DIGEST',
|
|
27
|
+
/** An argument was rejected before it reached the native module. */
|
|
28
|
+
INVALID_ARGUMENTS: 'INVALID_ARGUMENTS',
|
|
29
|
+
/** The operation was cancelled. */
|
|
30
|
+
CANCELLED: 'CANCELLED',
|
|
31
|
+
/** Any other native failure. */
|
|
32
|
+
ATTEST_ERROR: 'ATTEST_ERROR',
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
export type TrustPinAttestErrorCode = keyof typeof TrustPinAttestErrorCodes;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The error of a rejected call, of a failed state and of failure events.
|
|
39
|
+
* `code` is one of {@link TrustPinAttestErrorCodes} or a newer one.
|
|
40
|
+
*/
|
|
41
|
+
export class TrustPinAttestError extends Error {
|
|
42
|
+
readonly code: string;
|
|
43
|
+
/** For `ATTESTATION_FAILED`: the verifier's reason codes. Empty otherwise. */
|
|
44
|
+
readonly reasons: readonly string[];
|
|
45
|
+
|
|
46
|
+
constructor(code: string, message: string, reasons: readonly string[] = []) {
|
|
47
|
+
super(message);
|
|
48
|
+
this.name = 'TrustPinAttestError';
|
|
49
|
+
this.code = code;
|
|
50
|
+
this.reasons = reasons;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The message must name the missing native init step. */
|
|
55
|
+
export const NOT_INITIALIZED_MESSAGE =
|
|
56
|
+
'TrustPin attestation native module is unavailable. Call ' +
|
|
57
|
+
'TrustPinReactNativeAttest.start(...) right after TrustPinReactNative.start(...) ' +
|
|
58
|
+
'in AppDelegate.application(_:didFinishLaunchingWithOptions:) on iOS and in ' +
|
|
59
|
+
'Application.onCreate() on Android, then rebuild the app (pod install / gradle sync). ' +
|
|
60
|
+
'With Expo, add the @trustpin/react-native-attest config plugin and run prebuild.';
|
|
61
|
+
|
|
62
|
+
/** Converts a native rejection (or anything thrown) into a TrustPinAttestError. */
|
|
63
|
+
export function toAttestError(error: unknown): TrustPinAttestError {
|
|
64
|
+
if (error instanceof TrustPinAttestError) return error;
|
|
65
|
+
const candidate = error as { code?: unknown; message?: unknown; userInfo?: unknown } | null;
|
|
66
|
+
const code = typeof candidate?.code === 'string' ? candidate.code : TrustPinAttestErrorCodes.ATTEST_ERROR;
|
|
67
|
+
const message = typeof candidate?.message === 'string' ? candidate.message : String(error);
|
|
68
|
+
const info = candidate?.userInfo as { reasons?: unknown } | null | undefined;
|
|
69
|
+
const reasons = Array.isArray(info?.reasons)
|
|
70
|
+
? info.reasons.filter((reason): reason is string => typeof reason === 'string')
|
|
71
|
+
: [];
|
|
72
|
+
return new TrustPinAttestError(code, message, reasons);
|
|
73
|
+
}
|