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-hash"
ExtensionKey represents a key for the hashed 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 ¶
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.