1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-28 08:58:38 +02:00
authboss/values.go
Aaron L 982025bbc3 Finish implementing and testing confirm
- Rejig tests to remember to test the smtp mailer
2018-02-27 07:14:30 -08:00

78 lines
2.2 KiB
Go

package authboss
import (
"fmt"
"net/http"
)
// BodyReader reads data from the request
// and returns it in an abstract form.
// Typically used to decode JSON responses
// or Url Encoded request bodies.
//
// The first parameter is the page that this request
// was made on so we can tell what kind of JSON object
// or form was present as well as create the proper
// validation mechanisms.
//
// A typical example of this is taking the request
// and turning it into a JSON struct that knows how
// to validate itself and return certain fields.
type BodyReader interface {
Read(page string, r *http.Request) (Validator, error)
}
// UserValuer gets a string from a map-like data structure
// Typically a decoded JSON or form auth request
type UserValuer interface {
Validator
GetPID() string
GetPassword() string
}
// ArbitraryValuer provides the "rest" of the fields
// that aren't strictly needed for anything in particular,
// address, secondary e-mail, etc.
//
// There are two important notes about this interface:
//
// 1. That this is composed with Validator, as these fields
// should both be validated and culled of invalid pieces
// as they will be passed into ArbitraryUser.PutArbitrary()
//
// 2. These values will also be culled according to the RegisterPreserveFields
// whitelist and sent back in the data under the key DataPreserve.
type ArbitraryValuer interface {
Validator
GetValues() map[string]string
}
// ConfirmValuer allows us to pull out the token from the request
type ConfirmValuer interface {
Validator
GetToken() string
}
// MustHaveUserValues upgrades a validatable set of values
// to ones specific to an authenticating user.
func MustHaveUserValues(v Validator) UserValuer {
if u, ok := v.(UserValuer); ok {
return u
}
panic(fmt.Sprintf("bodyreader returned a type that could not be upgraded to UserValuer: %T", v))
}
// MustHaveConfirmValues upgrades a validatable set of values
// to ones specific to a user that needs to be confirmed.
func MustHaveConfirmValues(v Validator) ConfirmValuer {
if u, ok := v.(ConfirmValuer); ok {
return u
}
panic(fmt.Sprintf("bodyreader returned a type that could not be upgraded to ConfirmValuer: %T", v))
}