Skip to main content

iProov Portal Authentication Library

Project description

iProov Portal Authentication Library

A Python library for authenticating with iProov Portal services using Google OAuth2.

Features

  • Multi-environment support: Production, UAT, and Development environments
  • Token caching: Automatic local caching with smart expiration handling
  • Automatic token refresh: Handles token expiration and renewal
  • Comprehensive error handling: Detailed error messages for debugging
  • Type hints: Full type annotation support
  • Secure by default: Removes sensitive tokens from responses

Installation

pip install iproov-portal-auth

Quick Start

Basic Usage

import os
from iproov_portal_auth import IProovPortalAuth

# Make sure API key is set
os.environ['IPROOV_PORTAL_API_KEY'] = 'your-api-key'

# Initialize with default configuration
auth = IProovPortalAuth()

# Login and get session token
token = auth.login()
print(f"Session token: {token}")

# Get user details
user_details = auth.get_user_details()
print(f"User email: {user_details.get('email')}")
print(f"User name: {user_details.get('displayName')}")

Custom Configuration

from iproov_portal_auth import IProovPortalAuth, AuthConfig, Environment

# Create custom configuration
config = AuthConfig(
    client_id="your-client-id",
    api_key="your-api-key",
    environment=Environment.UAT,
    timeout=30.0
)

# Initialize with custom config
auth = IProovPortalAuth(config=config)

Environment Variables

You can configure the library using environment variables:

export IPROOV_PORTAL_API_KEY="your-api-key"  # REQUIRED
export IPROOV_PORTAL_CLIENT_ID="your-client-id"  # Optional, uses environment defaults
export IPROOV_PORTAL_ENV="uat"  # prod, uat, or dev
export IPROOV_PORTAL_TIMEOUT="20.0"

Note: The IPROOV_PORTAL_API_KEY is required. The IPROOV_PORTAL_CLIENT_ID is optional and will use environment-specific defaults if not provided.

API Reference

IProovPortalAuth Class

Methods

login(force_refresh: bool = False) -> str

Authenticate and get a session token.

Parameters:

  • force_refresh: If True, forces a new token even if cached token exists

Returns:

  • Session token string

Raises:

  • AuthenticationError: If authentication fails
  • NetworkError: If network request fails
get_user_details(force_refresh: bool = False) -> Dict[str, Any]

Get user details from the verify endpoint.

Parameters:

  • force_refresh: If True, forces a fresh request even if data is cached

Returns:

  • Dictionary containing user details
get_email() -> Optional[str]

Get user email address.

Returns:

  • User email address or None if not available
get_name() -> Optional[str]

Get user display name.

Returns:

  • User display name or None if not available
get_picture() -> Optional[str]

Get user profile picture URL.

Returns:

  • User profile picture URL or None if not available
get_roles() -> List[str]

Get user roles.

Returns:

  • List of user roles
logout() -> None

Logout and clear cached tokens.

is_authenticated() -> bool

Check if user is authenticated.

Returns:

  • True if user has valid authentication, False otherwise

Configuration

AuthConfig Class

@dataclass
class AuthConfig:
    client_id: str
    api_key: str
    environment: Environment = Environment.PRODUCTION
    timeout: float = 20.0

Environment Enum

class Environment(Enum):
    PRODUCTION = "prod"
    UAT = "uat"
    DEVELOPMENT = "dev"

Exception Hierarchy

IProovPortalAuthError
├── AuthenticationError
├── TokenExpiredError
├── NetworkError
└── ConfigurationError

Usage Examples

Basic Authentication Flow

from iproov_portal_auth import IProovPortalAuth

# Initialize
auth = IProovPortalAuth()

try:
    # Login
    token = auth.login()
    print(f"Successfully authenticated with token: {token[:20]}...")
    
    # Get user information
    email = auth.get_email()
    name = auth.get_name()
    roles = auth.get_roles()
    
    print(f"User: {name} <{email}>")
    print(f"Roles: {', '.join(roles)}")
    
except Exception as e:
    print(f"Authentication failed: {e}")

Environment-Specific Configuration

from iproov_portal_auth import IProovPortalAuth, AuthConfig, Environment

# UAT environment
uat_config = AuthConfig(
    client_id="your-client-id",
    api_key="your-api-key",
    environment=Environment.UAT
)

auth = IProovPortalAuth(config=uat_config)

Error Handling

from iproov_portal_auth import IProovPortalAuth
from iproov_portal_auth.exceptions import (
    AuthenticationError,
    TokenExpiredError,
    NetworkError,
    ConfigurationError
)

auth = IProovPortalAuth()

try:
    user_details = auth.get_user_details()
    print(f"User authenticated: {user_details['email']}")
    
except TokenExpiredError:
    print("Token has expired, attempting to refresh...")
    try:
        # Force refresh and try again
        user_details = auth.get_user_details(force_refresh=True)
        print(f"Successfully refreshed: {user_details['email']}")
    except Exception as e:
        print(f"Refresh failed: {e}")
        
except AuthenticationError as e:
    print(f"Authentication failed: {e}")
    
except NetworkError as e:
    print(f"Network error: {e}")
    
except ConfigurationError as e:
    print(f"Configuration error: {e}")

Token Caching

The library automatically caches tokens locally to avoid unnecessary authentication requests:

from iproov_portal_auth import IProovPortalAuth

auth = IProovPortalAuth()

# First call performs authentication
token1 = auth.login()
print("First login completed")

# Second call uses cached token
token2 = auth.login()
print("Second login used cached token")

# Tokens are the same
assert token1 == token2

# Force refresh to get new token
token3 = auth.login(force_refresh=True)
print("Third login forced refresh")

Custom Token Cache Directory

from iproov_portal_auth import IProovPortalAuth
from iproov_portal_auth.token_cache import TokenCache

# Custom cache directory
cache = TokenCache(cache_dir="/custom/cache/path")

# Note: Currently, you cannot pass custom cache to IProovPortalAuth
# This is for advanced use cases where you need to manage the cache manually

Token Expiration

Tokens typically expire on Sunday midnight each week. The library automatically handles this by:

  1. Checking expiration before returning cached tokens
  2. Automatic cleanup of expired tokens
  3. Retry logic when API returns 401 (Unauthorized)

Development

Running Tests

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=iproov_portal_auth

Code Quality

# Format code
black iproov_portal_auth/

# Lint code
flake8 iproov_portal_auth/

# Type checking
mypy iproov_portal_auth/

Security Considerations

  • No sensitive data in responses: Access tokens and refresh tokens are automatically removed from API responses
  • Local token storage: Tokens are stored in ~/.iproov_portal_auth/ directory with appropriate permissions
  • SSL verification: All HTTPS connections use proper SSL verification (except where explicitly disabled for testing)

Environment URLs

Environment Login URL Client ID
Production https://login.secure.iproov.me 105336304952-n6ddad4ea9lc2hrc70791ibq6pfdfemc.apps.googleusercontent.com
UAT https://login.uat.secure.iproov.me 973312410545-63o5poolj5kocnu0lceocvagajak4sgf.apps.googleusercontent.com
Development https://login.dev.secure.iproov.me 568086550639-lq968h5moe814cf0o45oftn1p0ck0tgn.apps.googleusercontent.com

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

Support

For issues and questions, please create an issue in the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iproov_portal_auth-1.0.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iproov_portal_auth-1.0.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file iproov_portal_auth-1.0.0.tar.gz.

File metadata

  • Download URL: iproov_portal_auth-1.0.0.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for iproov_portal_auth-1.0.0.tar.gz
Algorithm Hash digest
SHA256 09e7293760a9ccc7ed24cd685058452dac74a777f5f33b3bd12934ffc6bfd906
MD5 79b6b2ee464bc7adb5988f26e6c52fbb
BLAKE2b-256 b149cdaf08cf0249cd8d7624e974c62b04c4635a58b4c5d985b0e7ed9a0123c2

See more details on using hashes here.

File details

Details for the file iproov_portal_auth-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for iproov_portal_auth-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 604dd0a2fc0c5f6aa339c05b0861a1e6fcaa05db4beb1bb5cd934a5a5370299c
MD5 6feac7167a125ddb4d4b73afebb7e410
BLAKE2b-256 f4de970430d5818217555d8c774f0bd78bbf4741a449a2d093470f3d414be5fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page