auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package auth provides authentication and session management.

Index

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default auth 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 OAuthUser

type OAuthUser struct {
	ID        string
	Email     string
	Name      string
	AvatarURL string
	Provider  string
}

OAuthUser represents user info from an OAuth provider.

type Service

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

Service handles authentication operations.

func NewService

func NewService(client *ent.Client, cfg *Config) *Service

NewService creates a new auth service.

func (*Service) CleanExpiredSessions

func (s *Service) CleanExpiredSessions(ctx context.Context) (int, error)

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

func (s *Service) ExtendSession(ctx context.Context, token string) error

ExtendSession extends a session's expiration time.

func (*Service) HashPassword

func (s *Service) HashPassword(password string) (string, error)

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) Logout

func (s *Service) Logout(ctx context.Context, token string) error

Logout invalidates a session.

func (*Service) LogoutAll

func (s *Service) LogoutAll(ctx context.Context, userID int) error

LogoutAll invalidates all sessions for a user.

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

func (s *Service) VerifyPassword(hash, password string) bool

VerifyPassword checks if a password matches a hash.

Jump to

Keyboard shortcuts

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