Path: redb-route/redb.Route.Firebase/Auth/FirebaseCredentialProvider.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Firebase/Auth/FirebaseCredentialProvider.cs ---- using System.Collections.Concurrent; using FirebaseAdmin; using Google.Api.Gax; using Google.Apis.Auth.OAuth2; using Google.Cloud.Firestore; using Google.Cloud.Storage.V1; namespace redb.Route.Firebase; /// /// Resolves Firebase credentials from: /// 1. Explicit JSON path (credentialPath argument, else ) /// 2. GOOGLE_APPLICATION_CREDENTIALS env var (Application Default Credentials) /// 3. Emulator env vars — FIRESTORE_EMULATOR_HOST (Firestore), /// STORAGE_EMULATOR_HOST (Storage) — via . /// /// Firebase apps are created as NAMED instances (never the process-global [DEFAULT]), /// so the connector coexists with a host that initialized Firebase Admin SDK itself, and one /// process can serve several service accounts. Apps are cached per /// (credentialPath, projectId), Firestore clients per (projectId, credentialPath), /// storage clients per credentialPath. Thread-safe. /// /// /// Public so a named instance can be put into the context registry and referenced from /// endpoint URIs via connectionFactory=…: /// /// context.AddToRegistry("myFirebase", new FirebaseCredentialProvider /// { /// DefaultProjectId = "my-project", /// DefaultCredentialPath = "/secrets/firebase-sa.json", /// }); /// // fstore://orders?connectionFactory=myFirebase /// /// /// public sealed class FirebaseCredentialProvider : IFirebaseCredentialProvider, IDisposable { private readonly ConcurrentDictionary> _apps = new(); private readonly ConcurrentDictionary> _firestoreDbs = new(); private readonly ConcurrentDictionary> _storageClients = new(); private readonly string _instanceId = Guid.NewGuid().ToString("N")[..8]; private int _appCounter; /// Project ID used when the endpoint does not specify one. public string? DefaultProjectId { get; init; } /// Service-account JSON path used when the endpoint does not specify one. public string? DefaultCredentialPath { get; init; } /// public FirebaseApp GetOrCreateApp(string? credentialPath = null, string? projectId = null) { var credPath = credentialPath ?? DefaultCredentialPath; var pid = projectId ?? DefaultProjectId; // Lazy inside GetOrAdd so a racing second factory never creates a throwaway app. return _apps.GetOrAdd($"{credPath}\n{pid}", _ => new Lazy(() => { var options = new AppOptions { Credential = credPath is not null ? LoadCredential(credPath) : GoogleCredential.GetApplicationDefault(), }; if (!string.IsNullOrWhiteSpace(pid)) options.ProjectId = pid; // Named app: the host's [DEFAULT] instance is never touched. var name = $"redb.route:{_instanceId}:{Interlocked.Increment(ref _appCounter)}"; return FirebaseApp.Create(options, name); })).Value; } /// public FirestoreDb GetFirestoreDb(string? projectId = null, string? credentialPath = null, string? databaseId = null) { var credPath = credentialPath ?? DefaultCredentialPath; var pid = projectId ?? DefaultProjectId ?? Environment.GetEnvironmentVariable("FIREBASE_PROJECT") ?? throw new InvalidOperationException( "Firebase project id is not configured. Set it on the endpoint (?projectId=...), " + "in AddRedbRouteFirebase(o => o.ProjectId = ...), on the credential provider " + "(DefaultProjectId), or via the FIREBASE_PROJECT environment variable."); // Lazy inside GetOrAdd so a racing second factory never builds a throwaway client. return _firestoreDbs.GetOrAdd( $"{pid}\n{credPath}\n{databaseId}", _ => new Lazy(() => { var builder = new FirestoreDbBuilder { ProjectId = pid, DatabaseId = databaseId, EmulatorDetection = EmulatorDetection.EmulatorOrProduction, }; if (credPath is not null) builder.GoogleCredential = LoadCredential(credPath); return builder.Build(); })).Value; } /// public StorageClient GetStorageClient(string? credentialPath = null) { var credPath = credentialPath ?? DefaultCredentialPath; return _storageClients.GetOrAdd( credPath ?? "", _ => new Lazy(() => { var builder = new StorageClientBuilder { EmulatorDetection = EmulatorDetection.EmulatorOrProduction, }; if (credPath is not null) builder.GoogleCredential = LoadCredential(credPath); return builder.Build(); })).Value; } /// public UrlSigner GetUrlSigner(string? credentialPath = null) { var credPath = credentialPath ?? DefaultCredentialPath ?? Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") ?? throw new InvalidOperationException( "Signed URLs require a service-account JSON with a private key to sign with — " + "set credentialPath on the endpoint, DefaultCredentialPath on the provider, " + "or GOOGLE_APPLICATION_CREDENTIALS. Plain ADC cannot sign."); return _urlSigners.GetOrAdd( credPath, _ => new Lazy(() => UrlSigner.FromCredential(LoadCredential(credPath)))).Value; } private readonly ConcurrentDictionary> _urlSigners = new(); private static GoogleCredential LoadCredential(string credentialPath) { // GoogleCredential.FromStream is deprecated in favor of CredentialFactory, // but CredentialFactory is not available in all target framework versions. #pragma warning disable CS0618 using var stream = File.OpenRead(credentialPath); return GoogleCredential.FromStream(stream); #pragma warning restore CS0618 } /// public void Dispose() { foreach (var client in _storageClients.Values) { if (client.IsValueCreated) client.Value.Dispose(); } // Delete only OUR named apps — a host-owned [DEFAULT] is never ours. foreach (var app in _apps.Values) { if (app.IsValueCreated) app.Value.Delete(); } } }