auth

package
v0.0.0-...-014da05 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDuplicateUser      = errors.New("user already exists")
	ErrNotFound           = errors.New("user not found")
	ErrInvalidToken       = errors.New("token is invalid or expired")
	ErrInvalidCredentials = errors.New("invalid credentails")
)
View Source
var (
	AnonymousUser = &User{}
)

Functions

This section is empty.

Types

type CreateUserParams

type CreateUserParams struct {
	Username string `json:"username" validate:"required,min=3,max=60"`
	Password string `json:"password" validate:"required,min=8,max=120"`
}

type CredentialsParam

type CredentialsParam struct {
	Username string `json:"username" validate:"required,min=3,max=60"`
	Password string `json:"password" validate:"required,min=8,max=120"`
}

type Scope

type Scope string
const (
	// We could add diferent scopes, like password-reset, etc.
	ScopeAuthentication Scope = "authentication"
)

type Service

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

func NewService

func NewService(storer Storer) Service

func (Service) Authenticate

func (s Service) Authenticate(creds CredentialsParam) (*Token, error)

func (Service) CreateUser

func (s Service) CreateUser(params CreateUserParams) (*User, error)

func (Service) DeleteUser

func (s Service) DeleteUser(userId int64) error

func (Service) Get

func (s Service) Get(id int64) (*User, error)

func (Service) GetUserForToken

func (s Service) GetUserForToken(plainText string, scope Scope) (*User, error)

type Storer

type Storer interface {
	// Users
	InsertUser(*User) error
	GetUser(int64) (*User, error)
	GetUserByUsername(string) (*User, error)
	GetUserForToken([]byte, Scope) (*User, error)
	DeleteUser(int64) error

	// Tokens
	DeleteTokensForUser(int64, Scope) error
	InsertToken(*Token) error
}

type Token

type Token struct {
	PlainText string    `json:"token"`
	Hash      []byte    `json:"-"`
	Scope     Scope     `json:"-"`
	UserID    int64     `json:"-"`
	Expiry    time.Time `json:"expiry"`
}

type User

type User struct {
	ID           int64     `json:"id"`
	Username     string    `json:"username"`
	PasswordHash string    `json:"-"`
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`
}

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

Jump to

Keyboard shortcuts

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