auth

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 21 Imported by: 2

Documentation

Index

Constants

View Source
const (
	SessionCookieKey = "X-Session"
	DefaultDuration  = 24 * time.Hour
)
View Source
const (
	TfaCookieKey = "X-Tfa"
	TfaImageSize = 200
)
View Source
const (
	UserActive       = "active"
	UserRoles        = "roles"
	UserEmail        = "email"
	UserPassword     = "password"
	UserTfa          = "tfa"
	UserTfaSecret    = "tfa_secret"
	UserTfaCodes     = "tfa_codes"
	UserTfaUrl       = "tfa_url"
	UserLastActivity = "last_activity"
)
View Source
const (
	SessionCacheKey = "session"
	TfaCacheKey     = "tfa"
)

Variables

View Source
var (
	ErrorMissingSessionCookie = errors.New("session cookie does not exist")
	ErrorMissingTfaCookie     = errors.New("tfa cookie does not exist")
	ErrorCredentialsMismatch  = errors.New("client is not equal with session")
	ErrorMissingUser          = errors.New("user doesn't exist")
	ErrorMismatchPassword     = errors.New("passwords aren't equal")
	ErrorUserAlreadyExists    = errors.New("user already exists")
	ErrorInvalidUser          = errors.New("invalid user")
	ErrorInvalidOtp           = errors.New("invalid otp")
	ErrorInvalidCredentials   = errors.New("invalid credentials")
)

Functions

func CreateTable

func CreateTable(q *quirk.Quirk) error

func DropTable

func DropTable(q *quirk.Quirk) error

func MustCreateTable

func MustCreateTable(q *quirk.Quirk)

func MustDropTable

func MustDropTable(q *quirk.Quirk)

Types

type Config

type Config struct {
	Roles    map[string]Role `json:"roles" yaml:"roles" toml:"roles"`
	Duration time.Duration   `json:"duration" yaml:"duration" toml:"duration"`
}

type In

type In struct {
	Token string
	Ok    bool
	Tfa   bool `json:"tfa"`
}

type Manager

type Manager interface {
	Session() SessionManager
	Tfa() TfaManager
	User() UserManager
	CustomUser(id int, email string) UserManager

	In(email, password string) (In, error)
	Out() error

	MustIn(email, password string) In
	MustOut()
}

func New

func New(
	db *quirk.DB,
	req *http.Request,
	res http.ResponseWriter,
	cookie cookie.Cookie,
	cache cache.Client,
	config Config,
) Manager

type Role

type Role struct {
	Super bool `json:"super" yaml:"super" toml:"super"`
}

type Session

type Session struct {
	Id        int      `json:"id"`
	Email     string   `json:"email"`
	Roles     []string `json:"role"`
	Super     bool     `json:"super"`
	Ip        string   `json:"ip"`
	UserAgent string   `json:"userAgent"`
}

type SessionManager

type SessionManager interface {
	Token() string
	Exists() (bool, error)
	Get(token ...string) (Session, error)
	New(user User) (string, error)
	Renew() error
	Destroy() error

	MustExists() bool
	MustGet(token ...string) Session
	MustNew(user User) string
	MustRenew()
	MustDestroy()
}

type TfaManager

type TfaManager interface {
	GetPendingUserId() (int, error)
	GetPendingVerification() (bool, error)
	GetActive() (bool, error)
	Enable(id ...int) error
	Disable(id ...int) error
	Verify(otp string) (string, error)
	CreateQrImageBase64() (string, error)

	MustGetPendingUserId() int
	MustGetPendingVerification() bool
	MustGetActive() bool
	MustEnable(id ...int)
	MustDisable(id ...int)
	MustVerify(otp string) string
	MustCreateQrImageBase64() string
}

type User

type User struct {
	Id           int              `json:"id"`
	Active       bool             `json:"active"`
	Roles        []string         `json:"roles"`
	Email        string           `json:"email"`
	Password     string           `json:"password"`
	Tfa          bool             `json:"tfa"`
	TfaSecret    sql.Null[string] `json:"tfaSecret"`
	TfaCodes     sql.Null[string] `json:"tfaCodes"`
	TfaUrl       sql.Null[string] `json:"tfaUrl"`
	LastActivity time.Time        `json:"lastActivity"`
	CreatedAt    time.Time        `json:"createdAt"`
	UpdatedAt    time.Time        `json:"updatedAt"`
}

type UserManager

type UserManager interface {
	Get(id ...int) (User, error)
	Create(r User) (int, error)
	Update(r User, columns ...string) error
	ResetPassword(token ...string) (string, error)
	UpdatePassword(actualPassword, newPassword string) error
	ForceUpdatePassword(newPassword string) error
	Enable(id ...int) error
	Disable(id ...int) error

	MustGet(id ...int) User
	MustCreate(r User) int
	MustUpdate(r User, columns ...string)
	MustResetPassword(token ...string) string
	MustUpdatePassword(actualPassword, newPassword string)
	MustForceUpdatePassword(newPassword string)
	MustEnable(id ...int)
	MustDisable(id ...int)
}

func CreateUserManager added in v0.1.1

func CreateUserManager(db *quirk.DB, cache cache.Client, id int, email string) UserManager

Jump to

Keyboard shortcuts

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