Documentation
¶
Index ¶
- func GetUserFromContext(ctx HTTPContext) (string, error)
- type AuthResponse
- type AuthService
- func (a *AuthService) GetOAuthURL(provider OAuthProvider, redirectURI string) (string, error)
- func (a *AuthService) GetSession(ctx context.Context, sessionID string) (*SessionData, error)
- func (a *AuthService) Logout(ctx context.Context, sessionID string) error
- func (a *AuthService) LogoutAllSessions(ctx context.Context, userID string) error
- func (a *AuthService) OAuthSignIn(ctx context.Context, provider OAuthProvider, state, code string) (*AuthResponse, error)
- func (a *AuthService) RefreshToken(ctx context.Context, refreshToken string) (*AuthResponse, error)
- func (a *AuthService) SignIn(ctx context.Context, req *SignInRequest) (*AuthResponse, error)
- func (a *AuthService) SignUp(ctx context.Context, req *SignUpRequest) (*AuthResponse, error)
- func (a *AuthService) ValidateToken(token string) (*TokenClaims, error)
- type Config
- type GenericAuthHandlers
- func (h *GenericAuthHandlers) AuthMiddleware() HTTPMiddleware
- func (h *GenericAuthHandlers) GetUserHandler(ctx HTTPContext) error
- func (h *GenericAuthHandlers) LogoutHandler(ctx HTTPContext) error
- func (h *GenericAuthHandlers) OAuthCallbackHandler(provider string) HTTPHandler
- func (h *GenericAuthHandlers) OAuthHandler(provider string) HTTPHandler
- func (h *GenericAuthHandlers) OptionalAuthMiddleware() HTTPMiddleware
- func (h *GenericAuthHandlers) RefreshTokenHandler(ctx HTTPContext) error
- func (h *GenericAuthHandlers) SignInHandler(ctx HTTPContext) error
- func (h *GenericAuthHandlers) SignUpHandler(ctx HTTPContext) error
- type HTTPContext
- type HTTPHandler
- type HTTPMiddleware
- type JWTManager
- type MemorySessionStore
- func (m *MemorySessionStore) Delete(ctx context.Context, keys ...string) error
- func (m *MemorySessionStore) Exists(ctx context.Context, keys ...string) (bool, error)
- func (m *MemorySessionStore) Get(ctx context.Context, key string, dest interface{}) error
- func (m *MemorySessionStore) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- type OAuthManager
- type OAuthProvider
- type OAuthState
- type OAuthUserInfo
- type RedisSessionStore
- func (r *RedisSessionStore) Close() error
- func (r *RedisSessionStore) Delete(ctx context.Context, keys ...string) error
- func (r *RedisSessionStore) Exists(ctx context.Context, keys ...string) (bool, error)
- func (r *RedisSessionStore) Get(ctx context.Context, key string, dest interface{}) error
- func (r *RedisSessionStore) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- type Router
- type SessionData
- type SessionManager
- func (s *SessionManager) CreateSession(ctx context.Context, userID, email string, duration time.Duration) (string, error)
- func (s *SessionManager) GetSession(ctx context.Context, sessionID string) (*SessionData, error)
- func (s *SessionManager) InvalidateSession(ctx context.Context, sessionID string) error
- func (s *SessionManager) InvalidateUserSessions(ctx context.Context, userID string) error
- type SessionStore
- type SignInRequest
- type SignUpRequest
- type TokenClaims
- type User
- type UserStore
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetUserFromContext ¶
func GetUserFromContext(ctx HTTPContext) (string, error)
GetUserFromContext extracts user ID from context
Types ¶
type AuthResponse ¶
type AuthResponse struct {
User *User `json:"user"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token,omitempty"`
ExpiresIn int64 `json:"expires_in"`
}
AuthResponse is returned after successful authentication
type AuthService ¶
type AuthService struct {
// contains filtered or unexported fields
}
AuthService handles authentication operations
func NewAuthService ¶
func NewAuthService(config *Config, userStore UserStore, sessionStore SessionStore) *AuthService
NewAuthService creates a new authentication service
func (*AuthService) GetOAuthURL ¶
func (a *AuthService) GetOAuthURL(provider OAuthProvider, redirectURI string) (string, error)
GetOAuthURL generates OAuth authorization URL
func (*AuthService) GetSession ¶
func (a *AuthService) GetSession(ctx context.Context, sessionID string) (*SessionData, error)
GetSession retrieves session data
func (*AuthService) Logout ¶
func (a *AuthService) Logout(ctx context.Context, sessionID string) error
Logout invalidates a session
func (*AuthService) LogoutAllSessions ¶
func (a *AuthService) LogoutAllSessions(ctx context.Context, userID string) error
LogoutAllSessions invalidates all sessions for a user
func (*AuthService) OAuthSignIn ¶
func (a *AuthService) OAuthSignIn(ctx context.Context, provider OAuthProvider, state, code string) (*AuthResponse, error)
OAuthSignIn handles OAuth authentication
func (*AuthService) RefreshToken ¶
func (a *AuthService) RefreshToken(ctx context.Context, refreshToken string) (*AuthResponse, error)
RefreshToken generates new access token from refresh token
func (*AuthService) SignIn ¶
func (a *AuthService) SignIn(ctx context.Context, req *SignInRequest) (*AuthResponse, error)
SignIn authenticates a user with email and password
func (*AuthService) SignUp ¶
func (a *AuthService) SignUp(ctx context.Context, req *SignUpRequest) (*AuthResponse, error)
SignUp registers a new user with email and password
func (*AuthService) ValidateToken ¶
func (a *AuthService) ValidateToken(token string) (*TokenClaims, error)
ValidateToken validates an access token and returns claims
type Config ¶
type Config struct {
// JWT Configuration
JWTSecret string
JWTExpiration time.Duration
JWTIssuer string
// OAuth Google Configuration
GoogleClientID string
GoogleClientSecret string
GoogleRedirectURI string
GoogleScopes []string
// OAuth GitHub Configuration
GitHubClientID string
GitHubClientSecret string
GitHubRedirectURI string
GitHubScopes []string
// General OAuth Configuration
OAuthStateExpiration time.Duration
FrontendSuccessURL string
FrontendErrorURL string
// Redis Configuration (optional)
RedisURL string
EnableRedisCache bool
// Security Settings
BCryptCost int
AllowSignup bool
RequireEmailVerification bool
}
type GenericAuthHandlers ¶
type GenericAuthHandlers struct {
// contains filtered or unexported fields
}
GenericAuthHandlers provides framework-agnostic HTTP handlers for authentication
func NewGenericAuthHandlers ¶
func NewGenericAuthHandlers(authService *AuthService, config *Config) *GenericAuthHandlers
NewGenericAuthHandlers creates new framework-agnostic authentication handlers
func (*GenericAuthHandlers) AuthMiddleware ¶
func (h *GenericAuthHandlers) AuthMiddleware() HTTPMiddleware
AuthMiddleware validates JWT tokens and sets user context
func (*GenericAuthHandlers) GetUserHandler ¶
func (h *GenericAuthHandlers) GetUserHandler(ctx HTTPContext) error
GetUserHandler returns current user info
func (*GenericAuthHandlers) LogoutHandler ¶
func (h *GenericAuthHandlers) LogoutHandler(ctx HTTPContext) error
LogoutHandler handles user logout
func (*GenericAuthHandlers) OAuthCallbackHandler ¶
func (h *GenericAuthHandlers) OAuthCallbackHandler(provider string) HTTPHandler
OAuthCallbackHandler handles OAuth callback
func (*GenericAuthHandlers) OAuthHandler ¶
func (h *GenericAuthHandlers) OAuthHandler(provider string) HTTPHandler
OAuthHandler initiates OAuth flow
func (*GenericAuthHandlers) OptionalAuthMiddleware ¶
func (h *GenericAuthHandlers) OptionalAuthMiddleware() HTTPMiddleware
OptionalAuthMiddleware allows both authenticated and unauthenticated requests
func (*GenericAuthHandlers) RefreshTokenHandler ¶
func (h *GenericAuthHandlers) RefreshTokenHandler(ctx HTTPContext) error
RefreshTokenHandler handles token refresh
func (*GenericAuthHandlers) SignInHandler ¶
func (h *GenericAuthHandlers) SignInHandler(ctx HTTPContext) error
SignInHandler handles user login
func (*GenericAuthHandlers) SignUpHandler ¶
func (h *GenericAuthHandlers) SignUpHandler(ctx HTTPContext) error
SignUpHandler handles user registration
type HTTPContext ¶
type HTTPContext interface {
// Request operations
Context() context.Context
Request() *http.Request
GetHeader(key string) string
GetQueryParam(key string) string
GetFormValue(key string) string
Bind(dest interface{}) error
// Response operations
SetHeader(key, value string)
SetStatus(code int)
JSON(code int, data interface{}) error
Redirect(code int, url string) error
String(code int, text string) error
// Cookie operations
GetCookie(name string) (*http.Cookie, error)
SetCookie(cookie *http.Cookie)
// Context values (for middleware)
Set(key string, value interface{})
Get(key string) interface{}
}
HTTPContext is a framework-agnostic interface for handling HTTP requests
type HTTPHandler ¶
type HTTPHandler func(HTTPContext) error
HTTPHandler is a generic handler function
type HTTPMiddleware ¶
type HTTPMiddleware func(HTTPHandler) HTTPHandler
HTTPMiddleware is a generic middleware function
type JWTManager ¶
type JWTManager struct {
// contains filtered or unexported fields
}
func NewJWTManager ¶
func NewJWTManager(secret string, issuer string, expiresIn time.Duration) *JWTManager
func (*JWTManager) GenerateRefreshToken ¶
func (j *JWTManager) GenerateRefreshToken(userID string) (string, error)
func (*JWTManager) GenerateToken ¶
func (j *JWTManager) GenerateToken(claims TokenClaims) (string, error)
func (*JWTManager) ValidateRefreshToken ¶
func (j *JWTManager) ValidateRefreshToken(tokenString string) (string, error)
func (*JWTManager) ValidateToken ¶
func (j *JWTManager) ValidateToken(tokenString string) (*TokenClaims, error)
type MemorySessionStore ¶
type MemorySessionStore struct {
// contains filtered or unexported fields
}
MemorySessionStore uses in-memory storage (for development/testing)
func NewMemorySessionStore ¶
func NewMemorySessionStore() *MemorySessionStore
func (*MemorySessionStore) Delete ¶
func (m *MemorySessionStore) Delete(ctx context.Context, keys ...string) error
type OAuthManager ¶
type OAuthManager struct {
// contains filtered or unexported fields
}
func NewOAuthManager ¶
func NewOAuthManager(config *Config, sessionStore SessionStore) *OAuthManager
func (*OAuthManager) GetAuthURL ¶
func (o *OAuthManager) GetAuthURL(provider OAuthProvider, redirectURI string) (string, error)
GetAuthURL generates the OAuth authorization URL
func (*OAuthManager) ValidateCallback ¶
func (o *OAuthManager) ValidateCallback(provider OAuthProvider, state, code string) (*OAuthUserInfo, string, error)
ValidateCallback validates OAuth callback and returns user info
type OAuthProvider ¶
type OAuthProvider string
OAuthProvider represents an OAuth provider
const ( ProviderGoogle OAuthProvider = "google" ProviderGitHub OAuthProvider = "github" ProviderLocal OAuthProvider = "local" )
type OAuthState ¶
type OAuthState struct {
State string `json:"state"`
RedirectURI string `json:"redirect_uri"`
ExpiresAt time.Time `json:"expires_at"`
}
OAuthState represents OAuth state data
type OAuthUserInfo ¶
type OAuthUserInfo struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`
Provider string `json:"provider"`
}
OAuthUserInfo contains user information from OAuth providers
type RedisSessionStore ¶
type RedisSessionStore struct {
// contains filtered or unexported fields
}
RedisSessionStore uses Redis for session storage
func NewRedisSessionStore ¶
func NewRedisSessionStore(redisURL string) (*RedisSessionStore, error)
func (*RedisSessionStore) Close ¶
func (r *RedisSessionStore) Close() error
func (*RedisSessionStore) Delete ¶
func (r *RedisSessionStore) Delete(ctx context.Context, keys ...string) error
type Router ¶
type Router interface {
GET(path string, handler HTTPHandler, middleware ...HTTPMiddleware)
POST(path string, handler HTTPHandler, middleware ...HTTPMiddleware)
PUT(path string, handler HTTPHandler, middleware ...HTTPMiddleware)
DELETE(path string, handler HTTPHandler, middleware ...HTTPMiddleware)
Group(prefix string, middleware ...HTTPMiddleware) Router
}
Router interface for registering routes
type SessionData ¶
type SessionData struct {
UserID string `json:"user_id"`
Email string `json:"email"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
}
SessionData represents session information
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager handles session operations
func NewSessionManager ¶
func NewSessionManager(store SessionStore, prefix string) *SessionManager
func (*SessionManager) CreateSession ¶
func (*SessionManager) GetSession ¶
func (s *SessionManager) GetSession(ctx context.Context, sessionID string) (*SessionData, error)
func (*SessionManager) InvalidateSession ¶
func (s *SessionManager) InvalidateSession(ctx context.Context, sessionID string) error
func (*SessionManager) InvalidateUserSessions ¶
func (s *SessionManager) InvalidateUserSessions(ctx context.Context, userID string) error
type SessionStore ¶
type SignInRequest ¶
type SignInRequest struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required"`
}
SignInRequest for email/password login
type SignUpRequest ¶
type SignUpRequest struct {
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required,min=6"`
Name string `json:"name,omitempty"`
}
SignUpRequest for email/password registration
type TokenClaims ¶
type TokenClaims struct {
UserID string `json:"user_id"`
Email string `json:"email"`
Name string `json:"name,omitempty"`
Provider string `json:"provider,omitempty"`
}
TokenClaims represents JWT token claims
type User ¶
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
Provider string `json:"provider,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
User represents a user in the system
type UserStore ¶
type UserStore interface {
CreateUser(ctx context.Context, user *User, hashedPassword string) error
GetUserByEmail(ctx context.Context, email string) (*User, string, error) // returns user and hashed password
GetUserByID(ctx context.Context, userID string) (*User, error)
UpdateUser(ctx context.Context, user *User) error
UserExists(ctx context.Context, email string) (bool, error)
}
UserStore interface for user persistence