firebase_admin_dart 0.1.0
firebase_admin_dart: ^0.1.0 copied to clipboard
Unofficial, light, pure-Dart Firebase admin calls for servers, CLIs and tests: Auth custom tokens, users, custom claims, revocation and ID token checks.
firebase_admin_dart #
Unofficial, light, pure-Dart Firebase admin calls for servers, command line tools, CI jobs and integration tests. The first library is Firebase Auth: mint custom tokens, create, read, update and delete users, set custom claims, revoke sessions and verify ID tokens. It is not affiliated with or supported by Google or Firebase.
Status: 0.1.0. Unit tests and the Firebase Auth emulator cover every call. On 2026-11-18 the main path also passed against one real Firebase project, keyless: Application Default Credentials of a person, custom tokens signed by the IAM Credentials API (see Checking a real project for the steps and what was not run). Run
example/production_check.dartonce against your own test project before you rely on it.
Admin credentials bypass security rules and can act as any user. Use this package only in code you control. Never put a service account key in an app you ship, a web page, a Flutter asset or a repository. See Security.
Install #
dart pub add firebase_admin_dart
Dart 3.8 or later. It depends on firebase_auth_providers_dart (access tokens, the RS256 signer),
firebase_app_dart, http, crypto and meta; nothing else.
Quick start #
import 'package:firebase_admin_dart/auth.dart';
Future<void> main() async {
// The Auth emulator. With a real project:
// FirebaseAdminApp(projectId: ..., credential: AdminCredential.serviceAccount(keyJson)).
final FirebaseAdminApp app = FirebaseAdminApp.emulator(
projectId: 'demo-test',
authEmulatorHost: '127.0.0.1:9099',
);
final AdminUser user = await app.auth().createUser(
customClaims: <String, Object?>{'role': 'tester'},
);
final String token = await app.auth().createCustomToken(
user.uid,
claims: <String, Object?>{'tier': 'pro'},
);
// Hand the token to the client: FirebaseUserAuth.signInWithCustomToken(token), or
// FirebaseAuth.instance.signInWithCustomToken(token) of FlutterFire.
await app.auth().deleteUser(user.uid);
await app.close();
}
example/main.dart runs the whole loop against the emulator: create a user, mint a token with
claims, sign in with firebase_auth_providers_dart, read the claims of the ID token, verify it,
delete the user.
Layout #
| Library | What it has |
|---|---|
package:firebase_admin_dart/firebase_admin_dart.dart |
What every admin area shares: FirebaseAdminApp, AdminCredential, FirebaseAdminException |
package:firebase_admin_dart/auth.dart |
Firebase Auth: FirebaseAuthAdmin (app.auth()), AdminUser, VerifiedIdToken, FirebaseAuthAdminException. Exports the library above |
package:firebase_admin_dart/adc.dart |
applicationDefaultCredential(): Application Default Credentials. Needs dart:io |
The names do not collide with firebase_app_dart (FirebaseApp, FirebaseException) or with
FlutterFire (FirebaseAuth, User): you can import them side by side without prefixes.
The three environments #
| Where | App | Custom tokens are signed |
|---|---|---|
| A machine you control with a key file (CI, a CLI, a test runner) | FirebaseAdminApp(projectId: ..., credential: AdminCredential.serviceAccount(keyJson)) |
locally, RS256 |
Your own machine, keyless, after gcloud auth application-default login |
FirebaseAdminApp(projectId: ..., credential: applicationDefaultCredential(serviceAccountEmail: ..., quotaProjectId: ...)) |
by the IAM Credentials API (signBlob) as that service account |
| Google Cloud (Cloud Run, Functions, GKE, Compute Engine) | FirebaseAdminApp(projectId: ..., credential: applicationDefaultCredential()) |
by the IAM Credentials API (signBlob) as the default service account |
| The Auth emulator | FirebaseAdminApp.emulator(projectId: 'demo-test', authEmulatorHost: '127.0.0.1:9099') |
not at all (alg: none) |
You load the key file yourself and pass its content (a Map or the JSON string): the package never
reads a file or an environment variable on its own. The only exception is adc.dart, which uses
the Application Default Credentials lookup of firebase_auth_providers_dart.
The emulator is never picked from FIREBASE_AUTH_EMULATOR_HOST behind your back: only
FirebaseAdminApp.emulator turns emulator mode on, and only in emulator mode does verifyIdToken
accept unsigned tokens. Pass FirebaseUserAuth.emulatorHostFromEnvironment yourself if you want
the variable.
With AdminCredential.iam, signing needs the IAM Service Account Credentials API enabled, and the
role Service Account Token Creator for the caller on the signing account. Off Google Cloud, pass
serviceAccountEmail, or load the key and use AdminCredential.serviceAccount. With the
credentials of a person (gcloud auth application-default login) Google APIs may ask for a quota
project: pass quotaProjectId.
The service account needs the role Firebase Authentication Admin (roles/firebaseauth.admin).
What it offers #
| Call | Endpoint | Emulator |
|---|---|---|
createCustomToken(uid, {claims, tenantId}) |
none with a key; iamcredentials ...:signBlob without |
yes (unsigned) |
createUser({uid, email, password, emailVerified, displayName, photoUrl, phoneNumber, disabled, customClaims}) |
POST v1/projects/{p}/accounts (+ accounts:update for claims) |
yes |
getUser(uid), findUserByEmail(email) |
accounts:lookup |
yes |
updateUser(uid, {...}) |
accounts:update |
yes |
deleteUser(uid), deleteUsers(uids) (batches of 1000) |
accounts:delete, accounts:batchDelete |
yes |
setCustomUserClaims(uid, claims), revokeRefreshTokens(uid) |
accounts:update |
yes |
verifyIdToken(idToken, {checkRevoked, clockSkew}) |
Google's x509 keys; accounts:lookup with checkRevoked |
yes (unsigned tokens) |
Custom tokens follow the rules of the Firebase documentation: RS256, base64url without padding, the
Identity Toolkit audience, one hour of life, uid of 1 to 128 characters, no reserved claim names
(FirebaseAuthAdmin.reservedClaims).
verifyIdToken checks the signature with the key named by kid, then aud, iss, sub, exp,
iat and auth_time, with a clock skew of 10 seconds by default (at most 5 minutes). The keys are
fetched from
https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com, the
source the Firebase documentation names, and kept for the max-age of the answer.
Errors #
Every call throws a FirebaseAuthAdminException (a FirebaseException of firebase_app_dart).
Its code is the code of the Node.js Admin SDK without the auth/ prefix: user-not-found,
uid-already-exists, email-already-exists, id-token-expired, id-token-revoked,
argument-error, invalid-credential, insufficient-permission, ... It prints as
[auth/user-not-found] .... serverCode keeps the code of the REST API. No message holds a token,
a password or a key.
try {
await app.auth().getUser(uid);
} on FirebaseAuthAdminException catch (e) {
if (e.code != 'user-not-found') rethrow;
}
In Flutter integration tests #
The package has no Flutter dependency, so it runs inside integration_test on a device: the test
creates a user, mints a token, the app signs in with it and runs under the real security rules, and
the test deletes the user.
Where the credential comes from:
- The emulator needs none: pass the host with
--dart-define=FIREBASE_AUTH_EMULATOR_HOST=10.0.2.2:9099(the host machine as the Android emulator sees it;127.0.0.1on iOS simulators and desktop). - A real test project: a CI secret holds the key file, passed to the test build only, for
example
--dart-define=ADMIN_SERVICE_ACCOUNT_B64="$(base64 < key.json | tr -d '\n')". A--dart-definevalue is compiled into that test binary, so never pass it toflutter buildof an app you ship, and never keep the key inassets/. Use a separate test project and a service account with onlyroles/firebaseauth.adminthere.
example/integration_test_helper.dart is a small helper
(TestUsers) that builds the app from those values, creates users and deletes them. A test with
it:
// integration_test/signed_in_test.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'integration_test_helper.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
final TestUsers? users = TestUsers.fromEnvironment(projectId: 'demo-test');
tearDownAll(() => users?.deleteAll());
testWidgets('a tester sees the tester screen', (WidgetTester tester) async {
final (:String uid, :String customToken) = await users!.create(
claims: <String, Object?>{'role': 'tester'},
);
await FirebaseAuth.instance.signInWithCustomToken(customToken);
// ... pump the app and check what a user with the role "tester" sees.
expect(FirebaseAuth.instance.currentUser?.uid, uid);
}, skip: users == null);
}
Security #
- Never ship a service account key in an app, a web page, a Flutter asset or a repository. Prefer
Application Default Credentials and
signBlobon Google Cloud; a key file is for machines you control. Whoever holds the key can mint a token for any uid and so act as any user. - Admin calls and service account tokens bypass security rules. The rules apply again only to a client that signs in as a user, for example with a custom token.
- A custom token lives at most one hour and is only for
signInWithCustomToken; the ID token it yields lives one hour and the client refreshes it. Do not log either. revokeRefreshTokensdoes not end ID tokens already issued: they stay valid until they expire unless the verifier passescheckRevoked: true(one more request per call) or the rules compareauth_time. The cut-off has second precision.- Custom claims are at most 1000 bytes and appear in every ID token: no secrets, no large data.
signBlobcounts against 60,000 signing requests per project per minute and adds a request per token: reuse a token within its hour where you can.- Clock skew: custom tokens are issued at the current time, and
verifyIdTokenallows 10 seconds of skew by default. Keep the clocks of your machines in sync. - The package compiles for the web, but a browser is not a place for admin credentials, and Google's endpoints may refuse cross-origin calls anyway.
Checking a real project #
example/production_check.dart runs the main path against a
real project, for you to run by hand (no test runs it). It creates one user with a random uid,
mints a token with claims whose JSON forces + and / in standard base64, signs in with
firebase_auth_providers_dart, checks that the claims arrive in the ID token, verifies the ID token
with Google's keys and checkRevoked, sets and reads custom claims, revokes and checks that the old
token is rejected with id-token-revoked, and deletes the user (again in finally if a step
failed). It prints PASS or FAIL and never a token or a key. Use a test project.
Keyless (no key file anywhere): admin calls use the Application Default Credentials of the person signed in with gcloud, and custom tokens are signed by the IAM Credentials API as a service account that person may impersonate. One-time setup, with your own project id and account:
gcloud services enable iamcredentials.googleapis.com --project my-project
gcloud iam service-accounts create admin-check --project my-project
gcloud projects add-iam-policy-binding my-project \
--member serviceAccount:admin-check@my-project.iam.gserviceaccount.com \
--role roles/firebaseauth.admin
gcloud iam service-accounts add-iam-policy-binding \
admin-check@my-project.iam.gserviceaccount.com --project my-project \
--member user:you@example.com --role roles/iam.serviceAccountTokenCreator
gcloud auth application-default login
Then:
ADMIN_CHECK_PROJECT_ID=my-project \
ADMIN_CHECK_SERVICE_ACCOUNT=admin-check@my-project.iam.gserviceaccount.com \
FIREBASE_WEB_API_KEY=<the Web API key of the same project> \
dart run example/production_check.dart
GOOGLE_APPLICATION_CREDENTIALS must be unset. The admin calls run as you, so your account needs
the right to manage Firebase Auth users in the project (Firebase Authentication Admin, or Owner).
The check passes the project as quotaProjectId (X-Goog-User-Project), which Google APIs may ask
for with the credentials of a person. A role granted seconds before can still be refused: a first
run that fails at signBlob with HTTP 403 iam.serviceAccounts.signBlob denied passed when run
again 25 seconds later, once IAM had propagated the grant.
With a key file: admin calls use the key, and custom tokens are signed locally with its private
key. The key's service account needs roles/firebaseauth.admin.
GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json \
FIREBASE_WEB_API_KEY=<the Web API key of the same project> \
dart run example/production_check.dart
Add ADMIN_CHECK_SIGN_BLOB=1 to also sign through the IAM Credentials API with Application Default
Credentials, as the key's service account (the IAM Credentials API enabled, and Service Account
Token Creator for that account on itself).
What ran against a real project on 2026-11-18, with this package at main and
firebase_auth_providers_dart 0.2.1 as the client: the keyless mode, all seven steps passed. Not run
against a real project yet: signing locally with a key (AdminCredential.serviceAccount, the
key-file mode above), findUserByEmail, updateUser, deleteUsers batches, tenants, the email
lookup of the default service account on the metadata server of Google Cloud, and user credentials
without a quota project. These are covered by unit tests and the Auth emulator only.
What it does not do #
This is a small subset of the Firebase Admin SDKs. For anything else use the official
firebase_admin_sdk (Dart, VM only) or a Firebase
Admin SDK in another language: session cookies, listUsers, getUsers, lookups by phone or
provider, user import with password hashes, email action links, OIDC and SAML provider configs,
tenant management, project config, multi-factor fields, and every other Firebase service.
docs/guides/admin.md in the repository compares the options.
License #
Apache-2.0. See LICENSE and NOTICE.