userauth

package
v0.0.0-...-19f3900 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInviteLinkUsed    = errors.New("invite link already used")
	ErrUserAlreadyExists = errors.New("user with such username already exists")
	ErrUserNotFound      = errors.New("user not found")
	ErrRoomTokenNotFound = errors.New("room token not found")
)

Functions

func HashInviteValue

func HashInviteValue(val string) string

func HashRoomToken

func HashRoomToken(tok string) string

func ValidatePassword

func ValidatePassword(password string) error

func ValidateUsername

func ValidateUsername(username string) error

Types

type DB

type DB interface {
	CreateUser(ctx context.Context, user User, link InviteLink) error
	GetUser(ctx context.Context, userID string, o ...GetUserOptions) (User, error)
	GetUserByUsername(ctx context.Context, username string, o ...GetUserOptions) (User, error)
	ListUsers(ctx context.Context) ([]User, error)
	UpdateUser(ctx context.Context, user User, o ...UpdateUserOptions) error
	HasOwnerUser(ctx context.Context) (bool, error)
	CreateInviteLink(ctx context.Context, link InviteLink) error
	GetInviteLink(ctx context.Context, linkHash string, now timeutil.UTCTime) (InviteLink, error)
	PruneInviteLinks(ctx context.Context, now timeutil.UTCTime) error
	DeleteInviteLink(ctx context.Context, linkHash string, userID string) error
	CreateRoomToken(ctx context.Context, token RoomToken) error
	GetRoomToken(ctx context.Context, hash string) (RoomToken, error)
	DeleteRoomToken(ctx context.Context, tokenHash string, userID string) error
}

type ErrorInviteLinkVerify

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

func (*ErrorInviteLinkVerify) Error

func (e *ErrorInviteLinkVerify) Error() string

func (*ErrorInviteLinkVerify) Unwrap

func (e *ErrorInviteLinkVerify) Unwrap() error

type GetUserOptions

type GetUserOptions struct {
	WithInviteLinks bool
	WithRoomTokens  bool
}
type InviteLink struct {
	Hash        string  `gorm:"primaryKey"`
	OwnerUserID *string `gorm:"index"`
	Label       string
	Value       string
	Perms       Perms `gorm:"embedded"`
	CreatedAt   timeutil.UTCTime
	ExpiresAt   timeutil.UTCTime `gorm:"index"`
}

func (*InviteLink) GenerateNew

func (l *InviteLink) GenerateNew() error

func (InviteLink) Verify

func (l InviteLink) Verify(creator *User) error

type Manager

type Manager struct {
	DB
	// contains filtered or unexported fields
}

func NewManager

func NewManager(log *slog.Logger, db DB, o ManagerOptions) (*Manager, error)

func (*Manager) Close

func (m *Manager) Close()
func (m *Manager) GenerateInviteLink(ctx context.Context, label string, creator *User, perms Perms) (InviteLink, error)

func (*Manager) GenerateRoomToken

func (m *Manager) GenerateRoomToken(ctx context.Context, label string, creator *User) (string, error)

func (*Manager) InviteLinkURL

func (m *Manager) InviteLinkURL(l InviteLink) string

func (*Manager) SetPassword

func (m *Manager) SetPassword(u *User, password []byte) error

func (*Manager) VerifyPassword

func (m *Manager) VerifyPassword(u *User, password []byte) bool

type ManagerOptions

type ManagerOptions struct {
	GCInterval       time.Duration    `toml:"gc-interval"`
	LinkPrefix       string           `toml:"link-prefix"`
	Password         *PasswordOptions `toml:"password"`
	InviteLinkExpiry time.Duration    `toml:"invite-link-expiry"`
}

func (ManagerOptions) Clone

func (o ManagerOptions) Clone() ManagerOptions

func (*ManagerOptions) FillDefaults

func (o *ManagerOptions) FillDefaults()

type PasswordOptions

type PasswordOptions struct {
	Time    uint32 `toml:"time"`
	Memory  uint32 `toml:"memory"`
	Threads uint8  `toml:"threads"`
	KeyLen  uint32 `toml:"key-len"`
	SaltLen uint32 `toml:"salt-len"`
}

type PermKind

type PermKind int
const (
	PermInvite PermKind = iota
	PermDiscuss
	PermRunContests
	PermHostRooms
	PermAdmin
	PermMax
)

func (PermKind) PrettyString

func (k PermKind) PrettyString() string

func (PermKind) String

func (k PermKind) String() string

type Perms

type Perms struct {
	IsOwner   bool `gorm:"index"`
	IsBlocked bool

	CanInvite      bool
	CanDiscuss     bool
	CanRunContests bool
	CanHostRooms   bool
	CanAdmin       bool
}

func BlockedPerms

func BlockedPerms() Perms

func OwnerPerms

func OwnerPerms() Perms

func (Perms) Get

func (p Perms) Get(k PermKind) bool

func (*Perms) GetMut

func (p *Perms) GetMut(k PermKind) *bool

func (Perms) LessEq

func (p Perms) LessEq(q Perms) bool

type RoomToken

type RoomToken struct {
	Hash      string `gorm:"primaryKey"`
	Label     string
	UserID    string `gorm:"index"`
	CreatedAt timeutil.UTCTime
}

func (*RoomToken) GenerateNew

func (t *RoomToken) GenerateNew() (string, error)

type TokenChecker

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

func NewTokenChecker

func NewTokenChecker(o TokenCheckerOptions, db DB) *TokenChecker

func (*TokenChecker) Check

func (t *TokenChecker) Check(srcToken string) error

func (*TokenChecker) Close

func (t *TokenChecker) Close()

type TokenCheckerOptions

type TokenCheckerOptions struct {
	CacheExpiryInterval time.Duration `toml:"cache-expiry-interval"`
}

func (TokenCheckerOptions) Clone

func (*TokenCheckerOptions) FillDefaults

func (o *TokenCheckerOptions) FillDefaults()

type UpdateUserOptions

type UpdateUserOptions struct {
	InvalidatePerms bool
}

type User

type User struct {
	ID           string  `gorm:"primaryKey"`
	Username     string  `gorm:"index"`
	InviterID    *string `gorm:"index"`
	PasswordHash []byte
	PasswordSalt []byte
	Epoch        int
	Perms        Perms        `gorm:"embedded"`
	RoomTokens   []RoomToken  `gorm:"foreignKey:UserID"`
	InviteLinks  []InviteLink `gorm:"foreignKey:OwnerUserID"`
}

func (*User) CanChangePerms

func (u *User) CanChangePerms(initiator *User, newPerms Perms) error

func (*User) SetPassword

func (u *User) SetPassword(password []byte, o *PasswordOptions) error

func (*User) TryChangePerms

func (u *User) TryChangePerms(initiator *User, newPerms Perms) error

func (*User) VerifyPassword

func (u *User) VerifyPassword(password []byte, o *PasswordOptions) bool

Jump to

Keyboard shortcuts

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