users

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2020 License: MIT Imports: 24 Imported by: 1

README

users

Go Reference

A simple authentication library.

  1. Signup using email
  2. Complete Login flow(confirmation, recovery, change email).
  3. Magic Logins
  4. Oauth2 Login via goth integration.
  5. API Tokens
  6. Multiple database(mysql, postgres, sqlite) support via https://entgo.io/

Example Usage

A complete implementation usage exists at gomodest.

Documentation

Index

Constants

View Source
const (
	CtxUserIdKey = "key_user_id"
)

Variables

View Source
var (
	ErrInvalidPassword      = errors.New("password is invalid")
	ErrWrongPassword        = errors.New("password is wrong")
	ErrInvalidEmail         = errors.New("email is invalid")
	ErrUserNotFound         = errors.New("user not found")
	ErrUserNotLoggedIn      = errors.New("user is not logged in")
	ErrEmailNotConfirmed    = errors.New("email has not been confirmed")
	ErrLoginSessionNotFound = errors.New("a valid login session wasn't found")
	ErrSessionValNotFound   = errors.New("session val not found")
	ErrInternal             = errors.New("internal server error")
	ErrUserExists           = errors.New("email is already linked to a user")
	ErrSendingEmail         = errors.New("problem sending the email")
)

Functions

This section is empty.

Types

type API

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

func NewAPI

func NewAPI(ctx context.Context, apiMasterSecret string,
	userStore UserStore, sessionStore SessionsStore, workspaceStore WorkspaceStore,
	sendMail SendMailFunc, roles map[string][]Permission) *API

func NewDefaultAPI

func NewDefaultAPI(ctx context.Context, cfg Config) (*API, error)

func (*API) AddRole added in v0.1.5

func (a *API) AddRole(r *http.Request, role string) error

func (*API) Can added in v0.1.5

func (a *API) Can(r *http.Request, action, target string) (bool, error)

func (*API) ChangeEmail

func (a *API) ChangeEmail(id, newEmail string) error

func (*API) ClearRoles added in v0.1.5

func (a *API) ClearRoles(r *http.Request) error

func (*API) ConfirmEmail

func (a *API) ConfirmEmail(token string) error

func (*API) ConfirmEmailChange

func (a *API) ConfirmEmailChange(token string) error

func (*API) ConfirmRecovery

func (a *API) ConfirmRecovery(token, password string) error

func (*API) DelSessionVal

func (a *API) DelSessionVal(r *http.Request, w http.ResponseWriter, key string) error

func (*API) DeleteMetaDataKeys

func (a *API) DeleteMetaDataKeys(r *http.Request, keys []string) error

func (*API) DeleteRole added in v0.1.5

func (a *API) DeleteRole(r *http.Request, role string) error

func (*API) DeleteUser

func (a *API) DeleteUser(r *http.Request) error

func (*API) GetRoles added in v0.1.5

func (a *API) GetRoles(r *http.Request) ([]string, error)

func (*API) GetSessionStringVal added in v0.1.3

func (a *API) GetSessionStringVal(r *http.Request, key string) *string

func (*API) GetSessionVal

func (a *API) GetSessionVal(r *http.Request, key string) (interface{}, error)

func (*API) GetWorkspaceRole added in v0.1.7

func (a *API) GetWorkspaceRole(r *http.Request, workspaceID string) (string, error)

func (*API) HandleGothCallback

func (a *API) HandleGothCallback(w http.ResponseWriter, r *http.Request, role string, metadata map[string]interface{}) error

func (*API) HandleGothLogin

func (a *API) HandleGothLogin(w http.ResponseWriter, r *http.Request) error

func (*API) HandleGothLogout

func (a *API) HandleGothLogout(w http.ResponseWriter, r *http.Request) error

func (*API) InviteGuests added in v0.1.7

func (a *API) InviteGuests(r *http.Request, workspaceID string, guests []string) error

func (*API) IsAuthenticated

func (a *API) IsAuthenticated(next http.Handler) http.Handler

func (*API) LoggedInUser

func (a *API) LoggedInUser(r *http.Request) (*User, error)

func (*API) Login

func (a *API) Login(w http.ResponseWriter, r *http.Request, email, password string) error

func (*API) LoginWithOTP

func (a *API) LoginWithOTP(w http.ResponseWriter, r *http.Request, otp string) error

func (*API) Logout

func (a *API) Logout(w http.ResponseWriter, r *http.Request)

func (*API) OTP

func (a *API) OTP(email string) error

func (*API) Recovery

func (a *API) Recovery(email string) error

func (*API) ResetAPIToken

func (a *API) ResetAPIToken(r *http.Request) (string, error)

func (*API) SetSessionVal

func (a *API) SetSessionVal(r *http.Request, w http.ResponseWriter, key string, val interface{}) error

func (*API) Signup

func (a *API) Signup(email, password, role string, metadata map[string]interface{}) error

func (*API) UpdateBillingID added in v0.1.2

func (a *API) UpdateBillingID(r *http.Request, billingID string) error

func (*API) UpdateMetaData

func (a *API) UpdateMetaData(r *http.Request, metaData map[string]interface{}) error

type Config

type Config struct {
	Driver          string
	Datasource      string
	SessionSecret   string
	SendMail        SendMailFunc
	APIMasterSecret string
	GothProviders   []goth.Provider
	Roles           map[string][]Permission
}

type MailType

type MailType int
const (
	Confirmation MailType = iota
	Recovery
	ChangeEmail
	OTP
)

type Permission added in v0.1.5

type Permission struct {
	ActionMatcher rbac.Matcher
	TargetMatcher func(userID string) rbac.Matcher
}

func NewPermission added in v0.1.6

func NewPermission(action string, targetMatcher func(userID string) rbac.Matcher) Permission

type SendMailFunc

type SendMailFunc func(mailType MailType, token, sendTo string, metadata map[string]interface{}) error

type SessionsStore

type SessionsStore interface {
	sessions.Store
	Close() error
}

func NewDefaultSessionStore

func NewDefaultSessionStore(ctx context.Context, driver, dataSource string, keyPairs ...[]byte) (SessionsStore, error)

type User

type User struct {
	ID            string                 `json:"id"`
	BillingID     string                 `json:"billing_id"`
	Email         string                 `json:"email"`
	IsAPITokenSet bool                   `json:"is_api_token_set"`
	Metadata      map[string]interface{} `json:"metadata"`
	Workspaces    []Workspace            `json:"workspaces"`
}

type UserStore

type UserStore interface {
	// Create, Delete
	New(email, password, role, provider string, meta map[string]interface{}, sendMailFunc func(string, string) error) (string, error)
	UserData(id string) (string, string, string, map[string]interface{}, error)
	UserIDByEmail(email string) (string, error)
	UserIDByConfirmationToken(token string) (string, error)
	UserIDByRecoveryToken(token string) (string, error)
	UserIDByEmailChangeToken(token string) (string, error)
	UserIDByOTP(token string) (string, error)
	UserIDByAPIKey(apiKey string) (string, error)
	DeleteUser(id string) error

	GetPassword(id string) (string, error)
	GetAPIKey(id string) (string, error)
	GetEmailChange(id string) (string, error)
	IsEmailConfirmed(id string) (bool, error)
	GetRoles(id string) ([]string, error)

	AddRole(id, role string) error
	DeleteRole(id, role string) error
	ClearRoles(id string) error
	// Update Password
	UpdatePassword(id, password string) error
	// Update API Key
	UpdateAPIKey(id, apiKey string) error
	// Update Email
	UpdateEmail(id, email string) error
	UpdateBillingID(id, billingID string) error
	UpdateProvider(id, provider string) error

	// Confirm Email
	SaveConfirmationToken(id, token string) error
	SaveConfirmationTokenSentAt(id string, tokenSentAt time.Time) error
	MarkConfirmed(id string, confirmed bool) error
	DeleteConfirmToken(id string) error

	// One time password
	SaveOTP(id, otp string) error
	SaveOTPSentAt(id string, otpSentAt time.Time) error
	DeleteOTP(id string) error

	// Recover Password
	SaveRecoveryToken(id, token string) error
	SaveRecoveryTokenSentAt(id string, tokenSentAt time.Time) error
	DeleteRecoveryToken(id string) error

	// Change Email
	SaveEmailChangeToken(id, email, token string) error
	SaveEmailChangeTokenSentAt(id string, tokenSentAt time.Time) error
	DeleteEmailChangeToken(id string) error

	// Metadata
	UpsertMetaData(id string, metaData map[string]interface{}) error
	DeleteKeysMetaData(id string, keys []string) error
	DeleteAllMetadata(id string) error

	// Timestamps
	SetUpdatedAt(id string, time time.Time) error
	SetLastSignInAt(id string, time time.Time) error
	Close() error
}

UserStore represents the data store for the User model

func NewDefaultUserStore

func NewDefaultUserStore(ctx context.Context, driver, dataSource string) (UserStore, error)

type Workspace added in v0.1.7

type Workspace struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Role string `json:"role"`
}

type WorkspaceStore added in v0.1.7

type WorkspaceStore interface {
	GetWorkspace(id string) (string, string, map[string]interface{}, error)
	CreateWorkspace(userID, name, description, plan string, metadata map[string]interface{}) (string, error)
	UpdateWorkspace(workspaceID, name, description, plan string, metadata map[string]interface{}) error
	DeleteWorkspace(workspaceID string) error
	GetUserWorkspaces(userID string) (map[string][]string, error)

	WorkspaceUpsertUser(workspaceID, userID, role string) error
	WorkspaceRemoveUser(workspaceID, userID string) error
	GetWorkspaceGroups(workspaceID string) ([]string, error)

	GetGroup(id string) (string, string, map[string]interface{}, error)
	CreateGroup(userID, workspaceID, name, description string, metadata map[string]interface{}) (string, error)
	UpdateGroup(groupID, name, description string, metadata map[string]interface{}) error
	DeleteGroup(groupID string) error
	GetUserGroupRoles(userID string) (map[string]string, error)
	GroupUpsertUser(groupID, userID, role string) error
	GroupRemoveUser(groupID, userID string) error

	CreatePermission(roleID, action, target string) error
	UpdatePermission(roleID, action, target string) error
	DeletePermission(roleID, action string) error
	GetUserPermissions(userID string) (map[string]string, error)

	GetUserRoles(userID string) ([]string, error)
	CreateUserRole(userID, role string) error
	DeleteUserRole(userID, role string) error
}

func NewDefaultWorkspaceStore added in v0.1.7

func NewDefaultWorkspaceStore(ctx context.Context, driver, dataSource string) (WorkspaceStore, error)

Jump to

Keyboard shortcuts

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