Puoi utilizzare App Check per proteggere le risorse di backend personalizzate non Google per la tua app, come il tuo backend self-hosted. A questo scopo, dovrai fare entrambe le seguenti operazioni:
- Modifica il client dell'app per inviare un token App Check insieme a ogni richiesta al backend, come descritto in questa pagina.
- Modifica il backend in modo che richieda un token App Check valido per ogni richiesta, come descritto in Verificare i token App Check da un backend personalizzato.
Prima di iniziare
Aggiungi App Check alla tua app utilizzando App Attest, DeviceCheck o un provider personalizzato.
Inviare token App Check con richieste di backend
Per assicurarti che le richieste di backend includano un token App Check valido e non scaduto,
inserisci ogni richiesta in una chiamata a AppCheck.token(). La libreria App Check
aggiornerà il token se necessario e potrai accedervi nel
blocco di completamento del metodo.
Una volta ottenuto un token valido, invialo insieme alla richiesta al tuo backend. I dettagli su come farlo dipendono da te, ma non inviare token App Check come parte degli URL, inclusi i parametri di query, in quanto ciò li rende vulnerabili a perdite e intercettazioni accidentali. L'esempio seguente invia il token in un'intestazione HTTP personalizzata, che è l'approccio consigliato.
Swift
do { let token = try await AppCheck.appCheck().token(forcingRefresh: false) // Get the raw App Check token string. let tokenString = token.token // Include the App Check token with requests to your server. let url = URL(string: "https://yourbackend.example.com/yourApiEndpoint")! var request = URLRequest(url: url) request.httpMethod = "GET" request.setValue(tokenString, forHTTPHeaderField: "X-Firebase-AppCheck") let task = URLSession.shared.dataTask(with: request) { data, response, error in // Handle response from your backend. } task.resume() } catch(let error) { print("Unable to retrieve App Check token: \(error)") return }
Objective-C
[[FIRAppCheck appCheck] tokenForcingRefresh:NO completion:^(FIRAppCheckToken * _Nullable token, NSError * _Nullable error) { if (error != nil) { // Handle any errors if the token was not retrieved. NSLog(@"Unable to retrieve App Check token: %@", error); return; } if (token == nil) { NSLog(@"Unable to retrieve App Check token."); return; } // Get the raw App Check token string. NSString *tokenString = token.token; // Include the App Check token with requests to your server. NSURL *url = [[NSURL alloc] initWithString:@"https://yourbackend.example.com/yourApiEndpoint"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:@"GET"]; [request setValue:tokenString forHTTPHeaderField:@"X-Firebase-AppCheck"]; NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // Handle response from your backend. }]; [task resume]; }];
Protezione dalla riproduzione (beta)
Quando effettui una richiesta a un endpoint per il quale hai attivato la
protezione da replay,
inserisci la richiesta in una chiamata a limitedUseToken() anziché a token():
Swift
AppCheck.appCheck().limitedUseToken() { token, error in
// ...
}
Objective-C
[[FIRAppCheck appCheck] limitedUseTokenWithCompletion:^(FIRAppCheckToken * _Nullable token,
NSError * _Nullable error) {
// ...
}];