confirmcode

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2021 License: BSD-3-Clause Imports: 14 Imported by: 0

README

confirmcode

Experimental authboss package that allows confirming users via copy-paste codes instead of links.

There will be no support for this and no backward-compatibility guarantees until further notice.

Documentation

Overview

Package confirmcode implements confirmation of user registration via e-mail with copy-paste codes.

Index

Constants

View Source
const (
	// PageConfirm is only really used for the BodyReader
	PageConfirm = "confirm"

	// EmailConfirmHTML is the name of the html template for e-mails
	EmailConfirmHTML = "confirm_html"
	// EmailConfirmTxt is the name of the text template for e-mails
	EmailConfirmTxt = "confirm_txt"

	// DataConfirmCode is the name of the template field for the user's code
	DataConfirmCode = "code"
)

Variables

View Source
var (
	// ErrRetryLimit is returned when the user has requested a resend but
	// it is too soon.
	ErrRetryLimit = errors.New("retry limit reached please wait")
)

Functions

func GenerateConfirmCreds

func GenerateConfirmCreds(length int) (code string, hash string, err error)

GenerateConfirmCreds creates a code of given length and returns the sha512 of that code as well for storage in a database.

func Middleware

func Middleware(ab *authboss.Authboss) func(http.Handler) http.Handler

Middleware ensures that a user is confirmed, or else it will intercept the request and send them to the confirm page, this will load the user if he's not been loaded yet from the session.

Panics if the user was not able to be loaded in order to allow a panic handler to show a nice error page, also panics if it failed to redirect for whatever reason.

Types

type Config

type Config struct {
	// Add Dashes to codes after each N characters
	AddDashes int
	// Length of the token excluding automatically added dashes.
	Length int

	// Period between attempts
	AttemptPeriod time.Duration
	// CodeExpiry is the duration before a code expires.
	CodeExpiry time.Duration

	// PathOK is where to redirect when the user is confirmed
	PathOK string
	// PathNotOK is the url back to the confirm page
	PathNotOK string
}

Config for the confirm module

func Defaults

func Defaults() *Config

Defaults returns a safe default configuration

type Confirm

type Confirm struct {
	*authboss.Authboss
	// contains filtered or unexported fields
}

Confirm module

func Setup

func Setup(ab *authboss.Authboss, cfg *Config) (*Confirm, error)

Setup the confirm module

func (*Confirm) Get

func (c *Confirm) Get(w http.ResponseWriter, r *http.Request) error

Get shows a confirm page

func (*Confirm) Post

func (c *Confirm) Post(w http.ResponseWriter, r *http.Request) error

Post the code to the confirm page

func (*Confirm) PreventAuth

func (c *Confirm) PreventAuth(w http.ResponseWriter, r *http.Request, handled bool) (bool, error)

PreventAuth stops the EventAuth from succeeding when a user is not confirmed This relies on the fact that the context holds the user at this point in time loaded by the auth module (or something else).

func (*Confirm) SendConfirmEmail

func (c *Confirm) SendConfirmEmail(ctx context.Context, to, code string)

SendConfirmEmail sends a confirmation e-mail to a user

func (*Confirm) StartConfirmation

func (c *Confirm) StartConfirmation(ctx context.Context, user User, sendEmail bool) error

StartConfirmation begins confirmation on a user by setting them to require confirmation via a created token, and optionally sending them an e-mail.

func (*Confirm) StartConfirmationWeb

func (c *Confirm) StartConfirmationWeb(w http.ResponseWriter, r *http.Request, handled bool) (bool, error)

StartConfirmationWeb hijacks a request and forces a user to be confirmed first it's assumed that the current user is loaded into the request context.

type Storer

type Storer interface {
	authboss.ServerStorer

	// LoadByConfirmCode finds a user by his confirm selector field
	// and should return ErrUserNotFound if that user cannot be found.
	LoadByConfirmCode(ctx context.Context, code string) (User, error)
}

Storer for confirm module, allows looking up a user by a code

func EnsureCanConfirm

func EnsureCanConfirm(storer authboss.ServerStorer) Storer

EnsureCanConfirm makes sure the server storer supports confirm-lookup operations

type User

type User interface {
	authboss.User

	GetEmail() (email string)
	GetConfirmed() (confirmed bool)
	GetConfirmCode() (code string)
	GetConfirmExpiration() (date time.Time)
	GetConfirmLastAttempt() (date time.Time)

	PutEmail(email string)
	PutConfirmed(confirmed bool)
	PutConfirmCode(verifier string)
	PutConfirmExpiration(date time.Time)
	PutConfirmLastAttempt(date time.Time)
}

User can be in a state of confirmed or not.

func MustHaveConfirmFields

func MustHaveConfirmFields(u authboss.User) User

MustHaveConfirmFields ensures the user has confirm-related fields

Jump to

Keyboard shortcuts

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