Documentation
¶
Index ¶
- Variables
- func GetBearerToken(header http.Header) (token string, err error)
- func GetRemoteHostIP(r *http.Request, useXForwardedFor bool, xForwardedForIdx int) (remoteHostIP netip.Addr, err error)
- func IsWebSocket(header http.Header) bool
- type AppLoginAttempts
- func (appLa *AppLoginAttempts) Add(ip netip.Addr) (err error)
- func (appLa *AppLoginAttempts) All() (loginAttempts []LoginAttempt)
- func (appLa *AppLoginAttempts) Block(ip netip.Addr) error
- func (appLa *AppLoginAttempts) Count() int
- func (appLa *AppLoginAttempts) DeleteByIp(ip netip.Addr) (found bool, err error)
- func (appLa *AppLoginAttempts) IsBlocked(ip netip.Addr) (bool, error)
- func (appLa *AppLoginAttempts) ListByCreatedAt(asc bool) (sortedLoginAttempts []LoginAttempt)
- func (appLa *AppLoginAttempts) Load(loginAttempts []LoginAttempt) (err error)
- type AppRateLimits
- type AppSessions
- func (appS *AppSessions) Add(sessionInput SessionInput) (token string, err error)
- func (appS *AppSessions) All() (sessions []Session)
- func (appS *AppSessions) Count() int
- func (appS *AppSessions) DeleteByIp(ip netip.Addr) error
- func (appS *AppSessions) DeleteByTokens(tokens []string)
- func (appS *AppSessions) DeleteByUserId(userId int64)
- func (appS *AppSessions) FromRequest(r *http.Request, logger *slog.Logger) (sess Session, err error)
- func (appS *AppSessions) GetByUserId(userId int64) (sessions []Session)
- func (appS *AppSessions) GetExpired() (sessions []Session)
- func (appS *AppSessions) ListByLastAccessAt(asc bool) (sortedSessions []Session)
- func (appS *AppSessions) Load(sessions []Session) (err error)
- func (appS *AppSessions) UpdateProfilePicByUserId(userId int64, profilePic string)
- type LoginAttempt
- type Session
- type SessionInput
Constants ¶
This section is empty.
Variables ¶
var ( ErrBlocked = lyserr.User{Message: "IP is blocked", StatusCode: http.StatusForbidden} ErrMaxAttemptsExceeded = lyserr.User{Message: "Max login attempts exceeded", StatusCode: http.StatusForbidden} )
var (
ErrTooManyRequests = lyserr.User{Message: "Too many requests", StatusCode: http.StatusTooManyRequests}
)
Functions ¶
func GetBearerToken ¶
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 ¶
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) 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.