profile

package
v0.0.0-...-66b7006 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthorizationHeaderName = "Authorization"
	BearerAuthorizationType = "Bearer"
	TokenQueryParameterName = "token"
)
View Source
const (
	EmailMinLength       = 5
	EmailMaxLength       = 254
	PasswordMinLength    = 8
	PasswordMaxLength    = 64
	NameMinLength        = 1
	NameMaxLength        = 64
	CompanyNameMinLength = 1
	CompanyNameMaxLength = 64
)

Profile Field Lengths

View Source
const (
	EmailExistsInAccount = "EmailExistsInAccount"
)
View Source
const (
	MissingIpAddress = "0.0.0.0"
)

Variables

This section is empty.

Functions

func AddSessionCookie

func AddSessionCookie(w http.ResponseWriter, token string, domain string)

func ClearSessionCookie

func ClearSessionCookie(w http.ResponseWriter, domain string)

func EncryptPassword

func EncryptPassword(password string) (string, error)

func GenerateToken

func GenerateToken() (string, error)

func GenerateTokenExpiration

func GenerateTokenExpiration() time.Time

func GetCookieExpirationMinutes

func GetCookieExpirationMinutes() int

func GetSessionCookieFromRequest

func GetSessionCookieFromRequest(r *http.Request) *http.Cookie

func GetSessionCookieName

func GetSessionCookieName() string

func GetSessionExpiration

func GetSessionExpiration() time.Time

func GetTokenFromRequest

func GetTokenFromRequest(r *http.Request) (string, *api.Error)

func IsAccountStatusValid

func IsAccountStatusValid(status AccountStatus) bool

Returns true if the AccountStatus is new or valid

func IsAdmin

func IsAdmin(role AuthorizationRole) bool

Returns true if the AuthorizationRole has admin privileges

func IsProfileAccountStatusValid

func IsProfileAccountStatusValid(status ProfileAccountStatus) bool

Returns true if the ProfileAccountStatus is valid

func IsProfileStatusValid

func IsProfileStatusValid(status ProfileStatus) bool

Returns true if the ProfileStatus is new or valid

func IsRole

func IsRole(role string) bool

func IsWeekStart

func IsWeekStart(weekStart int) bool

Returns true if the start of week is between 0 and 6 (Sunday to Saturday)

func TokenHandler

func TokenHandler(next http.Handler) http.Handler

Types

type Account

type Account struct {
	AccountId       int           `json:"-" db:"account_id"`
	Company         string        `json:"-"`
	AccountStatus   AccountStatus `json:"-" db:"account_status"`
	WeekStart       int           `json:"-" db:"week_start"`
	AccountTimezone string        `json:"-" db:"account_timezone"`
	Created         time.Time     `json:"-"`
	Updated         time.Time     `json:"-"`
	CloseReason     string        `json:"-" db:"close_reason"`
}

type AccountIdRequest

type AccountIdRequest struct {
	AccountId int
}

type AccountRequest

type AccountRequest struct {
	FirstName string
	LastName  string
	Email     string
	Password  string
	Timezone  string
	Company   string
	Phone     string
}

type AccountResponse

type AccountResponse struct {
	Company   string `json:"company"`
	WeekStart int    `json:"weekStart"`
	Timezone  string `json:"timezone,omitempty"`
	Created   string `json:"created,omitempty"`
	Updated   string `json:"updated,omitempty"`
}

func NewAccountResponse

func NewAccountResponse(accountData *Account) *AccountResponse

type AccountStatus

type AccountStatus string
const (
	AccountNew      AccountStatus = "new"
	AccountValid    AccountStatus = "valid"
	AccountArchived AccountStatus = "archived"
)

Account Status

type AccountUpdateRequest

type AccountUpdateRequest struct {
	Company   string
	Phone     string
	Timezone  string
	WeekStart int
}

type AddUserRequest

type AddUserRequest struct {
	FirstName string
	LastName  string
	Email     string
	Role      string
}

type AuthResponse

type AuthResponse struct {
	Id        int    `json:"id,omitempty"`
	FirstName string `json:"firstName"`
	LastName  string `json:"lastName"`
	Company   string `json:"company"`
	WeekStart int    `json:"weekStart"`
}

func NewAuthResponse

func NewAuthResponse(values *Profile) *AuthResponse

type AuthorizationRole

type AuthorizationRole string
const (
	Owner     AuthorizationRole = "owner"
	Admin     AuthorizationRole = "admin"
	Reporting AuthorizationRole = "reporting"
	User      AuthorizationRole = "user"
)

type CompanyResponse

type CompanyResponse struct {
	Id      int    `json:"id"`
	Company string `json:"company"`
}

func NewAccountCompaniesResponse

func NewAccountCompaniesResponse(accounts []*Account) []*CompanyResponse

type EmailRequest

type EmailRequest struct {
	Email string
}

type ForgotPassword

type ForgotPassword struct {
	ProfileId                int            `json:"-" db:"profile_id"`
	ForgotPasswordToken      sql.NullString `json:"-" db:"forgot_password_token"`
	ForgotPasswordExpiration pq.NullTime    `json:"-" db:"forgot_password_expiration"`
}

type ForgotPasswordTokenRequest

type ForgotPasswordTokenRequest struct {
	ForgotPasswordToken string
}

type LoginRequest

type LoginRequest struct {
	EmailRequest
	Password string
}

type PasswordChangeRequest

type PasswordChangeRequest struct {
	CurrentPassword string
	Password        string
	ConfirmPassword string
}

type Profile

type Profile struct {
	Account
	Session
	ProfileId                int                  `json:"-" db:"profile_id"`
	FirstName                string               `json:"-" db:"first_name"`
	LastName                 string               `json:"-" db:"last_name"`
	Email                    string               `json:"-"`
	Password                 string               `json:"-"`
	Phone                    sql.NullString       `json:"-"`
	ProfileStatus            ProfileStatus        `json:"-" db:"profile_status"`
	Created                  pq.NullTime          `json:"-" db:"created"`
	Updated                  time.Time            `json:"-" db:"updated"`
	LockedUntil              pq.NullTime          `json:"-" db:"locked_until"`
	Role                     AuthorizationRole    `json:"-"`
	ProfileAccountStatus     ProfileAccountStatus `json:"-" db:"profile_account_status"`
	Timezone                 string               `json:"-"`
	ForgotPasswordToken      pq.NullTime          `json:"-" db:"forgot_password_token"`
	ForgotPasswordExpiration pq.NullTime          `json:"-" db:"forgot_password_expiration"`
}

type ProfileAccountStatus

type ProfileAccountStatus string
const (
	ProfileAccountValid   ProfileAccountStatus = "valid"
	ProfileAccountInvalid ProfileAccountStatus = "invalid"
)

type ProfileData

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

ProfileData implements database operations for user profiles

func (*ProfileData) AddFailedLoginAttempt

func (pa *ProfileData) AddFailedLoginAttempt(email string, ipAddress string) error

func (*ProfileData) AddToken

func (pa *ProfileData) AddToken(profileId int, accountId int, token string, expiration time.Time) error

func (*ProfileData) AddUser

func (pa *ProfileData) AddUser(accountId int, profileId int, email string, role AuthorizationRole, status ProfileAccountStatus) error

func (*ProfileData) CloseAccount

func (pa *ProfileData) CloseAccount(accountId int, reason string) error

func (*ProfileData) CreateAccount

func (pa *ProfileData) CreateAccount(newAccount *Account) (int, error)

func (*ProfileData) CreateProfile

func (pa *ProfileData) CreateProfile(user *Profile, accountId int, newSession bool, status ProfileStatus) (int, error)

func (*ProfileData) DeleteSessionByToken

func (pa *ProfileData) DeleteSessionByToken(token string) error

func (*ProfileData) GetAccount

func (pa *ProfileData) GetAccount(accountId int) (*Account, error)

func (*ProfileData) GetByEmail

func (pa *ProfileData) GetByEmail(email string) (*Profile, error)

func (*ProfileData) GetFailedLoginCount

func (pa *ProfileData) GetFailedLoginCount(email string, currentTime time.Time) (int, error)

func (*ProfileData) GetForgotPasswordToken

func (pa *ProfileData) GetForgotPasswordToken(token string) (*ForgotPassword, error)

func (*ProfileData) GetPasswordById

func (pa *ProfileData) GetPasswordById(profileId int) (string, error)

func (*ProfileData) GetProfileByToken

func (pa *ProfileData) GetProfileByToken(token string) (*Profile, error)

func (*ProfileData) GetProfiles

func (pa *ProfileData) GetProfiles(accountId int) ([]*Profile, error)

func (*ProfileData) RemoveUser

func (pa *ProfileData) RemoveUser(accountId int, userId int) error

func (*ProfileData) SetProfileLocked

func (pa *ProfileData) SetProfileLocked(email string) error

func (*ProfileData) UpdateAccount

func (pa *ProfileData) UpdateAccount(updateAccount *Account) error

func (*ProfileData) UpdateForgotPassword

func (pa *ProfileData) UpdateForgotPassword(profileId int, forgotPasswordToken string, forgotPasswordExpirationMinutes int) error

func (*ProfileData) UpdatePassword

func (pa *ProfileData) UpdatePassword(profileId int, encryptedPassword string) error

func (*ProfileData) UpdateProfile

func (pa *ProfileData) UpdateProfile(profile *Profile) error

func (*ProfileData) UpdateProfileState

func (pa *ProfileData) UpdateProfileState(profileId int, profileStatus ProfileStatus) error

func (*ProfileData) UpdateTokenExpiration

func (pa *ProfileData) UpdateTokenExpiration(token string, expiration time.Time) error

type ProfileRequest

type ProfileRequest struct {
	FirstName string
	LastName  string
	Email     string
	Password  string
	Timezone  string
}

type ProfileResource

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

func (*ProfileResource) AddUser

func (pr *ProfileResource) AddUser(request *AddUserRequest, account *Account) (*Profile, *api.Error)

func (*ProfileResource) CloseAccount

func (pr *ProfileResource) CloseAccount(accountId int, reason string) *api.Error

func (*ProfileResource) Create

func (pr *ProfileResource) Create(accountRequest *AccountRequest) (*Account, *Profile, *api.Error)

func (*ProfileResource) ForgotPassword

func (pr *ProfileResource) ForgotPassword(email string) *api.Error

func (*ProfileResource) GenerateNewTokenForProfile

func (pr *ProfileResource) GenerateNewTokenForProfile(profile *Profile) *api.Error

func (*ProfileResource) GetAccount

func (pr *ProfileResource) GetAccount(accountId int) (*Account, *api.Error)

func (*ProfileResource) GetAllProfiles

func (pr *ProfileResource) GetAllProfiles(accountId int) ([]*Profile, *api.Error)

func (*ProfileResource) GetProfile

func (pr *ProfileResource) GetProfile(token string) (*Profile, *api.Error)

func (*ProfileResource) GetProfileByToken

func (pr *ProfileResource) GetProfileByToken(token string) (*Profile, *api.Error)

func (*ProfileResource) Login

func (pr *ProfileResource) Login(email string, password string, ipAddress string) (*Profile, *api.Error)

func (*ProfileResource) Logout

func (pr *ProfileResource) Logout(token string) *api.Error

func (*ProfileResource) RemoveUser

func (pr *ProfileResource) RemoveUser(email string, account *Account) *api.Error

func (*ProfileResource) SetupNewUser

func (pr *ProfileResource) SetupNewUser(token string, password string) *api.Error

func (*ProfileResource) UpdateAccount

func (pr *ProfileResource) UpdateAccount(accountId int, request *AccountUpdateRequest) (*Account, *api.Error)

func (*ProfileResource) UpdatePassword

func (pr *ProfileResource) UpdatePassword(profileId int, currentPassword string, password string, confirmPassword string) *api.Error

func (*ProfileResource) UpdateProfile

func (pr *ProfileResource) UpdateProfile(profile *Profile, existingProfile *Profile) *api.Error

func (*ProfileResource) UpdateTokenExpiration

func (pr *ProfileResource) UpdateTokenExpiration(token string, expiration time.Time) *api.Error

func (*ProfileResource) ValidateForgotPasswordToken

func (pr *ProfileResource) ValidateForgotPasswordToken(token string) *api.Error

type ProfileResponse

type ProfileResponse struct {
	FirstName string `json:"firstName"`
	LastName  string `json:"lastName"`
	Company   string `json:"company"`
	Email     string `json:"email"`
	Phone     string `json:"phone,omitempty"`
	Timezone  string `json:"timezone,omitempty"`
}

func NewProfileListResponse

func NewProfileListResponse(profiles []*Profile) []ProfileResponse

func NewProfileResponse

func NewProfileResponse(user *Profile) *ProfileResponse

type ProfileRouter

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

func NewRouter

func NewRouter(store ProfileStore) *ProfileRouter

Returns a configured authentication profileService

func (*ProfileRouter) AccountRouter

func (pr *ProfileRouter) AccountRouter() *chi.Mux

func (*ProfileRouter) AdminPermissionHandler

func (pr *ProfileRouter) AdminPermissionHandler(next http.Handler) http.Handler

func (*ProfileRouter) AuthenticationRouter

func (pr *ProfileRouter) AuthenticationRouter() *chi.Mux

func (*ProfileRouter) ProfileRouter

func (pr *ProfileRouter) ProfileRouter() *chi.Mux

func (*ProfileRouter) ValidateProfileHandler

func (pr *ProfileRouter) ValidateProfileHandler(next http.Handler) http.Handler

func (*ProfileRouter) ValidateSessionHandler

func (pr *ProfileRouter) ValidateSessionHandler(next http.Handler) http.Handler

type ProfileService

type ProfileService interface {
	Login(email string, password string, ipAddress string) (*Profile, *api.Error)
	Logout(token string) *api.Error
	GetProfile(token string) (*Profile, *api.Error)
	ForgotPassword(email string) *api.Error
	ValidateForgotPasswordToken(token string) *api.Error
	SetupNewUser(token string, password string) *api.Error
	GetAccount(int) (*Account, *api.Error)
	GetAllProfiles(accountId int) ([]*Profile, *api.Error)
	GetProfileByToken(token string) (*Profile, *api.Error)
	Create(*AccountRequest) (*Account, *Profile, *api.Error)
	AddUser(*AddUserRequest, *Account) (*Profile, *api.Error)
	RemoveUser(email string, account *Account) *api.Error
	UpdateProfile(updatedProfile *Profile, existingProfile *Profile) *api.Error
	UpdateTokenExpiration(token string, expiration time.Time) *api.Error
	UpdatePassword(profileId int, currentPassword string, password string, confirmPassword string) *api.Error
	UpdateAccount(accountId int, request *AccountUpdateRequest) (*Account, *api.Error)
	CloseAccount(accountId int, reason string) *api.Error
}

func NewProfileService

func NewProfileService(store ProfileStore) ProfileService

type ProfileStatus

type ProfileStatus string
const (
	ProfileNew         ProfileStatus = "new"
	ProfileNotVerified ProfileStatus = "not-verified"
	ProfileValid       ProfileStatus = "valid"
)

type ProfileStore

type ProfileStore interface {
	// Profile
	CreateProfile(*Profile, int, bool, ProfileStatus) (int, error)
	GetByEmail(email string) (*Profile, error)
	GetFailedLoginCount(email string, currentTime time.Time) (int, error)
	GetPasswordById(profileId int) (string, error)
	UpdateProfile(*Profile) error
	UpdateTokenExpiration(string, time.Time) error
	UpdatePassword(profileId int, password string) error
	UpdateProfileState(profileId int, profileStatus ProfileStatus) error
	SetProfileLocked(email string) error
	DeleteSessionByToken(token string) error
	AddToken(profileId int, accountId int, token string, expiration time.Time) error
	UpdateForgotPassword(profileId int, forgotPasswordToken string, forgotPasswordExpirationMinutes int) error
	AddFailedLoginAttempt(email string, ipAddress string) error
	GetForgotPasswordToken(token string) (*ForgotPassword, error)

	// Account
	CreateAccount(account *Account) (int, error)
	GetAccount(accountId int) (*Account, error)
	GetProfiles(accountId int) ([]*Profile, error)
	GetProfileByToken(token string) (*Profile, error)
	UpdateAccount(account *Account) error
	CloseAccount(accountId int, reason string) error
	AddUser(accountId int, userId int, email string, role AuthorizationRole, status ProfileAccountStatus) error
	RemoveUser(accountId int, userId int) error
}

func NewProfileAccountStore

func NewProfileAccountStore(db *sqlx.DB) ProfileStore

type RemoveUserRequest

type RemoveUserRequest struct {
	Email string
}

type Session

type Session struct {
	Token           sql.NullString `json:"-"`
	TokenExpiration pq.NullTime    `json:"-" db:"token_expiration"`
	SessionType     string         `json:"-" db:"type"`
}

type SetupNewUserRequest

type SetupNewUserRequest struct {
	Token    string
	Password string
}

Jump to

Keyboard shortcuts

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