auth

package
v0.0.0-...-27e9d4f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthContextKey = "auth"
	AuthTypeJWT    = "jwt"
	AuthTypeAPIKey = "apikey"
)

Variables

View Source
var (
	ErrUserNotFound      = errors.New("user not found")
	ErrUserAlreadyExists = errors.New("user already exists")
	ErrAPIKeyNotFound    = errors.New("api key not found")
	ErrAPIKeyRevoked     = errors.New("api key has been revoked")
	ErrAPIKeyExpired     = errors.New("api key has expired")
	ErrInvalidAPIKey     = errors.New("invalid api key")
)

Functions

func HashPassword

func HashPassword(password string) (string, error)

func VerifyPassword

func VerifyPassword(password, hash string) bool

Types

type APIKey

type APIKey struct {
	ID        string     `json:"id"`
	KeyHash   string     `json:"-"`
	KeyPrefix string     `json:"keyPrefix"`
	Name      string     `json:"name"`
	NodeID    string     `json:"nodeId,omitempty"`
	Role      Role       `json:"role"`
	CreatedAt time.Time  `json:"createdAt"`
	ExpiresAt *time.Time `json:"expiresAt,omitempty"`
	LastUsed  *time.Time `json:"lastUsed,omitempty"`
	Revoked   bool       `json:"revoked"`
}

type APIKeyManager

type APIKeyManager struct {
	// contains filtered or unexported fields
}

func NewAPIKeyManager

func NewAPIKeyManager(secret string) *APIKeyManager

func (*APIKeyManager) CreateAPIKey

func (m *APIKeyManager) CreateAPIKey(name string, nodeID string, role Role) (*APIKey, string, error)

func (*APIKeyManager) GenerateKey

func (m *APIKeyManager) GenerateKey() (plainKey string, keyHash string, keyPrefix string, err error)

func (*APIKeyManager) HashKey

func (m *APIKeyManager) HashKey(key string) string

func (*APIKeyManager) ValidateKeyFormat

func (m *APIKeyManager) ValidateKeyFormat(key string) bool

type AuthContext

type AuthContext struct {
	UserID   string
	Username string
	Role     Role
	NodeID   string
	AuthType string
}

func GetAuthContext

func GetAuthContext(c echo.Context) *AuthContext

type AuthHandlers

type AuthHandlers struct {
	// contains filtered or unexported fields
}

func NewAuthHandlers

func NewAuthHandlers(
	authStore AuthStore, jwtManager *JWTManager, keyManager *APIKeyManager, config Config,
) *AuthHandlers

func (*AuthHandlers) CreateAPIKey

func (h *AuthHandlers) CreateAPIKey(c echo.Context) error

func (*AuthHandlers) CreateUser

func (h *AuthHandlers) CreateUser(c echo.Context) error

func (*AuthHandlers) DeleteUser

func (h *AuthHandlers) DeleteUser(c echo.Context) error

func (*AuthHandlers) GetCurrentUser

func (h *AuthHandlers) GetCurrentUser(c echo.Context) error

func (*AuthHandlers) GetUser

func (h *AuthHandlers) GetUser(c echo.Context) error

func (*AuthHandlers) ListAPIKeys

func (h *AuthHandlers) ListAPIKeys(c echo.Context) error

func (*AuthHandlers) ListUsers

func (h *AuthHandlers) ListUsers(c echo.Context) error

func (*AuthHandlers) Login

func (h *AuthHandlers) Login(c echo.Context) error

func (*AuthHandlers) RefreshToken

func (h *AuthHandlers) RefreshToken(c echo.Context) error

func (*AuthHandlers) RegisterRoutes

func (h *AuthHandlers) RegisterRoutes(e *echo.Echo, authMiddleware *Middleware)

func (*AuthHandlers) RevokeAPIKey

func (h *AuthHandlers) RevokeAPIKey(c echo.Context) error

func (*AuthHandlers) Signup

func (h *AuthHandlers) Signup(c echo.Context) error

func (*AuthHandlers) UpdateUser

func (h *AuthHandlers) UpdateUser(c echo.Context) error

type AuthStore

type AuthStore interface {
	AddUser(user User) error
	GetUser(userID string) (User, error)
	GetUserByUsername(username string) (User, error)
	UpdateUser(userID string, updates UserUpdate) error
	ListUsers() ([]User, error)
	DeleteUser(userID string) error

	AddAPIKey(apiKey APIKey) error
	GetAPIKey(keyID string) (APIKey, error)
	GetAPIKeyByHash(keyHash string) (APIKey, error)
	UpdateAPIKeyLastUsed(keyID string) error
	RevokeAPIKey(keyID string) error
	ListAPIKeys() ([]APIKey, error)
	ListAPIKeysByNodeID(nodeID string) ([]APIKey, error)
	DeleteAPIKey(keyID string) error
}

type Claims

type Claims struct {
	UserID   string `json:"userId"`
	Username string `json:"username"`
	Role     Role   `json:"role"`
	jwt.RegisteredClaims
}

type Config

type Config struct {
	Enabled        bool
	JWTSecret      string
	APIKeySecret   string
	TokenExpiry    time.Duration
	RefreshExpiry  time.Duration
	AllowedOrigins []string
}

func DefaultConfig

func DefaultConfig() Config

type CreateAPIKeyRequest

type CreateAPIKeyRequest struct {
	Name   string `json:"name"`
	NodeID string `json:"nodeId,omitempty"`
	Role   Role   `json:"role"`
}

type CreateAPIKeyResponse

type CreateAPIKeyResponse struct {
	Key    string `json:"key"`
	APIKey APIKey `json:"apiKey"`
}

type CreateUserRequest

type CreateUserRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
	Role     Role   `json:"role"`
}

type JWTManager

type JWTManager struct {
	// contains filtered or unexported fields
}

func NewJWTManager

func NewJWTManager(secret string, tokenExpiry, refreshExpiry time.Duration) *JWTManager

func (*JWTManager) GenerateRefreshToken

func (m *JWTManager) GenerateRefreshToken(user *User) (string, time.Time, error)

func (*JWTManager) GenerateToken

func (m *JWTManager) GenerateToken(user *User) (string, time.Time, error)

func (*JWTManager) RefreshToken

func (m *JWTManager) RefreshToken(refreshTokenString string) (string, time.Time, error)

func (*JWTManager) ValidateToken

func (m *JWTManager) ValidateToken(tokenString string) (*Claims, error)

type LoginRequest

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type LoginResponse

type LoginResponse struct {
	Token        string    `json:"token"`
	RefreshToken string    `json:"refreshToken,omitempty"`
	ExpiresAt    time.Time `json:"expiresAt"`
	User         UserInfo  `json:"user"`
}

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

func NewMiddleware

func NewMiddleware(config Config, authStore AuthStore) *Middleware

func (*Middleware) APIKeyManager

func (m *Middleware) APIKeyManager() *APIKeyManager

func (*Middleware) Authenticate

func (m *Middleware) Authenticate() echo.MiddlewareFunc

func (*Middleware) JWTManager

func (m *Middleware) JWTManager() *JWTManager

func (*Middleware) RequireDelete

func (m *Middleware) RequireDelete() echo.MiddlewareFunc

func (*Middleware) RequireNodeManagement

func (m *Middleware) RequireNodeManagement() echo.MiddlewareFunc

func (*Middleware) RequireRole

func (m *Middleware) RequireRole(roles ...Role) echo.MiddlewareFunc

func (*Middleware) RequireWrite

func (m *Middleware) RequireWrite() echo.MiddlewareFunc

func (*Middleware) SetSkipPaths

func (m *Middleware) SetSkipPaths(paths ...string)

func (*Middleware) SetSkipPrefixes

func (m *Middleware) SetSkipPrefixes(prefixes ...string)

func (*Middleware) SetSkipSuffixes

func (m *Middleware) SetSkipSuffixes(suffixes ...string)

type PostgresAuthStore

type PostgresAuthStore struct {
	// contains filtered or unexported fields
}

func NewPostgresAuthStore

func NewPostgresAuthStore(db *sql.DB) *PostgresAuthStore

func (*PostgresAuthStore) AddAPIKey

func (s *PostgresAuthStore) AddAPIKey(apiKey APIKey) error

func (*PostgresAuthStore) AddUser

func (s *PostgresAuthStore) AddUser(user User) error

func (*PostgresAuthStore) DeleteAPIKey

func (s *PostgresAuthStore) DeleteAPIKey(keyID string) error

func (*PostgresAuthStore) DeleteUser

func (s *PostgresAuthStore) DeleteUser(userID string) error

func (*PostgresAuthStore) GetAPIKey

func (s *PostgresAuthStore) GetAPIKey(keyID string) (APIKey, error)

func (*PostgresAuthStore) GetAPIKeyByHash

func (s *PostgresAuthStore) GetAPIKeyByHash(keyHash string) (APIKey, error)

func (*PostgresAuthStore) GetUser

func (s *PostgresAuthStore) GetUser(userID string) (User, error)

func (*PostgresAuthStore) GetUserByUsername

func (s *PostgresAuthStore) GetUserByUsername(username string) (User, error)

func (*PostgresAuthStore) ListAPIKeys

func (s *PostgresAuthStore) ListAPIKeys() ([]APIKey, error)

func (*PostgresAuthStore) ListAPIKeysByNodeID

func (s *PostgresAuthStore) ListAPIKeysByNodeID(nodeID string) ([]APIKey, error)

func (*PostgresAuthStore) ListUsers

func (s *PostgresAuthStore) ListUsers() ([]User, error)

func (*PostgresAuthStore) RevokeAPIKey

func (s *PostgresAuthStore) RevokeAPIKey(keyID string) error

func (*PostgresAuthStore) UpdateAPIKeyLastUsed

func (s *PostgresAuthStore) UpdateAPIKeyLastUsed(keyID string) error

func (*PostgresAuthStore) UpdateUser

func (s *PostgresAuthStore) UpdateUser(userID string, updates UserUpdate) error

type Role

type Role string
const (
	RoleAdmin    Role = "admin"
	RoleOperator Role = "operator"
	RoleViewer   Role = "viewer"
)

func (Role) CanDelete

func (r Role) CanDelete() bool

func (Role) CanManageNodes

func (r Role) CanManageNodes() bool

func (Role) CanWrite

func (r Role) CanWrite() bool

func (Role) IsValid

func (r Role) IsValid() bool

type SignupRequest

type SignupRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type UpdateUserRequest

type UpdateUserRequest struct {
	Password *string `json:"password,omitempty"`
	Role     *Role   `json:"role,omitempty"`
	Disabled *bool   `json:"disabled,omitempty"`
}

type User

type User struct {
	ID           string     `json:"id"`
	Username     string     `json:"username"`
	PasswordHash string     `json:"-"`
	Role         Role       `json:"role"`
	CreatedAt    time.Time  `json:"createdAt"`
	LastLogin    *time.Time `json:"lastLogin,omitempty"`
	Disabled     bool       `json:"disabled"`
}

type UserInfo

type UserInfo struct {
	ID       string `json:"id"`
	Username string `json:"username"`
	Role     Role   `json:"role"`
}

type UserUpdate

type UserUpdate struct {
	PasswordHash *string
	Role         *Role
	LastLogin    *time.Time
	Disabled     *bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL