@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
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// Delivers the attestation module's state changes, events and logs to the
|
|
4
|
+
/// TurboModule's emitters.
|
|
5
|
+
///
|
|
6
|
+
/// Events and logs from before JavaScript exists (the native init helper runs
|
|
7
|
+
/// at launch) wait in bounded buffers and replay once to the first JavaScript
|
|
8
|
+
/// listener of each stream, as in `@trustpin/react-native`. State changes are
|
|
9
|
+
/// live only: JavaScript reads the current state when it subscribes.
|
|
10
|
+
///
|
|
11
|
+
/// Native listeners call from background threads; every path hops onto one
|
|
12
|
+
/// serial queue, keeping callbacks non-blocking and order total.
|
|
13
|
+
final class TrustPinAttestEventHub: @unchecked Sendable {
|
|
14
|
+
|
|
15
|
+
static let shared = TrustPinAttestEventHub()
|
|
16
|
+
|
|
17
|
+
/// Bounded per stream; oldest entries drop first.
|
|
18
|
+
private static let capacity = 32
|
|
19
|
+
|
|
20
|
+
private let queue = DispatchQueue(label: "cloud.trustpin.reactnative.attest.events")
|
|
21
|
+
|
|
22
|
+
private var eventBuffer: [[String: Any]] = []
|
|
23
|
+
private var logBuffer: [[String: Any]] = []
|
|
24
|
+
private var eventsFlushed = false
|
|
25
|
+
private var logsFlushed = false
|
|
26
|
+
|
|
27
|
+
private var emitState: (([String: Any]) -> Void)?
|
|
28
|
+
private var emitEvent: (([String: Any]) -> Void)?
|
|
29
|
+
private var emitLog: (([String: Any]) -> Void)?
|
|
30
|
+
|
|
31
|
+
private init() {}
|
|
32
|
+
|
|
33
|
+
func recordState(_ state: [String: Any]) {
|
|
34
|
+
queue.async { self.emitState?(state) }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
func recordEvent(_ event: [String: Any]) {
|
|
38
|
+
queue.async {
|
|
39
|
+
if self.eventsFlushed {
|
|
40
|
+
self.emitEvent?(event)
|
|
41
|
+
} else {
|
|
42
|
+
self.append(&self.eventBuffer, event)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func recordLog(_ log: [String: Any]) {
|
|
48
|
+
queue.async {
|
|
49
|
+
if self.logsFlushed {
|
|
50
|
+
self.emitLog?(log)
|
|
51
|
+
} else {
|
|
52
|
+
self.append(&self.logBuffer, log)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// Called when the TurboModule is created; a reload replaces the closures.
|
|
58
|
+
func attach(
|
|
59
|
+
emitState: @escaping ([String: Any]) -> Void,
|
|
60
|
+
emitEvent: @escaping ([String: Any]) -> Void,
|
|
61
|
+
emitLog: @escaping ([String: Any]) -> Void
|
|
62
|
+
) {
|
|
63
|
+
queue.async {
|
|
64
|
+
self.emitState = emitState
|
|
65
|
+
self.emitEvent = emitEvent
|
|
66
|
+
self.emitLog = emitLog
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Replay triggers, one per stream, called once its first listener is
|
|
71
|
+
/// attached. Idempotent.
|
|
72
|
+
func flushEvents() {
|
|
73
|
+
queue.async {
|
|
74
|
+
guard !self.eventsFlushed else { return }
|
|
75
|
+
self.eventsFlushed = true
|
|
76
|
+
self.eventBuffer.forEach { self.emitEvent?($0) }
|
|
77
|
+
self.eventBuffer.removeAll()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func flushLogs() {
|
|
82
|
+
queue.async {
|
|
83
|
+
guard !self.logsFlushed else { return }
|
|
84
|
+
self.logsFlushed = true
|
|
85
|
+
self.logBuffer.forEach { self.emitLog?($0) }
|
|
86
|
+
self.logBuffer.removeAll()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private func append(_ buffer: inout [[String: Any]], _ entry: [String: Any]) {
|
|
91
|
+
if buffer.count >= Self.capacity {
|
|
92
|
+
buffer.removeFirst()
|
|
93
|
+
}
|
|
94
|
+
buffer.append(entry)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import TrustPinAttestKit
|
|
3
|
+
|
|
4
|
+
/// Keys of the maps sent to JavaScript. Keep in sync with
|
|
5
|
+
/// `src/NativeTrustPinReactNativeAttest.ts`.
|
|
6
|
+
enum AttestKey {
|
|
7
|
+
static let state = "state"
|
|
8
|
+
static let event = "event"
|
|
9
|
+
static let expiresAtMs = "expiresAtMs"
|
|
10
|
+
static let retryAtMs = "retryAtMs"
|
|
11
|
+
static let untilMs = "untilMs"
|
|
12
|
+
static let durationMs = "durationMs"
|
|
13
|
+
static let count = "count"
|
|
14
|
+
static let code = "code"
|
|
15
|
+
static let message = "message"
|
|
16
|
+
static let reasons = "reasons"
|
|
17
|
+
static let level = "level"
|
|
18
|
+
static let timestampMs = "timestampMs"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// Error codes sent to JavaScript. Keep in sync with `src/errors.ts`.
|
|
22
|
+
enum AttestErrorCode {
|
|
23
|
+
static let invalidConfiguration = "INVALID_CONFIGURATION"
|
|
24
|
+
static let alreadyInitialized = "ALREADY_INITIALIZED"
|
|
25
|
+
static let notSetUp = "NOT_SET_UP"
|
|
26
|
+
static let unsupportedDevice = "UNSUPPORTED_DEVICE"
|
|
27
|
+
static let evidenceUnavailable = "EVIDENCE_UNAVAILABLE"
|
|
28
|
+
static let bindingRequired = "BINDING_REQUIRED"
|
|
29
|
+
static let attestationFailed = "ATTESTATION_FAILED"
|
|
30
|
+
static let verifierUnavailable = "VERIFIER_UNAVAILABLE"
|
|
31
|
+
static let verifierIncompatible = "VERIFIER_INCOMPATIBLE"
|
|
32
|
+
static let invalidDigest = "INVALID_DIGEST"
|
|
33
|
+
static let invalidArguments = "INVALID_ARGUMENTS"
|
|
34
|
+
static let cancelled = "CANCELLED"
|
|
35
|
+
static let attest = "ATTEST_ERROR"
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// The stable code of each `TrustPinAttestError` case.
|
|
39
|
+
func attestErrorCode(_ error: TrustPinAttestError) -> String {
|
|
40
|
+
switch error {
|
|
41
|
+
case .invalidConfiguration: return AttestErrorCode.invalidConfiguration
|
|
42
|
+
case .alreadyInitialized: return AttestErrorCode.alreadyInitialized
|
|
43
|
+
case .notSetUp: return AttestErrorCode.notSetUp
|
|
44
|
+
case .invalidDigest: return AttestErrorCode.invalidDigest
|
|
45
|
+
case .unsupportedDevice: return AttestErrorCode.unsupportedDevice
|
|
46
|
+
case .evidenceUnavailable: return AttestErrorCode.evidenceUnavailable
|
|
47
|
+
case .bindingRequired: return AttestErrorCode.bindingRequired
|
|
48
|
+
case .attestationFailed: return AttestErrorCode.attestationFailed
|
|
49
|
+
case .verifierUnavailable: return AttestErrorCode.verifierUnavailable
|
|
50
|
+
case .verifierIncompatible: return AttestErrorCode.verifierIncompatible
|
|
51
|
+
@unknown default: return AttestErrorCode.attest
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// The reasons an `attestationFailed` error carries; empty for other cases.
|
|
56
|
+
func attestReasons(_ error: TrustPinAttestError) -> [String] {
|
|
57
|
+
if case .attestationFailed(let reasons) = error { return reasons }
|
|
58
|
+
return []
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// A rejection's code, message and reasons for any error a call throws.
|
|
62
|
+
func attestRejection(from error: Error) -> (code: String, message: String, reasons: [String]) {
|
|
63
|
+
switch error {
|
|
64
|
+
case is CancellationError:
|
|
65
|
+
return (AttestErrorCode.cancelled, "Operation was cancelled", [])
|
|
66
|
+
case let invalid as AttestInvalidArgument:
|
|
67
|
+
return (AttestErrorCode.invalidArguments, invalid.message, [])
|
|
68
|
+
case let attest as TrustPinAttestError:
|
|
69
|
+
return (attestErrorCode(attest), attest.localizedDescription, attestReasons(attest))
|
|
70
|
+
default:
|
|
71
|
+
return (AttestErrorCode.attest, error.localizedDescription, [])
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// An argument JavaScript sent that the native module can't use.
|
|
76
|
+
struct AttestInvalidArgument: Error {
|
|
77
|
+
let message: String
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
extension TrustPinAttestLogLevel {
|
|
81
|
+
/// Maps the JavaScript name to a level; unknown names turn logging off.
|
|
82
|
+
init(wireName: String?) {
|
|
83
|
+
switch wireName?.lowercased() {
|
|
84
|
+
case "error": self = .error
|
|
85
|
+
case "warning": self = .warning
|
|
86
|
+
case "info": self = .info
|
|
87
|
+
case "debug": self = .debug
|
|
88
|
+
default: self = .none
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
var wireName: String {
|
|
93
|
+
switch self {
|
|
94
|
+
case .none: return "none"
|
|
95
|
+
case .error: return "error"
|
|
96
|
+
case .warning: return "warning"
|
|
97
|
+
case .info: return "info"
|
|
98
|
+
case .debug: return "debug"
|
|
99
|
+
@unknown default: return "info"
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
extension TrustPinAttestState {
|
|
105
|
+
/// The map sent to JavaScript. A retry time a year or more away
|
|
106
|
+
/// (`.distantFuture`) is left out: no new attempt in this launch.
|
|
107
|
+
var wireMap: [String: Any] {
|
|
108
|
+
var map: [String: Any] = [:]
|
|
109
|
+
switch self {
|
|
110
|
+
case .unavailable:
|
|
111
|
+
map[AttestKey.state] = "unavailable"
|
|
112
|
+
case .attesting:
|
|
113
|
+
map[AttestKey.state] = "attesting"
|
|
114
|
+
case .ready(let expiresAt):
|
|
115
|
+
map[AttestKey.state] = "ready"
|
|
116
|
+
map[AttestKey.expiresAtMs] = milliseconds(expiresAt)
|
|
117
|
+
case .failed(let error, let retryAt):
|
|
118
|
+
map[AttestKey.state] = "failed"
|
|
119
|
+
map.merge(error.wireFields) { $1 }
|
|
120
|
+
map[AttestKey.retryAtMs] = retryTime(retryAt)
|
|
121
|
+
@unknown default:
|
|
122
|
+
map[AttestKey.state] = "unavailable"
|
|
123
|
+
}
|
|
124
|
+
return map
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
extension TrustPinAttestEvent {
|
|
129
|
+
/// The map sent to JavaScript: the event's name and its own fields.
|
|
130
|
+
/// Durations and times are milliseconds.
|
|
131
|
+
var wireMap: [String: Any] {
|
|
132
|
+
var map: [String: Any] = [:]
|
|
133
|
+
switch self {
|
|
134
|
+
case .keyGenerated:
|
|
135
|
+
map[AttestKey.event] = "keyGenerated"
|
|
136
|
+
case .keyReplaced(let count):
|
|
137
|
+
map[AttestKey.event] = "keyReplaced"
|
|
138
|
+
map[AttestKey.count] = count
|
|
139
|
+
case .attestationSucceeded(let duration):
|
|
140
|
+
map[AttestKey.event] = "attestationSucceeded"
|
|
141
|
+
map[AttestKey.durationMs] = milliseconds(duration)
|
|
142
|
+
case .refused(let retryAt):
|
|
143
|
+
map[AttestKey.event] = "refused"
|
|
144
|
+
map[AttestKey.retryAtMs] = retryTime(retryAt)
|
|
145
|
+
case .attestationFailed(let reasons, let retryAt):
|
|
146
|
+
map[AttestKey.event] = "attestationFailed"
|
|
147
|
+
map[AttestKey.reasons] = reasons
|
|
148
|
+
map[AttestKey.retryAtMs] = retryTime(retryAt)
|
|
149
|
+
case .refreshSucceeded(let duration):
|
|
150
|
+
map[AttestKey.event] = "refreshSucceeded"
|
|
151
|
+
map[AttestKey.durationMs] = milliseconds(duration)
|
|
152
|
+
case .refreshFailed(let error):
|
|
153
|
+
map[AttestKey.event] = "refreshFailed"
|
|
154
|
+
map.merge(error.wireFields) { $1 }
|
|
155
|
+
case .waitEntered(let until):
|
|
156
|
+
map[AttestKey.event] = "waitEntered"
|
|
157
|
+
map[AttestKey.untilMs] = milliseconds(until)
|
|
158
|
+
case .waitCleared:
|
|
159
|
+
map[AttestKey.event] = "waitCleared"
|
|
160
|
+
case .deviceUnsupported:
|
|
161
|
+
map[AttestKey.event] = "deviceUnsupported"
|
|
162
|
+
case .verifierUnavailable:
|
|
163
|
+
map[AttestKey.event] = "verifierUnavailable"
|
|
164
|
+
case .assertionSucceeded(let duration):
|
|
165
|
+
map[AttestKey.event] = "assertionSucceeded"
|
|
166
|
+
map[AttestKey.durationMs] = milliseconds(duration)
|
|
167
|
+
case .assertionFailed(let error):
|
|
168
|
+
map[AttestKey.event] = "assertionFailed"
|
|
169
|
+
map.merge(error.wireFields) { $1 }
|
|
170
|
+
@unknown default:
|
|
171
|
+
// A newer native event: JavaScript skips names it doesn't know.
|
|
172
|
+
map[AttestKey.event] = "unknown"
|
|
173
|
+
}
|
|
174
|
+
return map
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private extension TrustPinAttestError {
|
|
179
|
+
var wireFields: [String: Any] {
|
|
180
|
+
[
|
|
181
|
+
AttestKey.code: attestErrorCode(self),
|
|
182
|
+
AttestKey.message: localizedDescription,
|
|
183
|
+
AttestKey.reasons: attestReasons(self),
|
|
184
|
+
]
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/// `nil` (the key left out) for a retry a year or more away.
|
|
189
|
+
private func retryTime(_ date: Date) -> Double? {
|
|
190
|
+
date.timeIntervalSinceNow < 365 * 24 * 3600 ? milliseconds(date) : nil
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private func milliseconds(_ date: Date) -> Double {
|
|
194
|
+
(date.timeIntervalSince1970 * 1000).rounded()
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private func milliseconds(_ interval: TimeInterval) -> Double {
|
|
198
|
+
(interval * 1000).rounded()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
func attestNowMs() -> Double {
|
|
202
|
+
(Date().timeIntervalSince1970 * 1000).rounded()
|
|
203
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import TrustPinAttestKit
|
|
3
|
+
|
|
4
|
+
/// The native init helper of app attestation, called right after
|
|
5
|
+
/// `TrustPinReactNative.start()` in
|
|
6
|
+
/// `AppDelegate.application(_:didFinishLaunchingWithOptions:)`:
|
|
7
|
+
///
|
|
8
|
+
/// ```swift
|
|
9
|
+
/// TrustPinReactNative.start()
|
|
10
|
+
/// TrustPinReactNativeAttest.start()
|
|
11
|
+
/// // or: TrustPinReactNativeAttest.start(logLevel: .debug)
|
|
12
|
+
/// ```
|
|
13
|
+
///
|
|
14
|
+
/// It reads `AttestVerifierURL` and `AttestedHosts` from the bundled
|
|
15
|
+
/// `TrustPin-Info.plist`, the file pinning reads, through the native SDK's own
|
|
16
|
+
/// loader, and sets up `TrustPinAttest.default`, which registers itself on the
|
|
17
|
+
/// pinning SDK's default instance. From then on React Native's HTTPS requests
|
|
18
|
+
/// to the attested hosts carry the `TrustPin-Attestation` header.
|
|
19
|
+
///
|
|
20
|
+
/// Setup failures never crash the host app: they surface through the log
|
|
21
|
+
/// stream and the state, and requests go out without the header.
|
|
22
|
+
@objc public final class TrustPinReactNativeAttest: NSObject {
|
|
23
|
+
|
|
24
|
+
private static let installOnce: Void = {
|
|
25
|
+
TrustPinAttest.setLogSink(TrustPinAttestLogForwarder())
|
|
26
|
+
TrustPinAttest.default.setStateListener { state in
|
|
27
|
+
TrustPinAttestEventHub.shared.recordState(state.wireMap)
|
|
28
|
+
}
|
|
29
|
+
TrustPinAttest.default.setEventListener { event in
|
|
30
|
+
TrustPinAttestEventHub.shared.recordEvent(event.wireMap)
|
|
31
|
+
}
|
|
32
|
+
}()
|
|
33
|
+
|
|
34
|
+
/// Swift entry point (the Expo config plugin injects this line).
|
|
35
|
+
///
|
|
36
|
+
/// - Parameter debugToken: a debug token for the iOS simulator, from a
|
|
37
|
+
/// debug-only build setting; never from a bundled file. The native SDK
|
|
38
|
+
/// uses it only in its simulator build, and only when your policy and
|
|
39
|
+
/// your verifier allow debug tokens. A `TRUSTPIN_DEBUG_TOKEN` set in the
|
|
40
|
+
/// Xcode scheme wins.
|
|
41
|
+
public static func start(logLevel: TrustPinAttestLogLevel = .error, debugToken: String? = nil) {
|
|
42
|
+
_ = installOnce
|
|
43
|
+
TrustPinAttest.default.set(logLevel: logLevel)
|
|
44
|
+
exposeDebugToken(debugToken)
|
|
45
|
+
|
|
46
|
+
Task {
|
|
47
|
+
do {
|
|
48
|
+
let configuration = try TrustPinAttestConfiguration.fromPlist()
|
|
49
|
+
try await TrustPinAttest.default.setup(configuration)
|
|
50
|
+
} catch {
|
|
51
|
+
let rejection = attestRejection(from: error)
|
|
52
|
+
TrustPinAttestEventHub.shared.recordLog([
|
|
53
|
+
AttestKey.level: "error",
|
|
54
|
+
AttestKey.message: "TrustPin attestation native init failed [\(rejection.code)]: \(rejection.message)",
|
|
55
|
+
AttestKey.timestampMs: attestNowMs(),
|
|
56
|
+
])
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Objective-C entry point:
|
|
62
|
+
/// `[TrustPinReactNativeAttest startWithLogLevelName:nil debugToken:nil]`.
|
|
63
|
+
@objc public static func start(logLevelName: String?, debugToken: String?) {
|
|
64
|
+
if let logLevelName, !logLevelName.isEmpty {
|
|
65
|
+
start(logLevel: TrustPinAttestLogLevel(wireName: logLevelName), debugToken: debugToken)
|
|
66
|
+
} else {
|
|
67
|
+
start(debugToken: debugToken)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// TrustPinAttestKit reads `TRUSTPIN_DEBUG_TOKEN` from the process environment,
|
|
73
|
+
/// and only in its simulator build. `react-native run-ios` doesn't apply Xcode
|
|
74
|
+
/// scheme variables, so the init helper can set it. Device builds don't
|
|
75
|
+
/// contain this code; a token already set in the scheme wins.
|
|
76
|
+
private func exposeDebugToken(_ token: String?) {
|
|
77
|
+
#if targetEnvironment(simulator)
|
|
78
|
+
guard let token, !token.isEmpty else { return }
|
|
79
|
+
setenv("TRUSTPIN_DEBUG_TOKEN", token, 0)
|
|
80
|
+
#endif
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// Forwards the module's log output into the event hub.
|
|
84
|
+
private final class TrustPinAttestLogForwarder: TrustPinAttestLogSink, @unchecked Sendable {
|
|
85
|
+
func log(level: TrustPinAttestLogLevel, instanceId: String, message: String) {
|
|
86
|
+
TrustPinAttestEventHub.shared.recordLog([
|
|
87
|
+
AttestKey.level: level.wireName,
|
|
88
|
+
AttestKey.message: message,
|
|
89
|
+
AttestKey.timestampMs: attestNowMs(),
|
|
90
|
+
])
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#import <TrustPinReactNativeAttestSpec/TrustPinReactNativeAttestSpec.h>
|
|
2
|
+
|
|
3
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
4
|
+
|
|
5
|
+
/// Thin ObjC++ TurboModule adapter: conforms to the codegen-generated spec and
|
|
6
|
+
/// delegates every method to the Swift core. Registered under the name
|
|
7
|
+
/// `TrustPinReactNativeAttest`.
|
|
8
|
+
@interface TrustPinReactNativeAttestModule : NativeTrustPinReactNativeAttestSpecBase <NativeTrustPinReactNativeAttestSpec>
|
|
9
|
+
@end
|
|
10
|
+
|
|
11
|
+
NS_ASSUME_NONNULL_END
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#import "TrustPinReactNativeAttestModule.h"
|
|
2
|
+
|
|
3
|
+
#if __has_include(<TrustPinReactNativeAttest/TrustPinReactNativeAttest-Swift.h>)
|
|
4
|
+
#import <TrustPinReactNativeAttest/TrustPinReactNativeAttest-Swift.h>
|
|
5
|
+
#else
|
|
6
|
+
#import "TrustPinReactNativeAttest-Swift.h"
|
|
7
|
+
#endif
|
|
8
|
+
|
|
9
|
+
/// Rejects with the stable code; an attestation failure's reasons travel in
|
|
10
|
+
/// the error's userInfo, which React Native exposes as `error.userInfo`.
|
|
11
|
+
static void TrustPinAttestReject(RCTPromiseRejectBlock reject, NSString *code, NSString *message, NSArray<NSString *> *reasons)
|
|
12
|
+
{
|
|
13
|
+
NSError *error = [NSError errorWithDomain:@"cloud.trustpin.reactnative.attest"
|
|
14
|
+
code:0
|
|
15
|
+
userInfo:@{ @"reasons" : reasons ?: @[] }];
|
|
16
|
+
reject(code, message, error);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#define TP_REJECT ^(NSString *code, NSString *message, NSArray<NSString *> *reasons) { \
|
|
20
|
+
TrustPinAttestReject(reject, code, message, reasons); \
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@implementation TrustPinReactNativeAttestModule
|
|
24
|
+
|
|
25
|
+
RCT_EXPORT_MODULE(TrustPinReactNativeAttest)
|
|
26
|
+
|
|
27
|
+
+ (BOOL)requiresMainQueueSetup
|
|
28
|
+
{
|
|
29
|
+
return NO;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
- (instancetype)init
|
|
33
|
+
{
|
|
34
|
+
if (self = [super init]) {
|
|
35
|
+
// Route hub events through this instance's codegen emitters. Weak: the
|
|
36
|
+
// hub outlives host reloads; a gone module simply drops the event.
|
|
37
|
+
__weak TrustPinReactNativeAttestModule *weakSelf = self;
|
|
38
|
+
[TrustPinAttestBridge attachEmittersWithState:^(NSDictionary *value) {
|
|
39
|
+
[weakSelf emitOnStateChange:value];
|
|
40
|
+
} event:^(NSDictionary *value) {
|
|
41
|
+
[weakSelf emitOnEvent:value];
|
|
42
|
+
} log:^(NSDictionary *value) {
|
|
43
|
+
[weakSelf emitOnLogEvent:value];
|
|
44
|
+
}];
|
|
45
|
+
}
|
|
46
|
+
return self;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
- (void)prefetch:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
50
|
+
{
|
|
51
|
+
[TrustPinAttestBridge prefetchWithResolve:resolve reject:TP_REJECT];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
- (void)getToken:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
55
|
+
{
|
|
56
|
+
[TrustPinAttestBridge getTokenWithResolve:resolve reject:TP_REJECT];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
- (void)attestationHeaders:(NSString *)url
|
|
60
|
+
method:(NSString *)method
|
|
61
|
+
headers:(NSDictionary *)headers
|
|
62
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
63
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
64
|
+
{
|
|
65
|
+
[TrustPinAttestBridge attestationHeaders:url method:method headers:headers resolve:resolve reject:TP_REJECT];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
- (void)assertionHeaders:(NSString *)payloadDigestHex
|
|
69
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
70
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
71
|
+
{
|
|
72
|
+
[TrustPinAttestBridge assertionHeaders:payloadDigestHex resolve:resolve reject:TP_REJECT];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
- (void)setTokenBinding:(NSString *_Nullable)value
|
|
76
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
77
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
78
|
+
{
|
|
79
|
+
[TrustPinAttestBridge setTokenBinding:value resolve:resolve reject:TP_REJECT];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
- (void)bindToHeader:(NSString *_Nullable)name
|
|
83
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
84
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
85
|
+
{
|
|
86
|
+
[TrustPinAttestBridge bindToHeader:name resolve:resolve reject:TP_REJECT];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
- (void)getState:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
90
|
+
{
|
|
91
|
+
[TrustPinAttestBridge getStateWithResolve:resolve reject:TP_REJECT];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
- (void)reset:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject
|
|
95
|
+
{
|
|
96
|
+
[TrustPinAttestBridge resetWithResolve:resolve reject:TP_REJECT];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
- (void)setLogLevel:(NSString *)level
|
|
100
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
101
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
102
|
+
{
|
|
103
|
+
[TrustPinAttestBridge setLogLevel:level resolve:resolve reject:TP_REJECT];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
- (void)flushEarlyEvents
|
|
107
|
+
{
|
|
108
|
+
[TrustPinAttestBridge flushEarlyEvents];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
- (void)flushEarlyLogEvents
|
|
112
|
+
{
|
|
113
|
+
[TrustPinAttestBridge flushEarlyLogEvents];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
117
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
118
|
+
{
|
|
119
|
+
return std::make_shared<facebook::react::NativeTrustPinReactNativeAttestSpecJSI>(params);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
@end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { TurboModule } from 'react-native';
|
|
2
|
+
import type { EventEmitter, UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
|
|
3
|
+
/**
|
|
4
|
+
* Codegen surface of the attestation module. Setup happens natively, from the
|
|
5
|
+
* init helper, before JavaScript runs; this module exposes the calls an app
|
|
6
|
+
* makes during a session and the module's state, events and logs.
|
|
7
|
+
*/
|
|
8
|
+
/** The state of the native instance. Times are epoch milliseconds. */
|
|
9
|
+
export type StatePayload = {
|
|
10
|
+
/** 'unavailable' | 'attesting' | 'ready' | 'failed' */
|
|
11
|
+
state: string;
|
|
12
|
+
expiresAtMs?: number;
|
|
13
|
+
/** Absent on a failed state with no new attempt in this launch. */
|
|
14
|
+
retryAtMs?: number;
|
|
15
|
+
code?: string;
|
|
16
|
+
message?: string;
|
|
17
|
+
reasons?: string[];
|
|
18
|
+
};
|
|
19
|
+
/** One native event. Only the fields of that event are present. */
|
|
20
|
+
export type EventPayload = {
|
|
21
|
+
event: string;
|
|
22
|
+
count?: number;
|
|
23
|
+
durationMs?: number;
|
|
24
|
+
/** Absent when no new attempt happens in this launch. */
|
|
25
|
+
retryAtMs?: number;
|
|
26
|
+
untilMs?: number;
|
|
27
|
+
reasons?: string[];
|
|
28
|
+
code?: string;
|
|
29
|
+
message?: string;
|
|
30
|
+
};
|
|
31
|
+
export type LogEventPayload = {
|
|
32
|
+
/** 'error' | 'warning' | 'info' | 'debug' */
|
|
33
|
+
level: string;
|
|
34
|
+
message: string;
|
|
35
|
+
timestampMs: number;
|
|
36
|
+
};
|
|
37
|
+
export interface Spec extends TurboModule {
|
|
38
|
+
prefetch(): Promise<void>;
|
|
39
|
+
getToken(): Promise<string>;
|
|
40
|
+
attestationHeaders(url: string, method: string, headers: UnsafeObject): Promise<UnsafeObject>;
|
|
41
|
+
/** `payloadDigestHex`: the 32-byte digest as lowercase hex. */
|
|
42
|
+
assertionHeaders(payloadDigestHex: string): Promise<UnsafeObject>;
|
|
43
|
+
setTokenBinding(value: string | null): Promise<void>;
|
|
44
|
+
bindToHeader(name: string | null): Promise<void>;
|
|
45
|
+
getState(): Promise<StatePayload>;
|
|
46
|
+
reset(): Promise<void>;
|
|
47
|
+
setLogLevel(level: string): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Flushes the buffered early native events of one stream once a listener
|
|
50
|
+
* has been attached.
|
|
51
|
+
*/
|
|
52
|
+
flushEarlyEvents(): void;
|
|
53
|
+
flushEarlyLogEvents(): void;
|
|
54
|
+
readonly onStateChange: EventEmitter<StatePayload>;
|
|
55
|
+
readonly onEvent: EventEmitter<EventPayload>;
|
|
56
|
+
readonly onLogEvent: EventEmitter<LogEventPayload>;
|
|
57
|
+
}
|
|
58
|
+
declare const _default: Spec | null;
|
|
59
|
+
export default _default;
|
package/lib/errors.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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 declare const TrustPinAttestErrorCodes: {
|
|
7
|
+
/** A configuration value was rejected, or the configuration file is missing. */
|
|
8
|
+
readonly INVALID_CONFIGURATION: "INVALID_CONFIGURATION";
|
|
9
|
+
/** Setup ran twice. */
|
|
10
|
+
readonly ALREADY_INITIALIZED: "ALREADY_INITIALIZED";
|
|
11
|
+
/** Called before the native init helper set the module up. */
|
|
12
|
+
readonly NOT_SET_UP: "NOT_SET_UP";
|
|
13
|
+
/** App Attest is unsupported on this device (iOS). */
|
|
14
|
+
readonly UNSUPPORTED_DEVICE: "UNSUPPORTED_DEVICE";
|
|
15
|
+
/** The device can't produce the evidence the policy requires. */
|
|
16
|
+
readonly EVIDENCE_UNAVAILABLE: "EVIDENCE_UNAVAILABLE";
|
|
17
|
+
/** The policy requires a token binding and none is set yet. */
|
|
18
|
+
readonly BINDING_REQUIRED: "BINDING_REQUIRED";
|
|
19
|
+
/** The verifier refused; see `reasons`. */
|
|
20
|
+
readonly ATTESTATION_FAILED: "ATTESTATION_FAILED";
|
|
21
|
+
/** The verifier couldn't be reached, including when strict pinning refuses its host. */
|
|
22
|
+
readonly VERIFIER_UNAVAILABLE: "VERIFIER_UNAVAILABLE";
|
|
23
|
+
/** The verifier is too old for this SDK: update it. */
|
|
24
|
+
readonly VERIFIER_INCOMPATIBLE: "VERIFIER_INCOMPATIBLE";
|
|
25
|
+
/** `assertionHeaders` got a digest that isn't 32 bytes. */
|
|
26
|
+
readonly INVALID_DIGEST: "INVALID_DIGEST";
|
|
27
|
+
/** An argument was rejected before it reached the native module. */
|
|
28
|
+
readonly INVALID_ARGUMENTS: "INVALID_ARGUMENTS";
|
|
29
|
+
/** The operation was cancelled. */
|
|
30
|
+
readonly CANCELLED: "CANCELLED";
|
|
31
|
+
/** Any other native failure. */
|
|
32
|
+
readonly ATTEST_ERROR: "ATTEST_ERROR";
|
|
33
|
+
};
|
|
34
|
+
export type TrustPinAttestErrorCode = keyof typeof TrustPinAttestErrorCodes;
|
|
35
|
+
/**
|
|
36
|
+
* The error of a rejected call, of a failed state and of failure events.
|
|
37
|
+
* `code` is one of {@link TrustPinAttestErrorCodes} or a newer one.
|
|
38
|
+
*/
|
|
39
|
+
export declare class TrustPinAttestError extends Error {
|
|
40
|
+
readonly code: string;
|
|
41
|
+
/** For `ATTESTATION_FAILED`: the verifier's reason codes. Empty otherwise. */
|
|
42
|
+
readonly reasons: readonly string[];
|
|
43
|
+
constructor(code: string, message: string, reasons?: readonly string[]);
|
|
44
|
+
}
|
|
45
|
+
/** The message must name the missing native init step. */
|
|
46
|
+
export declare const NOT_INITIALIZED_MESSAGE: string;
|
|
47
|
+
/** Converts a native rejection (or anything thrown) into a TrustPinAttestError. */
|
|
48
|
+
export declare function toAttestError(error: unknown): TrustPinAttestError;
|