Documentation ¶
Index ¶
- Constants
- Variables
- func ValidateToken(v *common.Validator, token string)
- type AuthToken
- type DBModel
- type Password
- type Permission
- type Permissions
- type Token
- type User
- type UserService
- func (s *UserService) ActivateUser(ctx context.Context, token string) error
- func (s *UserService) CreateUser(ctx context.Context, username, email, password string) (*string, error)
- func (s *UserService) GetUserByAccessToken(ctx context.Context, token string) (*User, error)
- func (s *UserService) LoginUser(ctx context.Context, username, password string) (*AuthToken, error)
- func (s *UserService) LogoutUser(ctx context.Context, userId int) error
Constants ¶
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 ¶
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 Password ¶
type Password struct { Plain string `json:"-"` // contains filtered or unexported fields }
type Permission ¶
type Permission string
type Permissions ¶
type Permissions []Permission
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 (*User) IsAnonymous ¶
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 ¶
use cache to store the user
func (*UserService) LoginUser ¶
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
Click to show internal directories.
Click to hide internal directories.