auth package provides authentication and auditing capabilities for secure Go applications. It includes OpenID Connect (OIDC) authentication with PKCE support and comprehensive audit logging.
Sub-packages
OIDC
OpenID Connect authentication with PKCE flow
Audit
Audit logging for tracking user actions and system events
Installation
OIDC Package
Theoidc package provides OpenID Connect authentication with PKCE (Proof Key for Code Exchange) support for enhanced security.
Features
- PKCE Support: Implements RFC 7636 for secure authorization
- Browser-based Flow: Automatic browser opening for user authentication
- Token Management: Handles access tokens, refresh tokens, and ID tokens
- OAuth2 Integration: Built on top of
golang.org/x/oauth2
Creating a Token Source
PKCE Flow
The OIDC package automatically implements the PKCE flow:- Generate Code Verifier: Random 32-byte string
- Create Code Challenge: SHA256 hash of verifier
- Authorization Request: Includes code challenge
- Token Exchange: Includes code verifier
Google Service Account Support
The package also supports Google Service Account authentication:Cobra Integration
The package provides helpers for integrating OIDC authentication into Cobra CLI applications:Audit Package
Theaudit package provides comprehensive audit logging for tracking user actions and system events.
Features
- Actor Tracking: Records who performed each action
- Metadata Support: Attach custom metadata to audit logs
- Repository Pattern: Pluggable storage backends
- Context Integration: Extracts audit information from context
Core Types
Service
Log
Creating an Audit Service
Audit Options
WithRepository
WithActorExtractor
WithMetadataExtractor
Context Helpers
WithActor
WithMetadata
Logging Actions
PostgreSQL Repository
The package includes a PostgreSQL repository implementation:Complete Example: Authenticated API with Audit
Best Practices
Always Log Sensitive Actions
Always Log Sensitive Actions
Log all actions that involve data access, modification, or permission changes:
Include Relevant Context
Include Relevant Context
Attach meaningful metadata to audit logs:
Use Consistent Action Names
Use Consistent Action Names
Follow a naming convention for actions:
Secure Token Storage
Secure Token Storage
Never log tokens or passwords in audit logs: