auth

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 12, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSessionFromContext

func GetSessionFromContext(ctx context.Context) (behemoth.Session, bool)

GetSessionFromContext extracts a session from a given context if it was set by the SessionManager Middleware

func InitDatabase

func InitDatabase[T behemoth.User](
	cfg *behemoth.DatabaseConfig[T],
	sessionFactory behemoth.SessionFactory,
) (behemoth.Database[T], error)

Types

type Behemoth

type Behemoth[T behemoth.User] struct {
	DB          behemoth.Database[T]
	Password    *PasswordAuth[T]
	OAuth       *OAuthAuth[T]
	JWT         *JWTService
	Session     *SessionManager
	UseSessions bool
}

func New

func New[T behemoth.User](cfg *behemoth.Config[T]) (*Behemoth[T], error)

New creates a new Behemoth instance with the given config.

type DefaultJWTClaims

type DefaultJWTClaims struct {
	ID string `json:"id"`
	jwt.RegisteredClaims
}

type JWTService

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

func NewJWTService

func NewJWTService(cfg behemoth.JWTConfig) *JWTService

func (*JWTService) GenerateToken

func (j *JWTService) GenerateToken(user behemoth.User) (string, error)

func (*JWTService) ValidateToken

func (j *JWTService) ValidateToken(tokenStr string) (jwt.Claims, error)

type OAuthAuth

type OAuthAuth[T behemoth.User] struct {
	// contains filtered or unexported fields
}

OAuthAuth manages OAuth-based authentication for multiple providers. It supports generic user types and handles authentication flows for providers like Google and Facebook.

func NewOAuthAuth

func NewOAuthAuth[T behemoth.User](
	oAuthProviders []behemoth.Provider,
	jwtSvc *JWTService,
	useDefaultUser bool,
	user behemoth.User,
	db behemoth.Database[T],
) *OAuthAuth[T]

func (*OAuthAuth[T]) AuthURL

func (o *OAuthAuth[T]) AuthURL(req *http.Request, state string) (string, error)

func (*OAuthAuth[T]) Authenticate

func (o *OAuthAuth[T]) Authenticate(providerName string, creds any) (behemoth.User, error)

Authenticate performs OAuth authentication for the specified provider using the given credentials. It exchanges the OAuth code for a token, fetches user info, and saves the user to the database. Returns the authenticated user or an error if authentication fails.

func (*OAuthAuth[T]) Register

func (o *OAuthAuth[T]) Register(providerName string, creds any) (behemoth.User, error)

type PasswordAuth

type PasswordAuth[T behemoth.User] struct {
	// contains filtered or unexported fields
}

func NewPasswordAuth

func NewPasswordAuth[T behemoth.User](cfg behemoth.PasswordConfig,
	jwtSvc *JWTService,
	useDefaultUser bool,
	user behemoth.User,
	db behemoth.Database[T],
) *PasswordAuth[T]

func (*PasswordAuth[T]) Authenticate

func (p *PasswordAuth[T]) Authenticate(credentials any) (behemoth.User, error)

func (*PasswordAuth[T]) Create

func (p *PasswordAuth[T]) Create(
	email string,
	username string,
	firstname string,
	lastname string,
	password string,
) (*models.User, error)

type PasswordCredentials

type PasswordCredentials struct {
	PrimaryKey string
	Password   string
}

type SessionManager

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

SessionManager handles session creation, retrieval, and deletion.

func NewSessionManager

func NewSessionManager(
	store behemoth.SessionStore,
	expiry time.Duration,
	cookieName string,
	factory behemoth.SessionFactory,
) *SessionManager

NewSessionManager creates a new SessionManager with the given database and configuration.

func (*SessionManager) CreateSession

func (sm *SessionManager) CreateSession() (behemoth.Session, error)

CreateSession creates a new session with a unique ID and stores it in the backend.

func (*SessionManager) DeleteSession

func (sm *SessionManager) DeleteSession(sessionID string) error

DeleteSession removes a session by ID from the backend.

func (*SessionManager) Expiry

func (sm *SessionManager) Expiry() time.Duration

func (*SessionManager) GetSession

func (sm *SessionManager) GetSession(sessionID string) (behemoth.Session, error)

GetSession retrieves a session by ID, returning an error if not found or expired.

func (*SessionManager) Middleware

func (sm *SessionManager) Middleware(next http.Handler) http.Handler

Middleware checks for a session in incoming requests and adds it to the context.

func (*SessionManager) UpdateSession

func (sm *SessionManager) UpdateSession(session behemoth.Session) (behemoth.Session, error)

Jump to

Keyboard shortcuts

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