users

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2020 License: MIT Imports: 22 Imported by: 0

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

This section is empty.

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, sendMail SendMailFunc) *API

func NewDefaultAPI

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

func (*API) ChangeEmail

func (a *API) ChangeEmail(id, newEmail string) 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(id string, keys []string) error

func (*API) DeleteUser

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

func (*API) GetSessionVal

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

func (*API) HandleGothCallback

func (a *API) HandleGothCallback(w http.ResponseWriter, r *http.Request) 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) 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 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(id string, metaData map[string]interface{}) error

type Config

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

type MailType

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

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"`
}

type UserStore

type UserStore interface {
	// Create, Delete
	New(email, password, provider string, meta map[string]interface{}) (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)

	// 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)

Jump to

Keyboard shortcuts

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