# App attestation
Source: https://sovaria.in/docs/mobile-attestation

Accept traffic only from your genuine apps: Play Integrity on Android, App Attest and DeviceCheck on iOS, reCAPTCHA on the web, per service, sign-in included; limited-use tokens and signed attestation JWTs.

Your anon key ships inside the app, so anyone can copy it into a script. With attestation on, requests made with the anon key or by signed-in app users must also carry a short-lived **attestation token** that only your real app on a real device can get. Console members and server keys are never affected.

(Diagram: Attestation flow)

1. **Register your apps** — Android needs the package name and signing certificate SHA-256 (Play Integrity checks it); iOS the bundle id and team id.
2. **Add verification credentials** — Play Integrity: a Google Cloud service account allowed to decode integrity tokens (the FCM one is used if it belongs to the same Google Cloud project). DeviceCheck: an Apple key with DeviceCheck enabled (the APNs key is used if none is set).
3. **Give the client a provider**
```dart
final sovaria = Sovaria(
  url: url, anonKey: anonKey,
  // sovaria_flutter: App Attest on iOS, Play Integrity on Android (the app adds com.google.android.play:integrity)
  attestation: kReleaseMode
      ? SovariaFlutterAttestation(cloudProjectNumber: 123456789, iosAppId: 'TEAMID.com.acme.app')
      : DebugAttestationProvider(debugToken),
);
```
```swift
let sovaria = Sovaria(url: url, anonKey: anonKey,
    attestation: isSimulator ? DebugAttestationProvider(debugToken: debugToken)
                             : AppAttestProvider(appId: "com.acme.app", storage: KeychainStore(), development: isDebugBuild))
```
```kotlin
val sovaria = Sovaria(url, anonKey,
    // PlayIntegrityProvider is built in; the app adds com.google.android.play:integrity
    attestation = if (BuildConfig.DEBUG) DebugAttestationProvider(debugToken) else PlayIntegrityProvider(context, cloudProjectNumber))
```
4. **Monitor, then enforce** — Authentication › Mobile › **App attestation**: start with **Monitor** and watch the last-7-days counts. Switch to **Enforce** when “missing” is close to zero — older app versions without attestation stop working.

| Provider | Platform | Checks |
|---|---|---|
| Play Integrity | Android | Package name, signing certificate, Play recognised build, device integrity, our challenge, freshness. |
| App Attest | iOS 14+ | Apple-signed attestation of a hardware key for TEAMID.bundle, then signed assertions with an increasing counter. |
| DeviceCheck | Older iOS | A real Apple device running your team's app (cannot bind a challenge, so weaker). |
| Debug token | Simulators, CI | A secret you create in the console (shown once). Never in release builds. |

Unattested requests to an enforcing project get `401` with code `APP_ATTESTATION_REQUIRED`; the SDK fetches a new token and retries once. Attestation needs `auth.app_attestation` (Pro and up); after a downgrade enforcement stops.

## What is covered

Sign-in traffic is attested too: sign-up, password and OTP sign-in, SMS codes (no SMS pumping from scripts), magic links, password reset, token refresh, SSO code exchange, passkey sign-in and ID-token sign-in. Under **Covered traffic** turn enforcement off for a service you don't want it on (sign-in, database, storage, functions, realtime), like Firebase App Check per product.

## Web apps (reCAPTCHA)

Turn on **reCAPTCHA** with your site key and secret (v3) or API key and Google Cloud project (Enterprise), load Google's script on the page, then:

JavaScript:
```js
import { createClient, recaptchaAttestation } from '@sovaria/sdk'

const attest = recaptchaAttestation({ url: PROJECT_URL, anonKey: ANON })   // action "sovaria_attest"
const sovaria = createClient(PROJECT_URL, ANON, { attestationToken: attest })
```

Sovaria verifies the token with Google (score at least your minimum, our action, your hostnames) and accepts each token once.

## Limited-use tokens and attestation JWTs

- `POST /api/v1/attest/exchange` with `limited_use: true` returns a token that works for **one** request (5 minutes at most): use it for a payment or a password change (`attest.limitedUse()` on the web).
- Every exchange also returns `attestation_jwt`: an ES256 JWT signed with your project's key (`aud: sovaria-attestation`, `app_id`, `provider`), so your own backend or functions can check that a request came from your app with the project JWKS. It is never accepted as a sign-in token.
- DeviceCheck and reCAPTCHA evidence works once; App Attest counters are checked atomically; debug tokens expire (90 days by default) and deleting one ends the tokens it issued.

Swift ships `AppAttestProvider` (App Attest, falling back to DeviceCheck where unsupported; the key id is kept in your session storage). Kotlin ships `PlayIntegrityProvider(packageName) { requestHash -> token }`: you pass the call to Google's Standard Integrity API, the SDK stays free of Play dependencies. In Flutter you implement `AttestationProvider.evidence(challenge)`: Play Integrity (`requestHash = challenge`) or App Attest through a platform channel (`DCAppAttestService`, `clientDataHash = SHA-256(challenge)`).
