lysauth

package
v0.3.55 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 15 Imported by: 0

README

lysauth

Structs and functions for authentication topics. Work in progress.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBlocked             = lyserr.User{Message: "IP is blocked", StatusCode: http.StatusForbidden}
	ErrMaxAttemptsExceeded = lyserr.User{Message: "Max login attempts exceeded", StatusCode: http.StatusForbidden}
)
View Source
var (
	ErrTooManyRequests = lyserr.User{Message: "Too many requests", StatusCode: http.StatusTooManyRequests}
)

Functions

func GetBearerToken

func GetBearerToken(header http.Header) (token string, err error)

GetBearerToken returns the bearer token from the request's Authorization header.

func GetRemoteHostIP

func GetRemoteHostIP(r *http.Request, useXForwardedFor bool, xForwardedForIdx int) (remoteHostIP netip.Addr, err error)

GetRemoteHostIP returns the remote IP from either the request RemoteAddr, or the X-Forwarded-For header. useXForwardedFor: set to true when using nginx reverse proxy or services like Cloudflare, since the remote IP will be in the X-Forwarded-For header instead of RemoteAddr. xForwardedForIdx: if using X-Forwarded-For header, this specifies which IP to use from the header (0 for first, etc). This is needed when there are multiple proxies and thus multiple IPs in the header.

func IsWebSocket

func IsWebSocket(header http.Header) bool

IsWebSocket returns true if the request Upgrade header contains "websocket".

Types

type AppLoginAttempts

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

AppLoginAttempts contains login attempts and methods to manage them.

func NewAppLoginAttempts

func NewAppLoginAttempts(maxAttempts int) *AppLoginAttempts

NewAppLoginAttempts creates a new AppLoginAttempts instance.

func (*AppLoginAttempts) Add

func (appLa *AppLoginAttempts) Add(ip netip.Addr) (err error)

Add tries to increment the number of attempts of the supplied IP if it already exists, or adds it if it doesn't exist.

func (*AppLoginAttempts) All

func (appLa *AppLoginAttempts) All() (loginAttempts []LoginAttempt)

All returns all login attempts.

func (*AppLoginAttempts) Block

func (appLa *AppLoginAttempts) Block(ip netip.Addr) error

func (*AppLoginAttempts) Count

func (appLa *AppLoginAttempts) Count() int

Count returns the number of login attempts.

func (*AppLoginAttempts) DeleteByIp

func (appLa *AppLoginAttempts) DeleteByIp(ip netip.Addr) (found bool, err error)

DeleteByIp removes the login attempt for a given IP.

func (*AppLoginAttempts) IsBlocked

func (appLa *AppLoginAttempts) IsBlocked(ip netip.Addr) (bool, error)

func (*AppLoginAttempts) ListByCreatedAt

func (appLa *AppLoginAttempts) ListByCreatedAt(asc bool) (sortedLoginAttempts []LoginAttempt)

ListByCreatedAt returns all login attempts sorted by CreatedAt.

func (*AppLoginAttempts) Load

func (appLa *AppLoginAttempts) Load(loginAttempts []LoginAttempt) (err error)

Load loads the supplied login attempts into a freshly created AppLoginAttempts instance.

type AppRateLimits

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

func NewAppRateLimits

func NewAppRateLimits(rps float64, burst int, ttl time.Duration) *AppRateLimits

func (*AppRateLimits) Allow

func (m *AppRateLimits) Allow(key string) bool

func (*AppRateLimits) CleanupExpired

func (m *AppRateLimits) CleanupExpired()

type AppSessions

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

AppSessions contains sessions and methods to manage them.

func NewAppSessions

func NewAppSessions(validate *validator.Validate, sessionDuration time.Duration, useXForwardedFor bool, xForwardedForIdx int) *AppSessions

NewAppSessions creates a new AppSessions instance.

func (*AppSessions) Add

func (appS *AppSessions) Add(sessionInput SessionInput) (token string, err error)

Add validates and creates a new session in appSessions.

func (*AppSessions) All

func (appS *AppSessions) All() (sessions []Session)

All returns all sessions.

func (*AppSessions) Count

func (appS *AppSessions) Count() int

Count returns the number of active sessions.

func (*AppSessions) DeleteByIp

func (appS *AppSessions) DeleteByIp(ip netip.Addr) error

DeleteByIp deletes all sessions for the specified IP address.

func (*AppSessions) DeleteByTokens

func (appS *AppSessions) DeleteByTokens(tokens []string)

DeleteByTokens deletes the sessions for the specified tokens.

func (*AppSessions) DeleteByUserId

func (appS *AppSessions) DeleteByUserId(userId int64)

DeleteByUserId deletes all sessions for the specified user ID.

func (*AppSessions) FromRequest

func (appS *AppSessions) FromRequest(r *http.Request, logger *slog.Logger) (sess Session, err error)

FromRequest returns the session associated with the request, or an error if the session is invalid.

func (*AppSessions) GetByUserId

func (appS *AppSessions) GetByUserId(userId int64) (sessions []Session)

GetByUserId returns all sessions for the specified user ID.

func (*AppSessions) GetExpired

func (appS *AppSessions) GetExpired() (sessions []Session)

GetExpired returns all expired sessions.

func (*AppSessions) ListByLastAccessAt

func (appS *AppSessions) ListByLastAccessAt(asc bool) (sortedSessions []Session)

ListByLastAccessAt returns all sessions sorted by LastAccessAt.

func (*AppSessions) Load

func (appS *AppSessions) Load(sessions []Session) (err error)

Load loads the supplied sessions into a freshly created AppSessions instance.

func (*AppSessions) UpdateProfilePicByUserId added in v0.3.55

func (appS *AppSessions) UpdateProfilePicByUserId(userId int64, profilePic string)

UpdateProfilePicByUserId updates the ProfilePic for all sessions belonging to the specified user ID.

type LoginAttempt

type LoginAttempt struct {
	CreatedAt   lystype.Datetime `json:"created_at"`
	Ip          netip.Addr       `json:"ip"`
	IsBlocked   bool             `json:"is_blocked"`
	NumAttempts int              `json:"num_attempts"`
}

LoginAttempt is an unsuccessful login attempt from a given IP.

type Session

type Session struct {
	CreatedAt    lystype.Datetime `json:"created_at"`
	ExpiresAt    lystype.Datetime `json:"expires_at"`
	LastAccessAt lystype.Datetime `json:"last_access_at"`
	Token        string           `json:"-"`
	SessionInput
}

Session is a user session.

type SessionInput

type SessionInput struct {
	AllowMultipleSessions bool       `json:"allow_multiple_sessions"`
	Email                 string     `json:"email,omitempty" validate:"required,email,max=256"`
	FamilyName            string     `json:"family_name" validate:"required"`
	ForcePasswordChange   bool       `json:"force_password_change"`
	GeoIpCountryIsoCode   string     `json:"geo_ip_country_iso_code" validate:"required,len=2"`
	GeoIpLocation         string     `json:"geo_ip_location" validate:"required"`
	GivenName             string     `json:"given_name" validate:"required"`
	Ip                    netip.Addr `json:"ip" validate:"required"`
	ProfilePic            string     `json:"profile_pic"`
	Roles                 []string   `json:"roles" validate:"required"`
	UserAgent             string     `json:"user_agent" validate:"required"`
	UserId                int64      `json:"user_id" validate:"required,gt=0"`
	UserName              string     `json:"user_name" validate:"required"`
}

SessionInput is a user session input.

Jump to

Keyboard shortcuts

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