defaults

package
v2.4.1+incompatible Latest Latest
Warning

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

Go to latest
Published: May 19, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package defaults houses default implementations for the very many interfaces that authboss has. It's a goal of the defaults package to provide the core where authboss implements the shell.

It's simultaneously supposed to be possible to take as many or as few of these implementations as you desire, allowing you to reimplement where necessary, but reuse where possible.

Index

Constants

View Source
const (
	FormValueEmail    = "email"
	FormValuePassword = "password"
	FormValueUsername = "username"

	FormValueConfirm      = "cnf"
	FormValueToken        = "token"
	FormValueCode         = "code"
	FormValueRecoveryCode = "recovery_code"
	FormValuePhoneNumber  = "phone_number"
)

FormValue types

Variables

This section is empty.

Functions

func SetCore

func SetCore(config *authboss.Config, readJSON, useUsername bool)

SetCore creates instances of all the default pieces with the exception of ViewRenderer which should be already set before calling this method.

func URLValuesToMap

func URLValuesToMap(form url.Values) map[string]string

URLValuesToMap helps create a map from url.Values

Types

type ConfirmValues

type ConfirmValues struct {
	HTTPFormValidator

	Token string
}

ConfirmValues retrieves values on the confirm page.

func (ConfirmValues) GetToken

func (c ConfirmValues) GetToken() string

GetToken from the confirm values

type ErrorHandler

type ErrorHandler struct {
	LogWriter authboss.Logger
}

ErrorHandler wraps http handlers with errors with itself to provide error handling.

The pieces provided to this struct must be thread-safe since they will be handed to many pointers to themselves.

func NewErrorHandler

func NewErrorHandler(logger authboss.Logger) ErrorHandler

NewErrorHandler constructor

func (ErrorHandler) Wrap

func (e ErrorHandler) Wrap(handler func(w http.ResponseWriter, r *http.Request) error) http.Handler

Wrap an http handler with an error

type FieldError

type FieldError struct {
	FieldName string
	FieldErr  error
}

FieldError represents an error that occurs during validation and is always attached to field on a form.

func NewFieldError

func NewFieldError(name string, err error) FieldError

NewFieldError literally only exists because of poor name planning where name and err can't be exported on the struct due to the method names

func (FieldError) Err

func (f FieldError) Err() error

Err for the field

func (FieldError) Error

func (f FieldError) Error() string

Error in string form

func (FieldError) Name

func (f FieldError) Name() string

Name of the field the error is about

type HTTPBodyReader

type HTTPBodyReader struct {
	// ReadJSON if turned on reads json from the http request
	// instead of a encoded form.
	ReadJSON bool

	// UseUsername instead of e-mail address
	UseUsername bool

	// Rulesets for each page.
	Rulesets map[string][]Rules
	// Confirm fields for each page.
	Confirms map[string][]string
	// Whitelist values for each page through the html forms
	// this is for security so that we can properly protect the
	// arbitrary user API. In reality this really only needs to be set
	// for the register page since everything else is expecting
	// a hardcoded set of values.
	Whitelist map[string][]string
}

HTTPBodyReader reads forms from various pages and decodes them.

func NewHTTPBodyReader

func NewHTTPBodyReader(readJSON, useUsernameNotEmail bool) *HTTPBodyReader

NewHTTPBodyReader creates a form reader with default validation rules and fields for each page. If no defaults are required, simply construct this using the struct members itself for more control.

func (HTTPBodyReader) Read

Read the form pages

type HTTPFormValidator

type HTTPFormValidator struct {
	Values map[string]string

	Ruleset       []Rules
	ConfirmFields []string
}

HTTPFormValidator validates HTTP post type inputs

func (HTTPFormValidator) Validate

func (h HTTPFormValidator) Validate() []error

Validate validates a request using the given ruleset.

type JSONRenderer

type JSONRenderer struct {
	Failures []string
}

JSONRenderer simply renders the data provided in JSON. Known failure keys in the HTMLData can be passed in to force a status: failure in the JSON when they appear.

func (JSONRenderer) Load

func (JSONRenderer) Load(names ...string) error

Load is a no-op since json doesn't require any templates

func (JSONRenderer) Render

func (j JSONRenderer) Render(ctx context.Context, page string, data authboss.HTMLData) (output []byte, contentType string, err error)

Render the data

type LogMailer

type LogMailer struct {
	io.Writer
}

LogMailer logs e-mails instead of sending them.

func NewLogMailer

func NewLogMailer(writer io.Writer) *LogMailer

NewLogMailer creates a mailer that doesn't deliver e-mails but simply logs them.

func (LogMailer) Send

func (l LogMailer) Send(ctx context.Context, mail authboss.Email) error

Send an e-mail

type Logger

type Logger struct {
	Writer io.Writer
}

Logger writes exactly once for each log line to underlying io.Writer that's passed in and ends each message with a newline. It has RFC3339 as a date format, and emits a log level.

func NewLogger

func NewLogger(writer io.Writer) Logger

NewLogger creates a new logger from an io.Writer

func (Logger) Error

func (l Logger) Error(s string)

Error logs go here

func (Logger) Info

func (l Logger) Info(s string)

Info logs go here

type RecoverEndValues

type RecoverEndValues struct {
	HTTPFormValidator

	Token       string
	NewPassword string
}

RecoverEndValues for recover_end page

func (RecoverEndValues) GetPassword

func (r RecoverEndValues) GetPassword() string

GetPassword for recovery

func (RecoverEndValues) GetToken

func (r RecoverEndValues) GetToken() string

GetToken for recovery

type RecoverMiddleValues

type RecoverMiddleValues struct {
	HTTPFormValidator

	Token string
}

RecoverMiddleValues for recover_middle page

func (RecoverMiddleValues) GetToken

func (r RecoverMiddleValues) GetToken() string

GetToken for recovery

type RecoverStartValues

type RecoverStartValues struct {
	HTTPFormValidator

	PID string
}

RecoverStartValues for recover_start page

func (RecoverStartValues) GetPID

func (r RecoverStartValues) GetPID() string

GetPID for recovery

type Redirector

type Redirector struct {
	Renderer authboss.Renderer

	// FormValueName for the redirection
	FormValueName string

	// CoerceRedirectTo200 forces http.StatusTemporaryRedirect and
	// and http.StatusPermanentRedirect to http.StatusOK
	CorceRedirectTo200 bool
}

Redirector for http requests

func NewRedirector

func NewRedirector(renderer authboss.Renderer, formValueName string) *Redirector

NewRedirector constructor

func (*Redirector) Redirect

Redirect the client elsewhere. If it's an API request it will simply render a JSON response with information that should help a client to decide what to do.

type Responder

type Responder struct {
	Renderer authboss.Renderer
}

Responder helps respond to http requests

func NewResponder

func NewResponder(renderer authboss.Renderer) *Responder

NewResponder constructor

func (*Responder) Respond

func (r *Responder) Respond(w http.ResponseWriter, req *http.Request, code int, page string, data authboss.HTMLData) error

Respond to an HTTP request. It's main job is to merge data that comes in from various middlewares via the context with the data sent by the controller and render that.

type Router

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

Router implementation Does not use a dynamic map to hope to be slightly more performant

func NewRouter

func NewRouter() *Router

NewRouter creates a new router

func (*Router) Delete

func (r *Router) Delete(path string, handler http.Handler)

Delete method route

func (*Router) Get

func (r *Router) Get(path string, handler http.Handler)

Get method route

func (*Router) Post

func (r *Router) Post(path string, handler http.Handler)

Post method route

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP for http.Handler Only does get/posts, all other request types are a bad request

type Rules

type Rules struct {
	// FieldName is the name of the field this is intended to validate.
	FieldName string

	// MatchError describes the MustMatch regexp to a user.
	Required             bool
	MatchError           string
	MustMatch            *regexp.Regexp
	MinLength, MaxLength int
	MinLetters           int
	MinLower, MinUpper   int
	MinNumeric           int
	MinSymbols           int
	AllowWhitespace      bool
}

Rules defines a ruleset by which a string can be validated. The errors it produces are english only, with some basic pluralization.

func (Rules) Errors

func (r Rules) Errors(toValidate string) authboss.ErrorList

Errors returns an array of errors for each validation error that is present in the given string. Returns nil if there are no errors.

func (Rules) IsValid

func (r Rules) IsValid(toValidate string) bool

IsValid checks toValidate to make sure it's valid according to the rules.

func (Rules) Rules

func (r Rules) Rules() []string

Rules returns an array of strings describing the rules.

type SMSTwoFA

type SMSTwoFA struct {
	HTTPFormValidator

	Code         string
	RecoveryCode string
	PhoneNumber  string
}

SMSTwoFA for sms2fa_validate page

func (SMSTwoFA) GetCode

func (s SMSTwoFA) GetCode() string

GetCode from sms

func (SMSTwoFA) GetPhoneNumber

func (s SMSTwoFA) GetPhoneNumber() string

GetPhoneNumber from authenticator

func (SMSTwoFA) GetRecoveryCode

func (s SMSTwoFA) GetRecoveryCode() string

GetRecoveryCode from sms

type SMTPMailer

type SMTPMailer struct {
	Server string
	Auth   smtp.Auth
	// contains filtered or unexported fields
}

SMTPMailer uses smtp to actually send e-mails

func NewSMTPMailer

func NewSMTPMailer(server string, auth smtp.Auth) *SMTPMailer

NewSMTPMailer creates an SMTP Mailer to send emails with. An example usage might be something like:

NewSMTPMailer("smtp.gmail.com",
  smtp.PlainAuth("", "admin@yoursite.com", "password", "smtp.gmail.com"))

func (SMTPMailer) Send

func (s SMTPMailer) Send(ctx context.Context, mail authboss.Email) error

Send an e-mail

type TwoFA

type TwoFA struct {
	HTTPFormValidator

	Code         string
	RecoveryCode string
}

TwoFA for totp2fa_validate page

func (TwoFA) GetCode

func (t TwoFA) GetCode() string

GetCode from authenticator

func (TwoFA) GetRecoveryCode

func (t TwoFA) GetRecoveryCode() string

GetRecoveryCode for authenticator

type UserValues

type UserValues struct {
	HTTPFormValidator

	PID      string
	Password string

	Arbitrary map[string]string
}

UserValues from the login form

func (UserValues) GetPID

func (u UserValues) GetPID() string

GetPID from the values

func (UserValues) GetPassword

func (u UserValues) GetPassword() string

GetPassword from the values

func (UserValues) GetShouldRemember

func (u UserValues) GetShouldRemember() bool

GetShouldRemember checks the form values for

func (UserValues) GetValues

func (u UserValues) GetValues() map[string]string

GetValues from the form.

Jump to

Keyboard shortcuts

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