userservice

package
v0.0.0-...-d7d0311 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenScopeActivate tokenScope = "token:activate"

	ActivationTokenTime time.Duration = 3 * 24 * time.Hour
	AccessTokenTime     time.Duration = 7 * 24 * time.Hour
	RefreshTokenTime    time.Duration = 30 * 24 * time.Hour

	PermissionWriteBlog Permission = "blog:write"
)

Variables

View Source
var (
	ErrDuplicateUsername = errors.New("duplicate username")
	ErrDuplicateEmail    = errors.New("duplicate email")
)
View Source
var (
	EmailRX     = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
	UsernameRX  = regexp.MustCompile("^[a-zA-Z0-9]+$")
	UppercaseRX = regexp.MustCompile("[A-Z]")
	LowercaseRX = regexp.MustCompile("[a-z]")
	NumberRX    = regexp.MustCompile("[0-9]")
	SymbolRX    = regexp.MustCompile(`[#?!@$%^&*_\\-]`)
)
View Source
var (
	AnonymousUser = User{}
)
View Source
var (
	ErrAuthenticationFailure = fmt.Errorf("unauthorized access")
)

Functions

func ValidateToken

func ValidateToken(v *common.Validator, token string)

Types

type AuthToken

type AuthToken struct {
	AccessTokenPlain   string    `json:"access_token"`
	AccessTokenHash    []byte    `json:"-"`
	RefreshTokenPlain  string    `json:"refresh_token"`
	RefreshTokenHash   []byte    `json:"-"`
	UserID             int       `json:"user_id"`
	AccessTokenExpiry  time.Time `json:"access_token_expiry"`
	RefreshTokenExpiry time.Time `json:"refresh_token_expiry"`
}

Authentication Token

type DBModel

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

type Password

type Password struct {
	Plain string `json:"-"`
	// contains filtered or unexported fields
}

type Permission

type Permission string

type Permissions

type Permissions []Permission

type Token

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

type User

type User struct {
	ID        int       `json:"id"`
	Username  string    `json:"username"`
	Email     string    `json:"email"`
	Password  Password  `json:"-"`
	Activated bool      `json:"activated"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	Version   int       `json:"version"`

	Permissions Permissions `json:"permissions"`
}

func (*User) HasPermission

func (u *User) HasPermission(permission Permission) bool

func (*User) IsActivated

func (u *User) IsActivated() bool

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

type UserService

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

func NewUserService

func NewUserService(db *sql.DB, mb *common.MessageBroker, c *common.Cache) *UserService

func (*UserService) ActivateUser

func (s *UserService) ActivateUser(ctx context.Context, token string) error

ActivateUser activates a user account using the token and deletes the token from the database and adds permission for the user to perform write operation.

func (*UserService) CreateUser

func (s *UserService) CreateUser(ctx context.Context, username, email, password string) (*string, error)

CreateUser creates a new user account and publish an user.created event.

func (*UserService) GetUserByAccessToken

func (s *UserService) GetUserByAccessToken(ctx context.Context, token string) (*User, error)

use cache to store the user

func (*UserService) LoginUser

func (s *UserService) LoginUser(ctx context.Context, username, password string) (*AuthToken, error)

LoginUser logs in a user and returns the access token and refresh token.

func (*UserService) LogoutUser

func (s *UserService) LogoutUser(ctx context.Context, userId int) error

Jump to

Keyboard shortcuts

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