Puoi proteggere le risorse non Firebase della tua app, come i backend self-hosted, con App Check. Per fare ciò, dovrai eseguire entrambe le seguenti operazioni:
- Modifica il client dell'app per inviare un token App Check insieme a ogni richiesta al tuo back-end, come descritto in questa pagina.
- Modifica il tuo back-end per richiedere un token App Check valido con ogni richiesta, come descritto in Verifica dei token App Check da un back-end personalizzato .
Prima di iniziare
Aggiungi App Check alla tua app, utilizzando App Attest , DeviceCheck o un provider personalizzato .
Invia token App Check con richieste di back-end
Per assicurarti che le tue richieste di back-end includano un token App Check valido e non scaduto, avvolgi ogni richiesta in una chiamata a AppCheck.token()
. La libreria App Check aggiornerà il token se necessario e potrai accedere al token nel blocco di completamento del metodo.
Una volta che hai un token valido, invialo insieme alla richiesta al tuo back-end. Le specifiche su come eseguire questa operazione dipendono da te, ma non inviare token App Check come parte degli URL , inclusi i parametri di query, poiché ciò li rende vulnerabili a perdite e intercettazioni accidentali. L'esempio seguente invia il token in un'intestazione HTTP personalizzata, che è l'approccio consigliato.
Veloce
AppCheck.appCheck().token(forcingRefresh: false) { token, error in guard error == nil else { // Handle any errors if the token was not retrieved. print("Unable to retrieve App Check token: \(error!)") return } guard let token = token else { print("Unable to retrieve App Check token.") return } // 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() }
Obiettivo-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]; }];