|
| 1 | +import * as AppIntegrity from "@expo/app-integrity"; |
| 2 | +import * as SecureStore from "expo-secure-store"; |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { Button, Platform, ScrollView, StyleSheet, Text, View } from "react-native"; |
| 5 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 6 | + |
| 7 | +// Public URL of the deployed Node server running the API routes. Leave empty to |
| 8 | +// use the dev server (relative requests). Required for device/production builds. |
| 9 | +const API_URL = process.env.EXPO_PUBLIC_API_URL ?? ""; |
| 10 | +const CLOUD_PROJECT_NUMBER = process.env.EXPO_PUBLIC_CLOUD_PROJECT_NUMBER ?? ""; |
| 11 | + |
| 12 | +// SecureStore key under which the attested App Attest key identifier is kept. |
| 13 | +const KEY_ID_STORE = "appattest.keyId"; |
| 14 | + |
| 15 | +async function getChallenge(): Promise<{ challenge: string }> { |
| 16 | + const res = await fetch(`${API_URL}/api/challenge`); |
| 17 | + if (!res.ok) throw new Error(`challenge failed: ${res.status}`); |
| 18 | + return res.json(); |
| 19 | +} |
| 20 | + |
| 21 | +async function verify(body: Record<string, unknown>) { |
| 22 | + const res = await fetch(`${API_URL}/api/verify`, { |
| 23 | + method: "POST", |
| 24 | + headers: { "Content-Type": "application/json" }, |
| 25 | + body: JSON.stringify(body), |
| 26 | + }); |
| 27 | + const json = await res.json(); |
| 28 | + if (!res.ok) throw new Error(json.error ?? `verify failed: ${res.status}`); |
| 29 | + return json; |
| 30 | +} |
| 31 | + |
| 32 | +export default function Index() { |
| 33 | + const insets = useSafeAreaInsets(); |
| 34 | + const [log, setLog] = useState<string[]>([]); |
| 35 | + const [busy, setBusy] = useState(false); |
| 36 | + // The attested keyId, restored from SecureStore. When set, assertions are |
| 37 | + // available without re-attesting. |
| 38 | + const [keyId, setKeyId] = useState<string | null>(null); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (Platform.OS === "ios") { |
| 42 | + SecureStore.getItemAsync(KEY_ID_STORE) |
| 43 | + .then(setKeyId) |
| 44 | + .catch(() => {}); |
| 45 | + } |
| 46 | + }, []); |
| 47 | + |
| 48 | + const append = (line: string) => setLog((prev) => [...prev, line]); |
| 49 | + |
| 50 | + async function run(fn: () => Promise<void>) { |
| 51 | + setBusy(true); |
| 52 | + setLog([]); |
| 53 | + try { |
| 54 | + await fn(); |
| 55 | + } catch (e) { |
| 56 | + append(`❌ ${e instanceof Error ? e.message : String(e)}`); |
| 57 | + } finally { |
| 58 | + setBusy(false); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + async function runAttestation() { |
| 63 | + append(`▶️ Attesting on ${Platform.OS}…`); |
| 64 | + if (!AppIntegrity.isSupported) { |
| 65 | + append("❌ App Attest is not supported on this device."); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const newKeyId = await AppIntegrity.generateKeyAsync(); |
| 70 | + append(`🔑 keyId: ${newKeyId}`); |
| 71 | + |
| 72 | + const { challenge } = await getChallenge(); |
| 73 | + const attestation = await AppIntegrity.attestKeyAsync(newKeyId, challenge); |
| 74 | + append("📤 sending attestation to server…"); |
| 75 | + const result = await verify({ |
| 76 | + kind: "ios-attest", |
| 77 | + challenge, |
| 78 | + keyId: newKeyId, |
| 79 | + attestation, |
| 80 | + }); |
| 81 | + append(`✅ attestation verified: ${JSON.stringify(result)}`); |
| 82 | + |
| 83 | + // Persist the attested key so assertions can use it later. |
| 84 | + await SecureStore.setItemAsync(KEY_ID_STORE, newKeyId); |
| 85 | + setKeyId(newKeyId); |
| 86 | + append("💾 keyId saved to SecureStore"); |
| 87 | + } |
| 88 | + |
| 89 | + async function runAssertion() { |
| 90 | + const storedKeyId = await SecureStore.getItemAsync(KEY_ID_STORE); |
| 91 | + if (!storedKeyId) { |
| 92 | + append("❌ No attested key found. Run attestation first."); |
| 93 | + setKeyId(null); |
| 94 | + return; |
| 95 | + } |
| 96 | + append(`🔑 using stored keyId: ${storedKeyId}`); |
| 97 | + |
| 98 | + const { challenge } = await getChallenge(); |
| 99 | + const assertion = await AppIntegrity.generateAssertionAsync(storedKeyId, challenge); |
| 100 | + append("📤 sending assertion to server…"); |
| 101 | + const result = await verify({ |
| 102 | + kind: "ios-assert", |
| 103 | + challenge, |
| 104 | + keyId: storedKeyId, |
| 105 | + assertion, |
| 106 | + }); |
| 107 | + append(`✅ assertion verified: ${JSON.stringify(result)}`); |
| 108 | + } |
| 109 | + |
| 110 | + async function runAndroid() { |
| 111 | + append("▶️ Running integrity check on android…"); |
| 112 | + if (!CLOUD_PROJECT_NUMBER) { |
| 113 | + append("❌ Set EXPO_PUBLIC_CLOUD_PROJECT_NUMBER in your .env first."); |
| 114 | + return; |
| 115 | + } |
| 116 | + await AppIntegrity.prepareIntegrityTokenProviderAsync(CLOUD_PROJECT_NUMBER); |
| 117 | + append("⚙️ integrity token provider ready"); |
| 118 | + |
| 119 | + const { challenge } = await getChallenge(); |
| 120 | + const token = await AppIntegrity.requestIntegrityCheckAsync(challenge); |
| 121 | + append("📤 sending integrity token to server…"); |
| 122 | + const result = await verify({ kind: "android", challenge, token }); |
| 123 | + append(`✅ token verified: ${JSON.stringify(result)}`); |
| 124 | + } |
| 125 | + |
| 126 | + return ( |
| 127 | + <View style={[styles.container, { paddingBottom: insets.bottom + 16 }]}> |
| 128 | + <Text style={styles.title}>Device & app attestation</Text> |
| 129 | + <Text style={styles.subtitle}>Run the platform integrity flow, then verify the result on the server.</Text> |
| 130 | + |
| 131 | + <View style={styles.buttons}> |
| 132 | + {Platform.OS === "android" ? ( |
| 133 | + <Button title={busy ? "Running…" : "Run integrity check"} onPress={() => run(runAndroid)} disabled={busy} /> |
| 134 | + ) : ( |
| 135 | + <> |
| 136 | + <Button title={busy ? "Running…" : "Run attestation"} onPress={() => run(runAttestation)} disabled={busy} /> |
| 137 | + {keyId ? ( |
| 138 | + <Button title={busy ? "Running…" : "Run assertion"} onPress={() => run(runAssertion)} disabled={busy} /> |
| 139 | + ) : null} |
| 140 | + </> |
| 141 | + )} |
| 142 | + </View> |
| 143 | + |
| 144 | + <ScrollView style={styles.log} contentContainerStyle={styles.logContent}> |
| 145 | + {log.length === 0 ? ( |
| 146 | + <Text style={styles.placeholder}>Results will appear here.</Text> |
| 147 | + ) : ( |
| 148 | + log.map((line, i) => ( |
| 149 | + <Text key={i} style={styles.logLine} selectable> |
| 150 | + {line} |
| 151 | + </Text> |
| 152 | + )) |
| 153 | + )} |
| 154 | + </ScrollView> |
| 155 | + </View> |
| 156 | + ); |
| 157 | +} |
| 158 | + |
| 159 | +const styles = StyleSheet.create({ |
| 160 | + container: { |
| 161 | + flex: 1, |
| 162 | + padding: 16, |
| 163 | + gap: 12, |
| 164 | + backgroundColor: "#fff", |
| 165 | + }, |
| 166 | + title: { |
| 167 | + fontSize: 22, |
| 168 | + fontWeight: "600", |
| 169 | + }, |
| 170 | + subtitle: { |
| 171 | + fontSize: 14, |
| 172 | + color: "#555", |
| 173 | + }, |
| 174 | + buttons: { |
| 175 | + gap: 8, |
| 176 | + }, |
| 177 | + log: { |
| 178 | + flex: 1, |
| 179 | + borderWidth: 1, |
| 180 | + borderColor: "#e0e0e0", |
| 181 | + borderRadius: 8, |
| 182 | + }, |
| 183 | + logContent: { |
| 184 | + padding: 12, |
| 185 | + gap: 6, |
| 186 | + }, |
| 187 | + placeholder: { |
| 188 | + color: "#999", |
| 189 | + }, |
| 190 | + logLine: { |
| 191 | + fontFamily: Platform.select({ ios: "Menlo", android: "monospace" }), |
| 192 | + fontSize: 12, |
| 193 | + }, |
| 194 | +}); |
0 commit comments