authn

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: MIT Imports: 21 Imported by: 2

README

authn

Go Reference

A simple authentication library for server side web apps.

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

Documentation

Index

Constants

View Source
const (
	// AccountIDKey IsAuthenticated stores the account id in request context using this key. Used in AccountIDFromContext
	AccountIDKey = "key_account_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")
	ErrInternal             = errors.New("internal server error")
	ErrUserExists           = errors.New("email is already linked to a user")
	ErrSendingEmail         = errors.New("problem sending the email")
	ErrInvalidToken         = errors.New("token is invalid")
	ErrUpdatingUser         = errors.New("failed updating user")
	ErrAttributeNotFound    = errors.New("attribute not found")
)

Functions

func AccountIDFromContext

func AccountIDFromContext(r *http.Request) string

AccountIDFromContext retrieves the account id stored by IsAuthenticated Middleware

Types

type API

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

func New

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

New authn client

func (*API) ConfirmRecovery

func (a *API) ConfirmRecovery(ctx context.Context, token, password string) error

func (*API) ConfirmSignupEmail

func (a *API) ConfirmSignupEmail(ctx context.Context, token string) error

ConfirmSignupEmail confirms the email used in signup

func (*API) CurrentAccount

func (a *API) CurrentAccount(r *http.Request) (*Account, error)

CurrentAccount retrieves the current logged in current

func (*API) GetAccount added in v0.0.3

func (a *API) GetAccount(ctx context.Context, userID string) (*Account, error)

GetAccount retrieves an account by user ID

func (*API) IsAuthenticated

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

IsAuthenticated is an authentication middleware It assumes form content type. For API authentication, please use CurrentAccount to implement your own mw.

func (*API) Login

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

Login logs in the account with email and password

func (*API) LoginProviderCallback

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

LoginProviderCallback handles the callback from a provider. It authenticates an account using a third-party provider(https://github.com/markbates/goth). A new account is created if not present.

func (*API) LoginWithPasswordlessToken

func (a *API) LoginWithPasswordlessToken(w http.ResponseWriter, r *http.Request, loginToken string) error

LoginWithPasswordlessToken authenticates the token sent to email previously with SendPasswordlessLoginLink and logs in the account

func (*API) LoginWithProvider

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

LoginWithProvider attempts to log in an existing account. If not successful, redirects to the provider signup flow for authentication. The callback from the provider redirect is to be handled by `LoginProviderCallback`

func (*API) Recovery

func (a *API) Recovery(ctx context.Context, email string) error

func (*API) SendPasswordlessToken

func (a *API) SendPasswordlessToken(ctx context.Context, email string) error

SendPasswordlessToken sends a passwordless login token to a previously signed up email

func (*API) Signup

func (a *API) Signup(ctx context.Context, email, password string, attributes map[string]interface{}) error

Signup a new account with email and password

type Account

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

func (*Account) Attributes

func (a *Account) Attributes() *Attributes

func (*Account) ChangeEmail

func (a *Account) ChangeEmail(ctx context.Context, newEmail string) error

func (*Account) ConfirmEmailChange

func (a *Account) ConfirmEmailChange(ctx context.Context, token string) error

func (*Account) Delete

func (a *Account) Delete(ctx context.Context) error

func (*Account) Email

func (a *Account) Email() string

func (*Account) ID

func (a *Account) ID() uuid.UUID

func (*Account) Logout

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

type Attributes

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

func (*Attributes) Del

func (a *Attributes) Del(ctx context.Context, key string) error

Del deletes an attribute from the database for the account

func (*Attributes) DelBytes

func (a *Attributes) DelBytes(ctx context.Context) error

DelBytes delete byte data from the database for the account

func (*Attributes) DelMany

func (a *Attributes) DelMany(ctx context.Context, keys []string) error

DelMany deletes many attributes from the database for the account

func (*Attributes) Get

func (a *Attributes) Get(key string) (interface{}, error)

Get gets an attribute from the database for the account

func (*Attributes) GetBytes

func (a *Attributes) GetBytes() []byte

GetBytes gets byte data from the database for the account

func (*Attributes) Map

func (a *Attributes) Map() M

Map gets all attributes from the database for the account

func (*Attributes) Session

func (a *Attributes) Session() *SessionAttributes

func (*Attributes) Set

func (a *Attributes) Set(ctx context.Context, key string, val interface{}) error

Set stores an attribute in the database for the account

func (*Attributes) SetBytes

func (a *Attributes) SetBytes(ctx context.Context, attr []byte) error

SetBytes stores byte data in the database for the account

func (*Attributes) SetMany

func (a *Attributes) SetMany(ctx context.Context, attrs map[string]interface{}) error

SetMany stores many attributes in the database for the account

type Config

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

type M

type M map[string]interface{}

func (M) Bool

func (m M) Bool(key string) (bool, bool)

func (M) Int

func (m M) Int(key string) (int, bool)

func (M) String

func (m M) String(key string) (string, bool)

type MailType

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

type SendMailFunc

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

type SessionAttributes

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

func (*SessionAttributes) Del

Del deletes an attribute from the temporary session store for the account

func (*SessionAttributes) Get

func (a *SessionAttributes) Get(key string) (interface{}, error)

Get gets an attribute from the temporary session store for the account

func (*SessionAttributes) Map

func (a *SessionAttributes) Map() (M, error)

Get gets an attribute from the temporary session store for the account

func (*SessionAttributes) Set

func (a *SessionAttributes) Set(w http.ResponseWriter, key string, val interface{}) error

Set sets an attribute in the temporary session store for the account

type SessionsStore

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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