basicauth

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2020 License: MIT Imports: 4 Imported by: 0

README

basicauth

A Go "net/http"-compatible Basic Authentication middleware

This is an opinionated library that assumes you have stored bcrypt-hashed passwords for your users. In addition, to keep this library performant, it is currently configured for passwords encrypted using bcrypt.MinCost. I'll make this configurable at some point, but increasing from bcrypt.MinCost (4) to 8 increases the execution time of the handler from 1ms to 25ms according to the benchmark.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(source AuthSource, realm string, cost int) func(http.Handler) http.Handler

New returns a middleware that adds HTTP basic authentication using the provided AuthSource.

Types

type AuthSource

type AuthSource interface {
	LookupHash(context.Context, string) []byte
}

AuthSource is the interface the middleware uses to retrieve password hashes. The provided SimpleSource takes a single username/password combination and returns the password's hash when the username is provided. A more complex implementation could use a database or external service to provide password hashes for usernames. A nil result indicates that the user with the given name was not found.

type SimpleSource

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

SimpleSource takes a single username/password combination and provides an AuthSource that returns the password's hash when the username is provided. All other usernames will return a non-nil error.

func NewSimpleSource

func NewSimpleSource(username, password string, cost int) (*SimpleSource, error)

NewSimpleSource constructs a SimpleSource from the given username and password.

func (*SimpleSource) LookupHash

func (s *SimpleSource) LookupHash(_ context.Context, username string) []byte

LookupHash implements the AuthSource interface

Jump to

Keyboard shortcuts

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