App Check カスタム プロバイダを実装する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
App Check には、複数のプロバイダに対するサポートが組み込まれています。Apple プラットフォームでは DeviceCheck と App Attest、Android では Play Integrity、ウェブアプリでは reCAPTCHA Enterprise がそれぞれサポートされています(概要)。ほとんどのデベロッパーの要件は、これらはプロバイダで満たすことができますが、独自の App Check カスタム プロバイダを実装することもできます。次のような場合には、カスタム プロバイダが必要になります。
組み込みのプロバイダ以外のプロバイダを使用したい。
サポートされていない方法で組み込みのプロバイダを使用したい。
Apple、Android、ウェブ以外のプラットフォームを使用するデバイスを確認したい。たとえば、デスクトップ OS や IoT(モノのインターネット)デバイス用に App Check プロバイダを作成する場合など。
どのプラットフォームにも独自の検証技術を実装したい。
概要
カスタム App Check プロバイダを実装するには、Node.js Firebase Admin SDK を実行できる安全なバックエンド環境が必要です。このような環境としては、Cloud Functions、Cloud Run などのコンテナ プラットフォーム、独自のサーバーなどがあります。
この環境で、ネットワークを介してアプリ クライアントから真正性の証明書を受け取り、真正性が証明された場合に App Check トークンを返すサービスを用意します。真正性の証明として使用できるインジケーターは、使用しているサードパーティ プロバイダによって異なります。また、カスタム ロジックを実装する場合は、独自のインジケーターを構築する必要があります。
通常、このサービスは REST エンドポイントまたは gRPC エンドポイントとして公開しますが、その詳細はデベロッパーが決定します。
トークン取得エンドポイントを作成する
Admin SDK をインストールして初期化します。
クライアントから真正性データをネットワーク経由で受信するエンドポイントを作成します。Cloud Functions の使用例を次に示します。
// Create endpoint at https://example-app.cloudfunctions.net/fetchAppCheckToken
exports.fetchAppCheckToken = functions.https.onRequest((request, response) => {
// ...
});
真正性データを評価するロジックをエンドポイントに追加します。これは App Check カスタム プロバイダのコアロジックで、デベロッパー自身で作成する必要があります。
クライアントの真正性が確認された場合は、Admin SDK を使用して App Check トークンを作成し、作成したトークンと有効期限をクライアントに返します。
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);
});
クライアントの真正性を確認できない場合は、エラーを返します(HTTP 403 エラーなど)。
省略可: createToken()
オブジェクトを AppCheckTokenOptions
に渡して、カスタム プロバイダによって発行される App Check トークンの有効期間(TTL)を設定します。TTL は 30 分から 7 日までの任意の値に設定できます。この値を設定する場合は、次のトレードオフに注意してください。
- セキュリティ: TTL が短いほど、漏えいしたトークンや傍受されたトークンが攻撃者によって悪用される可能性が低減するため、セキュリティが向上します。
- パフォーマンス: TTL が短いほど、アプリで証明書の取得が頻繁に行われます。アプリで証明書が取得されるたびにネットワーク リクエストのレイテンシが増加するため、TTL が短いと、アプリのパフォーマンスに影響する可能性があります。
通常は、デフォルトの TTL(1 時間)で十分です。
次のステップ
ここでは、カスタム プロバイダのサーバー側のロジックを実装しました。次に、このロジックを Apple、Android、ウェブ クライアントから使用する方法を学習します。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-23 UTC。
[null,null,["最終更新日 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."]]