Implementa un provider App Check personalizzato
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
App Check supporta diversi provider: DeviceCheck e App
Attest sulle piattaforme Apple, Play Integrity su Android e reCAPTCHA Enterprise
nelle app web (panoramica). Si tratta di fornitori ben noti
che dovrebbero soddisfare le esigenze della maggior parte degli sviluppatori. Tuttavia, puoi anche implementare
i tuoi provider App Check personalizzati. L'utilizzo di un provider personalizzato è necessario
quando:
Vuoi utilizzare un provider diverso da quelli integrati.
Vuoi utilizzare i provider integrati in modi non supportati.
Vuoi verificare i dispositivi utilizzando piattaforme diverse da Apple, Android e dal web. Ad esempio, puoi creare App Check provider per sistemi operativi desktop o
dispositivi IoT.
Vuoi implementare le tue tecniche di verifica su qualsiasi piattaforma.
Panoramica
Per implementare un provider App Check personalizzato, hai bisogno di un ambiente backend sicuro
in grado di eseguire Firebase Admin SDK Node.js.
Può trattarsi di Cloud Functions, di una piattaforma container come
Cloud Run o del tuo server.
Da questo ambiente, fornirai un servizio accessibile dalla rete che
riceve la prova di autenticità dai client della tua app e, se la prova di
autenticità supera la tua valutazione di autenticità, restituisce un token App Check. Gli indicatori specifici che utilizzi come prova di autenticità dipendono dal fornitore di terze parti che utilizzi o dagli indicatori che hai inventato, se implementi una logica personalizzata.
In genere, esponi questo servizio come endpoint REST o gRPC, ma questo dettaglio dipende da te.
Crea l'endpoint di acquisizione dei token
Installa e inizializza Admin SDK.
Crea un endpoint accessibile dalla rete che possa ricevere dati di autenticità dai
tuoi client. Ad esempio, utilizzando Cloud Functions:
// Create endpoint at https://example-app.cloudfunctions.net/fetchAppCheckToken
exports.fetchAppCheckToken = functions.https.onRequest((request, response) => {
// ...
});
Aggiungi alla logica dell'endpoint che valuta i dati di autenticità. Questa è la
logica di base del tuo fornitore App Check personalizzato, che dovrai
scrivere tu.
Se determini che il client è autentico, utilizza Admin SDK per creare
un token App Check e restituirlo al client insieme alla relativa ora di scadenza:
const admin = require('firebase-admin');
admin.initializeApp();
// ...
admin.appCheck().createToken(appId)
.then(function (appCheckToken) {
// Token expires in an hour.
const expiresAt = Math.floor(Date.now() / 1000) + 60 * 60;
// Return appCheckToken and expiresAt to the client.
})
.catch(function (err) {
console.error('Unable to create App Check token.');
console.error(err);
});
Se non riesci a verificare l'autenticità del client, restituisci un errore (ad esempio,
restituisci un errore HTTP 403).
(Facoltativo): imposta la durata (TTL) per i token App Check emessi dal tuo provider personalizzato passando un oggetto AppCheckTokenOptions
a createToken()
. Puoi impostare il TTL su qualsiasi valore compreso tra 30 minuti e 7 giorni. Quando imposti questo valore, tieni presente i seguenti compromessi:
- Sicurezza: i TTL più brevi offrono una maggiore sicurezza, perché riducono
il periodo di tempo in cui un token compromesso o intercettato può essere utilizzato in modo illecito da un
malintenzionato.
- Rendimento: TTL più brevi significano che la tua app eseguirà l'attestazione più
spesso. Poiché il processo di attestazione dell'app aggiunge latenza alle richieste di rete ogni volta che viene eseguito, un TTL breve può influire sulle prestazioni dell'app.
Il TTL predefinito di 1 ora è ragionevole per la maggior parte delle app.
Passaggi successivi
Ora che hai implementato la logica lato server del tuo fornitore personalizzato, scopri come
utilizzarla dai client Apple,
Android e web.
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-08-23 UTC.
[null,null,["Ultimo aggiornamento 2025-08-23 UTC."],[],[],null,["App Check has built-in support for several providers: DeviceCheck and App\nAttest on Apple platforms, Play Integrity on Android, and reCAPTCHA Enterprise\nin web apps ([overview](/docs/app-check)). These are well-understood providers\nthat should meet the needs of most developers. You can, however, also implement\nyour own custom App Check providers. Using a custom provider is necessary\nwhen:\n\n- You want to use a provider other than the built-in ones.\n\n- You want to use the built-in providers in unsupported ways.\n\n- You want to verify devices using platforms other than Apple, Android, and the\n web. For example, you could create App Check providers for desktop OSes or\n Internet-of-Things devices.\n\n- You want to implement your own verification techniques on any platform.\n\nOverview\n\nTo implement a custom App Check provider, you need a secure backend\nenvironment that can run the Node.js [Firebase Admin SDK](/docs/admin/setup).\nThis can be Cloud Functions, a container platform such as\n[Cloud Run](https://cloud.google.com/run), or your own server.\n\nFrom this environment, you will provide a network-accessible service that\nreceives proof of authenticity from your app clients, and---if the proof of\nauthenticity passes your authenticity assessment---returns an App Check\ntoken. The specific indicators you use as proof of authenticity will depend on\neither the third-party provider you're using, or the indicators of your own\ninvention, if you're implementing custom logic.\n\nUsually, you expose this service as a REST or gRPC endpoint, but this detail is\nup to you.\n\nCreate the token acquisition endpoint\n\n1. [Install and initialize the Admin SDK](/docs/admin/setup).\n\n2. Create a network-accessible endpoint that can receive authenticity data from\n your clients. For example, using Cloud Functions:\n\n // Create endpoint at https://example-app.cloudfunctions.net/fetchAppCheckToken\n exports.fetchAppCheckToken = functions.https.onRequest((request, response) =\u003e {\n // ...\n });\n\n3. Add to the endpoint logic that assesses the authenticity data. This is the\n core logic of your custom App Check provider, which you will need to\n write yourself.\n\n4. If you determine the client to be authentic, use the Admin SDK to mint\n an App Check token and return it and its expiration time to the client:\n\n const admin = require('firebase-admin');\n admin.initializeApp();\n\n // ...\n\n admin.appCheck().createToken(appId)\n .then(function (appCheckToken) {\n // Token expires in an hour.\n const expiresAt = Math.floor(Date.now() / 1000) + 60 * 60;\n\n // Return appCheckToken and expiresAt to the client.\n })\n .catch(function (err) {\n console.error('Unable to create App Check token.');\n console.error(err);\n });\n\n | **Note:** If you encounter a token signing error while creating a new token, make sure your service account has the required permissions. See the [Admin SDK troubleshooting guide](/docs/auth/admin/create-custom-tokens#troubleshooting).\n\n If you can't verify the client's authenticity, return an error (for example,\n return an HTTP 403 error).\n5. **Optional** : Set the time-to-live (TTL) for App Check tokens issued by\n your custom provider by passing an `AppCheckTokenOptions` object to\n `createToken()`. You can set the TTL to any value between 30 minutes and 7\n days. When setting this value, be aware of the following tradeoffs:\n\n - Security: Shorter TTLs provide stronger security, because it reduces the window in which a leaked or intercepted token can be abused by an attacker.\n - Performance: Shorter TTLs mean your app will perform attestation more frequently. Because the app attestation process adds latency to network requests every time it's performed, a short TTL can impact the performance of your app.\n\n The default TTL of 1 hour is reasonable for most apps.\n\nNext steps\n\nNow that you've implemented your custom provider's server-side logic, learn how\nto use it from your [Apple](/docs/app-check/ios/custom-provider),\n[Android](/docs/app-check/android/custom-provider), and [web](/docs/app-check/web/custom-provider) clients."]]