Documentation
¶
Index ¶
- Variables
- type APIKey
- type APIKeyInfo
- type APIKeyManager
- func (m *APIKeyManager) CleanupExpiredKeys() int
- func (m *APIKeyManager) DeleteAPIKey(keyID string) error
- func (m *APIKeyManager) GenerateAPIKey(userID, name string, permissions []Permission, ...) (string, *APIKey, error)
- func (m *APIKeyManager) GetAPIKey(keyID string) (*APIKey, error)
- func (m *APIKeyManager) GetKeyStats() map[string]int
- func (m *APIKeyManager) HasPermission(apiKey *APIKey, permission Permission) bool
- func (m *APIKeyManager) HasRepositoryPermission(apiKey *APIKey, repoID string, permission Permission) bool
- func (m *APIKeyManager) ListAPIKeys(userID string) ([]*APIKey, error)
- func (m *APIKeyManager) RevokeAPIKey(keyID string) error
- func (m *APIKeyManager) UpdateAPIKeyPermissions(keyID string, permissions []Permission, repoPerms map[string][]Permission) error
- func (m *APIKeyManager) ValidateAPIKey(keyString string) (*APIKey, error)
- type AuthMiddleware
- type AuthenticatedUser
- type CachedToken
- type Claims
- type JWTAuth
- func (j *JWTAuth) ExtractUserID(tokenString string) string
- func (j *JWTAuth) GenerateToken(userID, username, email string, permissions []string) (string, error)
- func (j *JWTAuth) GetTokenInfo(tokenString string) *TokenInfo
- func (j *JWTAuth) RefreshToken(tokenString string) (string, error)
- func (j *JWTAuth) ValidateToken(tokenString string) (*Claims, error)
- type Permission
- type RBAC
- func (r *RBAC) ActivateUser(userID string) error
- func (r *RBAC) AssignRole(userID, roleName string) error
- func (r *RBAC) CreateRole(name, description string, permissions []Permission) error
- func (r *RBAC) CreateUser(id, username, email string, roles []string) error
- func (r *RBAC) DeactivateUser(userID string) error
- func (r *RBAC) GetRole(name string) (*Role, error)
- func (r *RBAC) GetUser(userID string) (*User, error)
- func (r *RBAC) GetUserPermissions(userID string) ([]Permission, error)
- func (r *RBAC) GrantRepositoryPermission(userID, repoID string, permission Permission) error
- func (r *RBAC) HasPermission(userID string, permission Permission) bool
- func (r *RBAC) HasRepositoryPermission(userID, repoID string, permission Permission) bool
- func (r *RBAC) ListRoles() []*Role
- func (r *RBAC) RemoveRole(userID, roleName string) error
- func (r *RBAC) RevokeRepositoryPermission(userID, repoID string, permission Permission) error
- type Role
- type TokenInfo
- type User
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoAuthProvided = fmt.Errorf("no authentication provided") ErrInvalidToken = fmt.Errorf("invalid token") ErrExpiredToken = fmt.Errorf("token expired") ErrInvalidAPIKey = fmt.Errorf("invalid API key") )
Custom errors
Functions ¶
This section is empty.
Types ¶
type APIKey ¶
type APIKey struct {
ID string `json:"id"`
Name string `json:"name"`
HashedKey string `json:"-"` // Never expose the actual key
UserID string `json:"user_id"`
Permissions []Permission `json:"permissions"`
RepoPerms map[string][]Permission `json:"repo_permissions"`
Active bool `json:"active"`
LastUsed *time.Time `json:"last_used,omitempty"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
}
APIKey represents an API key for authentication
func (*APIKey) ToInfo ¶
func (k *APIKey) ToInfo() *APIKeyInfo
ToInfo converts an APIKey to APIKeyInfo (removes sensitive data)
type APIKeyInfo ¶
type APIKeyInfo struct {
ID string `json:"id"`
Name string `json:"name"`
UserID string `json:"user_id"`
Permissions []Permission `json:"permissions"`
RepoPerms map[string][]Permission `json:"repo_permissions"`
Active bool `json:"active"`
LastUsed *time.Time `json:"last_used,omitempty"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
}
APIKeyInfo represents public information about an API key
type APIKeyManager ¶
type APIKeyManager struct {
// contains filtered or unexported fields
}
APIKeyManager manages API keys
func NewAPIKeyManager ¶
func NewAPIKeyManager(rbac *RBAC) *APIKeyManager
NewAPIKeyManager creates a new API key manager
func (*APIKeyManager) CleanupExpiredKeys ¶
func (m *APIKeyManager) CleanupExpiredKeys() int
CleanupExpiredKeys removes expired API keys
func (*APIKeyManager) DeleteAPIKey ¶
func (m *APIKeyManager) DeleteAPIKey(keyID string) error
DeleteAPIKey permanently removes an API key
func (*APIKeyManager) GenerateAPIKey ¶
func (m *APIKeyManager) GenerateAPIKey(userID, name string, permissions []Permission, repoPerms map[string][]Permission, expiresAt *time.Time) (string, *APIKey, error)
GenerateAPIKey creates a new API key for a user
func (*APIKeyManager) GetAPIKey ¶
func (m *APIKeyManager) GetAPIKey(keyID string) (*APIKey, error)
GetAPIKey retrieves an API key by ID
func (*APIKeyManager) GetKeyStats ¶
func (m *APIKeyManager) GetKeyStats() map[string]int
GetKeyStats returns statistics about API keys
func (*APIKeyManager) HasPermission ¶
func (m *APIKeyManager) HasPermission(apiKey *APIKey, permission Permission) bool
HasPermission checks if an API key has a specific permission
func (*APIKeyManager) HasRepositoryPermission ¶
func (m *APIKeyManager) HasRepositoryPermission(apiKey *APIKey, repoID string, permission Permission) bool
HasRepositoryPermission checks if an API key has permission for a specific repository
func (*APIKeyManager) ListAPIKeys ¶
func (m *APIKeyManager) ListAPIKeys(userID string) ([]*APIKey, error)
ListAPIKeys returns all API keys for a user
func (*APIKeyManager) RevokeAPIKey ¶
func (m *APIKeyManager) RevokeAPIKey(keyID string) error
RevokeAPIKey deactivates an API key
func (*APIKeyManager) UpdateAPIKeyPermissions ¶
func (m *APIKeyManager) UpdateAPIKeyPermissions(keyID string, permissions []Permission, repoPerms map[string][]Permission) error
UpdateAPIKeyPermissions updates the permissions for an API key
func (*APIKeyManager) ValidateAPIKey ¶
func (m *APIKeyManager) ValidateAPIKey(keyString string) (*APIKey, error)
ValidateAPIKey validates an API key and returns the associated API key record
type AuthMiddleware ¶
type AuthMiddleware struct {
// contains filtered or unexported fields
}
AuthMiddleware provides authentication middleware
func NewAuthMiddleware ¶
func NewAuthMiddleware(jwtAuth *JWTAuth, apiKeyMgr *APIKeyManager, rbac *RBAC) *AuthMiddleware
NewAuthMiddleware creates a new authentication middleware
func (*AuthMiddleware) AuthRequired ¶
func (m *AuthMiddleware) AuthRequired() gin.HandlerFunc
AuthRequired middleware that requires authentication
func (*AuthMiddleware) OptionalAuth ¶
func (m *AuthMiddleware) OptionalAuth() gin.HandlerFunc
OptionalAuth middleware that attempts authentication but doesn't require it
func (*AuthMiddleware) RequirePermission ¶
func (m *AuthMiddleware) RequirePermission(permission Permission) gin.HandlerFunc
RequirePermission middleware that requires a specific permission
func (*AuthMiddleware) RequireRepositoryPermission ¶
func (m *AuthMiddleware) RequireRepositoryPermission(permission Permission) gin.HandlerFunc
RequireRepositoryPermission middleware that requires permission for a specific repository
type AuthenticatedUser ¶
type AuthenticatedUser struct {
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Permissions []Permission `json:"permissions"`
RepoPerms map[string][]Permission `json:"repo_permissions"`
AuthMethod string `json:"auth_method"` // "jwt" or "apikey"
TokenInfo interface{} `json:"token_info,omitempty"`
}
AuthenticatedUser represents an authenticated user context
func GetCurrentUser ¶
func GetCurrentUser(c *gin.Context) (*AuthenticatedUser, bool)
GetCurrentUser returns the current authenticated user from gin context
type CachedToken ¶
CachedToken represents a cached token validation result
type Claims ¶
type Claims struct {
UserID string `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
Permissions []string `json:"permissions"`
jwt.RegisteredClaims
}
Claims represents the JWT claims
type JWTAuth ¶
type JWTAuth struct {
Secret string
Issuer string
TTL time.Duration
// contains filtered or unexported fields
}
JWTAuth handles JWT token generation and validation
func NewJWTAuth ¶
NewJWTAuth creates a new JWT authentication handler
func (*JWTAuth) ExtractUserID ¶
ExtractUserID extracts user ID from token without full validation (for logging/metrics)
func (*JWTAuth) GenerateToken ¶
func (j *JWTAuth) GenerateToken(userID, username, email string, permissions []string) (string, error)
GenerateToken creates a new JWT token for a user
func (*JWTAuth) GetTokenInfo ¶
GetTokenInfo returns detailed information about a token
func (*JWTAuth) RefreshToken ¶
RefreshToken generates a new token with the same claims but extended expiration
type Permission ¶
type Permission string
Permission represents a specific permission
const ( // Repository permissions PermissionRepoRead Permission = "repo:read" PermissionRepoWrite Permission = "repo:write" PermissionRepoDelete Permission = "repo:delete" PermissionRepoAdmin Permission = "repo:admin" // System permissions PermissionSystemRead Permission = "system:read" PermissionSystemWrite Permission = "system:write" PermissionSystemAdmin Permission = "system:admin" // User management permissions PermissionUserRead Permission = "user:read" PermissionUserWrite Permission = "user:write" PermissionUserDelete Permission = "user:delete" // Webhook permissions PermissionWebhookRead Permission = "webhook:read" PermissionWebhookWrite Permission = "webhook:write" PermissionWebhookDelete Permission = "webhook:delete" )
type RBAC ¶
type RBAC struct {
// contains filtered or unexported fields
}
RBAC manages role-based access control
func (*RBAC) ActivateUser ¶
ActivateUser activates a user account
func (*RBAC) AssignRole ¶
AssignRole assigns a role to a user
func (*RBAC) CreateRole ¶
func (r *RBAC) CreateRole(name, description string, permissions []Permission) error
CreateRole creates a new role
func (*RBAC) CreateUser ¶
CreateUser creates a new user
func (*RBAC) DeactivateUser ¶
DeactivateUser deactivates a user account
func (*RBAC) GetUserPermissions ¶
func (r *RBAC) GetUserPermissions(userID string) ([]Permission, error)
GetUserPermissions returns all permissions for a user (from roles + direct permissions)
func (*RBAC) GrantRepositoryPermission ¶
func (r *RBAC) GrantRepositoryPermission(userID, repoID string, permission Permission) error
GrantRepositoryPermission grants a user permission to a specific repository
func (*RBAC) HasPermission ¶
func (r *RBAC) HasPermission(userID string, permission Permission) bool
HasPermission checks if a user has a specific permission
func (*RBAC) HasRepositoryPermission ¶
func (r *RBAC) HasRepositoryPermission(userID, repoID string, permission Permission) bool
HasRepositoryPermission checks if a user has permission for a specific repository
func (*RBAC) RemoveRole ¶
RemoveRole removes a role from a user
func (*RBAC) RevokeRepositoryPermission ¶
func (r *RBAC) RevokeRepositoryPermission(userID, repoID string, permission Permission) error
RevokeRepositoryPermission removes a user's permission from a specific repository
type Role ¶
type Role struct {
Name string `json:"name"`
Description string `json:"description"`
Permissions []Permission `json:"permissions"`
}
Role represents a collection of permissions
type TokenInfo ¶
type TokenInfo struct {
UserID string `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
Permissions []string `json:"permissions"`
IssuedAt time.Time `json:"issued_at"`
ExpiresAt time.Time `json:"expires_at"`
Valid bool `json:"valid"`
}
TokenInfo represents information about a token
type User ¶
type User struct {
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Roles []string `json:"roles"`
Permissions []Permission `json:"permissions"` // Direct permissions
RepoPerms map[string][]Permission `json:"repo_permissions"` // Repository-specific permissions
Active bool `json:"active"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
User represents a user in the system