user

package
v0.14.1 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2020 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const ErrCodeCouldNotGetUserID = 1006

ErrCodeCouldNotGetUserID holds the unique world-error code of this error

View Source
const ErrCodeEmailNotConfirmed = 1012

ErrCodeEmailNotConfirmed holds the unique world-error code of this error

View Source
const ErrCodeEmptyNewPassword = 1013

ErrCodeEmptyNewPassword holds the unique world-error code of this error

View Source
const ErrCodeEmptyOldPassword = 1014

ErrCodeEmptyOldPassword holds the unique world-error code of this error

View Source
const ErrCodeInvalidEmailConfirmToken = 1010

ErrCodeInvalidEmailConfirmToken holds the unique world-error code of this error

View Source
const ErrCodeInvalidPasswordResetToken = 1009

ErrCodeInvalidPasswordResetToken holds the unique world-error code of this error

View Source
const ErrCodeInvalidTOTPPasscode = 1017

ErrCodeInvalidTOTPPasscode holds the unique world-error code of this error

View Source
const ErrCodeNoPasswordResetToken = 1008

ErrCodeNoPasswordResetToken holds the unique world-error code of this error

View Source
const ErrCodeNoUsernamePassword = 1004

ErrCodeNoUsernamePassword holds the unique world-error code of this error

View Source
const ErrCodeTOTPAlreadyEnabled = 1015

ErrCodeTOTPAlreadyEnabled holds the unique world-error code of this error

View Source
const ErrCodeTOTPNotEnabled = 1016

ErrCodeTOTPNotEnabled holds the unique world-error code of this error

View Source
const ErrCodeUserDoesNotExist = 1005

ErrCodeUserDoesNotExist holds the unique world-error code of this error

View Source
const ErrCodeWrongUsernameOrPassword = 1011

ErrCodeWrongUsernameOrPassword holds the unique world-error code of this error

View Source
const ErrorCodeUserEmailExists = 1002

ErrorCodeUserEmailExists holds the unique world-error code of this error

View Source
const ErrorCodeUsernameExists = 1001

ErrorCodeUsernameExists holds the unique world-error code of this error

Variables

This section is empty.

Functions

func CheckUserPassword

func CheckUserPassword(user *User, password string) error

CheckUserPassword checks and verifies a user's password. The user object needs to contain the hashed password from the database.

func ConfirmEmail

func ConfirmEmail(c *EmailConfirm) (err error)

ConfirmEmail handles the confirmation of an email address

func DisableTOTP

func DisableTOTP(user *User) (err error)

DisableTOTP removes all totp settings for a user.

func EnableTOTP

func EnableTOTP(passcode *TOTPPasscode) (err error)

EnableTOTP enables totp for a user. The provided passcode is used to verify the user has a working totp setup.

func GetTOTPQrCodeForUser

func GetTOTPQrCodeForUser(user *User) (qrcode image.Image, err error)

GetTOTPQrCodeForUser returns a qrcode for a user's totp setting

func GetTables

func GetTables() []interface{}

GetTables returns all structs which are also a table.

func InitDB

func InitDB() (err error)

InitDB sets up the database connection to use in this module

func InitTests

func InitTests()

InitTests handles the actual bootstrapping of the test env

func IsErrCouldNotGetUserID

func IsErrCouldNotGetUserID(err error) bool

IsErrCouldNotGetUserID checks if an error is a ErrCouldNotGetUserID.

func IsErrEmailNotConfirmed

func IsErrEmailNotConfirmed(err error) bool

IsErrEmailNotConfirmed checks if an error is a IsErrEmailNotConfirmed.

func IsErrEmptyNewPassword

func IsErrEmptyNewPassword(err error) bool

IsErrEmptyNewPassword checks if an error is a ErrEmptyNewPassword.

func IsErrEmptyOldPassword

func IsErrEmptyOldPassword(err error) bool

IsErrEmptyOldPassword checks if an error is a ErrEmptyOldPassword.

func IsErrInvalidEmailConfirmToken

func IsErrInvalidEmailConfirmToken(err error) bool

IsErrInvalidEmailConfirmToken checks if an error is a ErrInvalidEmailConfirmToken.

func IsErrInvalidPasswordResetToken

func IsErrInvalidPasswordResetToken(err error) bool

IsErrInvalidPasswordResetToken checks if an error is a ErrInvalidPasswordResetToken.

func IsErrInvalidTOTPPasscode

func IsErrInvalidTOTPPasscode(err error) bool

IsErrInvalidTOTPPasscode checks if an error is a ErrInvalidTOTPPasscode.

func IsErrNoUsernamePassword

func IsErrNoUsernamePassword(err error) bool

IsErrNoUsernamePassword checks if an error is a ErrNoUsernamePassword.

func IsErrTOTPAlreadyEnabled

func IsErrTOTPAlreadyEnabled(err error) bool

IsErrTOTPAlreadyEnabled checks if an error is a ErrTOTPAlreadyEnabled.

func IsErrTOTPNotEnabled

func IsErrTOTPNotEnabled(err error) bool

IsErrTOTPNotEnabled checks if an error is a ErrTOTPNotEnabled.

func IsErrUserDoesNotExist

func IsErrUserDoesNotExist(err error) bool

IsErrUserDoesNotExist checks if an error is a ErrUserDoesNotExist.

func IsErrUserEmailExists

func IsErrUserEmailExists(err error) bool

IsErrUserEmailExists checks if an error is a ErrUserEmailExists.

func IsErrUsernameExists

func IsErrUsernameExists(err error) bool

IsErrUsernameExists checks if an error is a ErrUsernameExists.

func IsErrWrongUsernameOrPassword

func IsErrWrongUsernameOrPassword(err error) bool

IsErrWrongUsernameOrPassword checks if an error is a IsErrEmailNotConfirmed.

func RequestUserPasswordResetToken

func RequestUserPasswordResetToken(tr *PasswordTokenRequest) (err error)

RequestUserPasswordResetToken inserts a random token to reset a users password into the databsse

func ResetPassword

func ResetPassword(reset *PasswordReset) (err error)

ResetPassword resets a users password

func TOTPEnabledForUser

func TOTPEnabledForUser(user *User) (bool, error)

TOTPEnabledForUser checks if totp is enabled for a user - not if it is activated, use GetTOTPForUser to check that.

func UpdateEmail

func UpdateEmail(update *EmailUpdate) (err error)

UpdateEmail lets a user update their email address

func UpdateUserPassword

func UpdateUserPassword(user *User, newPassword string) (err error)

UpdateUserPassword updates the password of a user

Types

type APIUserPassword

type APIUserPassword struct {
	// The unique, numeric id of this user.
	ID int64 `json:"id"`
	// The username of the username. Is always unique.
	Username string `json:"username" valid:"length(3|250)" minLength:"3" maxLength:"250"`
	// The user's password in clear text. Only used when registering the user.
	Password string `json:"password" valid:"length(8|250)" minLength:"8" maxLength:"250"`
	// The user's email address
	Email string `json:"email" valid:"email,length(0|250)" maxLength:"250"`
}

APIUserPassword represents a user object without timestamps and a json password field.

func (*APIUserPassword) APIFormat

func (apiUser *APIUserPassword) APIFormat() *User

APIFormat formats an API User into a normal user struct

type EmailConfirm

type EmailConfirm struct {
	// The email confirm token sent via email.
	Token string `json:"token"`
}

EmailConfirm holds the token to confirm a mail address

type EmailUpdate

type EmailUpdate struct {
	User *User `json:"-"`
	// The new email address. Needs to be a valid email address.
	NewEmail string `json:"new_email" valid:"email,length(0|250),required"`
	// The password of the user for confirmation.
	Password string `json:"password"`
}

EmailUpdate is the data structure to update a user's email address

type ErrCouldNotGetUserID

type ErrCouldNotGetUserID struct{}

ErrCouldNotGetUserID represents a "ErrCouldNotGetuser_id" kind of error.

func (ErrCouldNotGetUserID) Error

func (err ErrCouldNotGetUserID) Error() string

func (ErrCouldNotGetUserID) HTTPError

func (err ErrCouldNotGetUserID) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrEmailNotConfirmed

type ErrEmailNotConfirmed struct {
	UserID int64
}

ErrEmailNotConfirmed is an error where the email was not confirmed

func (ErrEmailNotConfirmed) Error

func (err ErrEmailNotConfirmed) Error() string

func (ErrEmailNotConfirmed) HTTPError

func (err ErrEmailNotConfirmed) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrEmptyNewPassword

type ErrEmptyNewPassword struct{}

ErrEmptyNewPassword represents a "EmptyNewPassword" kind of error.

func (ErrEmptyNewPassword) Error

func (err ErrEmptyNewPassword) Error() string

func (ErrEmptyNewPassword) HTTPError

func (err ErrEmptyNewPassword) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrEmptyOldPassword

type ErrEmptyOldPassword struct{}

ErrEmptyOldPassword represents a "EmptyOldPassword" kind of error.

func (ErrEmptyOldPassword) Error

func (err ErrEmptyOldPassword) Error() string

func (ErrEmptyOldPassword) HTTPError

func (err ErrEmptyOldPassword) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrInvalidEmailConfirmToken

type ErrInvalidEmailConfirmToken struct {
	Token string
}

ErrInvalidEmailConfirmToken is an error where the email confirm token is invalid

func (ErrInvalidEmailConfirmToken) Error

func (err ErrInvalidEmailConfirmToken) Error() string

func (ErrInvalidEmailConfirmToken) HTTPError

func (err ErrInvalidEmailConfirmToken) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrInvalidPasswordResetToken

type ErrInvalidPasswordResetToken struct {
	Token string
}

ErrInvalidPasswordResetToken is an error where the password reset token is invalid

func (ErrInvalidPasswordResetToken) Error

func (ErrInvalidPasswordResetToken) HTTPError

func (err ErrInvalidPasswordResetToken) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrInvalidTOTPPasscode

type ErrInvalidTOTPPasscode struct {
	Passcode string
}

ErrInvalidTOTPPasscode represents a "InvalidTOTPPasscode" kind of error.

func (ErrInvalidTOTPPasscode) Error

func (err ErrInvalidTOTPPasscode) Error() string

func (ErrInvalidTOTPPasscode) HTTPError

func (err ErrInvalidTOTPPasscode) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrNoPasswordResetToken

type ErrNoPasswordResetToken struct {
	UserID int64
}

ErrNoPasswordResetToken represents an error where no password reset token exists for that user

func (ErrNoPasswordResetToken) Error

func (err ErrNoPasswordResetToken) Error() string

func (ErrNoPasswordResetToken) HTTPError

func (err ErrNoPasswordResetToken) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrNoUsernamePassword

type ErrNoUsernamePassword struct{}

ErrNoUsernamePassword represents a "NoUsernamePassword" kind of error.

func (ErrNoUsernamePassword) Error

func (err ErrNoUsernamePassword) Error() string

func (ErrNoUsernamePassword) HTTPError

func (err ErrNoUsernamePassword) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrTOTPAlreadyEnabled

type ErrTOTPAlreadyEnabled struct{}

ErrTOTPAlreadyEnabled represents a "TOTPAlreadyEnabled" kind of error.

func (ErrTOTPAlreadyEnabled) Error

func (err ErrTOTPAlreadyEnabled) Error() string

func (ErrTOTPAlreadyEnabled) HTTPError

func (err ErrTOTPAlreadyEnabled) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrTOTPNotEnabled

type ErrTOTPNotEnabled struct{}

ErrTOTPNotEnabled represents a "TOTPNotEnabled" kind of error.

func (ErrTOTPNotEnabled) Error

func (err ErrTOTPNotEnabled) Error() string

func (ErrTOTPNotEnabled) HTTPError

func (err ErrTOTPNotEnabled) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrUserDoesNotExist

type ErrUserDoesNotExist struct {
	UserID int64
}

ErrUserDoesNotExist represents a "UserDoesNotExist" kind of error.

func (ErrUserDoesNotExist) Error

func (err ErrUserDoesNotExist) Error() string

func (ErrUserDoesNotExist) HTTPError

func (err ErrUserDoesNotExist) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrUserEmailExists

type ErrUserEmailExists struct {
	UserID int64
	Email  string
}

ErrUserEmailExists represents a "UserEmailExists" kind of error.

func (ErrUserEmailExists) Error

func (err ErrUserEmailExists) Error() string

func (ErrUserEmailExists) HTTPError

func (err ErrUserEmailExists) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrUsernameExists

type ErrUsernameExists struct {
	UserID   int64
	Username string
}

ErrUsernameExists represents a "UsernameAlreadyExists" kind of error.

func (ErrUsernameExists) Error

func (err ErrUsernameExists) Error() string

func (ErrUsernameExists) HTTPError

func (err ErrUsernameExists) HTTPError() web.HTTPError

HTTPError holds the http error description

type ErrWrongUsernameOrPassword

type ErrWrongUsernameOrPassword struct {
}

ErrWrongUsernameOrPassword is an error where the email was not confirmed

func (ErrWrongUsernameOrPassword) Error

func (err ErrWrongUsernameOrPassword) Error() string

func (ErrWrongUsernameOrPassword) HTTPError

func (err ErrWrongUsernameOrPassword) HTTPError() web.HTTPError

HTTPError holds the http error description

type Login

type Login struct {
	// The username used to log in.
	Username string `json:"username"`
	// The password for the user.
	Password string `json:"password"`
	// The totp passcode of a user. Only needs to be provided when enabled.
	TOTPPasscode string `json:"totp_passcode"`
}

Login Object to recive user credentials in JSON format

type PasswordReset

type PasswordReset struct {
	// The previously issued reset token.
	Token string `json:"token"`
	// The new password for this user.
	NewPassword string `json:"new_password"`
}

PasswordReset holds the data to reset a password

type PasswordTokenRequest

type PasswordTokenRequest struct {
	Email string `json:"email" valid:"email,length(0|250)" maxLength:"250"`
}

PasswordTokenRequest defines the request format for password reset resqest

type TOTP

type TOTP struct {
	ID     int64  `xorm:"int(11) autoincr not null unique pk" json:"-"`
	UserID int64  `xorm:"int(11) not null" json:"-"`
	Secret string `xorm:"text not null" json:"secret"`
	// The totp entry will only be enabled after the user verified they have a working totp setup.
	Enabled bool `xorm:"null" json:"enabled"`
	// The totp url used to be able to enroll the user later
	URL string `xorm:"text null" json:"url"`
}

TOTP holds a user's totp setting in the database.

func EnrollTOTP

func EnrollTOTP(user *User) (t *TOTP, err error)

EnrollTOTP creates a new TOTP entry for the user - it does not enable it yet.

func GetTOTPForUser

func GetTOTPForUser(user *User) (t *TOTP, err error)

GetTOTPForUser returns the current state of totp settings for the user.

func ValidateTOTPPasscode

func ValidateTOTPPasscode(passcode *TOTPPasscode) (t *TOTP, err error)

ValidateTOTPPasscode validated totp codes of users.

func (*TOTP) TableName

func (T *TOTP) TableName() string

TableName holds the table name for totp secrets

type TOTPPasscode

type TOTPPasscode struct {
	User     *User  `json:"-"`
	Passcode string `json:"passcode"`
}

TOTPPasscode is used to validate a users totp passcode

type User

type User struct {
	// The unique, numeric id of this user.
	ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
	// The username of the user. Is always unique.
	Username string `xorm:"varchar(250) not null unique" json:"username" valid:"length(1|250)" minLength:"3" maxLength:"250"`
	Password string `xorm:"varchar(250) not null" json:"-"`
	// The user's email address.
	Email    string `xorm:"varchar(250) null" json:"email,omitempty" valid:"email,length(0|250)" maxLength:"250"`
	IsActive bool   `xorm:"null" json:"-"`

	PasswordResetToken string `xorm:"varchar(450) null" json:"-"`
	EmailConfirmToken  string `xorm:"varchar(450) null" json:"-"`

	// A timestamp when this task was created. You cannot change this value.
	Created time.Time `xorm:"created not null" json:"created"`
	// A timestamp when this task was last updated. You cannot change this value.
	Updated time.Time `xorm:"updated not null" json:"updated"`

	web.Auth `xorm:"-" json:"-"`
}

User holds information about an user

func CheckUserCredentials

func CheckUserCredentials(u *Login) (*User, error)

CheckUserCredentials checks user credentials

func CreateUser

func CreateUser(user *User) (newUser *User, err error)

CreateUser creates a new user and inserts it into the database

func GetCurrentUser

func GetCurrentUser(c echo.Context) (user *User, err error)

GetCurrentUser returns the current user based on its jwt token

func GetFromAuth

func GetFromAuth(a web.Auth) (*User, error)

GetFromAuth returns a user object from a web.Auth object and returns an error if the underlying type is not a user object

func GetUser

func GetUser(user *User) (userOut *User, err error)

GetUser gets a user object

func GetUserByID

func GetUserByID(id int64) (user *User, err error)

GetUserByID gets informations about a user by its ID

func GetUserByUsername

func GetUserByUsername(username string) (user *User, err error)

GetUserByUsername gets a user from its user name. This is an extra function to be able to add an extra error check.

func GetUserFromClaims

func GetUserFromClaims(claims jwt.MapClaims) (user *User, err error)

GetUserFromClaims Returns a new user from jwt claims

func GetUserWithEmail

func GetUserWithEmail(user *User) (userOut *User, err error)

GetUserWithEmail returns a user object with email

func ListUsers

func ListUsers(searchterm string) (users []User, err error)

ListUsers returns a list with all users, filtered by an optional searchstring

func UpdateUser

func UpdateUser(user *User) (updatedUser *User, err error)

UpdateUser updates a user

func (*User) GetID

func (u *User) GetID() int64

GetID implements the Auth interface

func (User) TableName

func (User) TableName() string

TableName returns the table name for users

Jump to

Keyboard shortcuts

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