auth

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MIT, MIT Imports: 22 Imported by: 0

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(db *esquel.DB) error

func DropTable

func DropTable(q *esquel.DB) error

func MustCreateTable

func MustCreateTable(q *esquel.DB)

func MustDropTable

func MustDropTable(q *esquel.DB)

Types

type Config

type Config struct {
	Roles    []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
	Manager() UserManager

	In(email, password string, roles ...Role) (In, error)
	Out() error

	MustIn(email, password string, roles ...Role) In
	MustOut()
}

func New

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

type Role

type Role struct {
	Name       string   `json:"name"`
	Super      bool     `json:"super"`
	Securables []string `json:"securables"`
}

func (Role) Compare

func (r Role) Compare(role Role) bool

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)
	VerifyCodes(email, codes string) (bool, error)
	CreateQrImageBase64(id ...int) (string, error)

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

type User

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

type UserManager

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

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

func CreateUserManager

func CreateUserManager(db *esquel.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