@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,139 @@
|
|
|
1
|
+
package cloud.trustpin.reactnative.attest
|
|
2
|
+
|
|
3
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttestError
|
|
4
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttestEvent
|
|
5
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttestLogLevel
|
|
6
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttestState
|
|
7
|
+
|
|
8
|
+
/** Keys of the maps sent to JavaScript. Keep in sync with the TypeScript spec. */
|
|
9
|
+
internal object AttestKey {
|
|
10
|
+
const val STATE = "state"
|
|
11
|
+
const val EVENT = "event"
|
|
12
|
+
const val EXPIRES_AT_MS = "expiresAtMs"
|
|
13
|
+
const val RETRY_AT_MS = "retryAtMs"
|
|
14
|
+
const val UNTIL_MS = "untilMs"
|
|
15
|
+
const val DURATION_MS = "durationMs"
|
|
16
|
+
const val COUNT = "count"
|
|
17
|
+
const val CODE = "code"
|
|
18
|
+
const val MESSAGE = "message"
|
|
19
|
+
const val REASONS = "reasons"
|
|
20
|
+
const val LEVEL = "level"
|
|
21
|
+
const val TIMESTAMP_MS = "timestampMs"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Error codes sent to JavaScript. Keep in sync with `src/errors.ts`. */
|
|
25
|
+
internal object AttestErrorCode {
|
|
26
|
+
const val INVALID_CONFIGURATION = "INVALID_CONFIGURATION"
|
|
27
|
+
const val ALREADY_INITIALIZED = "ALREADY_INITIALIZED"
|
|
28
|
+
const val NOT_SET_UP = "NOT_SET_UP"
|
|
29
|
+
const val UNSUPPORTED_DEVICE = "UNSUPPORTED_DEVICE"
|
|
30
|
+
const val EVIDENCE_UNAVAILABLE = "EVIDENCE_UNAVAILABLE"
|
|
31
|
+
const val BINDING_REQUIRED = "BINDING_REQUIRED"
|
|
32
|
+
const val ATTESTATION_FAILED = "ATTESTATION_FAILED"
|
|
33
|
+
const val VERIFIER_UNAVAILABLE = "VERIFIER_UNAVAILABLE"
|
|
34
|
+
const val VERIFIER_INCOMPATIBLE = "VERIFIER_INCOMPATIBLE"
|
|
35
|
+
const val INVALID_DIGEST = "INVALID_DIGEST"
|
|
36
|
+
const val INVALID_ARGUMENTS = "INVALID_ARGUMENTS"
|
|
37
|
+
const val CANCELLED = "CANCELLED"
|
|
38
|
+
const val ATTEST = "ATTEST_ERROR"
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A retry time at least this far away is sent as "no retry": the key is left out. */
|
|
42
|
+
private const val NO_RETRY_THRESHOLD_MS: Long = 365L * 24 * 3600 * 1000
|
|
43
|
+
|
|
44
|
+
/** The stable code of each [TrustPinAttestError] case. */
|
|
45
|
+
internal fun TrustPinAttestError.code(): String = when (this) {
|
|
46
|
+
is TrustPinAttestError.InvalidConfiguration -> AttestErrorCode.INVALID_CONFIGURATION
|
|
47
|
+
is TrustPinAttestError.AlreadyInitialized -> AttestErrorCode.ALREADY_INITIALIZED
|
|
48
|
+
is TrustPinAttestError.NotSetUp -> AttestErrorCode.NOT_SET_UP
|
|
49
|
+
is TrustPinAttestError.UnsupportedDevice -> AttestErrorCode.UNSUPPORTED_DEVICE
|
|
50
|
+
is TrustPinAttestError.EvidenceUnavailable -> AttestErrorCode.EVIDENCE_UNAVAILABLE
|
|
51
|
+
is TrustPinAttestError.BindingRequired -> AttestErrorCode.BINDING_REQUIRED
|
|
52
|
+
is TrustPinAttestError.AttestationFailed -> AttestErrorCode.ATTESTATION_FAILED
|
|
53
|
+
is TrustPinAttestError.VerifierUnavailable -> AttestErrorCode.VERIFIER_UNAVAILABLE
|
|
54
|
+
is TrustPinAttestError.VerifierIncompatible -> AttestErrorCode.VERIFIER_INCOMPATIBLE
|
|
55
|
+
is TrustPinAttestError.InvalidDigest -> AttestErrorCode.INVALID_DIGEST
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The reasons an `AttestationFailed` error carries; empty for other cases. */
|
|
59
|
+
internal fun TrustPinAttestError.reasonList(): List<String> =
|
|
60
|
+
(this as? TrustPinAttestError.AttestationFailed)?.reasons ?: emptyList()
|
|
61
|
+
|
|
62
|
+
/** The map sent to JavaScript for this state. */
|
|
63
|
+
internal fun TrustPinAttestState.toWireMap(nowMillis: Long = System.currentTimeMillis()): Map<String, Any?> =
|
|
64
|
+
when (this) {
|
|
65
|
+
is TrustPinAttestState.Unavailable -> mapOf(AttestKey.STATE to "unavailable")
|
|
66
|
+
is TrustPinAttestState.Attesting -> mapOf(AttestKey.STATE to "attesting")
|
|
67
|
+
is TrustPinAttestState.Ready -> mapOf(
|
|
68
|
+
AttestKey.STATE to "ready",
|
|
69
|
+
AttestKey.EXPIRES_AT_MS to expiresAtMillis.toDouble(),
|
|
70
|
+
)
|
|
71
|
+
is TrustPinAttestState.Failed ->
|
|
72
|
+
mapOf(AttestKey.STATE to "failed") + error.wireFields() + retryField(retryAtMillis, nowMillis)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The map sent to JavaScript for this event: its name and its own fields. */
|
|
76
|
+
internal fun TrustPinAttestEvent.toWireMap(nowMillis: Long = System.currentTimeMillis()): Map<String, Any?> =
|
|
77
|
+
when (this) {
|
|
78
|
+
is TrustPinAttestEvent.KeyGenerated -> mapOf(AttestKey.EVENT to "keyGenerated")
|
|
79
|
+
is TrustPinAttestEvent.KeyReplaced -> mapOf(AttestKey.EVENT to "keyReplaced", AttestKey.COUNT to count)
|
|
80
|
+
is TrustPinAttestEvent.AttestationSucceeded ->
|
|
81
|
+
mapOf(AttestKey.EVENT to "attestationSucceeded", AttestKey.DURATION_MS to durationMillis.toDouble())
|
|
82
|
+
is TrustPinAttestEvent.Refused ->
|
|
83
|
+
mapOf(AttestKey.EVENT to "refused") + retryField(retryAtMillis, nowMillis)
|
|
84
|
+
is TrustPinAttestEvent.AttestationFailed ->
|
|
85
|
+
mapOf(AttestKey.EVENT to "attestationFailed", AttestKey.REASONS to reasons) +
|
|
86
|
+
retryField(retryAtMillis, nowMillis)
|
|
87
|
+
is TrustPinAttestEvent.RefreshSucceeded ->
|
|
88
|
+
mapOf(AttestKey.EVENT to "refreshSucceeded", AttestKey.DURATION_MS to durationMillis.toDouble())
|
|
89
|
+
is TrustPinAttestEvent.RefreshFailed -> mapOf(AttestKey.EVENT to "refreshFailed") + error.wireFields()
|
|
90
|
+
is TrustPinAttestEvent.WaitEntered ->
|
|
91
|
+
mapOf(AttestKey.EVENT to "waitEntered", AttestKey.UNTIL_MS to untilMillis.toDouble())
|
|
92
|
+
is TrustPinAttestEvent.WaitCleared -> mapOf(AttestKey.EVENT to "waitCleared")
|
|
93
|
+
is TrustPinAttestEvent.DeviceUnsupported -> mapOf(AttestKey.EVENT to "deviceUnsupported")
|
|
94
|
+
is TrustPinAttestEvent.VerifierUnavailable -> mapOf(AttestKey.EVENT to "verifierUnavailable")
|
|
95
|
+
is TrustPinAttestEvent.AssertionSucceeded ->
|
|
96
|
+
mapOf(AttestKey.EVENT to "assertionSucceeded", AttestKey.DURATION_MS to durationMillis.toDouble())
|
|
97
|
+
is TrustPinAttestEvent.AssertionFailed -> mapOf(AttestKey.EVENT to "assertionFailed") + error.wireFields()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private fun TrustPinAttestError.wireFields(): Map<String, Any?> = mapOf(
|
|
101
|
+
AttestKey.CODE to code(),
|
|
102
|
+
AttestKey.MESSAGE to message,
|
|
103
|
+
AttestKey.REASONS to reasonList(),
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
/** Long.MAX_VALUE means no new attempt in this process: the key is left out. */
|
|
107
|
+
private fun retryField(retryAtMillis: Long, nowMillis: Long): Map<String, Any?> =
|
|
108
|
+
if (retryAtMillis - nowMillis < NO_RETRY_THRESHOLD_MS) {
|
|
109
|
+
mapOf(AttestKey.RETRY_AT_MS to retryAtMillis.toDouble())
|
|
110
|
+
} else {
|
|
111
|
+
emptyMap()
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Maps the JavaScript name to a level; unknown names turn logging off. */
|
|
115
|
+
internal fun String?.toAttestLogLevel(): TrustPinAttestLogLevel = when (this?.lowercase()) {
|
|
116
|
+
"error" -> TrustPinAttestLogLevel.ERROR
|
|
117
|
+
"warning" -> TrustPinAttestLogLevel.WARNING
|
|
118
|
+
"info" -> TrustPinAttestLogLevel.INFO
|
|
119
|
+
"debug" -> TrustPinAttestLogLevel.DEBUG
|
|
120
|
+
else -> TrustPinAttestLogLevel.NONE
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
internal fun TrustPinAttestLogLevel.toWireName(): String = when (this) {
|
|
124
|
+
TrustPinAttestLogLevel.NONE -> "none"
|
|
125
|
+
TrustPinAttestLogLevel.ERROR -> "error"
|
|
126
|
+
TrustPinAttestLogLevel.WARNING -> "warning"
|
|
127
|
+
TrustPinAttestLogLevel.INFO -> "info"
|
|
128
|
+
TrustPinAttestLogLevel.DEBUG -> "debug"
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Exactly 64 hex characters as 32 bytes, or null. */
|
|
132
|
+
internal fun String.hexToBytes(): ByteArray? {
|
|
133
|
+
if (length != 64) return null
|
|
134
|
+
return ByteArray(32) { index ->
|
|
135
|
+
substring(index * 2, index * 2 + 2).toIntOrNull(16)?.toByte() ?: return null
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
internal fun nowMs(): Double = System.currentTimeMillis().toDouble()
|
package/android/src/main/kotlin/cloud/trustpin/reactnative/attest/TrustPinReactNativeAttest.kt
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
package cloud.trustpin.reactnative.attest
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttest
|
|
5
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttestConfiguration
|
|
6
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttestLogLevel
|
|
7
|
+
import cloud.trustpin.kotlin.attest.fromAssets
|
|
8
|
+
import kotlinx.coroutines.CoroutineScope
|
|
9
|
+
import kotlinx.coroutines.Dispatchers
|
|
10
|
+
import kotlinx.coroutines.SupervisorJob
|
|
11
|
+
import kotlinx.coroutines.launch
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The native init helper of app attestation, called right after
|
|
15
|
+
* `TrustPinReactNative.start(this)` in `Application.onCreate()`:
|
|
16
|
+
*
|
|
17
|
+
* ```kotlin
|
|
18
|
+
* override fun onCreate() {
|
|
19
|
+
* TrustPinReactNative.start(this)
|
|
20
|
+
* TrustPinReactNativeAttest.start(this)
|
|
21
|
+
* // or: TrustPinReactNativeAttest.start(this, TrustPinAttestLogLevel.DEBUG, BuildConfig.TRUSTPIN_DEBUG_TOKEN)
|
|
22
|
+
* super.onCreate()
|
|
23
|
+
* loadReactNative(this)
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* It reads `attest_verifier_url`, `attested_hosts` and
|
|
28
|
+
* `google_cloud_project_number` from the bundled `assets/trustpin.json`, the
|
|
29
|
+
* file pinning reads, through the native SDK's own loader, and sets up
|
|
30
|
+
* `TrustPinAttest.default`, which registers itself on the pinning SDK's
|
|
31
|
+
* default instance. From then on React Native's HTTPS requests to the
|
|
32
|
+
* attested hosts carry the `TrustPin-Attestation` header.
|
|
33
|
+
*
|
|
34
|
+
* Setup failures never crash the host app: they surface through the log
|
|
35
|
+
* stream and the state, and requests go out without the header.
|
|
36
|
+
*/
|
|
37
|
+
object TrustPinReactNativeAttest {
|
|
38
|
+
|
|
39
|
+
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
|
40
|
+
|
|
41
|
+
private var initialized = false
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param debugToken a debug token for emulators and debug devices, from a
|
|
45
|
+
* debug-only build field; never from a bundled file. The native SDK uses
|
|
46
|
+
* it only in debuggable apps, and only when your policy and your
|
|
47
|
+
* verifier allow debug tokens.
|
|
48
|
+
*/
|
|
49
|
+
@JvmStatic
|
|
50
|
+
@JvmOverloads
|
|
51
|
+
fun start(
|
|
52
|
+
context: Context,
|
|
53
|
+
logLevel: TrustPinAttestLogLevel = TrustPinAttestLogLevel.ERROR,
|
|
54
|
+
debugToken: String? = null,
|
|
55
|
+
) {
|
|
56
|
+
synchronized(this) {
|
|
57
|
+
if (initialized) return
|
|
58
|
+
initialized = true
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
val applicationContext = context.applicationContext
|
|
62
|
+
val attest = TrustPinAttest.default
|
|
63
|
+
|
|
64
|
+
TrustPinAttest.setLogSink { level, _, message ->
|
|
65
|
+
AttestEventHub.recordLog(
|
|
66
|
+
mapOf(
|
|
67
|
+
AttestKey.LEVEL to level.toWireName(),
|
|
68
|
+
AttestKey.MESSAGE to message,
|
|
69
|
+
AttestKey.TIMESTAMP_MS to nowMs(),
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
attest.setStateListener { state -> AttestEventHub.recordState(state.toWireMap()) }
|
|
74
|
+
attest.setEventListener { event -> AttestEventHub.recordEvent(event.toWireMap()) }
|
|
75
|
+
attest.setLogLevel(logLevel)
|
|
76
|
+
|
|
77
|
+
scope.launch {
|
|
78
|
+
try {
|
|
79
|
+
val loaded = TrustPinAttestConfiguration.fromAssets(applicationContext)
|
|
80
|
+
// The asset loader never reads a debug token: it comes from the
|
|
81
|
+
// build, through this helper.
|
|
82
|
+
val configuration = if (debugToken.isNullOrEmpty()) loaded else TrustPinAttestConfiguration(
|
|
83
|
+
loaded.verifierUrl,
|
|
84
|
+
loaded.attestedHosts,
|
|
85
|
+
loaded.googleCloudProjectNumber,
|
|
86
|
+
debugToken,
|
|
87
|
+
)
|
|
88
|
+
attest.setup(applicationContext, configuration)
|
|
89
|
+
} catch (e: Exception) {
|
|
90
|
+
val rejection = attestRejection(e)
|
|
91
|
+
AttestEventHub.recordLog(
|
|
92
|
+
mapOf(
|
|
93
|
+
AttestKey.LEVEL to "error",
|
|
94
|
+
AttestKey.MESSAGE to
|
|
95
|
+
"TrustPin attestation native init failed [${rejection.code}]: ${rejection.message}",
|
|
96
|
+
AttestKey.TIMESTAMP_MS to nowMs(),
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Java-friendly entry point taking the log level's wire name, used by the
|
|
105
|
+
* Expo config plugin's generated init line.
|
|
106
|
+
*/
|
|
107
|
+
@JvmStatic
|
|
108
|
+
fun start(context: Context, logLevelName: String?, debugToken: String?) {
|
|
109
|
+
start(
|
|
110
|
+
context,
|
|
111
|
+
if (logLevelName.isNullOrEmpty()) TrustPinAttestLogLevel.ERROR else logLevelName.toAttestLogLevel(),
|
|
112
|
+
debugToken,
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
}
|
package/android/src/main/kotlin/cloud/trustpin/reactnative/attest/TrustPinReactNativeAttestModule.kt
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
package cloud.trustpin.reactnative.attest
|
|
2
|
+
|
|
3
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttest
|
|
4
|
+
import cloud.trustpin.kotlin.attest.TrustPinAttestError
|
|
5
|
+
import com.facebook.react.bridge.Promise
|
|
6
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
7
|
+
import com.facebook.react.bridge.ReadableMap
|
|
8
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
9
|
+
import kotlinx.coroutines.CoroutineScope
|
|
10
|
+
import kotlinx.coroutines.Dispatchers
|
|
11
|
+
import kotlinx.coroutines.SupervisorJob
|
|
12
|
+
import kotlinx.coroutines.cancel
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The attestation TurboModule. Each method forwards to the native call of the
|
|
16
|
+
* same name on `TrustPinAttest.default`; setup happens in the native init
|
|
17
|
+
* helper, before JavaScript exists.
|
|
18
|
+
*/
|
|
19
|
+
@ReactModule(name = TrustPinReactNativeAttestModule.NAME)
|
|
20
|
+
class TrustPinReactNativeAttestModule(
|
|
21
|
+
reactContext: ReactApplicationContext,
|
|
22
|
+
) : NativeTrustPinReactNativeAttestSpec(reactContext) {
|
|
23
|
+
|
|
24
|
+
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
|
25
|
+
|
|
26
|
+
private val attest: TrustPinAttest get() = TrustPinAttest.default
|
|
27
|
+
|
|
28
|
+
init {
|
|
29
|
+
// A reload creates a new module, which replaces these callbacks.
|
|
30
|
+
AttestEventHub.attach(
|
|
31
|
+
emitState = { emitOnStateChange(it.toWritableMap()) },
|
|
32
|
+
emitEvent = { emitOnEvent(it.toWritableMap()) },
|
|
33
|
+
emitLog = { emitOnLogEvent(it.toWritableMap()) },
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
override fun invalidate() {
|
|
38
|
+
scope.cancel()
|
|
39
|
+
super.invalidate()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
override fun prefetch(promise: Promise) {
|
|
43
|
+
execute(scope, promise) {
|
|
44
|
+
attest.prefetch()
|
|
45
|
+
null
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
override fun getToken(promise: Promise) {
|
|
50
|
+
execute(scope, promise) { attest.getToken() }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
override fun attestationHeaders(url: String, method: String, headers: ReadableMap, promise: Promise) {
|
|
54
|
+
// The native call takes the request's headers, each as a list of values.
|
|
55
|
+
val requestHeaders = buildMap {
|
|
56
|
+
for ((name, value) in headers.toHashMap()) {
|
|
57
|
+
if (value is String) put(name, listOf(value))
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
execute(scope, promise) {
|
|
61
|
+
attest.attestationHeaders(url, requestHeaders).toWritableMap()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
override fun assertionHeaders(payloadDigestHex: String, promise: Promise) {
|
|
66
|
+
execute(scope, promise) {
|
|
67
|
+
val digest = payloadDigestHex.hexToBytes() ?: throw TrustPinAttestError.InvalidDigest
|
|
68
|
+
attest.assertionHeaders(digest).toWritableMap()
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
override fun setTokenBinding(value: String?, promise: Promise) {
|
|
73
|
+
execute(scope, promise) {
|
|
74
|
+
attest.setTokenBinding(value)
|
|
75
|
+
null
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
override fun bindToHeader(name: String?, promise: Promise) {
|
|
80
|
+
execute(scope, promise) {
|
|
81
|
+
attest.bindToHeader(name)
|
|
82
|
+
null
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
override fun getState(promise: Promise) {
|
|
87
|
+
execute(scope, promise) { attest.state.toWireMap().toWritableMap() }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
override fun reset(promise: Promise) {
|
|
91
|
+
execute(scope, promise) {
|
|
92
|
+
try {
|
|
93
|
+
attest.reset()
|
|
94
|
+
} catch (_: TrustPinAttestError.NotSetUp) {
|
|
95
|
+
// A no-op before setup, as on iOS.
|
|
96
|
+
}
|
|
97
|
+
null
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
override fun setLogLevel(level: String, promise: Promise) {
|
|
102
|
+
execute(scope, promise) {
|
|
103
|
+
attest.setLogLevel(level.toAttestLogLevel())
|
|
104
|
+
null
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
override fun flushEarlyEvents() {
|
|
109
|
+
AttestEventHub.flushEvents()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
override fun flushEarlyLogEvents() {
|
|
113
|
+
AttestEventHub.flushLogs()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
companion object {
|
|
117
|
+
const val NAME: String = "TrustPinReactNativeAttest"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
package cloud.trustpin.reactnative.attest
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
|
|
9
|
+
/** Autolinking entry point for the attestation TurboModule. */
|
|
10
|
+
class TrustPinReactNativeAttestPackage : BaseReactPackage() {
|
|
11
|
+
|
|
12
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
|
|
13
|
+
if (name == TrustPinReactNativeAttestModule.NAME) {
|
|
14
|
+
TrustPinReactNativeAttestModule(reactContext)
|
|
15
|
+
} else {
|
|
16
|
+
null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider {
|
|
20
|
+
mapOf(
|
|
21
|
+
TrustPinReactNativeAttestModule.NAME to ReactModuleInfo(
|
|
22
|
+
TrustPinReactNativeAttestModule.NAME,
|
|
23
|
+
TrustPinReactNativeAttestModule::class.java.name,
|
|
24
|
+
false, // canOverrideExistingModule
|
|
25
|
+
false, // needsEagerInit
|
|
26
|
+
false, // isCxxModule
|
|
27
|
+
true, // isTurboModule
|
|
28
|
+
)
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
}
|
package/app.plugin.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import TrustPinAttestKit
|
|
3
|
+
|
|
4
|
+
/// Boxes the ObjC promise blocks so they can cross into a `@Sendable` task
|
|
5
|
+
/// under Swift 6 strict concurrency, and guarantees exactly one delivery.
|
|
6
|
+
private final class AttestPromiseBox: @unchecked Sendable {
|
|
7
|
+
private let lock = NSLock()
|
|
8
|
+
private var resolve: ((Any?) -> Void)?
|
|
9
|
+
private var reject: ((String, String, [String]) -> Void)?
|
|
10
|
+
|
|
11
|
+
init(resolve: @escaping (Any?) -> Void, reject: @escaping (String, String, [String]) -> Void) {
|
|
12
|
+
self.resolve = resolve
|
|
13
|
+
self.reject = reject
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
func fulfill(_ value: Any?) {
|
|
17
|
+
take()?.resolve(value)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func fail(_ code: String, _ message: String, _ reasons: [String]) {
|
|
21
|
+
take()?.reject(code, message, reasons)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private func take() -> (resolve: (Any?) -> Void, reject: (String, String, [String]) -> Void)? {
|
|
25
|
+
lock.lock()
|
|
26
|
+
defer { lock.unlock() }
|
|
27
|
+
guard let resolve, let reject else { return nil }
|
|
28
|
+
self.resolve = nil
|
|
29
|
+
self.reject = nil
|
|
30
|
+
return (resolve, reject)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// The method implementations behind the TurboModule adapter, against
|
|
35
|
+
/// `TrustPinAttest.default`. Each forwards to the native call of the same
|
|
36
|
+
/// name; setup happens in the native init helper.
|
|
37
|
+
@objc public final class TrustPinAttestBridge: NSObject {
|
|
38
|
+
|
|
39
|
+
private static func run(
|
|
40
|
+
resolve: @escaping (Any?) -> Void,
|
|
41
|
+
reject: @escaping (String, String, [String]) -> Void,
|
|
42
|
+
operation: @escaping @Sendable () async throws -> Any?
|
|
43
|
+
) {
|
|
44
|
+
let box = AttestPromiseBox(resolve: resolve, reject: reject)
|
|
45
|
+
Task {
|
|
46
|
+
do {
|
|
47
|
+
box.fulfill(try await operation())
|
|
48
|
+
} catch {
|
|
49
|
+
let rejection = attestRejection(from: error)
|
|
50
|
+
box.fail(rejection.code, rejection.message, rejection.reasons)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@objc public static func prefetch(
|
|
56
|
+
resolve: @escaping (Any?) -> Void,
|
|
57
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
58
|
+
) {
|
|
59
|
+
run(resolve: resolve, reject: reject) {
|
|
60
|
+
TrustPinAttest.default.prefetch()
|
|
61
|
+
return nil
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@objc public static func getToken(
|
|
66
|
+
resolve: @escaping (Any?) -> Void,
|
|
67
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
68
|
+
) {
|
|
69
|
+
run(resolve: resolve, reject: reject) {
|
|
70
|
+
try await TrustPinAttest.default.getToken()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@objc public static func attestationHeaders(
|
|
75
|
+
_ url: String,
|
|
76
|
+
method: String,
|
|
77
|
+
headers: [String: Any],
|
|
78
|
+
resolve: @escaping (Any?) -> Void,
|
|
79
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
80
|
+
) {
|
|
81
|
+
let fields = headers.compactMapValues { $0 as? String }
|
|
82
|
+
run(resolve: resolve, reject: reject) {
|
|
83
|
+
guard let parsed = URL(string: url), parsed.scheme != nil, parsed.host != nil else {
|
|
84
|
+
throw AttestInvalidArgument(message: "attestationHeaders: invalid url")
|
|
85
|
+
}
|
|
86
|
+
var request = URLRequest(url: parsed)
|
|
87
|
+
request.httpMethod = method
|
|
88
|
+
for (name, value) in fields {
|
|
89
|
+
request.setValue(value, forHTTPHeaderField: name)
|
|
90
|
+
}
|
|
91
|
+
return await TrustPinAttest.default.attestationHeaders(for: request)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
@objc public static func assertionHeaders(
|
|
96
|
+
_ payloadDigestHex: String,
|
|
97
|
+
resolve: @escaping (Any?) -> Void,
|
|
98
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
99
|
+
) {
|
|
100
|
+
run(resolve: resolve, reject: reject) {
|
|
101
|
+
guard let digest = Data(hex: payloadDigestHex) else {
|
|
102
|
+
throw TrustPinAttestError.invalidDigest
|
|
103
|
+
}
|
|
104
|
+
return try await TrustPinAttest.default.assertionHeaders(payloadDigest: digest)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
@objc public static func setTokenBinding(
|
|
109
|
+
_ value: String?,
|
|
110
|
+
resolve: @escaping (Any?) -> Void,
|
|
111
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
112
|
+
) {
|
|
113
|
+
run(resolve: resolve, reject: reject) {
|
|
114
|
+
TrustPinAttest.default.setTokenBinding(value)
|
|
115
|
+
return nil
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@objc public static func bindToHeader(
|
|
120
|
+
_ name: String?,
|
|
121
|
+
resolve: @escaping (Any?) -> Void,
|
|
122
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
123
|
+
) {
|
|
124
|
+
run(resolve: resolve, reject: reject) {
|
|
125
|
+
TrustPinAttest.default.bindToHeader(name)
|
|
126
|
+
return nil
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@objc public static func getState(
|
|
131
|
+
resolve: @escaping (Any?) -> Void,
|
|
132
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
133
|
+
) {
|
|
134
|
+
run(resolve: resolve, reject: reject) {
|
|
135
|
+
TrustPinAttest.default.state.wireMap
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@objc public static func reset(
|
|
140
|
+
resolve: @escaping (Any?) -> Void,
|
|
141
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
142
|
+
) {
|
|
143
|
+
run(resolve: resolve, reject: reject) {
|
|
144
|
+
await TrustPinAttest.default.reset()
|
|
145
|
+
return nil
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@objc public static func setLogLevel(
|
|
150
|
+
_ level: String,
|
|
151
|
+
resolve: @escaping (Any?) -> Void,
|
|
152
|
+
reject: @escaping (String, String, [String]) -> Void
|
|
153
|
+
) {
|
|
154
|
+
run(resolve: resolve, reject: reject) {
|
|
155
|
+
TrustPinAttest.default.set(logLevel: TrustPinAttestLogLevel(wireName: level))
|
|
156
|
+
return nil
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
@objc public static func flushEarlyEvents() {
|
|
161
|
+
TrustPinAttestEventHub.shared.flushEvents()
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@objc public static func flushEarlyLogEvents() {
|
|
165
|
+
TrustPinAttestEventHub.shared.flushLogs()
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/// Wires the TurboModule's codegen emitters into the event hub. The blocks
|
|
169
|
+
/// must tolerate any calling thread.
|
|
170
|
+
@objc public static func attachEmitters(
|
|
171
|
+
state: @escaping (NSDictionary) -> Void,
|
|
172
|
+
event: @escaping (NSDictionary) -> Void,
|
|
173
|
+
log: @escaping (NSDictionary) -> Void
|
|
174
|
+
) {
|
|
175
|
+
TrustPinAttestEventHub.shared.attach(
|
|
176
|
+
emitState: { state($0 as NSDictionary) },
|
|
177
|
+
emitEvent: { event($0 as NSDictionary) },
|
|
178
|
+
emitLog: { log($0 as NSDictionary) }
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private extension Data {
|
|
184
|
+
/// Exactly 64 hex characters, or nil.
|
|
185
|
+
init?(hex: String) {
|
|
186
|
+
guard hex.count == 64 else { return nil }
|
|
187
|
+
var bytes = [UInt8]()
|
|
188
|
+
bytes.reserveCapacity(32)
|
|
189
|
+
var index = hex.startIndex
|
|
190
|
+
while index < hex.endIndex {
|
|
191
|
+
let next = hex.index(index, offsetBy: 2)
|
|
192
|
+
guard let byte = UInt8(hex[index..<next], radix: 16) else { return nil }
|
|
193
|
+
bytes.append(byte)
|
|
194
|
+
index = next
|
|
195
|
+
}
|
|
196
|
+
self.init(bytes)
|
|
197
|
+
}
|
|
198
|
+
}
|