minotaur

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2022 License: MIT Imports: 13 Imported by: 0

README

Minotaur

As the minotaur was the security guard for the labyrinth, minotaur brings security to go projects.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorAuthenticatingUser = errors.New("error authenticating user")
View Source
var ErrorPasswordResetExpired = errors.New("password reset expired")
View Source
var ErrorPasswordsMustMatch = errors.New("passwords must match")

Functions

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

func GenerateRandomURLSafeString

func GenerateRandomURLSafeString(n int) (string, error)

func SetToken

func SetToken(token string, r *http.Request)

func SetUserInfo

func SetUserInfo(userInfo UserInfo, r *http.Request)

func TokenFromContext

func TokenFromContext(r *http.Request) (string, bool)

Types

type AuthHandler

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

AuthHandler is the entry point into the minotaur managed

func NewAuthHandler

func NewAuthHandler(sessionAdapter SessionAdapter, authManager AuthManager, options ...AuthHandlerConfig) *AuthHandler

NewAuthHandler constructs a new AuthHandler with the defaults

func (*AuthHandler) AuthenticateUser

func (ah *AuthHandler) AuthenticateUser(w http.ResponseWriter, r *http.Request, userInfo UserInfo, fingerprint string) error

func (*AuthHandler) DoInitiateMagicLinkLogin

func (ah *AuthHandler) DoInitiateMagicLinkLogin(w http.ResponseWriter, r *http.Request)

func (*AuthHandler) DoLogin

func (ah *AuthHandler) DoLogin(w http.ResponseWriter, r *http.Request)

DoLogin this is a post endpoint

func (*AuthHandler) DoLogout

func (ah *AuthHandler) DoLogout(w http.ResponseWriter, r *http.Request)
func (ah *AuthHandler) DoRedeemMagicLink(w http.ResponseWriter, r *http.Request)

func (*AuthHandler) RedirectAuthedMiddleware

func (ah *AuthHandler) RedirectAuthedMiddleware(path string) func(handler http.Handler) http.Handler

func (*AuthHandler) SecurityMiddleware

func (ah *AuthHandler) SecurityMiddleware(errorResponse ErrorResponse, validRoles ...string) func(handler http.Handler) http.Handler

type AuthHandlerConfig

type AuthHandlerConfig func(handler *AuthHandler) error

AuthHandlerConfig this is the definition of the function to configure the AuthHandler. The constructor takes a variatic parameter of these.

func SetAuthSuccessRoute

func SetAuthSuccessRoute(successRoute string) AuthHandlerConfig

func SetFormEmailKey

func SetFormEmailKey(emailKey string) AuthHandlerConfig

func SetFormPasswordKey

func SetFormPasswordKey(passwordKey string) AuthHandlerConfig

func SetIdQueryKey

func SetIdQueryKey(idQueryKey string) AuthHandlerConfig

func SetLoginRedirect

func SetLoginRedirect(loginRedirect string) AuthHandlerConfig

func SetSessionTTL

func SetSessionTTL(sessionTTL time.Duration) AuthHandlerConfig

func SetTokenHeaderKey

func SetTokenHeaderKey(tokenHeaderKey string) AuthHandlerConfig

func SetTokenKey

func SetTokenKey(tokenKey string) AuthHandlerConfig

func SetTokenQueryKey

func SetTokenQueryKey(tokenQueryKey string) AuthHandlerConfig

type AuthManager

type AuthManager interface {
	TryLogin(email, password, fingerprint string) (UserInfo, UserToken, error)
	InitiateLogin(email string)
	RedeemLogin(token, tokenID, fingerprint string) (UserInfo, UserToken, error)
	AuthenticateUser(userInfo UserInfo, fingerprint string) (UserToken, error)
	LoadUser(token string) (UserInfo, UserToken, error)
	ListUserTokens(userID string) ([]UserToken, error)
	DeleteToken(token string) error
	RequestPasswordReset(email string) error
	RedeemPasswordResetRequest(passwordResetRequestID, token, password, verify string) error
}

func NewAuthManager

func NewAuthManager(userRepo UserInfoRepo, userTokenRepo UserTokenRepo, passwordEncoder PasswordEncoder, passwordResetRepo PasswordResetRequestRepo, opts ...AuthManagerConfig) AuthManager

type AuthManagerConfig

type AuthManagerConfig func(manager *AuthManagerImpl) error

func SetMagicLinkTemplateKey

func SetMagicLinkTemplateKey(templateKey string) AuthManagerConfig

func SetPasswordResetTemplateKey

func SetPasswordResetTemplateKey(passwordResetTemplate string) AuthManagerConfig

func SetPasswordResetTimeout

func SetPasswordResetTimeout(timeout time.Duration) AuthManagerConfig

type AuthManagerImpl

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

func (AuthManagerImpl) AuthenticateUser

func (a AuthManagerImpl) AuthenticateUser(userInfo UserInfo, fingerprint string) (UserToken, error)

func (AuthManagerImpl) DeleteToken

func (a AuthManagerImpl) DeleteToken(token string) error

func (AuthManagerImpl) InitiateLogin

func (a AuthManagerImpl) InitiateLogin(email string)

func (AuthManagerImpl) ListUserTokens

func (a AuthManagerImpl) ListUserTokens(userID string) ([]UserToken, error)

func (AuthManagerImpl) LoadUser

func (a AuthManagerImpl) LoadUser(token string) (UserInfo, UserToken, error)

func (AuthManagerImpl) RedeemLogin

func (a AuthManagerImpl) RedeemLogin(token, tokenID, fingerprint string) (UserInfo, UserToken, error)

func (AuthManagerImpl) RedeemPasswordResetRequest

func (a AuthManagerImpl) RedeemPasswordResetRequest(passwordResetRequestID, token, password, verify string) error

func (AuthManagerImpl) RequestPasswordReset

func (a AuthManagerImpl) RequestPasswordReset(email string) error

func (*AuthManagerImpl) SetupMagicLinkAuthentication

func (am *AuthManagerImpl) SetupMagicLinkAuthentication(magicLinkRepo MagicLinkRepo, messenger Messenger, sender Sender, fromEmail string)

func (AuthManagerImpl) TryLogin

func (a AuthManagerImpl) TryLogin(email, password, fingerprint string) (UserInfo, UserToken, error)

type BCryptPasswordEncoder

type BCryptPasswordEncoder struct {
}

func (BCryptPasswordEncoder) Encode

func (B BCryptPasswordEncoder) Encode(rawPassword string) (string, error)

func (BCryptPasswordEncoder) Matches

func (B BCryptPasswordEncoder) Matches(rawPassword, encodedPassword string) error

type DefaultUser

type DefaultUser struct {
	ID        uuid.UUID  `json:"id"`
	CreatedAt time.Time  `json:"createdAt"`
	UpdatedAt time.Time  `json:"UpdatedAt"`
	DeletedAt *time.Time `sql:"index" json:"deletedAt"`
	Email     string     `gorm:"index:user_email_idx" json:"email"`
	Name      string     `json:"name"`
	Role      string     `json:"role"`
	Password  string     `json:"-"`
	Avatar    string     `json:"avatar"`
	Bio       string     `gorm:"type:text" json:"bio"`
}

func (DefaultUser) AreCredentialsViable

func (d DefaultUser) AreCredentialsViable() bool

func (DefaultUser) GetEmail

func (d DefaultUser) GetEmail() string

func (DefaultUser) GetID

func (d DefaultUser) GetID() string

func (DefaultUser) GetName

func (d DefaultUser) GetName() string

func (DefaultUser) GetPassword

func (d DefaultUser) GetPassword() string

func (DefaultUser) GetRoles

func (d DefaultUser) GetRoles() []string

func (DefaultUser) IsAccountViable

func (d DefaultUser) IsAccountViable() bool

func (DefaultUser) IsEnabled

func (d DefaultUser) IsEnabled() bool

func (DefaultUser) TableName

func (DefaultUser) TableName() string

type DefaultUserManager

type DefaultUserManager interface {
	CreateUser(email, name, role, password string) (DefaultUser, error)
	StoreUser(r io.Reader) (DefaultUser, error)
	UpdateUser(r io.Reader) error
	UpdatePassword(id, password, verify string) error
	FindAndSanitizeUser(id string) (DefaultUser, error)
	UserCount() (int64, error)
}

func NewDefaultUserManagerImpl

func NewDefaultUserManagerImpl(repo DefaultUserRepo, passwordEncoder PasswordEncoder) DefaultUserManager

type DefaultUserManagerImpl

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

func (*DefaultUserManagerImpl) CreateUser

func (dm *DefaultUserManagerImpl) CreateUser(email, name, role, password string) (DefaultUser, error)

func (*DefaultUserManagerImpl) FindAndSanitizeUser

func (dm *DefaultUserManagerImpl) FindAndSanitizeUser(id string) (DefaultUser, error)

func (*DefaultUserManagerImpl) StoreUser

func (dm *DefaultUserManagerImpl) StoreUser(r io.Reader) (DefaultUser, error)

func (*DefaultUserManagerImpl) UpdatePassword

func (dm *DefaultUserManagerImpl) UpdatePassword(id, password, verify string) error

func (*DefaultUserManagerImpl) UpdateUser

func (dm *DefaultUserManagerImpl) UpdateUser(r io.Reader) error

func (*DefaultUserManagerImpl) UserCount

func (dm *DefaultUserManagerImpl) UserCount() (int64, error)

type DefaultUserRepo

type DefaultUserRepo interface {
	LoadUserByEmail(email string) (UserInfo, error)
	FindUser(id string) (UserInfo, error)
	Store(defUser DefaultUser) error
	UpdateUser(defuser DefaultUser) error
	UpdatePassword(id, password string) error
	UserCount() (int64, error)
}

func NewDefaultUserRepoGorm

func NewDefaultUserRepoGorm(db *gorm.DB) DefaultUserRepo

type DefaultUserRepoGorm

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

func (DefaultUserRepoGorm) FindUser

func (d DefaultUserRepoGorm) FindUser(id string) (UserInfo, error)

func (DefaultUserRepoGorm) LoadUserByEmail

func (d DefaultUserRepoGorm) LoadUserByEmail(email string) (UserInfo, error)

func (DefaultUserRepoGorm) Store

func (d DefaultUserRepoGorm) Store(defUser DefaultUser) error

func (DefaultUserRepoGorm) UpdatePassword

func (d DefaultUserRepoGorm) UpdatePassword(id, password string) error

func (DefaultUserRepoGorm) UpdateUser

func (d DefaultUserRepoGorm) UpdateUser(defUser DefaultUser) error

func (DefaultUserRepoGorm) UserCount

func (d DefaultUserRepoGorm) UserCount() (int64, error)

type Email

type Email interface {
	GetName() string
	GetAddress() string
}

type EmailImpl

type EmailImpl struct {
	Name    string
	Address string
}

func (EmailImpl) GetAddress

func (e EmailImpl) GetAddress() string

func (EmailImpl) GetName

func (e EmailImpl) GetName() string

type ErrorResponse

type ErrorResponse int
const (
	Redirect ErrorResponse = iota
	ResponseCode
)
type MagicLink struct {
	ID        uuid.UUID `json:"id"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`
	Token     string     `json:"token"`
	UserID    string     `json:"userId"`
}

func (MagicLink) TableName

func (MagicLink) TableName() string

type MagicLinkRepo

type MagicLinkRepo interface {
	Store(magicLink MagicLink) error
	Find(id string) (MagicLink, error)
	Delete(id string) error
}

type MagicLinkRepoGorm

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

func NewMagicLinkRepoGorm

func NewMagicLinkRepoGorm(db *gorm.DB) *MagicLinkRepoGorm

func (*MagicLinkRepoGorm) Delete

func (mr *MagicLinkRepoGorm) Delete(id string) error

func (*MagicLinkRepoGorm) Find

func (mr *MagicLinkRepoGorm) Find(id string) (MagicLink, error)

func (*MagicLinkRepoGorm) Store

func (mr *MagicLinkRepoGorm) Store(magicLink MagicLink) error

type Message

type Message interface {
	GetSubject() string
	GetPlain() string
	GetHtml() string
}

type MessageImpl

type MessageImpl struct {
	Subject string
	Plain   string
	Html    string
}

func (MessageImpl) GetHtml

func (m MessageImpl) GetHtml() string

func (MessageImpl) GetPlain

func (m MessageImpl) GetPlain() string

func (MessageImpl) GetSubject

func (m MessageImpl) GetSubject() string

type Messenger

type Messenger interface {
	RenderMessagePlain(w io.Writer, key string, data map[string]interface{}) error
	RenderMessageHtml(w io.Writer, key string, data map[string]interface{}) error
}

type PasswordEncoder

type PasswordEncoder interface {
	Encode(rawPassword string) (string, error)
	Matches(rawPassword, encodedPassword string) error
}

type PasswordResetRequest

type PasswordResetRequest struct {
	ID        uuid.UUID `json:"id"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`
	Email     string     `json:"email"`
	UserID    string
	Token     string
}

func (PasswordResetRequest) TableName

func (PasswordResetRequest) TableName() string

type PasswordResetRequestRepo

type PasswordResetRequestRepo interface {
	Store(prr PasswordResetRequest) error
	Find(id uuid.UUID) (PasswordResetRequest, error)
}

func NewPasswordResetRequestRepoImpl

func NewPasswordResetRequestRepoImpl(db *gorm.DB) PasswordResetRequestRepo

type PasswordResetRequestRepoImpl

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

func (*PasswordResetRequestRepoImpl) Find

func (*PasswordResetRequestRepoImpl) Store

type Sender

type Sender interface {
	Send(to, from Email, message Message) error
}

type SessionAdapter

type SessionAdapter interface {
	SetErrorFlash(w http.ResponseWriter, r *http.Request, msg string) error
	SetSuccessFlash(w http.ResponseWriter, r *http.Request, msg string) error
	SetValue(w http.ResponseWriter, r *http.Request, key, val string, ttl int) error
	GetValue(w http.ResponseWriter, r *http.Request, key string) (string, error)
	ResetSession(w http.ResponseWriter, r *http.Request) error
}

type UserInfo

type UserInfo interface {
	GetID() string
	GetEmail() string
	GetName() string
	GetRoles() []string
	GetPassword() string
	IsAccountViable() bool
	AreCredentialsViable() bool
	IsEnabled() bool
}

func UserInfoFromContext

func UserInfoFromContext(r *http.Request) (UserInfo, bool)

type UserInfoRepo

type UserInfoRepo interface {
	LoadUserByEmail(email string) (UserInfo, error)
	FindUser(id string) (UserInfo, error)
	UpdatePassword(id, password string) error
}

type UserToken

type UserToken struct {
	ID          uuid.UUID `json:"id"`
	CreatedAt   time.Time
	UpdatedAt   time.Time
	DeletedAt   *time.Time `sql:"index"`
	UserID      string     `json:"userId" gorm:"index:usertoken_userid_idx"`
	Token       string     `json:"token" gorm:"index:usertoken_token_idx"`
	Fingerprint string     `json:"fingerprint"`
}

func (UserToken) TableName

func (UserToken) TableName() string

type UserTokenRepo

type UserTokenRepo interface {
	Store(userToken UserToken) error
	FindByID(tokenID string) (UserToken, error)
	FindByToken(token string) (UserToken, error)
	DeleteToken(token string) error
	DeleteTokenById(tokenID string) error
	ListUserTokens(userID string) ([]UserToken, error)
}

type UserTokenRepoGorm

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

func NewUserTokenRepoGorm

func NewUserTokenRepoGorm(db *gorm.DB) *UserTokenRepoGorm

func (*UserTokenRepoGorm) DeleteToken

func (u *UserTokenRepoGorm) DeleteToken(token string) error

func (*UserTokenRepoGorm) DeleteTokenById

func (u *UserTokenRepoGorm) DeleteTokenById(tokenID string) error

func (*UserTokenRepoGorm) FindByID

func (u *UserTokenRepoGorm) FindByID(tokenID string) (UserToken, error)

func (*UserTokenRepoGorm) FindByToken

func (u *UserTokenRepoGorm) FindByToken(token string) (UserToken, error)

func (*UserTokenRepoGorm) ListUserTokens

func (u *UserTokenRepoGorm) ListUserTokens(userID string) ([]UserToken, error)

func (*UserTokenRepoGorm) Store

func (u *UserTokenRepoGorm) Store(userToken UserToken) error

Directories

Path Synopsis
scsadapter module

Jump to

Keyboard shortcuts

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