Documentation
¶
Overview ¶
Package auth provides authentication and session management.
Index ¶
- Variables
- type Config
- type OAuthProvider
- type OAuthService
- func (s *OAuthService) ConfigureGitHub(clientID, clientSecret, redirectURL string)
- func (s *OAuthService) ConfigureGoogle(clientID, clientSecret, redirectURL string)
- func (s *OAuthService) ConfigureOIDC(name, clientID, clientSecret, issuerURL, redirectURL string) error
- func (s *OAuthService) ExchangeCode(ctx context.Context, providerName, code string, orgID int, ...) (*ent.Session, error)
- func (s *OAuthService) GetAuthURL(providerName, state string) (string, error)
- func (s *OAuthService) RegisterProvider(provider *OAuthProvider)
- type OAuthUser
- type Service
- func (s *Service) CleanExpiredSessions(ctx context.Context) (int, error)
- func (s *Service) CreateSession(ctx context.Context, userID int, ipAddress, userAgent string) (*ent.Session, error)
- func (s *Service) ExtendSession(ctx context.Context, token string) error
- func (s *Service) HashPassword(password string) (string, error)
- func (s *Service) Login(ctx context.Context, orgID int, email, password, ipAddress, userAgent string) (*ent.Session, error)
- func (s *Service) Logout(ctx context.Context, token string) error
- func (s *Service) LogoutAll(ctx context.Context, userID int) error
- func (s *Service) ValidateSession(ctx context.Context, token string) (*ent.User, *ent.Session, error)
- func (s *Service) VerifyPassword(hash, password string) bool
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidCredentials is returned when email/password don't match. ErrInvalidCredentials = errors.New("invalid credentials") // ErrUserNotFound is returned when user doesn't exist. ErrUserNotFound = errors.New("user not found") // ErrUserInactive is returned when user account is disabled. ErrUserInactive = errors.New("user account is inactive") // ErrSessionExpired is returned when session has expired. ErrSessionExpired = errors.New("session expired") // ErrSessionInvalid is returned when session token is invalid. ErrSessionInvalid = errors.New("invalid session") )
var ( // ErrOAuthFailed is returned when OAuth authentication fails. ErrOAuthFailed = errors.New("OAuth authentication failed") // ErrProviderNotConfigured is returned when OAuth provider is not configured. ErrProviderNotConfigured = errors.New("OAuth provider not configured") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// SessionDuration is how long sessions last (default: 24 hours)
SessionDuration time.Duration
// BcryptCost is the bcrypt hashing cost (default: 12)
BcryptCost int
}
Config holds auth service configuration.
type OAuthProvider ¶
type OAuthProvider struct {
Name string
ClientID string
ClientSecret string
AuthURL string
TokenURL string
UserInfoURL string
Scopes []string
RedirectURL string
}
OAuthProvider represents an OAuth 2.0 provider configuration.
type OAuthService ¶
type OAuthService struct {
// contains filtered or unexported fields
}
OAuthService handles OAuth authentication.
func NewOAuthService ¶
func NewOAuthService(client *ent.Client, auth *Service) *OAuthService
NewOAuthService creates a new OAuth service.
func (*OAuthService) ConfigureGitHub ¶
func (s *OAuthService) ConfigureGitHub(clientID, clientSecret, redirectURL string)
ConfigureGitHub configures GitHub OAuth.
func (*OAuthService) ConfigureGoogle ¶
func (s *OAuthService) ConfigureGoogle(clientID, clientSecret, redirectURL string)
ConfigureGoogle configures Google OAuth.
func (*OAuthService) ConfigureOIDC ¶
func (s *OAuthService) ConfigureOIDC(name, clientID, clientSecret, issuerURL, redirectURL string) error
ConfigureOIDC configures a generic OIDC provider.
func (*OAuthService) ExchangeCode ¶
func (s *OAuthService) ExchangeCode(ctx context.Context, providerName, code string, orgID int, ipAddress, userAgent string) (*ent.Session, error)
ExchangeCode exchanges an authorization code for user info and creates a session.
func (*OAuthService) GetAuthURL ¶
func (s *OAuthService) GetAuthURL(providerName, state string) (string, error)
GetAuthURL returns the OAuth authorization URL for a provider.
func (*OAuthService) RegisterProvider ¶
func (s *OAuthService) RegisterProvider(provider *OAuthProvider)
RegisterProvider registers an OAuth provider.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service handles authentication operations.
func NewService ¶
NewService creates a new auth service.
func (*Service) CleanExpiredSessions ¶
CleanExpiredSessions removes all expired sessions.
func (*Service) CreateSession ¶
func (s *Service) CreateSession(ctx context.Context, userID int, ipAddress, userAgent string) (*ent.Session, error)
CreateSession creates a new session for a user.
func (*Service) ExtendSession ¶
ExtendSession extends a session's expiration time.
func (*Service) HashPassword ¶
HashPassword hashes a password using bcrypt.
func (*Service) Login ¶
func (s *Service) Login(ctx context.Context, orgID int, email, password, ipAddress, userAgent string) (*ent.Session, error)
Login authenticates a user with email and password.
func (*Service) ValidateSession ¶
func (s *Service) ValidateSession(ctx context.Context, token string) (*ent.User, *ent.Session, error)
ValidateSession validates a session token and returns the associated user.
func (*Service) VerifyPassword ¶
VerifyPassword checks if a password matches a hash.