Documentation ¶
Overview ¶
Package basic provides authentication strategy, to authenticate HTTP requests using the standard basic scheme.
Example ¶
strategy := AuthenticateFunc(exampleAuthFunc) authenticator := auth.New() authenticator.EnableStrategy(StrategyKey, strategy) // user request req, _ := http.NewRequest("GET", "/", nil) req.SetBasicAuth("test", "test") user, err := authenticator.Authenticate(req) fmt.Println(user.ID(), err) req.SetBasicAuth("test", "1234") _, err = authenticator.Authenticate(req) fmt.Println(err.(errors.MultiError)[1])
Output: 10 <nil> Invalid credentials
Example (Second) ¶
// This example show how to caches the result of basic auth. // With LRU cache cache := store.New(2) strategy := New(exampleAuthFunc, cache) authenticator := auth.New() authenticator.EnableStrategy(StrategyKey, strategy) // user request req, _ := http.NewRequest("GET", "/", nil) req.SetBasicAuth("test", "test") user, err := authenticator.Authenticate(req) fmt.Println(user.ID(), err) req.SetBasicAuth("test", "1234") _, err = authenticator.Authenticate(req) fmt.Println(err.(errors.MultiError)[1])
Output: 10 <nil> basic: Invalid user credentials
Index ¶
Examples ¶
Constants ¶
const ExtensionKey = "x-go-guardian-basic-password"
ExtensionKey represents a key for the password in info extensions. Typically used when basic strategy cache the authentication decisions.
const StrategyKey = auth.StrategyKey("Basic.Strategy")
StrategyKey export identifier for the basic strategy, commonly used when enable/add strategy to go-guardian authenticator.
Variables ¶
var ErrInvalidCredentials = errors.New("basic: Invalid user credentials")
ErrInvalidCredentials is returned by Authenticate Strategy method, when user password is invalid.
var ErrMissingPrams = errors.New("basic: Request missing BasicAuth")
ErrMissingPrams is returned by Authenticate Strategy method, when failed to retrieve user credentials from request.
Functions ¶
func New ¶ added in v1.0.0
func New(f AuthenticateFunc, cache store.Cache) auth.Strategy
New return new auth.Strategy. The returned strategy, caches the invocation result of authenticate function.
func NewWithOptions ¶ added in v1.3.1
NewWithOptions return new auth.Strategy. The returned strategy, caches the invocation result of authenticate function.
func SetComparator ¶ added in v1.5.3
func SetComparator(c Comparator) auth.Option
SetComparator set password comparator.
Types ¶
type AuthenticateFunc ¶ added in v1.0.0
type AuthenticateFunc func(ctx context.Context, r *http.Request, userName, password string) (auth.Info, error)
AuthenticateFunc declare custom function to authenticate request using user credentials. the authenticate function invoked by Authenticate Strategy method after extracting user credentials to compare against DB or other service, if extracting user credentials from request failed a nil info with ErrMissingPrams returned, Otherwise, return Authenticate invocation result.
func (AuthenticateFunc) Authenticate ¶ added in v1.0.0
Authenticate implement Authenticate Strategy method, and return user info or an appropriate error.
func (AuthenticateFunc) Challenge ¶ added in v1.2.2
func (auth AuthenticateFunc) Challenge(realm string) string
Challenge returns string indicates the authentication scheme. Typically used to adds a HTTP WWW-Authenticate header.
type Comparator ¶ added in v1.5.3
type Comparator interface { Hash(password string) (string, error) Verify(hashedPassword, password string) error }
Comparator is the interface implemented by types, that can generate password hash and compares the hashed password with its possible plaintext equivalent