Skip to main content

firebase_admin_sdk/
lib.rs

1//! # Firebase Admin SDK for Rust
2//!
3//! This crate provides a Rust implementation of the Firebase Admin SDK, allowing interaction
4//! with Firebase services such as Authentication, Cloud Messaging (FCM), Remote Config, and Firestore.
5//!
6//! ## Modules
7//!
8//! - [`auth`]: Firebase Authentication (User management, ID token verification).
9//! - [`messaging`]: Firebase Cloud Messaging (Send messages, topic management).
10//! - [`remote_config`]: Firebase Remote Config (Get, update, rollback configurations).
11//! - [`firestore`]: Cloud Firestore (Read/Write documents).
12//! - [`storage`]: Cloud Storage (Upload, download, delete files).
13//! - [`crashlytics`]: Firebase Crashlytics (Manage crash reports).
14//!
15//! ## Usage
16//!
17//! The entry point is the [`FirebaseApp`] struct. You initialize it with a `ServiceAccountKey`
18//! (typically loaded from a JSON file), and then access the various services.
19//!
20//! ```rust,no_run
21//! use firebase_admin_sdk::{FirebaseApp, yup_oauth2};
22//!
23//! async fn example() {
24//!     let key = yup_oauth2::read_service_account_key("service-account.json").await.unwrap();
25//!
26//!     let app = FirebaseApp::new(key);
27//!     let auth = app.auth();
28//!     let messaging = app.messaging();
29//! }
30//! ```
31
32#[cfg(feature = "auth")]
33pub mod auth;
34pub mod core;
35#[cfg(feature = "crashlytics")]
36pub mod crashlytics;
37#[cfg(feature = "firestore")]
38pub mod firestore;
39#[cfg(feature = "messaging")]
40pub mod messaging;
41#[cfg(feature = "remote_config")]
42pub mod remote_config;
43#[cfg(feature = "storage")]
44pub mod storage;
45
46// Re-export yup_oauth2 for user convenience so they don't need to add it separately
47pub use yup_oauth2;
48
49#[cfg(feature = "auth")]
50use auth::FirebaseAuth;
51use core::middleware::AuthMiddleware;
52#[cfg(feature = "crashlytics")]
53use crashlytics::FirebaseCrashlytics;
54#[cfg(feature = "firestore")]
55use firestore::FirebaseFirestore;
56#[cfg(feature = "messaging")]
57use messaging::FirebaseMessaging;
58#[cfg(feature = "remote_config")]
59use remote_config::FirebaseRemoteConfig;
60#[cfg(feature = "storage")]
61use storage::FirebaseStorage;
62use yup_oauth2::ServiceAccountKey;
63
64/// The entry point for the Firebase Admin SDK.
65///
66/// `FirebaseApp` holds the service account credentials and acts as a factory for creating
67/// clients for specific Firebase services (Auth, Messaging, etc.).
68///
69/// It uses a "synchronous constructor, lazy async authentication" pattern.
70/// The `new` method is synchronous and cheap, while the actual OAuth2 authentication
71/// happens asynchronously and lazily upon the first API request made by any service client.
72pub struct FirebaseApp {
73    middleware: AuthMiddleware,
74}
75
76impl FirebaseApp {
77    /// Creates a new `FirebaseApp` instance.
78    ///
79    /// This method is synchronous. The service account key is stored, but no network
80    /// requests are made until a service (like `auth()` or `messaging()`) actually performs an action.
81    ///
82    /// # Arguments
83    ///
84    /// * `service_account_key` - A `yup_oauth2::ServiceAccountKey` struct containing the credentials.
85    pub fn new(service_account_key: ServiceAccountKey) -> Self {
86        Self {
87            middleware: AuthMiddleware::new(service_account_key),
88        }
89    }
90
91    /// Returns a client for interacting with Firebase Authentication.
92    #[cfg(feature = "auth")]
93    pub fn auth(&self) -> FirebaseAuth {
94        FirebaseAuth::new(self.middleware.clone())
95    }
96
97    /// Returns a client for interacting with Firebase Cloud Messaging (FCM).
98    #[cfg(feature = "messaging")]
99    pub fn messaging(&self) -> FirebaseMessaging {
100        FirebaseMessaging::new(self.middleware.clone())
101    }
102
103    /// Returns a client for interacting with Firebase Remote Config.
104    #[cfg(feature = "remote_config")]
105    pub fn remote_config(&self) -> FirebaseRemoteConfig {
106        FirebaseRemoteConfig::new(self.middleware.clone())
107    }
108
109    /// Returns a client for interacting with Firebase Crashlytics.
110    #[cfg(feature = "crashlytics")]
111    pub fn crashlytics(&self) -> FirebaseCrashlytics {
112        FirebaseCrashlytics::new(self.middleware.clone())
113    }
114
115    /// Returns a client for interacting with Cloud Firestore.
116    #[cfg(feature = "firestore")]
117    pub fn firestore(&self) -> FirebaseFirestore {
118        FirebaseFirestore::new(self.middleware.clone())
119    }
120
121    /// Returns a client for interacting with Firebase Storage.
122    #[cfg(feature = "storage")]
123    pub fn storage(&self) -> FirebaseStorage {
124        FirebaseStorage::new(self.middleware.clone())
125    }
126}