user

package
v0.0.0-...-7aaaf19 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAccountNotExist    = errors.New("user not exist")
	ErrInvalidCredentials = errors.New("wrong password")
	ErrAccountStorer      = errors.New("error account storer")
	ErrAccountExist       = errors.New("account exist")

	ErrTokenNotExist = errors.New("token not exist")
	ErrTokenExpired  = errors.New("token is expired")
)
View Source
var (
	Default_Token_Expire = time.Duration(24 * 7 * time.Hour) // a week
)

Functions

This section is empty.

Types

type Account

type Account struct {
	ID           uuid.UUID
	Name         string
	HashPassword string
}

func CreateAccount

func CreateAccount(username, password string) *Account

func (*Account) CheckPassword

func (acc *Account) CheckPassword(password string) bool

func (*Account) MustGenerateID

func (acc *Account) MustGenerateID()

type AccountStorer

type AccountStorer interface {
	FindUsername(ctx context.Context, username string) (*Account, error)
	FindID(ctx context.Context, id string) (*Account, bool, error)
	Insert(ctx context.Context, acc *Account) error
}

type BasicAuthResponse

type BasicAuthResponse struct {
	ID string
	*Token
}

type Token

type Token struct {
	Key    string
	UserID string
	Expire time.Time
}

func NewToken

func NewToken(userID string, expire time.Duration) *Token

func (*Token) IsNotExpire

func (t *Token) IsNotExpire() bool

func (*Token) RefreshExpire

func (t *Token) RefreshExpire(dur time.Duration) error

func (*Token) ValidUntil

func (t *Token) ValidUntil() time.Time

type TokenOption

type TokenOption struct {
	Expire time.Duration
}

type TokenStorer

type TokenStorer interface {
	GetToken(ctx context.Context, tkn string) (*Token, error)
	GetTokenUserID(ctx context.Context, id string) (*Token, bool)
	PutToken(ctx context.Context, token *Token) error
	Delete(ctx context.Context, tkn string) error
}

type Users

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

func New

func New(ctx context.Context, accounts AccountStorer, tokens TokenStorer) (*Users, error)

func NewWithMemory

func NewWithMemory() *Users

func (*Users) BasicAuth

func (s *Users) BasicAuth(ctx context.Context, username, password string) (*BasicAuthResponse, error)

func (*Users) CreateToken

func (s *Users) CreateToken(ctx context.Context, username, password string, opts TokenOption) (*Token, error)

func (*Users) GetAccount

func (s *Users) GetAccount(ctx context.Context, id string) (*Account, bool, error)

func (*Users) Register

func (s *Users) Register(ctx context.Context, username, password string) (*Account, error)

func (*Users) TokenAuth

func (s *Users) TokenAuth(ctx context.Context, tkn string) (*Token, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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