kiarina-lib-google-auth
A Python library for Google Cloud authentication with configuration management using pydantic-settings-manager.
Features
- Multiple Authentication Methods: Default credentials (ADC), service accounts, and user accounts
- Service Account Impersonation: Delegated access with configurable scopes
- Configuration Management: Flexible configuration with pydantic-settings-manager
- Credentials Caching: Automatic caching and refresh for user accounts
- Self-Signed JWT: Generate JWTs for service account authentication
- Type Safety: Full type hints and Pydantic validation
Installation
pip install kiarina-lib-google-auth
Quick Start
Default Credentials (ADC)
from kiarina.lib.google.auth import get_credentials
# Uses Application Default Credentials
credentials = get_credentials()
Service Account
from kiarina.lib.google.auth import get_credentials, GoogleAuthSettings
# From key file
credentials = get_credentials(
settings=GoogleAuthSettings(
type="service_account",
service_account_file="~/path/to/key.json"
)
)
# From JSON data
credentials = get_credentials(
settings=GoogleAuthSettings(
type="service_account",
service_account_data='{"type":"service_account",...}'
)
)
User Account (OAuth2)
# From authorized user file
credentials = get_credentials(
settings=GoogleAuthSettings(
type="user_account",
authorized_user_file="~/.config/gcloud/application_default_credentials.json",
scopes=["https://www.googleapis.com/auth/drive"]
)
)
Service Account Impersonation
# Impersonate a service account
credentials = get_credentials(
settings=GoogleAuthSettings(
type="service_account",
service_account_file="~/source-key.json",
impersonate_service_account="target@project.iam.gserviceaccount.com",
scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
)
Note: Source principal requires roles/iam.serviceAccountTokenCreator role.
Credentials Caching
from kiarina.lib.google.auth import CredentialsCache
class InMemoryCache(CredentialsCache):
def __init__(self):
self._cache: str | None = None
def get(self) -> str | None:
return self._cache
def set(self, value: str) -> None:
self._cache = value
# Use cache for user account credentials
credentials = get_credentials(
settings=GoogleAuthSettings(
type="user_account",
authorized_user_file="~/authorized-user.json",
scopes=["https://www.googleapis.com/auth/drive"]
),
cache=InMemoryCache()
)
Self-Signed JWT
from kiarina.lib.google.auth import get_self_signed_jwt
jwt_token = get_self_signed_jwt(
settings=GoogleAuthSettings(
type="service_account",
service_account_file="~/key.json"
),
audience="https://your-service.example.com/"
)
Configuration
YAML Configuration (Recommended)
kiarina.lib.google.auth:
development:
type: user_account
authorized_user_file: ~/.config/gcloud/application_default_credentials.json
scopes:
- https://www.googleapis.com/auth/cloud-platform
production:
type: service_account
service_account_file: /secrets/prod-sa-key.json
project_id: your-project-id
scopes:
- https://www.googleapis.com/auth/cloud-platform
impersonation:
type: service_account
service_account_file: ~/source-key.json
impersonate_service_account: target@project.iam.gserviceaccount.com
scopes:
- https://www.googleapis.com/auth/cloud-platform
Load configuration:
from pydantic_settings_manager import load_user_configs
import yaml
with open("config.yaml") as f:
config = yaml.safe_load(f)
load_user_configs(config)
# Use configured credentials
from kiarina.lib.google.auth import get_credentials
credentials = get_credentials("production")
Environment Variables
export KIARINA_LIB_GOOGLE_AUTH_TYPE="service_account"
export KIARINA_LIB_GOOGLE_AUTH_SERVICE_ACCOUNT_FILE="~/key.json"
export KIARINA_LIB_GOOGLE_AUTH_PROJECT_ID="your-project-id"
export KIARINA_LIB_GOOGLE_AUTH_SCOPES="https://www.googleapis.com/auth/cloud-platform"
Programmatic Configuration
from kiarina.lib.google.auth import settings_manager
settings_manager.user_config = {
"dev": {
"type": "user_account",
"authorized_user_file": "~/.config/gcloud/application_default_credentials.json"
},
"prod": {
"type": "service_account",
"service_account_file": "/secrets/key.json"
}
}
settings_manager.active_key = "prod"
credentials = get_credentials()
API Reference
Main Functions
get_credentials(settings_key=None, *, settings=None, scopes=None, cache=None)
Get Google Cloud credentials based on configuration.
Parameters:
settings_key(str | None): Configuration key for multi-config setupsettings(GoogleAuthSettings | None): Settings object (overrides settings_key)scopes(list[str] | None): OAuth2 scopes (overrides settings.scopes)cache(CredentialsCache | None): Credentials cache for user accounts
Returns: Credentials - Google Cloud credentials
get_self_signed_jwt(settings_key=None, *, settings=None, audience)
Generate a self-signed JWT for service account authentication.
Parameters:
settings_key(str | None): Configuration keysettings(GoogleAuthSettings | None): Settings objectaudience(str): JWT audience (target service URL)
Returns: str - Self-signed JWT token
Utility Functions
get_default_credentials()
Get default credentials using Application Default Credentials (ADC).
Returns: Credentials
get_service_account_credentials(*, service_account_file=None, service_account_data=None)
Get service account credentials from file or data.
Returns: google.oauth2.service_account.Credentials
get_user_account_credentials(*, authorized_user_file=None, authorized_user_data=None, scopes, cache=None)
Get user account credentials from file or data with optional caching.
Returns: google.oauth2.credentials.Credentials
Configuration
GoogleAuthSettings
Pydantic settings model for authentication configuration.
Key Fields:
type: Authentication type ("default","service_account","user_account")service_account_file: Path to service account key fileservice_account_data: Service account key data (JSON string, SecretStr)authorized_user_file: Path to authorized user fileauthorized_user_data: Authorized user data (JSON string, SecretStr)impersonate_service_account: Target service account email for impersonationscopes: OAuth2 scopes (default: cloud-platform, drive, spreadsheets)project_id: GCP project ID
Helper Methods:
get_service_account_data(): Parse service_account_data JSONget_client_secret_data(): Parse client_secret_data JSONget_authorized_user_data(): Parse authorized_user_data JSON
CredentialsCache (Protocol)
Protocol for implementing credentials cache.
Methods:
get() -> str | None: Retrieve cached credentials (JSON string)set(value: str) -> None: Store credentials (JSON string)
Authentication Priority
Default Credentials
Uses Application Default Credentials (ADC) in this order:
GOOGLE_APPLICATION_CREDENTIALSenvironment variable (service account)gcloud auth application-default logincredentials (user account)- Compute Engine metadata server (compute engine)
Default Scopes
https://www.googleapis.com/auth/cloud-platform- All GCP resourceshttps://www.googleapis.com/auth/drive- Google Drivehttps://www.googleapis.com/auth/spreadsheets- Google Sheets
Override by specifying custom scopes in configuration or function call.
Testing
Setup Test Configuration
# Copy sample configuration
cp packages/kiarina-lib-google-auth/test_settings.sample.yaml \
packages/kiarina-lib-google-auth/test_settings.yaml
# Edit with your credentials
# Set environment variable
export KIARINA_LIB_GOOGLE_AUTH_TEST_SETTINGS_FILE="packages/kiarina-lib-google-auth/test_settings.yaml"
Run Tests
# Run all checks
mise run package kiarina-lib-google-auth
# Run tests with coverage
mise run package:test kiarina-lib-google-auth --coverage
Dependencies
- google-api-python-client - Google API client
- pydantic-settings - Settings management
- pydantic-settings-manager - Advanced settings management
License
MIT License - see the LICENSE file for details.
Related Projects
- kiarina-python - Main monorepo
- pydantic-settings-manager - Configuration management library
Metadata
Release files for kiarina-lib-google-auth 1.37.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kiarina_lib_google_auth-1.37.0.tar.gz | 10.5 kB | Without provenance | Details for kiarina_lib_google_auth-1.37.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kiarina_lib_google_auth-1.37.0-py3-none-any.whl | Python 3 | none | any | Details for kiarina_lib_google_auth-1.37.0-py3-none-any.whl |
Total release size: 21.4 kB
Release files / kiarina_lib_google_auth-1.37.0.tar.gz
| Download URL | kiarina_lib_google_auth-1.37.0.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ec609d2f74d34b6b4a71d74a1edb17f9cafa525b0ebe67d77584cb4b80acd5b0
|
|
BLAKE2b-256 checksum How to use checksums |
afe7bc141eb5bfa6008923593837e0063699201c3d608e98413aa11b998fab13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / kiarina_lib_google_auth-1.37.0-py3-none-any.whl
| Download URL | kiarina_lib_google_auth-1.37.0-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b2f6195b8c0b7d695d32b200036861185ed120ae1a05d77b883bcf41d0904414
|
|
BLAKE2b-256 checksum How to use checksums |
919d5f53f6e6c0c0aad156b39e6c1a66c68a6189bc9067c418e1d1424a37f776
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Security for kiarina-lib-google-auth 1.37.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.