kiarina-lib-firebase 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. kiarina_lib_firebase-2.0.0/.env.sample +1 -0
  2. kiarina_lib_firebase-2.0.0/.gitignore +35 -0
  3. kiarina_lib_firebase-2.0.0/.python-version +1 -0
  4. kiarina_lib_firebase-2.0.0/.vscode/settings.json +7 -0
  5. kiarina_lib_firebase-2.0.0/CHANGELOG.md +71 -0
  6. kiarina_lib_firebase-2.0.0/PKG-INFO +372 -0
  7. kiarina_lib_firebase-2.0.0/README.md +344 -0
  8. kiarina_lib_firebase-2.0.0/pyproject.toml +50 -0
  9. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/__init__.py +65 -0
  10. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_exceptions/firebase_api_error.py +16 -0
  11. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_exceptions/firebase_auth_error.py +4 -0
  12. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_exceptions/invalid_custom_token_error.py +7 -0
  13. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_exceptions/invalid_refresh_token_error.py +7 -0
  14. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_schemas/token_data.py +43 -0
  15. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_services/token_manager.py +109 -0
  16. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_settings.py +26 -0
  17. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_types/token_data_cache.py +36 -0
  18. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_utils/exchange_custom_token.py +54 -0
  19. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/_utils/refresh_id_token.py +57 -0
  20. kiarina_lib_firebase-2.0.0/src/kiarina/lib/firebase/py.typed +0 -0
  21. kiarina_lib_firebase-2.0.0/test_settings.sample.yaml +12 -0
  22. kiarina_lib_firebase-2.0.0/tests/__init__.py +0 -0
  23. kiarina_lib_firebase-2.0.0/tests/_services/__init__.py +0 -0
  24. kiarina_lib_firebase-2.0.0/tests/_services/test_token_manager.py +78 -0
  25. kiarina_lib_firebase-2.0.0/tests/_utils/__init__.py +0 -0
  26. kiarina_lib_firebase-2.0.0/tests/_utils/test_exchange_custom_token.py +58 -0
  27. kiarina_lib_firebase-2.0.0/tests/_utils/test_refresh_id_token.py +64 -0
  28. kiarina_lib_firebase-2.0.0/tests/conftest.py +80 -0
@@ -0,0 +1 @@
1
+ KIARINA_LIB_FIREBASE_TEST_SETTINGS_FILE=/path/to/your/test_settings.yaml
@@ -0,0 +1,35 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.so
5
+ *.egg-info/
6
+ dist/
7
+ build/
8
+ .ruff_cache/
9
+ .mypy_cache/
10
+ .pytest_cache/
11
+ .coverage
12
+ coverage.xml
13
+ htmlcov/
14
+
15
+ # uv
16
+ .uv_cache/
17
+
18
+ # Virtual environments & config
19
+ .venv/
20
+ .env
21
+
22
+ # OS
23
+ .DS_Store
24
+
25
+ # Project specific
26
+ *.log
27
+ tmp/
28
+ packages/*/test_settings.yaml
29
+
30
+ # Test data
31
+ tests/data/large/
32
+
33
+ # mise tasks (always include)
34
+ !.mise/tasks/
35
+ !.mise/tasks/**
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,7 @@
1
+ {
2
+ "python.testing.pytestArgs": [
3
+ "tests"
4
+ ],
5
+ "python.testing.unittestEnabled": false,
6
+ "python.testing.pytestEnabled": true
7
+ }
@@ -0,0 +1,71 @@
1
+ # Changelog
2
+
3
+ All notable changes to kiarina-lib-firebase will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [2.0.0] - 2026-06-10
11
+
12
+ ### Changed
13
+ - No changes
14
+
15
+ ## [1.37.0] - 2026-05-27
16
+
17
+ ### Changed
18
+ - No changes
19
+
20
+ ## [1.35.0] - 2026-01-31
21
+
22
+ ### Added
23
+ - `TokenDataCache` protocol for persistent token storage implementations
24
+ - `TokenManager` now supports `token_data_cache` parameter for automatic token persistence
25
+ - Automatic token data loading from cache on first `get_id_token()` call
26
+ - Automatic token data saving to cache after refresh
27
+
28
+ ### Changed
29
+ - `TokenManager.refresh_token` and `TokenManager.token_data` are now properties that raise `AssertionError` if accessed before initialization
30
+
31
+ ## [1.34.0] - 2026-01-31
32
+
33
+ ### Changed
34
+ - **BREAKING**: Renamed `TokenResponse` to `TokenData` for better semantic clarity
35
+ - **BREAKING**: Changed `TokenData.expires_in` field to `TokenData.expires_at` (datetime) for improved usability
36
+ - **BREAKING**: Changed `TokenManager.__init__` to use keyword-only arguments with `api_key` required and either `refresh_token` or `token_data` required
37
+ - **BREAKING**: Changed `TokenManager.refresh()` return type from `TokenResponse` to `TokenData`
38
+
39
+ ### Added
40
+ - `TokenData.from_api_response()` classmethod for creating TokenData from Firebase API responses
41
+ - Field order in `TokenData`: `refresh_token`, `id_token`, `expires_at` for better readability
42
+
43
+ ### Fixed
44
+ - Fixed bug where `expires_at` property calculated incorrect expiration time on each access
45
+
46
+ ## [1.33.1] - 2026-01-31
47
+
48
+ ### Changed
49
+ - No changes
50
+
51
+ ## [1.33.0] - 2026-01-31
52
+
53
+ ### Changed
54
+ - No changes
55
+
56
+ ## [1.32.0] - 2026-01-30
57
+
58
+ ### Added
59
+ - Initial release with Firebase authentication REST API integration
60
+ - `exchange_custom_token()` function for custom token exchange
61
+ - `refresh_id_token()` function for ID token refresh
62
+ - `TokenManager` service class for automatic ID token lifecycle management
63
+ - `TokenResponse` schema for Firebase token exchange responses
64
+ - `FirebaseAuthSettings` for configuration management
65
+ - Exception classes: `FirebaseAuthError`, `InvalidCustomTokenError`, `InvalidRefreshTokenError`, `FirebaseAPIError`
66
+ - Secure API key management with SecretStr
67
+ - Multi-configuration support via pydantic-settings-manager
68
+ - Thread-safe token refresh with asyncio.Lock
69
+ - Automatic token refresh 5 minutes before expiration
70
+ - Async-only API for modern Python applications
71
+ - Environment variable configuration with `KIARINA_LIB_FIREBASE_AUTH_` prefix
@@ -0,0 +1,372 @@
1
+ Metadata-Version: 2.4
2
+ Name: kiarina-lib-firebase
3
+ Version: 2.0.0
4
+ Summary: Firebase authentication library for kiarina namespace
5
+ Project-URL: Homepage, https://github.com/kiarina/kiarina-python
6
+ Project-URL: Repository, https://github.com/kiarina/kiarina-python
7
+ Project-URL: Issues, https://github.com/kiarina/kiarina-python/issues
8
+ Project-URL: Changelog, https://github.com/kiarina/kiarina-python/blob/main/packages/kiarina-lib-firebase/CHANGELOG.md
9
+ Project-URL: Documentation, https://github.com/kiarina/kiarina-python/tree/main/packages/kiarina-lib-firebase#readme
10
+ Author-email: kiarina <kiarinadawa@gmail.com>
11
+ Maintainer-email: kiarina <kiarinadawa@gmail.com>
12
+ License-Expression: MIT
13
+ Keywords: authentication,client,firebase,pydantic,settings
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Classifier: Typing :: Typed
22
+ Requires-Python: >=3.12
23
+ Requires-Dist: httpx>=0.28.1
24
+ Requires-Dist: pydantic-settings-manager>=3.2.0
25
+ Requires-Dist: pydantic-settings>=2.10.1
26
+ Requires-Dist: pydantic>=2.10.6
27
+ Description-Content-Type: text/markdown
28
+
29
+ # kiarina-lib-firebase
30
+
31
+ Firebase authentication library with REST API integration and automatic token management.
32
+
33
+ ## Purpose
34
+
35
+ `kiarina-lib-firebase` provides a simple and secure way to manage Firebase authentication using REST APIs. This library enables custom token exchange and automatic ID token lifecycle management with configuration management using pydantic-settings-manager.
36
+
37
+ Key features:
38
+ - Custom token exchange for refresh/ID tokens via Firebase REST API
39
+ - Automatic ID token lifecycle management with `TokenManager`
40
+ - Token refresh 5 minutes before expiration
41
+ - Thread-safe token refresh with `asyncio.Lock`
42
+ - Secure API key management with SecretStr
43
+ - Multi-configuration support for different projects/environments
44
+ - Async-only API for modern Python applications
45
+ - Environment variable configuration
46
+
47
+ ## Installation
48
+
49
+ ```bash
50
+ pip install kiarina-lib-firebase
51
+ ```
52
+
53
+ ## Quick Start
54
+
55
+ ### Basic Usage
56
+
57
+ ```python
58
+ from kiarina.lib.firebase import (
59
+ TokenManager,
60
+ exchange_custom_token,
61
+ settings_manager,
62
+ )
63
+
64
+ # Configure settings
65
+ settings_manager.user_config = {
66
+ "default": {
67
+ "project_id": "your-project-id",
68
+ "api_key": "your-firebase-api-key",
69
+ }
70
+ }
71
+
72
+ # Get settings
73
+ settings = settings_manager.get_settings()
74
+ api_key = settings.api_key.get_secret_value()
75
+
76
+ # Exchange custom token for ID and refresh tokens
77
+ custom_token = "your_custom_token_here"
78
+ token_data = await exchange_custom_token(custom_token, api_key)
79
+
80
+ # Create token manager for automatic token refresh
81
+ manager = TokenManager(
82
+ api_key=api_key,
83
+ token_data=token_data,
84
+ )
85
+
86
+ # Get ID token (automatically refreshes when needed)
87
+ id_token = await manager.get_id_token()
88
+ print(f"ID Token: {id_token}")
89
+
90
+ # Use the ID token for Firebase API calls
91
+ # The token will be automatically refreshed 5 minutes before expiration
92
+ ```
93
+
94
+ ### Manual Token Refresh
95
+
96
+ ```python
97
+ from kiarina.lib.firebase import refresh_id_token
98
+
99
+ # Manually refresh ID token using refresh token
100
+ token_data = await refresh_id_token(
101
+ refresh_token="your_refresh_token",
102
+ api_key="your_api_key",
103
+ )
104
+
105
+ print(f"New ID Token: {token_data.id_token}")
106
+ print(f"Expires at: {token_data.expires_at}")
107
+ ```
108
+
109
+ ## API Reference
110
+
111
+ ### Settings
112
+
113
+ #### `FirebaseSettings`
114
+
115
+ Configuration for Firebase authentication.
116
+
117
+ ```python
118
+ from pydantic import SecretStr
119
+ from kiarina.lib.firebase import FirebaseSettings
120
+
121
+ settings = FirebaseSettings(
122
+ project_id="your-project-id",
123
+ api_key=SecretStr("your-firebase-api-key"),
124
+ )
125
+ ```
126
+
127
+ **Fields:**
128
+ - `project_id: str` - Firebase project ID
129
+ - `api_key: SecretStr` - Firebase Web API Key (obtain from Firebase Console)
130
+
131
+ ### Functions
132
+
133
+ #### `exchange_custom_token(custom_token: str, api_key: str) -> TokenData`
134
+
135
+ Exchange a Firebase custom token for an ID token and refresh token.
136
+
137
+ **Parameters:**
138
+ - `custom_token: str` - Firebase custom token (JWT)
139
+ - `api_key: str` - Firebase Web API Key
140
+
141
+ **Returns:**
142
+ - `TokenData` - Contains `refresh_token`, `id_token`, and `expires_at`
143
+
144
+ **Raises:**
145
+ - `InvalidCustomTokenError` - If the custom token is invalid or expired
146
+ - `FirebaseAPIError` - If Firebase API returns an error
147
+
148
+ #### `refresh_id_token(refresh_token: str, api_key: str) -> TokenData`
149
+
150
+ Refresh ID token using refresh token.
151
+
152
+ **Parameters:**
153
+ - `refresh_token: str` - Firebase refresh token
154
+ - `api_key: str` - Firebase Web API Key
155
+
156
+ **Returns:**
157
+ - `TokenData` - Contains new `refresh_token`, `id_token`, and `expires_at`
158
+
159
+ **Raises:**
160
+ - `InvalidRefreshTokenError` - If refresh token is invalid or expired
161
+ - `FirebaseAPIError` - If Firebase API returns an error
162
+
163
+ ### Classes
164
+
165
+ #### `TokenManager`
166
+
167
+ Service class for automatic ID token lifecycle management.
168
+
169
+ ```python
170
+ from kiarina.lib.firebase import TokenManager, TokenData
171
+
172
+ # Option 1: With token_data (recommended)
173
+ manager = TokenManager(
174
+ api_key="your_api_key",
175
+ token_data=token_data,
176
+ refresh_buffer_seconds=300, # Default: 5 minutes
177
+ )
178
+
179
+ # Option 2: With refresh_token only
180
+ manager = TokenManager(
181
+ api_key="your_api_key",
182
+ refresh_token="your_refresh_token",
183
+ refresh_buffer_seconds=300, # Default: 5 minutes
184
+ )
185
+
186
+ # Option 3: With token_data_cache for persistent storage
187
+ manager = TokenManager(
188
+ api_key="your_api_key",
189
+ token_data_cache=my_cache_implementation,
190
+ refresh_buffer_seconds=300, # Default: 5 minutes
191
+ )
192
+ ```
193
+
194
+ **Constructor Parameters (all keyword-only):**
195
+ - `api_key: str` - **Required.** Firebase Web API Key
196
+ - `refresh_token: str | None` - Firebase refresh token (at least one of `refresh_token`, `token_data`, or `token_data_cache` is required)
197
+ - `token_data: TokenData | None` - Initial token data (at least one of `refresh_token`, `token_data`, or `token_data_cache` is required)
198
+ - `token_data_cache: TokenDataCache | None` - Cache implementation for persistent token storage (at least one of `refresh_token`, `token_data`, or `token_data_cache` is required)
199
+ - `refresh_buffer_seconds: int` - Refresh buffer time in seconds (default: 300)
200
+
201
+ **Methods:**
202
+ - `async get_id_token() -> str` - Get current ID token (auto-refreshes if needed)
203
+ - `async refresh() -> TokenData` - Manually refresh ID token
204
+
205
+ **Properties:**
206
+ - `id_token: str` - Current ID token
207
+ - `expires_at: datetime` - Token expiration time (UTC)
208
+
209
+ #### `TokenData`
210
+
211
+ Schema for Firebase authentication token data.
212
+
213
+ **Fields:**
214
+ - `refresh_token: str` - Refresh token for getting new ID tokens
215
+ - `id_token: str` - Firebase ID token (JWT)
216
+ - `expires_at: datetime` - ID token expiration time (UTC)
217
+
218
+ **Class Methods:**
219
+ - `from_api_response(id_token: str, refresh_token: str, expires_in: int, *, issued_at: datetime | None = None) -> TokenData` - Create TokenData from Firebase API response
220
+
221
+ #### `TokenDataCache`
222
+
223
+ Protocol for token data cache implementations.
224
+
225
+ Implementations should provide persistent storage for `TokenData`, allowing `TokenManager` to automatically save and restore token state.
226
+
227
+ ```python
228
+ from kiarina.lib.firebase import TokenDataCache, TokenData
229
+
230
+ class MyTokenCache(TokenDataCache):
231
+ async def get(self) -> TokenData:
232
+ # Load token data from persistent storage
233
+ ...
234
+
235
+ async def set(self, token_data: TokenData) -> None:
236
+ # Save token data to persistent storage
237
+ ...
238
+
239
+ # Use with TokenManager
240
+ manager = TokenManager(
241
+ api_key="your_api_key",
242
+ token_data_cache=MyTokenCache(),
243
+ )
244
+ ```
245
+
246
+ **Methods:**
247
+ - `async get() -> TokenData` - Retrieve cached token data
248
+ - `async set(token_data: TokenData) -> None` - Store token data in cache
249
+
250
+ ### Exceptions
251
+
252
+ #### `FirebaseAuthError`
253
+
254
+ Base exception for Firebase Auth errors.
255
+
256
+ #### `InvalidCustomTokenError`
257
+
258
+ Raised when custom token is invalid or expired.
259
+
260
+ #### `InvalidRefreshTokenError`
261
+
262
+ Raised when refresh token is invalid or expired.
263
+
264
+ #### `FirebaseAPIError`
265
+
266
+ Raised when Firebase API returns an error response.
267
+
268
+ **Attributes:**
269
+ - `status_code: int | None` - HTTP status code
270
+ - `error_code: str | None` - Firebase error code
271
+
272
+ ## Configuration
273
+
274
+ ### YAML Configuration
275
+
276
+ ```yaml
277
+ kiarina.lib.firebase:
278
+ default:
279
+ project_id: your-project-id
280
+ api_key: your-firebase-api-key
281
+
282
+ production:
283
+ project_id: prod-project-id
284
+ api_key: ${FIREBASE_API_KEY} # From environment variable
285
+ ```
286
+
287
+ ### Environment Variables
288
+
289
+ Settings can be configured via environment variables with the `KIARINA_LIB_FIREBASE_` prefix:
290
+
291
+ ```bash
292
+ export KIARINA_LIB_FIREBASE_PROJECT_ID=your-project-id
293
+ export KIARINA_LIB_FIREBASE_API_KEY=your-firebase-api-key
294
+ ```
295
+
296
+ ### Multi-Configuration Support
297
+
298
+ ```python
299
+ from kiarina.lib.firebase import settings_manager
300
+
301
+ # Configure multiple environments
302
+ settings_manager.user_config = {
303
+ "development": {
304
+ "project_id": "dev-project",
305
+ "api_key": "dev-api-key",
306
+ },
307
+ "production": {
308
+ "project_id": "prod-project",
309
+ "api_key": "prod-api-key",
310
+ },
311
+ }
312
+
313
+ # Get settings for specific environment
314
+ dev_settings = settings_manager.get_settings("development")
315
+ prod_settings = settings_manager.get_settings("production")
316
+ ```
317
+
318
+ ## Testing
319
+
320
+ This package includes integration tests that require Firebase Admin SDK and Google Cloud authentication.
321
+
322
+ ### Setup
323
+
324
+ 1. Create a test settings file:
325
+
326
+ ```yaml
327
+ # test_settings.yaml
328
+ kiarina.lib.google:
329
+ default:
330
+ type: service_account
331
+ project_id: your-project-id
332
+ service_account_email: your-service-account@your-project.iam.gserviceaccount.com
333
+ service_account_file: ~/.gcp/service-account/your-project/key.json
334
+
335
+ kiarina.lib.firebase:
336
+ default:
337
+ project_id: your-project-id
338
+ api_key: your-firebase-api-key
339
+ ```
340
+
341
+ 2. Set environment variable:
342
+
343
+ ```bash
344
+ export KIARINA_LIB_FIREBASE_TEST_SETTINGS_FILE=/path/to/test_settings.yaml
345
+ ```
346
+
347
+ 3. Run tests:
348
+
349
+ ```bash
350
+ pytest tests/
351
+ ```
352
+
353
+ ## Dependencies
354
+
355
+ - `httpx>=0.28.1` - Async HTTP client for Firebase REST API
356
+ - `pydantic>=2.10.6` - Data validation and settings management
357
+ - `pydantic-settings>=2.10.1` - Settings management from environment
358
+ - `pydantic-settings-manager>=2.3.0` - Multi-configuration settings management
359
+
360
+ ### Development Dependencies
361
+
362
+ - `firebase-admin>=6.6.0` - Firebase Admin SDK (for testing)
363
+ - `kiarina-lib-google>=1.22.0` - Google Cloud authentication (for testing)
364
+
365
+ ## License
366
+
367
+ This project is licensed under the MIT License.
368
+
369
+ ## Related Projects
370
+
371
+ - [kiarina-lib-google](https://github.com/kiarina/kiarina-python/tree/main/packages/kiarina-lib-google) - Google Cloud authentication library
372
+ - [pydantic-settings-manager](https://github.com/kiarina/pydantic-settings-manager) - Multi-configuration settings management