basic

package
v1.5.11 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2020 License: MIT Imports: 10 Imported by: 2

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

View Source
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.

View Source
const StrategyKey = auth.StrategyKey("Basic.Strategy")

StrategyKey export identifier for the basic strategy, commonly used when enable/add strategy to go-guardian authenticator.

Variables

View Source
var ErrInvalidCredentials = errors.New("basic: Invalid user credentials")

ErrInvalidCredentials is returned by Authenticate Strategy method, when user password is invalid.

View Source
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

New return new auth.Strategy. The returned strategy, caches the invocation result of authenticate function.

func NewWithOptions added in v1.3.1

func NewWithOptions(f AuthenticateFunc, cache store.Cache, opts ...auth.Option) auth.Strategy

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.

func SetHash added in v1.3.1

func SetHash(h crypto.Hash) auth.Option

SetHash set the hashing algorithm to hash the user password.

Example
opt := SetHash(crypto.SHA256) // import _ crypto/sha256
cache := store.New(2)
NewWithOptions(exampleAuthFunc, cache, opt)
Output:

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

func (auth AuthenticateFunc) Authenticate(ctx context.Context, r *http.Request) (auth.Info, error)

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

Jump to

Keyboard shortcuts

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