Documentation
¶
Overview ¶
Package auth provides centralized authentication and authorization for the DevOps MCP platform
Index ¶
- Constants
- Variables
- func CheckPassword(password, hash string) bool
- func CredentialExtractionMiddleware(logger observability.Logger) gin.HandlerFunc
- func CredentialValidationMiddleware(requiredTools []string, logger observability.Logger) gin.HandlerFunc
- func ExtractAllowedServices(metadata map[string]interface{}) []string
- func ExtractCredentialsFromBody(c *gin.Context) (*models.ToolCredentials, bool)
- func GenerateTestAPIKey() string
- func GetAgentID(ctx context.Context) string
- func GetGatewayID(ctx context.Context) (string, bool)
- func GetTenantFromContext(c *gin.Context) (string, bool)
- func GetTenantID(ctx context.Context) uuid.UUID
- func GetTokenProvider(ctx context.Context) (string, bool)
- func GetToolCredential(ctx context.Context, tool string) (*models.TokenCredential, bool)
- func GetToolCredentials(ctx context.Context) (*models.ToolCredentials, bool)
- func GetUserID(ctx context.Context) string
- func HTTPAuthMiddleware() func(http.Handler) http.Handler
- func HasToolCredential(ctx context.Context, tool string) bool
- func HashPassword(password string) (string, error)
- func RateLimitMiddleware(rateLimiter *RateLimiter, logger observability.Logger) func(http.Handler) http.Handler
- func SetupTestAuth(t *testing.T) (*AuthMiddleware, *TestCache, observability.MetricsClient)
- func SetupTestAuthWithConfig(t *testing.T, config *AuthSystemConfig) (*AuthMiddleware, *TestCache)
- func ValidateAuthConfiguration(logger observability.Logger) error
- func ValidateProviderAllowed(provider string, allowedServices []string) bool
- func WithAgentID(ctx context.Context, agentID string) context.Context
- func WithGatewayID(ctx context.Context, gatewayID string) context.Context
- func WithPassthroughToken(ctx context.Context, token PassthroughToken) context.Context
- func WithTenantID(ctx context.Context, tenantID uuid.UUID) context.Context
- func WithTokenProvider(ctx context.Context, provider string) context.Context
- func WithToolCredentials(ctx context.Context, creds *models.ToolCredentials) context.Context
- func WithUserID(ctx context.Context, userID string) context.Context
- type APIKey
- type APIKeyConfig
- type APIKeySettings
- type AuditEvent
- type AuditLogger
- func (al *AuditLogger) LogAPIKeyCreated(ctx context.Context, userID, tenantID, keyName string)
- func (al *AuditLogger) LogAPIKeyRevoked(ctx context.Context, userID, tenantID, keyID string)
- func (al *AuditLogger) LogAuthAttempt(ctx context.Context, event AuditEvent)
- func (al *AuditLogger) LogAuthorizationDenial(ctx context.Context, subject, resource, action, reason string)
- func (al *AuditLogger) LogAuthorizationSuccess(ctx context.Context, subject, resource, action string)
- func (al *AuditLogger) LogPolicyChange(ctx context.Context, action string, policy interface{}, reason string)
- func (al *AuditLogger) LogRateLimitExceeded(ctx context.Context, identifier, ipAddress string)
- func (al *AuditLogger) LogRoleAssignment(ctx context.Context, action, user, role string)
- type AuthConfig
- type AuthMiddleware
- func NewAuthMiddleware(service *Service, rateLimiter *RateLimiter, metrics *MetricsCollector, ...) *AuthMiddleware
- func SetupAuthentication(db *sqlx.DB, cache cache.Cache, logger observability.Logger, ...) (*AuthMiddleware, error)
- func SetupAuthenticationWithConfig(config *AuthSystemConfig, db *sqlx.DB, cache cache.Cache, ...) (*AuthMiddleware, error)
- func SetupEnhancedAuthentication(db *sqlx.DB, cache cache.Cache, logger observability.Logger, ...) (*AuthMiddleware, error)
- type AuthMode
- type AuthRequest
- type AuthSystemConfig
- type Authorizer
- type BaseOAuthProvider
- type Claims
- type CreateAPIKeyRequest
- type CredentialContext
- func (cc *CredentialContext) Context() context.Context
- func (cc *CredentialContext) GetCredential(tool string) (*models.TokenCredential, bool)
- func (cc *CredentialContext) GetCredentials() (*models.ToolCredentials, bool)
- func (cc *CredentialContext) HasCredential(tool string) bool
- func (cc *CredentialContext) WithCredentials(creds *models.ToolCredentials) *CredentialContext
- type Decision
- type FactoryConfig
- type KeyConfig
- type KeyType
- type MetricsCollector
- type OAuthProvider
- type OAuthToken
- type OAuthUserInfo
- type PassthroughToken
- type Permission
- type Policy
- type ProductionAuthorizer
- func (a *ProductionAuthorizer) AddPolicy(ctx context.Context, policy Policy) error
- func (a *ProductionAuthorizer) AddRole(ctx context.Context, user, role string) error
- func (a *ProductionAuthorizer) Authorize(ctx context.Context, permission Permission) Decision
- func (a *ProductionAuthorizer) AuthorizeRequest(ctx context.Context, req *AuthRequest) error
- func (a *ProductionAuthorizer) CheckPermission(ctx context.Context, resource, action string) bool
- func (a *ProductionAuthorizer) GetRolesForUser(ctx context.Context, user string) ([]string, error)
- func (a *ProductionAuthorizer) GetUsersForRole(ctx context.Context, role string) ([]string, error)
- func (a *ProductionAuthorizer) RemovePolicy(ctx context.Context, policy Policy) error
- func (a *ProductionAuthorizer) RemoveRole(ctx context.Context, user, role string) error
- type RateLimiter
- type RateLimiterConfig
- type Service
- func (s *Service) AddAPIKey(key string, settings APIKeySettings) error
- func (s *Service) AuthorizeScopes(user *User, requiredScopes []string) error
- func (s *Service) CreateAPIKey(ctx context.Context, tenantID, userID, name string, scopes []string, ...) (*APIKey, error)
- func (s *Service) CreateAPIKeyWithType(ctx context.Context, req CreateAPIKeyRequest) (*APIKey, error)
- func (s *Service) GenerateJWT(ctx context.Context, user *User) (string, error)
- func (s *Service) GetConfig() *ServiceConfig
- func (s *Service) GinMiddleware(authTypes ...Type) gin.HandlerFunc
- func (s *Service) GinMiddlewareWithPassthrough(authTypes ...Type) gin.HandlerFunc
- func (s *Service) InitializeAPIKeysWithConfig(keysConfig map[string]interface{})
- func (s *Service) InitializeDefaultAPIKeys(keys map[string]string)
- func (s *Service) LoadAPIKeys(config *APIKeyConfig) error
- func (s *Service) LoadAuthConfigBasedOnEnvironment() error
- func (s *Service) RequireScopes(scopes ...string) gin.HandlerFunc
- func (s *Service) RevokeAPIKey(ctx context.Context, apiKey string) error
- func (s *Service) StandardMiddleware(authTypes ...Type) func(http.Handler) http.Handler
- func (s *Service) StandardMiddlewareWithPassthrough(authTypes ...Type) func(http.Handler) http.Handler
- func (s *Service) ValidateAPIKey(ctx context.Context, apiKey string) (*User, error)
- func (s *Service) ValidateJWT(ctx context.Context, tokenString string) (*User, error)
- type ServiceConfig
- type TestCache
- func (c *TestCache) Close() error
- func (c *TestCache) Delete(ctx context.Context, key string) error
- func (c *TestCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *TestCache) Flush(ctx context.Context) error
- func (c *TestCache) Get(ctx context.Context, key string, value interface{}) error
- func (c *TestCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- type TestProvider
- func (tp *TestProvider) Authorize(ctx context.Context, permission Permission) Decision
- func (tp *TestProvider) CheckPermission(ctx context.Context, resource, action string) bool
- func (tp *TestProvider) CleanupExpiredTokens()
- func (tp *TestProvider) Close() error
- func (tp *TestProvider) GenerateTestToken(userID, tenantID uuid.UUID, role string, scopes []string) (string, error)
- func (tp *TestProvider) GetUserRole(ctx context.Context, userID, tenantID uuid.UUID) (string, error)
- func (tp *TestProvider) ListUserPermissions(ctx context.Context, userID, tenantID uuid.UUID) ([]string, error)
- func (tp *TestProvider) RevokeToken(tokenID string) error
- func (tp *TestProvider) ValidateTestToken(tokenString string) (*Claims, error)
- type Type
- type User
Examples ¶
Constants ¶
const ( // ContextKeyIPAddress is the context key for IP address ContextKeyIPAddress contextKey = "ip_address" // ContextKeyUserAgent is the context key for user agent ContextKeyUserAgent contextKey = "user_agent" )
const ( // PassthroughTokenKey is the key for storing passthrough token in context PassthroughTokenKey contextKey = "passthrough_token" // TokenProviderKey is the key for storing token provider in context TokenProviderKey contextKey = "token_provider" // GatewayIDKey is the key for storing gateway ID in context GatewayIDKey contextKey = "gateway_id" )
Define additional context keys for passthrough functionality
const (
// UserContextKey is the key for storing user in context
UserContextKey contextKey = "auth_user"
)
Variables ¶
var ( ErrInvalidCredentials = errors.New("invalid credentials") ErrTokenExpired = errors.New("token expired") ErrInvalidToken = errors.New("invalid token") ErrNoAPIKey = errors.New("no API key provided") ErrInvalidAPIKey = errors.New("invalid API key") ErrInsufficientScope = errors.New("insufficient scope") )
Common errors
Functions ¶
func CheckPassword ¶
CheckPassword compares a password with its hash
func CredentialExtractionMiddleware ¶
func CredentialExtractionMiddleware(logger observability.Logger) gin.HandlerFunc
CredentialExtractionMiddleware extracts tool credentials from requests
func CredentialValidationMiddleware ¶
func CredentialValidationMiddleware(requiredTools []string, logger observability.Logger) gin.HandlerFunc
CredentialValidationMiddleware validates that required credentials are present
func ExtractAllowedServices ¶
ExtractAllowedServices extracts allowed services from user metadata
func ExtractCredentialsFromBody ¶
func ExtractCredentialsFromBody(c *gin.Context) (*models.ToolCredentials, bool)
ExtractCredentialsFromBody is a helper to extract credentials from a parsed body
func GenerateTestAPIKey ¶
func GenerateTestAPIKey() string
GenerateTestAPIKey generates a secure test API key
func GetAgentID ¶
GetAgentID gets the agent ID from context
func GetGatewayID ¶
GetGatewayID retrieves the gateway ID from the context
func GetTenantFromContext ¶
GetTenantFromContext extracts the tenant ID from the Gin context
func GetTenantID ¶
GetTenantID gets the tenant ID from context
func GetTokenProvider ¶
GetTokenProvider retrieves the token provider from the context
func GetToolCredential ¶
GetToolCredential retrieves a specific tool credential from context
func GetToolCredentials ¶
func GetToolCredentials(ctx context.Context) (*models.ToolCredentials, bool)
GetToolCredentials retrieves tool credentials from context
func HTTPAuthMiddleware ¶
HTTPAuthMiddleware creates HTTP middleware that adds IP to context
func HasToolCredential ¶
HasToolCredential checks if a specific tool credential exists in context
func HashPassword ¶
HashPassword hashes a password using bcrypt
func RateLimitMiddleware ¶
func RateLimitMiddleware(rateLimiter *RateLimiter, logger observability.Logger) func(http.Handler) http.Handler
RateLimitMiddleware creates HTTP middleware for rate limiting
func SetupTestAuth ¶
func SetupTestAuth(t *testing.T) (*AuthMiddleware, *TestCache, observability.MetricsClient)
SetupTestAuth creates a complete test authentication system
func SetupTestAuthWithConfig ¶
func SetupTestAuthWithConfig(t *testing.T, config *AuthSystemConfig) (*AuthMiddleware, *TestCache)
SetupTestAuthWithConfig allows custom configuration
func ValidateAuthConfiguration ¶
func ValidateAuthConfiguration(logger observability.Logger) error
ValidateAuthConfiguration validates that auth configuration is consistent across services
func ValidateProviderAllowed ¶
ValidateProviderAllowed checks if a provider is in the allowed services list
func WithAgentID ¶
WithAgentID adds agent ID to context
func WithGatewayID ¶
WithGatewayID adds the gateway ID to the context
func WithPassthroughToken ¶
func WithPassthroughToken(ctx context.Context, token PassthroughToken) context.Context
WithPassthroughToken adds a passthrough token to the context
func WithTenantID ¶
WithTenantID adds tenant ID to context
func WithTokenProvider ¶
WithTokenProvider adds the token provider to the context
func WithToolCredentials ¶
WithToolCredentials adds tool credentials to context
Types ¶
type APIKey ¶
type APIKey struct {
Key string `db:"key"`
KeyHash string `db:"key_hash"`
KeyPrefix string `db:"key_prefix"`
TenantID string `db:"tenant_id"`
UserID string `db:"user_id"`
Name string `db:"name"`
KeyType KeyType `db:"key_type"` // NEW
Scopes []string `db:"scopes"`
ExpiresAt *time.Time `db:"expires_at"`
CreatedAt time.Time `db:"created_at"`
LastUsed *time.Time `db:"last_used"`
Active bool `db:"is_active"`
// Gateway-specific fields
ParentKeyID *string `db:"parent_key_id"` // NEW
AllowedServices []string `db:"allowed_services"` // NEW
// Rate limiting
RateLimitRequests int `db:"rate_limit_requests"`
RateLimitWindowSeconds int `db:"rate_limit_window_seconds"`
}
APIKey represents an API key
type APIKeyConfig ¶
type APIKeyConfig struct {
// Development keys (only loaded in dev/test environments)
DevelopmentKeys map[string]APIKeySettings `yaml:"development_keys"`
// Production key sources
ProductionKeySource string `yaml:"production_key_source"` // "env", "vault", "aws-secrets"
}
APIKeyConfig represents API key configuration
func LoadAuthConfigFromFile ¶
func LoadAuthConfigFromFile(filename string) (*APIKeyConfig, error)
LoadAuthConfigFromFile loads auth configuration from a YAML file
type APIKeySettings ¶
type APIKeySettings struct {
Role string `yaml:"role"`
Scopes []string `yaml:"scopes"`
TenantID string `yaml:"tenant_id"`
ExpiresIn string `yaml:"expires_in"` // Duration string like "30d"
}
APIKeySettings represents settings for an API key
type AuditEvent ¶
type AuditEvent struct {
Timestamp time.Time `json:"timestamp"`
EventType string `json:"event_type"`
UserID string `json:"user_id,omitempty"`
TenantID string `json:"tenant_id,omitempty"`
AuthType string `json:"auth_type"`
Success bool `json:"success"`
IPAddress string `json:"ip_address,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
Error string `json:"error,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
AuditEvent represents an authentication audit event
type AuditLogger ¶
type AuditLogger struct {
// contains filtered or unexported fields
}
AuditLogger handles authentication audit logging
func NewAuditLogger ¶
func NewAuditLogger(logger observability.Logger) *AuditLogger
NewAuditLogger creates a new audit logger
func (*AuditLogger) LogAPIKeyCreated ¶
func (al *AuditLogger) LogAPIKeyCreated(ctx context.Context, userID, tenantID, keyName string)
LogAPIKeyCreated logs API key creation
func (*AuditLogger) LogAPIKeyRevoked ¶
func (al *AuditLogger) LogAPIKeyRevoked(ctx context.Context, userID, tenantID, keyID string)
LogAPIKeyRevoked logs API key revocation
func (*AuditLogger) LogAuthAttempt ¶
func (al *AuditLogger) LogAuthAttempt(ctx context.Context, event AuditEvent)
LogAuthAttempt logs an authentication attempt
func (*AuditLogger) LogAuthorizationDenial ¶
func (al *AuditLogger) LogAuthorizationDenial(ctx context.Context, subject, resource, action, reason string)
LogAuthorizationDenial logs authorization denials
func (*AuditLogger) LogAuthorizationSuccess ¶
func (al *AuditLogger) LogAuthorizationSuccess(ctx context.Context, subject, resource, action string)
LogAuthorizationSuccess logs successful authorization
func (*AuditLogger) LogPolicyChange ¶
func (al *AuditLogger) LogPolicyChange(ctx context.Context, action string, policy interface{}, reason string)
LogPolicyChange logs policy changes
func (*AuditLogger) LogRateLimitExceeded ¶
func (al *AuditLogger) LogRateLimitExceeded(ctx context.Context, identifier, ipAddress string)
LogRateLimitExceeded logs rate limit exceeded events
func (*AuditLogger) LogRoleAssignment ¶
func (al *AuditLogger) LogRoleAssignment(ctx context.Context, action, user, role string)
LogRoleAssignment logs role assignment changes
type AuthConfig ¶
type AuthConfig struct {
ModelPath string
PolicyPath string
DBDriver string
DBSource string
Cache cache.Cache
Logger observability.Logger
Metrics observability.MetricsClient
Tracer observability.StartSpanFunc
AuditLogger *AuditLogger
CacheEnabled bool
CacheDuration time.Duration
}
AuthConfig holds configuration for the production authorizer
type AuthMiddleware ¶
type AuthMiddleware struct {
// contains filtered or unexported fields
}
AuthMiddleware wraps the auth service with production features
func NewAuthMiddleware ¶
func NewAuthMiddleware(service *Service, rateLimiter *RateLimiter, metrics *MetricsCollector, audit *AuditLogger) *AuthMiddleware
NewAuthMiddleware creates middleware with rate limiting and metrics
func SetupAuthentication ¶
func SetupAuthentication(db *sqlx.DB, cache cache.Cache, logger observability.Logger, metrics observability.MetricsClient) (*AuthMiddleware, error)
SetupAuthentication sets up the enhanced authentication service
func SetupAuthenticationWithConfig ¶
func SetupAuthenticationWithConfig( config *AuthSystemConfig, db *sqlx.DB, cache cache.Cache, logger observability.Logger, metrics observability.MetricsClient, ) (*AuthMiddleware, error)
SetupAuthenticationWithConfig provides full control over auth system initialization
func SetupEnhancedAuthentication ¶
func SetupEnhancedAuthentication( db *sqlx.DB, cache cache.Cache, logger observability.Logger, metrics observability.MetricsClient, ) (*AuthMiddleware, error)
SetupEnhancedAuthentication maintains backward compatibility for enhanced auth
func (*AuthMiddleware) GinMiddleware ¶
func (m *AuthMiddleware) GinMiddleware() gin.HandlerFunc
GinMiddleware returns a Gin middleware that uses the enhanced auth service
func (*AuthMiddleware) ValidateAPIKeyWithMetrics ¶
func (m *AuthMiddleware) ValidateAPIKeyWithMetrics(ctx context.Context, apiKey string) (*User, error)
ValidateAPIKeyWithMetrics validates an API key with rate limiting and metrics
func (*AuthMiddleware) ValidateJWTWithMetrics ¶
func (m *AuthMiddleware) ValidateJWTWithMetrics(ctx context.Context, tokenString string) (*User, error)
ValidateJWTWithMetrics validates a JWT with rate limiting and metrics
type AuthRequest ¶
type AuthRequest struct {
Subject string `json:"subject"`
Resource string `json:"resource"`
Action string `json:"action"`
Tenant string `json:"tenant"`
Context map[string]interface{} `json:"context,omitempty"`
}
AuthRequest represents an authorization request
type AuthSystemConfig ¶
type AuthSystemConfig struct {
Service *ServiceConfig
RateLimiter *RateLimiterConfig
APIKeys map[string]APIKeySettings
}
AuthSystemConfig holds complete auth system configuration
func TestAuthConfig ¶
func TestAuthConfig() *AuthSystemConfig
TestAuthConfig creates a complete test configuration
type Authorizer ¶
type Authorizer interface {
Authorize(ctx context.Context, permission Permission) Decision
CheckPermission(ctx context.Context, resource, action string) bool
}
Authorizer provides authorization functionality
func NewAuthorizer ¶
func NewAuthorizer(config FactoryConfig) (Authorizer, error)
NewAuthorizer creates the appropriate authorizer based on configuration
type BaseOAuthProvider ¶
type BaseOAuthProvider struct {
ClientID string
ClientSecret string
AuthURL string
TokenURL string
UserInfoURL string
}
BaseOAuthProvider provides common OAuth functionality
func (*BaseOAuthProvider) GetAuthorizationURL ¶
func (p *BaseOAuthProvider) GetAuthorizationURL(state, redirectURI string) string
GetAuthorizationURL returns the authorization URL
func (*BaseOAuthProvider) GetAuthorizationURLWithPKCE ¶
func (p *BaseOAuthProvider) GetAuthorizationURLWithPKCE(state, redirectURI, codeChallenge string) string
GetAuthorizationURLWithPKCE returns the authorization URL with PKCE
func (*BaseOAuthProvider) ValidateState ¶
func (p *BaseOAuthProvider) ValidateState(providedState, expectedState string) bool
ValidateState validates the state parameter
type Claims ¶
type Claims struct {
jwt.RegisteredClaims
UserID string `json:"user_id"`
TenantID string `json:"tenant_id"`
Scopes []string `json:"scopes,omitempty"`
Email string `json:"email,omitempty"`
}
Claims represents JWT claims
type CreateAPIKeyRequest ¶
type CreateAPIKeyRequest struct {
Name string `json:"name" binding:"required"`
TenantID string `json:"tenant_id" binding:"required"`
UserID string `json:"user_id"`
KeyType KeyType `json:"key_type" binding:"required"`
Scopes []string `json:"scopes"`
ExpiresAt *time.Time `json:"expires_at"`
// Gateway-specific
AllowedServices []string `json:"allowed_services,omitempty"`
ParentKeyID *string `json:"parent_key_id,omitempty"`
// Rate limiting
RateLimit *int `json:"rate_limit,omitempty"`
}
CreateAPIKeyRequest represents a request to create a new API key
type CredentialContext ¶
type CredentialContext struct {
// contains filtered or unexported fields
}
CredentialContext wraps common credential operations
func NewCredentialContext ¶
func NewCredentialContext(ctx context.Context) *CredentialContext
NewCredentialContext creates a new credential context wrapper
func (*CredentialContext) Context ¶
func (cc *CredentialContext) Context() context.Context
Context returns the underlying context
func (*CredentialContext) GetCredential ¶
func (cc *CredentialContext) GetCredential(tool string) (*models.TokenCredential, bool)
GetCredential retrieves a specific tool credential
func (*CredentialContext) GetCredentials ¶
func (cc *CredentialContext) GetCredentials() (*models.ToolCredentials, bool)
GetCredentials retrieves all credentials
func (*CredentialContext) HasCredential ¶
func (cc *CredentialContext) HasCredential(tool string) bool
HasCredential checks if a specific tool credential exists
func (*CredentialContext) WithCredentials ¶
func (cc *CredentialContext) WithCredentials(creds *models.ToolCredentials) *CredentialContext
WithCredentials adds credentials to the context
type FactoryConfig ¶
type FactoryConfig struct {
Mode AuthMode
ProductionConfig *AuthConfig
Logger observability.Logger
Tracer observability.StartSpanFunc
}
FactoryConfig contains configuration for the auth factory
type KeyType ¶
type KeyType string
KeyType represents the type of API key
func (KeyType) GetRateLimit ¶
GetRateLimit returns the default rate limit for a key type
type MetricsCollector ¶
type MetricsCollector struct {
// contains filtered or unexported fields
}
MetricsCollector collects authentication metrics
func NewMetricsCollector ¶
func NewMetricsCollector(metrics observability.MetricsClient) *MetricsCollector
NewMetricsCollector creates a new metrics collector
func (*MetricsCollector) RecordAuthAttempt ¶
func (mc *MetricsCollector) RecordAuthAttempt(ctx context.Context, authType string, success bool, duration time.Duration)
RecordAuthAttempt records an authentication attempt
func (*MetricsCollector) RecordRateLimitExceeded ¶
func (mc *MetricsCollector) RecordRateLimitExceeded(ctx context.Context, identifier string)
RecordRateLimitExceeded records rate limit exceeded events
func (*MetricsCollector) UpdateActiveSessions ¶
func (mc *MetricsCollector) UpdateActiveSessions(count float64)
UpdateActiveSessions updates the active sessions gauge
type OAuthProvider ¶
type OAuthProvider interface {
// GetAuthorizationURL returns the authorization URL
GetAuthorizationURL(state, redirectURI string) string
// GetAuthorizationURLWithPKCE returns the authorization URL with PKCE
GetAuthorizationURLWithPKCE(state, redirectURI, codeChallenge string) string
// ExchangeCode exchanges an authorization code for tokens
ExchangeCode(ctx context.Context, code, redirectURI string) (*OAuthToken, error)
// ExchangeCodeWithPKCE exchanges an authorization code with PKCE
ExchangeCodeWithPKCE(ctx context.Context, code, redirectURI, codeVerifier string) (*OAuthToken, error)
// RefreshToken refreshes an access token
RefreshToken(ctx context.Context, refreshToken string) (*OAuthToken, error)
// ValidateToken validates an access token and returns user info
ValidateToken(ctx context.Context, accessToken string) (*OAuthUserInfo, error)
// ValidateState validates the state parameter
ValidateState(providedState, expectedState string) bool
}
OAuthProvider defines the interface for OAuth providers
type OAuthToken ¶
type OAuthToken struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
ExpiresAt time.Time `json:"expires_at"`
}
OAuthToken represents an OAuth token
type OAuthUserInfo ¶
type OAuthUserInfo struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
OAuthUserInfo represents user information from OAuth provider
type PassthroughToken ¶
type PassthroughToken struct {
Provider string // github, gitlab, bitbucket
Token string // The actual token
Scopes []string // Token scopes if known
}
PassthroughToken represents a token to be passed to external services
func GetPassthroughToken ¶
func GetPassthroughToken(ctx context.Context) (*PassthroughToken, bool)
GetPassthroughToken retrieves a passthrough token from the context
func GetPassthroughTokenFromGin ¶
func GetPassthroughTokenFromGin(c interface{}) (*PassthroughToken, bool)
GetPassthroughTokenFromGin retrieves a passthrough token from Gin context This is an alternative to GetPassthroughToken for use in Gin handlers
type Permission ¶
type Permission struct {
Resource string `json:"resource"`
Action string `json:"action"`
Conditions map[string]interface{} `json:"conditions,omitempty"`
}
Permission represents a permission request
type Policy ¶
type Policy struct {
Subject string `json:"subject"`
Resource string `json:"resource"`
Action string `json:"action"`
Effect string `json:"effect,omitempty"`
Tenant string `json:"tenant,omitempty"`
}
Policy represents an authorization policy
type ProductionAuthorizer ¶
type ProductionAuthorizer struct {
// contains filtered or unexported fields
}
ProductionAuthorizer implements production-grade authorization
func NewProductionAuthorizer ¶
func NewProductionAuthorizer(config AuthConfig) (*ProductionAuthorizer, error)
NewProductionAuthorizer creates a production authorizer
func (*ProductionAuthorizer) AddPolicy ¶
func (a *ProductionAuthorizer) AddPolicy(ctx context.Context, policy Policy) error
AddPolicy adds a new authorization policy
func (*ProductionAuthorizer) AddRole ¶
func (a *ProductionAuthorizer) AddRole(ctx context.Context, user, role string) error
AddRole assigns a role to a user
func (*ProductionAuthorizer) Authorize ¶
func (a *ProductionAuthorizer) Authorize(ctx context.Context, permission Permission) Decision
Authorize implements the Authorizer interface
func (*ProductionAuthorizer) AuthorizeRequest ¶
func (a *ProductionAuthorizer) AuthorizeRequest(ctx context.Context, req *AuthRequest) error
AuthorizeRequest checks if a subject can perform an action on a resource
func (*ProductionAuthorizer) CheckPermission ¶
func (a *ProductionAuthorizer) CheckPermission(ctx context.Context, resource, action string) bool
CheckPermission implements the Authorizer interface
func (*ProductionAuthorizer) GetRolesForUser ¶
GetRolesForUser returns all roles assigned to a user
func (*ProductionAuthorizer) GetUsersForRole ¶
GetUsersForRole returns all users assigned to a role
func (*ProductionAuthorizer) RemovePolicy ¶
func (a *ProductionAuthorizer) RemovePolicy(ctx context.Context, policy Policy) error
RemovePolicy removes an authorization policy
func (*ProductionAuthorizer) RemoveRole ¶
func (a *ProductionAuthorizer) RemoveRole(ctx context.Context, user, role string) error
RemoveRole removes a role from a user
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter provides rate limiting for authentication endpoints
func NewRateLimiter ¶
func NewRateLimiter(cache cache.Cache, logger observability.Logger, config *RateLimiterConfig) *RateLimiter
NewRateLimiter creates a new rate limiter
func (*RateLimiter) CheckLimit ¶
func (rl *RateLimiter) CheckLimit(ctx context.Context, identifier string) error
CheckLimit checks if the identifier has exceeded rate limits
func (*RateLimiter) GetLockoutPeriod ¶
func (rl *RateLimiter) GetLockoutPeriod() time.Duration
GetLockoutPeriod returns the configured lockout period
func (*RateLimiter) RecordAttempt ¶
func (rl *RateLimiter) RecordAttempt(ctx context.Context, identifier string, success bool)
RecordAttempt records an authentication attempt
type RateLimiterConfig ¶
type RateLimiterConfig struct {
Enabled bool // Whether rate limiting is enabled
MaxAttempts int // Max attempts per window
WindowSize time.Duration // Time window for attempts
LockoutPeriod time.Duration // Lockout duration after max attempts
}
RateLimiterConfig holds rate limiter configuration
func DefaultRateLimiterConfig ¶
func DefaultRateLimiterConfig() *RateLimiterConfig
DefaultRateLimiterConfig returns sensible defaults
func TestRateLimiterConfig ¶
func TestRateLimiterConfig() *RateLimiterConfig
TestRateLimiterConfig returns test-friendly defaults
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service provides authentication services
func NewService ¶
func NewService(config *ServiceConfig, db *sqlx.DB, cache cache.Cache, logger observability.Logger) *Service
NewService creates a new auth service
func (*Service) AddAPIKey ¶
func (s *Service) AddAPIKey(key string, settings APIKeySettings) error
AddAPIKey adds an API key to the service at runtime (thread-safe)
func (*Service) AuthorizeScopes ¶
AuthorizeScopes checks if a user has the required scopes
func (*Service) CreateAPIKey ¶
func (s *Service) CreateAPIKey(ctx context.Context, tenantID, userID, name string, scopes []string, expiresAt *time.Time) (*APIKey, error)
CreateAPIKey creates a new API key
Example ¶
package main
import (
"context"
"fmt"
"github.com/S-Corkum/devops-mcp/pkg/auth"
"github.com/S-Corkum/devops-mcp/pkg/observability"
)
func main() {
// Create auth service
config := auth.DefaultConfig()
logger := observability.NewLogger("example")
authService := auth.NewService(config, nil, nil, logger)
// Create an API key
ctx := context.Background()
apiKey, err := authService.CreateAPIKey(
ctx,
"tenant-123",
"user-456",
"My API Key",
[]string{"read", "write"},
nil, // No expiration
)
if err == nil {
fmt.Printf("Created API key: %s\n", apiKey.Name)
}
}
Output: Created API key: My API Key
func (*Service) CreateAPIKeyWithType ¶
func (s *Service) CreateAPIKeyWithType(ctx context.Context, req CreateAPIKeyRequest) (*APIKey, error)
CreateAPIKeyWithType creates a new API key with the specified type
func (*Service) GenerateJWT ¶
GenerateJWT generates a new JWT token for a user
Example ¶
package main
import (
"context"
"fmt"
"github.com/S-Corkum/devops-mcp/pkg/auth"
"github.com/S-Corkum/devops-mcp/pkg/observability"
)
func main() {
// Create auth service
config := auth.DefaultConfig()
config.JWTSecret = "example-secret"
logger := observability.NewLogger("example")
authService := auth.NewService(config, nil, nil, logger)
// Generate a JWT token
ctx := context.Background()
user := &auth.User{
ID: "user-123",
TenantID: "tenant-456",
Email: "user@example.com",
Scopes: []string{"read"},
}
_, err := authService.GenerateJWT(ctx, user)
if err == nil {
fmt.Println("Generated JWT token")
}
}
Output: Generated JWT token
func (*Service) GetConfig ¶
func (s *Service) GetConfig() *ServiceConfig
GetConfig returns the service configuration
func (*Service) GinMiddleware ¶
func (s *Service) GinMiddleware(authTypes ...Type) gin.HandlerFunc
GinMiddleware creates a Gin middleware for authentication
Example ¶
package main
import (
"fmt"
"net/http/httptest"
"github.com/S-Corkum/devops-mcp/pkg/auth"
"github.com/S-Corkum/devops-mcp/pkg/observability"
"github.com/gin-gonic/gin"
)
func main() {
// Create auth service
config := auth.DefaultConfig()
config.JWTSecret = "example-secret"
logger := observability.NewLogger("example")
authService := auth.NewService(config, nil, nil, logger)
// Initialize some API keys
authService.InitializeDefaultAPIKeys(map[string]string{
"test-key": "read",
})
// Create Gin router with auth
gin.SetMode(gin.ReleaseMode)
router := gin.New()
// Apply auth middleware
router.Use(authService.GinMiddleware(auth.TypeAPIKey))
// Add a protected endpoint
router.GET("/api/data", func(c *gin.Context) {
user, _ := auth.GetUserFromContext(c)
c.JSON(200, gin.H{
"message": fmt.Sprintf("Hello user %s", user.ID),
})
})
// Test the endpoint with valid API key
req := httptest.NewRequest("GET", "/api/data", nil)
req.Header.Set("Authorization", "Bearer test-key")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
fmt.Println("Response:", w.Code)
}
Output: Response: 200
func (*Service) GinMiddlewareWithPassthrough ¶
func (s *Service) GinMiddlewareWithPassthrough(authTypes ...Type) gin.HandlerFunc
GinMiddlewareWithPassthrough creates a Gin middleware for authentication with token passthrough support
func (*Service) InitializeAPIKeysWithConfig ¶
InitializeAPIKeysWithConfig initializes API keys with full configuration including tenant IDs
func (*Service) InitializeDefaultAPIKeys ¶
InitializeDefaultAPIKeys initializes default API keys from configuration
func (*Service) LoadAPIKeys ¶
func (s *Service) LoadAPIKeys(config *APIKeyConfig) error
LoadAPIKeys loads API keys based on environment
func (*Service) LoadAuthConfigBasedOnEnvironment ¶
LoadAuthConfigBasedOnEnvironment loads the appropriate auth config file
func (*Service) RequireScopes ¶
func (s *Service) RequireScopes(scopes ...string) gin.HandlerFunc
RequireScopes creates a middleware that checks for required scopes
func (*Service) RevokeAPIKey ¶
RevokeAPIKey revokes an API key
func (*Service) StandardMiddleware ¶
StandardMiddleware returns standard HTTP middleware for authentication
Example ¶
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"github.com/S-Corkum/devops-mcp/pkg/auth"
"github.com/S-Corkum/devops-mcp/pkg/observability"
)
func main() {
// Create auth service
config := auth.DefaultConfig()
logger := observability.NewLogger("example")
authService := auth.NewService(config, nil, nil, logger)
// Initialize some API keys
authService.InitializeDefaultAPIKeys(map[string]string{
"test-key": "read",
})
// Create standard HTTP handler
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, ok := auth.GetUserFromRequest(r)
if ok {
if _, err := fmt.Fprintf(w, "Hello user %s", user.ID); err != nil {
// Log error if needed - in example code, we'll just ignore
_ = err
}
}
})
// Wrap with auth middleware
protectedHandler := authService.StandardMiddleware(auth.TypeAPIKey)(handler)
// Test the handler
req := httptest.NewRequest("GET", "/api/data", nil)
req.Header.Set("Authorization", "test-key")
w := httptest.NewRecorder()
protectedHandler.ServeHTTP(w, req)
fmt.Println("Response:", w.Code)
}
Output: Response: 200
func (*Service) StandardMiddlewareWithPassthrough ¶
func (s *Service) StandardMiddlewareWithPassthrough(authTypes ...Type) func(http.Handler) http.Handler
StandardMiddlewareWithPassthrough returns standard HTTP middleware for authentication with passthrough support
func (*Service) ValidateAPIKey ¶
ValidateAPIKey validates an API key and returns the associated user
type ServiceConfig ¶
type ServiceConfig struct {
JWTSecret string
JWTExpiration time.Duration
APIKeyHeader string
EnableAPIKeys bool
EnableJWT bool
CacheEnabled bool
CacheTTL time.Duration
MaxFailedAttempts int
LockoutDuration time.Duration
}
ServiceConfig represents auth configuration
func DefaultConfig ¶
func DefaultConfig() *ServiceConfig
DefaultConfig returns the default configuration
type TestCache ¶
type TestCache struct {
// contains filtered or unexported fields
}
TestCache implements cache.Cache interface for testing
type TestProvider ¶
type TestProvider struct {
// contains filtered or unexported fields
}
TestProvider implements the Authorizer interface for test environments It provides JWT-based authentication with rate limiting and audit logging
func NewTestProvider ¶
func NewTestProvider(logger observability.Logger, tracer observability.StartSpanFunc) (*TestProvider, error)
NewTestProvider creates a new test authentication provider
func (*TestProvider) Authorize ¶
func (tp *TestProvider) Authorize(ctx context.Context, permission Permission) Decision
Authorize implements the Authorizer interface
func (*TestProvider) CheckPermission ¶
func (tp *TestProvider) CheckPermission(ctx context.Context, resource, action string) bool
CheckPermission implements the Authorizer interface
func (*TestProvider) CleanupExpiredTokens ¶
func (tp *TestProvider) CleanupExpiredTokens()
CleanupExpiredTokens removes expired tokens from tracking
func (*TestProvider) GenerateTestToken ¶
func (tp *TestProvider) GenerateTestToken(userID, tenantID uuid.UUID, role string, scopes []string) (string, error)
GenerateTestToken generates a JWT token for testing
func (*TestProvider) GetUserRole ¶
func (tp *TestProvider) GetUserRole(ctx context.Context, userID, tenantID uuid.UUID) (string, error)
GetUserRole returns the role for a user (test helper)
func (*TestProvider) ListUserPermissions ¶
func (tp *TestProvider) ListUserPermissions(ctx context.Context, userID, tenantID uuid.UUID) ([]string, error)
ListUserPermissions returns permissions for a user (test helper)
func (*TestProvider) RevokeToken ¶
func (tp *TestProvider) RevokeToken(tokenID string) error
RevokeToken revokes a test token
func (*TestProvider) ValidateTestToken ¶
func (tp *TestProvider) ValidateTestToken(tokenString string) (*Claims, error)
ValidateTestToken validates a test JWT token
type User ¶
type User struct {
ID string `json:"id"`
TenantID string `json:"tenant_id"`
Email string `json:"email,omitempty"`
Scopes []string `json:"scopes,omitempty"`
AuthType Type `json:"auth_type"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
User represents an authenticated user
func GetUserFromContext ¶
GetUserFromContext extracts the authenticated user from the Gin context
Source Files
¶
- api_keys.go
- audit_logger.go
- auth.go
- auth_middleware.go
- authorizer.go
- config_keys.go
- config_types.go
- context.go
- credential_context.go
- credential_middleware.go
- factory.go
- key_types.go
- metrics.go
- middleware.go
- oauth_provider.go
- passthrough.go
- passthrough_middleware.go
- production_authorizer.go
- rate_limit_middleware.go
- rate_limiter.go
- setup.go
- setup_enhanced.go
- test_helpers.go
- test_provider.go