model

package
v0.0.0-...-8495d1f Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2021 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserRoleName  = "user"
	AdminRoleName = "admin"
)
View Source
const (
	// SessionCookieToken is the cookie key for the authorization token.
	SessionCookieToken = "DASHBOARDAUTHTOKEN"
	// SessionCookieCSRF is the cookie key for the CSRF token.
	SessionCookieCSRF = "DASHBOARDCSRF"
	// SessionCookieUser is the cookie key for the logged in user.
	SessionCookieUser = "DASHBOARDUSERID"
	// SessionHeader is the header key for a session.
	SessionHeader = "Token"
	// SessionLengthMilliseconds is the session length in milliseconds.
	SessionLengthMilliseconds = 1000 * 60 * 60 * 24 * 15 // 15 days
	// HeaderRequestedWith is the HTTP header X-Requested-With.
	HeaderRequestedWith = "X-Requested-With"
	// HeaderRequestedWithXML is the HTTP header value XMLHttpRequest.
	HeaderRequestedWithXML = "XMLHttpRequest"
	// HeaderForwardedProto is the HTTP header X-Forwarded-Proto.
	HeaderForwardedProto = "X-Forwarded-Proto"
	// HeaderRequestID is the custom header to track request ID.
	HeaderRequestID = "X-Request-ID"
	// HeaderAuthorization is the HTTP header Authorization.
	HeaderAuthorization = "Authorization"
	// HeaderApiKey is the HTTP header containing API key.
	HeaderApiKey = "X-CTRL-Api-Key"
	// HeaderCSRFToken is the HTTP header for holding the CSRF token.
	HeaderCSRFToken = "X-CSRF-Token"
	// AuthorizationBearer is the bearer HTTP authorization type.
	AuthorizationBearer = "BEARER"
)
View Source
const (
	// TokenSize is the size of the random value of a token.
	TokenSize = 64
	// TokenSizeDigits is the size of the smaller token used during signup.
	TokenSizeDigits = 6
	// TokenTypeVerifyEmail is the token type used for user email verification.
	TokenTypeVerifyEmail = "verify_email"
	// TokenTypeResetPassword the token type used for resetting user passwords.
	TokenTypeResetPassword = "reset_password"
	// TokenDefaultExpiryTime is the default time for tokens to expire.
	TokenDefaultExpiryTime = 1000 * 60 * 60 * 24 // 24 hour
)
View Source
const (
	// UserStateActive means the user is fully active and no restrictions exist
	UserStateActive = "active"
	// UserStateLocked means the user should not be able to access the dashboard
	UserStateLocked = "locked"
)
View Source
const DefaultHTTPTimeout = time.Minute

DefaultHTTPTimeout is the default timeout for HTTP requests made by Client

View Source
const (
	// DefaultTokenTTL is the OAuth state expiry duration in milliseconds
	DefaultTokenTTL = 10 * 60
)
View Source
const (
	EventTypeSendAdminWelcomeEmail = "send-admin-welcome-email"
)

Variables

View Source
var (
	CurrentVersion string = versions[0]
	BuildNumber    string
	BuildTime      string
	BuildHash      string
)

Functions

func ComparePassword

func ComparePassword(hash string, password string) bool

ComparePassword compares the hash to the password.

func CreateTokenTypeResetPasswordExtra

func CreateTokenTypeResetPasswordExtra(email string) (string, error)

CreateTokenTypeResetPasswordExtra returns the correct extra values for a token of type TokenTypeResetPassword.

func GetMillis

func GetMillis() int64

GetMillis is a convenience method to get milliseconds since epoch.

func HashPassword

func HashPassword(password string) string

HashPassword generates a hash using the bcrypt.GenerateFromPassword

func IsLower

func IsLower(s string) bool

IsLower will check if a string is lowercased

func IsValidEmail

func IsValidEmail(email string) bool

IsValidEmail will perform some minimal validations on whether the provided string is a valid email address.

func IsValidPassword

func IsValidPassword(password string) bool

IsValidPassword will perform some minimal validations on whether the provided password meets our requirements.

func NewID

func NewID() string

NewID is a globally unique identifier. It is a [A-Z0-9] string 26 characters long. It is a UUID version 4 Guid that is zbased32 encoded with the padding stripped off.

func NewRandomNumber

func NewRandomNumber(length int) string

NewRandomNumber returns a random string of the given length, built from digits

func NewRandomString

func NewRandomString(length int) string

NewRandomString returns a random string of the given length. The resulting entropy will be (5 * length) bits.

Types

type APIError

type APIError struct {
	Message string `json:"message"`
}

APIError is the error struct returned by REST API endpoints.

func APIErrorFromReader

func APIErrorFromReader(reader io.Reader) (*APIError, error)

APIErrorFromReader decodes a json-encoded APIError from the given io.Reader.

type Client

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

Client is the programmatic interface to the server API.

func NewClient

func NewClient(address string) *Client

NewClient creates a client to the server at the given address.

func NewClientWithHeaders

func NewClientWithHeaders(address string, headers map[string]string) *Client

NewClientWithHeaders creates a client to the server at the given address and uses the provided headers.

func (*Client) BuildURL

func (c *Client) BuildURL(urlPath string, args ...interface{}) string

BuildURL builds the request URL based on the configured address.

func (*Client) ForgotPassword

func (c *Client) ForgotPassword(request *ForgotPasswordRequest) error

ForgotPassword starts the forgot-password flow for a user

func (*Client) GetMe

func (c *Client) GetMe() (*User, error)

GetMe gets the currently logged in user.

func (*Client) Headers

func (c *Client) Headers() map[string]string

Headers will return a copy of the HTTP headers that the client sends with all requests.

func (*Client) Login

func (c *Client) Login(request *LoginRequest) (*User, error)

Login will log a user in.

func (*Client) Logout

func (c *Client) Logout() error

Logout will log out the current user.

func (*Client) ResetPassword

func (c *Client) ResetPassword(request *ResetPasswordRequest) error

ResetPassword finishes the forgot-password flow for a user

func (*Client) SignUp

func (c *Client) SignUp(request *SignUpRequest) (*SignUpResponse, error)

SignUp creates a user.

func (*Client) UpdateMe

func (c *Client) UpdateMe(user *User) (*User, error)

UpdateMe updates the logged in user.

func (*Client) UpdatePassword

func (c *Client) UpdatePassword(request *UpdatePasswordRequest) error

UpdatePassword updates a user's password.

func (*Client) VerifyEmailComplete

func (c *Client) VerifyEmailComplete(request *VerifyEmailRequest) error

VerifyEmailComplete finishes the verify email flow for the user

func (*Client) VerifyEmailStart

func (c *Client) VerifyEmailStart() error

VerifyEmailStart starts the verify email flow for the user

type ForgotPasswordRequest

type ForgotPasswordRequest struct {
	Email string `json:"email"`
}

ForgotPasswordRequest specifies the forgot password parameters.

type LoginRequest

type LoginRequest struct {
	Email    string `json:"email"`
	Password string `json:"password"`
}

LoginRequest specifies the login parameters.

type OAuthState

type OAuthState struct {
	ID        string `json:"id"`
	Token     string `json:"token"`
	CreateAt  int64  `json:"create_at" db:"create_at"`
	ExpiresAt int64  `json:"expires_at" db:"expires_at"`
}

func (*OAuthState) IsExpired

func (o *OAuthState) IsExpired() bool

IsExpired returns true if the OAuth state is expired.

func (*OAuthState) PreSave

func (o *OAuthState) PreSave()

PreSave will set the ID, Token, ExpiresAt and CreateAt for the OAuth state.

type ResetPasswordRequest

type ResetPasswordRequest struct {
	Password string `json:"password"`
	Token    string `json:"token"`
}

ResetPasswordRequest specifies the reset password parameters.

type Role

type Role struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	CreateAt int64  `json:"create_at" db:"create_at"`
	UpdateAt int64  `json:"update_at" db:"update_at"`
}

func (*Role) CreatePreSave

func (r *Role) CreatePreSave()

CreatePreSave will set the correct values for a new role that is about to be saved.

type Session

type Session struct {
	ID        string `json:"id"`
	Token     string `json:"token"`
	CreateAt  int64  `json:"create_at" db:"create_at"`
	ExpiresAt int64  `json:"expires_at" db:"expires_at"`
	UserID    string `json:"user_id" db:"user_id"`
	CSRFToken string `json:"csrf_token" db:"csrf_token"`
}

Session is an authentication session for a user.

func (*Session) IsExpired

func (s *Session) IsExpired() bool

IsExpired returns true if the session is expired.

func (*Session) PreSave

func (s *Session) PreSave()

PreSave will set the ID, Token, CSRFToken, ExpiresAt and CreateAt for the session.

type SignUpRequest

type SignUpRequest struct {
	Email     string `json:"email"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	Password  string `json:"password"`
}

SignUpRequest contains the fields for a request to the sign-up API.

type SignUpResponse

type SignUpResponse struct {
	User *User `json:"user"`
}

SignUpResponse contains the user.

func SignUpResponseFromReader

func SignUpResponseFromReader(reader io.Reader) (*SignUpResponse, error)

SignUpResponseFromReader decodes a json-encoded sign-up API response from the given io.Reader.

type Token

type Token struct {
	Token    string `json:"token"`
	CreateAt int64  `json:"create_at" db:"create_at"`
	Type     string `json:"type"`
	Extra    string `json:"extra"`
}

Token is a one-time-use expiring object used in place of passwords or other authentication values.

func NewToken

func NewToken(tokentype, extra string) *Token

NewToken returns a new token.

func (*Token) GetExtraEmail

func (t *Token) GetExtraEmail() (string, error)

GetExtraEmail returns the correct extra values for a token of type TokenTypeChangePassword.

func (*Token) IsValid

func (t *Token) IsValid() error

IsValid checks a token for valid configuration

type TokenExtraEmail

type TokenExtraEmail struct {
	Email string `json:"email"`
}

TokenExtraEmail is a token extra field containing an email value.

type UpdatePasswordRequest

type UpdatePasswordRequest struct {
	NewPassword     string `json:"new_password"`
	CurrentPassword string `json:"current_password"`
}

UpdatePasswordRequest specifies the update password parameters.

type User

type User struct {
	ID            string `json:"id"`
	CreateAt      int64  `json:"create_at" db:"create_at"`
	UpdateAt      int64  `json:"update_at" db:"update_at"`
	Email         string `json:"email"`
	EmailVerified bool   `json:"email_verified" db:"email_verified"`
	Password      string `json:"password,omitempty"`
	FirstName     string `json:"first_name,omitempty" db:"first_name"`
	LastName      string `json:"last_name,omitempty" db:"last_name"`
	State         string `json:"state"`
	IsAdmin       bool   `json:"is_admin" db:"is_admin"`
}

TODO: cleanup User model represents a user on the system.

func UserFromReader

func UserFromReader(reader io.Reader) (*User, error)

UserFromReader decodes a json-encoded user from the given io.Reader.

func (*User) CreatePreSave

func (u *User) CreatePreSave()

CreatePreSave will set the correct values for a new user that is about to be saved.

func (*User) GetUserEmailDomain

func (u *User) GetUserEmailDomain() string

func (*User) IsValid

func (u *User) IsValid() error

IsValid will determine if the user fields are all valid.

func (*User) IsValidPassword

func (u *User) IsValidPassword() error

IsValidPassword returns an error if the user's password is not valid.

func (*User) Sanitize

func (u *User) Sanitize()

Sanitize clears any sensitive data from the user.

type UserAuthInfo

type UserAuthInfo struct {
	UserID        string       `json:"user_id"`
	OAuthProvider string       `json:"oauth_provider"`
	Token         oauth2.Token `json:"token"`
	Username      string       `json:"username"`
	Email         string       `json:"email,omitempty"`
	Name          string       `json:"name,omitempty"`
	AvatarURL     string       `json:"avatar_url"`
	CreateAt      int64        `json:"create_at" db:"create_at"`
	UpdateAt      int64        `json:"update_at" db:"update_at"`
	DeleteAt      int64        `json:"update_at" db:"delete_at"`
}

func (*UserAuthInfo) PreSave

func (u *UserAuthInfo) PreSave()

PreSave will set the ID and CreateAt for the UserAuthInfo.

type UserFacingError

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

UserFacingError is an error type meant to be read by end users.

func NewUserFacingError

func NewUserFacingError(messageForUser, internalDetails string) *UserFacingError

NewUserFacingError creates a new error including a user facing message.

func (*UserFacingError) Error

func (u *UserFacingError) Error() string

func (*UserFacingError) ErrorForUser

func (u *UserFacingError) ErrorForUser() string

ErrorForUser returns the error message meant for the end user.

type UserRole

type UserRole struct {
	UserID   string `json:"user_id" db:"user_id"`
	RoleID   string `json:"role_id" db:"role_id"`
	CreateAt int64  `json:"create_at" db:"create_at"`
	UpdateAt int64  `json:"update_at" db:"update_at"`
}

type VerifyEmailRequest

type VerifyEmailRequest struct {
	Token string `json:"token"`
}

VerifyEmailRequest specifies the verify-email parameters.

Jump to

Keyboard shortcuts

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