auth

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CostHashPasswordProduction is the cost of hashing password in production
	CostHashPasswordProduction int = 14
	// CostHashPasswordDevelopment is the cost of hashing the password in development mode
	CostHashPasswordDevelopment int = 1
)

Variables

This section is empty.

Functions

func ErrEmailOrPasswordIsNotValid

func ErrEmailOrPasswordIsNotValid() *oops.Error

ErrEmailOrPasswordIsNotValid creates and returns an error when the email or password is not valid

func ErrNotHavePermissionLogin

func ErrNotHavePermissionLogin() *oops.Error

ErrNotHavePermissionLogin creates and returns an error when the user does not have permission to login

func ErrOTPTokenInvalid

func ErrOTPTokenInvalid() *oops.Error

ErrOTPTokenInvalid creates and returns an error when validate token OTP

func ErrTokenIsNotValid

func ErrTokenIsNotValid() *oops.Error

ErrTokenIsNotValid creates and returns an error when the token is not valid

func ErrUserBlockedTemporarily

func ErrUserBlockedTemporarily() *oops.Error

ErrUserBlockedTemporarily creates and returns an error when the user is blocked temporarily

func ErrUserExists

func ErrUserExists() *oops.Error

ErrUserExists creates and returns an error when the user already exists

func ErrUserNotExists

func ErrUserNotExists() *oops.Error

ErrUserNotExists creates and returns an error when the user does not exists

Types

type ActivateAccount

type ActivateAccount struct {
	ID        *uuid.UUID `sql:"id"`
	UserID    *uuid.UUID `sql:"user_id"`
	Used      *bool      `sql:"used"`
	Valid     *bool
	ExpiresAt *time.Time `sql:"expires_at"`
	CreatedAt *time.Time `sql:"created_at"`
}

ActivateAccount model the data to activate user account

func (*ActivateAccount) IsValid

func (a *ActivateAccount) IsValid() bool

IsValid check if the token is valid

type CreateAccount

type CreateAccount struct {
	FirstName *string `sql:"first_name" json:"first_name"`
	LastName  *string `sql:"last_name" json:"last_name"`
	Email     *string `sql:"email" json:"email"`
	Password  *string `sql:"password" json:"password"`
	Key       *string `sql:"key" json:"-"`
}

CreateAccount models the data to create an account

func (*CreateAccount) GeneratePassword

func (rr *CreateAccount) GeneratePassword() error

GeneratePassword hash user password with bcrypt

func (*CreateAccount) Prepare

func (rr *CreateAccount) Prepare() (err error)

Prepare prepare data for registration

func (*CreateAccount) RefreshTokenKey

func (rr *CreateAccount) RefreshTokenKey()

RefreshTokenKey generates and sets new random token key. >> invalidate previously issued tokens

func (*CreateAccount) SanitizePassword

func (rr *CreateAccount) SanitizePassword()

SanitizePassword sanitize user password

type Flag

type Flag int

Flag set the data type to flag the user

const (
	// FlagEnabledAccount defines that the user has already activated his account
	FlagEnabledAccount Flag = iota + 1
	// FlagOTPEnable defines that the user has OTP enabled
	FlagOTPEnable
	// FlagOTPSetup defines that the user has OTP configured
	FlagOTPSetup
)

type IAuth

type IAuth interface {
	CreateAccount(*CreateAccount) (userID *uuid.UUID, err error)
	SendMailActivationAccount(email *string, token *uuid.UUID) error
	GetActivateAccountToken(data *ActivateAccount) error
	CreateAccessToken(userID *uuid.UUID) (*uuid.UUID, error)
	MarkTokenAsUsed(token *uuid.UUID) error
	AddAttempts(userID *uuid.UUID) error
	LoginSteps(email *string) (*Steps, error)
}

IAuth define an interface for data layer access methods

type IOTP

type IOTP interface {
	GetToken(userID *uuid.UUID) (*string, *string, error)
	Configure(userID *uuid.UUID, secret *string) error
	Unconfigure(userID *uuid.UUID) error
}

IOTP define an interface for data layer access methods

type IRole

type IRole interface {
	Set(userID *uuid.UUID, flag *Flag) error
}

IRole define an interface for data layer access methods

type ISession

type ISession interface {
	Create(userID *uuid.UUID, clientIP, userAgent *string) (*uuid.UUID, error)
	Delete(sessionID *uuid.UUID) error
}

ISession define an interface for data layer access methods

type IUser

type IUser interface {
	Get(user *User) error
	Exist(email *string) error
	Disable(userUUID *uuid.UUID) error
}

IUser define an interface for data layer access methods

type Level

type Level string

Level set data type to user level

const (
	// UserLevel is the user role
	UserLevel Level = "user"
	// AdminLevel is the admin role
	AdminLevel Level = "admin"
	// IntegrationLevel is the integration role
	IntegrationLevel Level = "integration"
)

type Login

type Login struct {
	Email     *string `json:"email" binding:"required,lte=60,email"`
	Password  *string `json:"password" binding:"required,gte=6"`
	OTP       *string `json:"otp,omitempty"`
	ClientIP  *string `json:"-"`
	UserAgent *string `json:"-"`
}

Login models the data for the user to log in with their account

func (*Login) ComparePasswords

func (l *Login) ComparePasswords(passw, key *string) error

ComparePasswords compare user password and payload

func (*Login) SanitizePassword

func (l *Login) SanitizePassword()

SanitizePassword sanitize user password

func (*Login) Validate

func (l *Login) Validate()

Validate prepare data for login

type QRCode

type QRCode struct {
	Url *string `json:"url,omitempty"`
}

QRCode wraps the data to return the qr code url

type Session

type Session struct {
	SessionID *uuid.UUID     `json:"session_id,omitempty"`
	UserID    *uuid.UUID     `json:"user_id,omitempty"`
	Email     *string        `json:"email,omitempty"`
	FirstName *string        `json:"first_name,omitempty"`
	LastName  *string        `json:"last_name,omitempty"`
	Level     *Level         `json:"level,omitempty"`
	Token     *string        `json:"token,omitempty"`
	CreatedAt *time.Time     `json:"created_at,omitempty"`
	ExpiresAt *time.Time     `json:"expires_at,omitempty"`
	RawData   map[string]any `json:"data,omitempty"`
}

Session models the data of a user session

type Steps

type Steps struct {
	Name *string
	OTP  *bool
}

Steps contains login steps

type User

type User struct {
	ID        *uuid.UUID
	Email     *string
	Password  *string `json:"-"`
	FirstName *string
	LastName  *string
	Flag      *Flag
	Level     *Level
	Blocked   *bool
	Key       *string
	Active    *bool
	OTPToken  *string
	OTPEnable *bool
	OTPSetUp  *bool
	CreatedBy *uuid.UUID
	CreatedAt *time.Time
	LastLogin *time.Time
}

func (*User) HasFlag

func (u *User) HasFlag(flag Flag) bool

HasFlag return 'true' if has flag

func (*User) IsActive

func (u *User) IsActive() bool

IsActive check if the user has their account activated

func (*User) IsBlocked

func (u *User) IsBlocked() bool

IsBlocked check if the user has the account temporarily blocked

func (*User) OTPConfigured

func (u *User) OTPConfigured() bool

Jump to

Keyboard shortcuts

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