| name | authentication-patterns |
| description | Authentication patterns for external services: API keys, OAuth, token management, verification.
Triggers: authentication, API keys, OAuth, token management, credentials
Use when: integrating external services or implementing authentication flows
|
| category | infrastructure |
| tags | ["authentication","api-keys","oauth","tokens","security"] |
| dependencies | ["error-patterns"] |
| provides | {"infrastructure":["authentication","credential-management","auth-verification"],"patterns":["api-key-auth","oauth-flow","token-refresh"]} |
| usage_patterns | ["service-authentication","credential-verification","token-management"] |
| complexity | beginner |
| estimated_tokens | 400 |
| progressive_loading | true |
| modules | ["modules/auth-methods.md","modules/verification-patterns.md"] |
Authentication Patterns
Overview
Common authentication patterns for integrating with external services. Provides consistent approaches to credential management, verification, and error handling.
When to Use
- Integrating with external APIs
- Need credential verification
- Managing multiple auth methods
- Handling auth failures gracefully
Authentication Methods
| Method | Best For | Environment Variable |
|---|
| API Key | Simple integrations | {SERVICE}_API_KEY |
| OAuth | User-authenticated | Browser-based flow |
| Token | Session-based | {SERVICE}_TOKEN |
| None | Public APIs | N/A |
Quick Start
Verify Authentication
from leyline.auth import verify_auth, AuthMethod
status = verify_auth(
service="gemini",
method=AuthMethod.API_KEY,
env_var="GEMINI_API_KEY"
)
if not status.authenticated:
print(f"Auth failed: {status.message}")
print(f"Action: {status.suggested_action}")
Smoke Test
def verify_with_smoke_test(service: str) -> bool:
"""Verify auth with simple request."""
result = execute_simple_request(service, "ping")
return result.success
Standard Flow
Step 1: Check Environment
def check_credentials() -> :
value = os.getenv(env_var)
value:
()