Python client library for APIGEE Service and MOMRAH SSO authentication
Project description
SSO APIGEE Client
A comprehensive Python SDK for integrating with the SSO APIGEE Service, providing JWT-based stateless authentication with MOMRAH SSO and APIGEE API Gateway functionality.
Features
- 🔐 MOMRAH SSO Integration: Complete OAuth2/OIDC authentication flow with JWT tokens
- 🚀 APIGEE API Gateway: Token management and API proxying
- ⚡ FastAPI Integration: Ready-to-use routers and dependencies
- 🔒 JWT Security: Stateless authentication, JWKS validation, secure token handling
- 🛠️ Developer Friendly: Simple API, comprehensive error handling
- 📦 Production Ready: Docker support, health checks, monitoring
- 🏛️ Ministry-Grade: Security features for government applications
Installation
Basic Installation
pip install sso-apigee-client
With FastAPI Support
pip install sso-apigee-client[fastapi]
Development Installation
pip install sso-apigee-client[dev]
Quick Start
1. Basic Usage
from sso_apigee_client import ApigeeClient, MOMRAHSSOClient
# Initialize APIGEE client
apigee_client = ApigeeClient(
base_url="http://localhost:3300",
consumer_key="your_consumer_key",
consumer_secret="your_consumer_secret"
)
# Initialize MOMRAH SSO client
sso_client = MOMRAHSSOClient(
service_url="http://localhost:3300",
redirect_uri="https://myapp.com/auth/callback"
)
# Get APIGEE token
token = await apigee_client.get_token()
print(f"Access token: {token.access_token}")
# Get MOMRAH SSO login URL
login_url = await sso_client.get_login_url()
print(f"Login URL: {login_url}")
2. FastAPI Integration
from fastapi import FastAPI
from sso_apigee_client.fastapi_integration import ApigeeRouter, MOMRAHSSORouter
from sso_apigee_client import ApigeeClient, MOMRAHSSOClient
app = FastAPI()
# Initialize clients
apigee_client = ApigeeClient(
base_url="http://localhost:3300",
consumer_key="your_consumer_key",
consumer_secret="your_consumer_secret"
)
sso_client = MOMRAHSSOClient(
base_url="http://localhost:3300",
client_id="your_client_id",
client_secret="your_client_secret"
)
# Create and include routers
apigee_router = ApigeeRouter(apigee_client)
sso_router = MOMRAHSSORouter(sso_client)
app.include_router(apigee_router, prefix="/api/apigee", tags=["APIGEE"])
app.include_router(sso_router, prefix="/api/sso", tags=["MOMRAH SSO"])
@app.get("/")
async def root():
return {"message": "Welcome to your FastAPI app with SSO APIGEE integration"}
Configuration
Environment Variables
Create a .env file in your project root:
# SSO APIGEE Service Configuration
SSO_APIGEE_SERVICE_URL=http://localhost:3300
# APIGEE Configuration
APIGEE_CONSUMER_KEY=your_apigee_consumer_key
APIGEE_CONSUMER_SECRET=your_apigee_consumer_secret
# MOMRAH SSO Configuration
MOMRAH_CLIENT_ID=your_momrah_client_id
MOMRAH_CLIENT_SECRET=your_momrah_client_secret
Client Configuration
from sso_apigee_client import ApigeeClient, MOMRAHSSOClient
# APIGEE Client with custom configuration
apigee_client = ApigeeClient(
base_url="http://localhost:3300",
consumer_key="your_consumer_key",
consumer_secret="your_consumer_secret",
timeout=30, # Request timeout in seconds
auto_refresh=True # Auto-refresh tokens (default: True)
)
# MOMRAH SSO Client with custom configuration
sso_client = MOMRAHSSOClient(
base_url="http://localhost:3300",
client_id="your_client_id",
client_secret="your_client_secret",
timeout=30, # Request timeout in seconds
session_secret="your_session_secret" # Custom session secret
)
API Reference
ApigeeClient
The ApigeeClient provides functionality for interacting with APIGEE API Gateway.
Methods
get_token(force_refresh=False): Get APIGEE access tokencall_api(method, endpoint, headers=None, data=None): Call APIGEE APIget_token_status(): Get token status and expiry informationhealth_check(): Check APIGEE service healthclose(): Close the client and cleanup resources
Example
# Get access token
token_response = await apigee_client.get_token()
print(f"Access token: {token_response.access_token}")
print(f"Token type: {token_response.token_type}")
print(f"Expires in: {token_response.expires_in} seconds")
# Call APIGEE API
response = await apigee_client.call_api(
method="GET",
endpoint="/api/data",
headers={"Custom-Header": "value"}
)
print(f"API Response: {response}")
# Check token status
status = await apigee_client.get_token_status()
print(f"Token expires at: {status.expires_at}")
print(f"Token is valid: {status.is_valid}")
MOMRAHSSOClient
The MOMRAHSSOClient provides functionality for MOMRAH SSO authentication.
Methods
get_login_url(): Get MOMRAH SSO login URLget_current_user(): Get current authenticated userget_optional_user(): Get current user (returns None if not authenticated)logout(): Logout current userrefresh_token(): Refresh user authentication tokenhealth_check(): Check MOMRAH SSO service health
Example
# Get login URL
login_url = await sso_client.get_login_url()
print(f"Login URL: {login_url}")
# Get current user
user = await sso_client.get_current_user()
if user:
print(f"User: {user.name} ({user.email})")
print(f"User ID: {user.sub}")
print(f"Roles: {user.roles}")
# Logout user
await sso_client.logout()
print("User logged out successfully")
FastAPI Integration
ApigeeRouter
Provides automatic APIGEE token management and API proxying.
Endpoints:
GET /token- Get APIGEE access tokenGET /status- Get token status and expiryPOST /proxy- Proxy API calls to APIGEE
MOMRAHSSORouter
Provides MOMRAH SSO authentication endpoints.
Endpoints:
GET /login- Get MOMRAH SSO login URLGET /callback- Handle OAuth callbackGET /user- Get current authenticated userPOST /logout- Logout current userPOST /refresh- Refresh user token
Example
from fastapi import FastAPI, Depends, HTTPException
from sso_apigee_client.fastapi_integration import ApigeeRouter, MOMRAHSSORouter
from sso_apigee_client import ApigeeClient, MOMRAHSSOClient
from sso_apigee_client.models import UserInfo
app = FastAPI()
# Initialize clients
apigee_client = ApigeeClient(
base_url="http://localhost:3300",
consumer_key="your_consumer_key",
consumer_secret="your_consumer_secret"
)
sso_client = MOMRAHSSOClient(
base_url="http://localhost:3300",
client_id="your_client_id",
client_secret="your_client_secret"
)
# Create routers
apigee_router = ApigeeRouter(apigee_client)
sso_router = MOMRAHSSORouter(sso_client)
# Include routers
app.include_router(apigee_router, prefix="/api/apigee", tags=["APIGEE"])
app.include_router(sso_router, prefix="/api/sso", tags=["MOMRAH SSO"])
# Dependency functions
async def get_current_user() -> UserInfo:
"""Get current authenticated user."""
user = await sso_client.get_current_user()
if not user:
raise HTTPException(status_code=401, detail="Not authenticated")
return user
# Your application routes
@app.get("/")
async def root():
return {"message": "Welcome to your FastAPI app"}
@app.get("/profile")
async def get_profile(user: UserInfo = Depends(get_current_user)):
"""Get user profile."""
return {"user": user.dict()}
@app.get("/apigee-data")
async def get_apigee_data():
"""Get data from APIGEE API."""
token = await apigee_client.get_token()
response = await apigee_client.call_api(
method="GET",
endpoint="/api/data"
)
return {"data": response}
Data Models
UserInfo
Represents a MOMRAH SSO user.
from sso_apigee_client.models import UserInfo
# UserInfo attributes
user.sub # User ID
user.name # Full name
user.email # Email address
user.given_name # First name
user.family_name # Last name
user.roles # List of user roles
user.groups # List of user groups
user.organization # Organization name
user.department # Department name
user.position # Job position
TokenResponse
Represents an APIGEE token response.
from sso_apigee_client.models import TokenResponse
# TokenResponse attributes
token.access_token # Access token
token.token_type # Token type (usually "Bearer")
token.expires_in # Expiration time in seconds
token.scope # Token scope
token.refresh_token # Refresh token (if available)
Error Handling
The SDK provides comprehensive error handling with specific exception types.
Exception Types
ApigeeAuthenticationError: APIGEE authentication failuresMOMRAHSSOError: MOMRAH SSO authentication failuresNetworkError: Network connectivity issuesConfigurationError: Configuration problems
Example
from sso_apigee_client.exceptions import ApigeeAuthenticationError, MOMRAHSSOError
try:
# Get APIGEE token
token = await apigee_client.get_token()
# Call APIGEE API
response = await apigee_client.call_api(
method="GET",
endpoint="/api/data"
)
return response
except ApigeeAuthenticationError as e:
print(f"APIGEE authentication failed: {e}")
# Handle authentication error
except MOMRAHSSOError as e:
print(f"SSO authentication failed: {e}")
# Handle SSO error
except Exception as e:
print(f"Unexpected error: {e}")
# Handle other errors
Advanced Usage
Custom Headers
# Add custom headers to APIGEE API calls
response = await apigee_client.call_api(
method="POST",
endpoint="/api/data",
headers={
"Custom-Header": "value",
"X-Request-ID": "12345"
},
data={"key": "value"}
)
Token Management
# Check token status
status = await apigee_client.get_token_status()
if not status.is_valid:
# Force refresh token
token = await apigee_client.get_token(force_refresh=True)
print("Token refreshed")
Session Management
# Get optional user (doesn't raise exception if not authenticated)
user = await sso_client.get_optional_user()
if user:
print(f"Welcome back, {user.name}")
else:
print("Please log in")
Testing
Unit Tests
import pytest
from sso_apigee_client import ApigeeClient, MOMRAHSSOClient
@pytest.mark.asyncio
async def test_apigee_client():
client = ApigeeClient(
base_url="http://localhost:3300",
consumer_key="test_key",
consumer_secret="test_secret"
)
# Test token retrieval
token = await client.get_token()
assert token.access_token is not None
await client.close()
@pytest.mark.asyncio
async def test_sso_client():
client = MOMRAHSSOClient(
base_url="http://localhost:3300",
client_id="test_client",
client_secret="test_secret"
)
# Test login URL generation
login_url = await client.get_login_url()
assert "login" in login_url
await client.close()
Integration Tests
import pytest
from fastapi.testclient import TestClient
from your_app import app
def test_fastapi_integration():
client = TestClient(app)
# Test health endpoint
response = client.get("/health")
assert response.status_code == 200
# Test APIGEE endpoints
response = client.get("/api/apigee/token")
assert response.status_code == 200
# Test SSO endpoints
response = client.get("/api/sso/login")
assert response.status_code == 200
Production Deployment
Docker
FROM python:3.11-slim
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Install SDK
RUN pip install sso-apigee-client[fastapi]
# Copy application
COPY . .
# Run application
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Environment Configuration
# Production environment variables
SSO_APIGEE_SERVICE_URL=https://your-sso-service.com
APIGEE_CONSUMER_KEY=your_production_key
APIGEE_CONSUMER_SECRET=your_production_secret
MOMRAH_CLIENT_ID=your_production_client_id
MOMRAH_CLIENT_SECRET=your_production_client_secret
Health Checks
@app.get("/health")
async def health_check():
"""Health check endpoint."""
try:
# Check APIGEE service
apigee_health = await apigee_client.health_check()
# Check MOMRAH SSO service
sso_health = await sso_client.health_check()
return {
"status": "healthy",
"apigee": apigee_health,
"sso": sso_health
}
except Exception as e:
return {
"status": "unhealthy",
"error": str(e)
}
Best Practices
- Environment Variables: Store sensitive configuration in environment variables
- Error Handling: Always handle authentication and API errors gracefully
- Token Management: Let the SDK handle token refresh automatically
- Security: Use HTTPS in production
- Logging: Implement proper logging for debugging
- Health Checks: Implement health check endpoints
- Testing: Write comprehensive tests for your integration
- Monitoring: Monitor token usage and API calls
- Caching: Use appropriate caching strategies for tokens
- Documentation: Document your integration patterns
Support
For issues and questions:
- Check the FastAPI Integration Guide
- Review the SSO APIGEE Service logs
- Contact your system administrator
License
MIT License - see LICENSE file for details.
Changelog
Version 1.1.0
- Added MOMRAH SSO integration
- Enhanced FastAPI integration
- Improved error handling
- Added comprehensive documentation
Version 1.0.2
- Initial release with APIGEE support
- Basic FastAPI integration
- Token management
- API proxying
This SDK provides everything you need to integrate with the SSO APIGEE Service. It handles all the complexity of authentication and API management, allowing you to focus on building your application.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sso_apigee_client-2.2.0.tar.gz.
File metadata
- Download URL: sso_apigee_client-2.2.0.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f636361453447f85716de969536e120c1798667904ad329ea478d10689d017fd
|
|
| MD5 |
9b27eb88dca856b6563159173e85f804
|
|
| BLAKE2b-256 |
9dde81d39584aab80b197618f12d1ce9b5a342bd77a23a693ea0ed405609d200
|
File details
Details for the file sso_apigee_client-2.2.0-py3-none-any.whl.
File metadata
- Download URL: sso_apigee_client-2.2.0-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d28a2b788dcb3aa734ff6962569e734f5e3f7e2d247ee8a92e5c33d5169fa8e5
|
|
| MD5 |
ded025d3dd5ad5013d0b023af82d1fc5
|
|
| BLAKE2b-256 |
3464c9bae68428b9e2a68f00e5f5d2fc3d62d56e97fef362007769a57b8f433c
|