basicauth

package
v0.0.0-...-0253919 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: AGPL-3.0 Imports: 15 Imported by: 0

README

basicauth

import go.pitz.tech/lib/auth/basic

Usage

var ErrBadRequest = errors.New("bad lookup request")

ErrBadRequest is returned when a lookup request does not contain a required field.

var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a credential is not found.

func Basic
func Basic(store Store) auth.HandlerFunc

Basic implements a basic access authentication handler function.

func Bearer
func Bearer(store Store) auth.HandlerFunc

Bearer returns a handler func that translates bearer tokens into user information.

func Handler
func Handler(ctx context.Context, cfg Config) (auth.HandlerFunc, error)

Handler returns the appropriate handler based on the provided configuration.

func Static
func Static(username, password string, groups ...string) auth.HandlerFunc

Static returns an auth.HandlerFunc that uses a static username/password for the system.

type AccessToken
type AccessToken struct {
	Token string `json:"token" usage:"the access token used to authenticate requests"`
}

AccessToken is used to authenticate a user using a bearer token.

type ClientConfig
type ClientConfig struct {
	UsernamePassword
	AccessToken
}

ClientConfig defines the options available to a client.

func (ClientConfig) Token
func (c ClientConfig) Token() (*oauth2.Token, error)
type Config
type Config struct {
	PasswordFile   string           `json:"password_file" usage:"path to the csv file containing usernames and passwords"`
	TokenFile      string           `json:"token_file" usage:"path to the csv file containing tokens"`
	StaticUsername string           `json:"static_username" usage:"provide a static username to authenticate the user" hidden:"true"`
	StaticPassword string           `json:"static_password" usage:"provide a static password to authenticate the user" hidden:"true"`
	StaticGroups   *cli.StringSlice `json:"static_groups" usage:"provide a static set of groups to assign to the user" hidden:"true"`
}

Config defines the options available to a server.

type LazyStore
type LazyStore struct {
	Provider func() (Store, error)
}

LazyStore provides a convenient way to lazily load an underlying store.

func (*LazyStore) Lookup
func (c *LazyStore) Lookup(req LookupRequest) (resp LookupResponse, err error)
type LookupRequest
type LookupRequest struct {
	User  string
	Token string
}
type LookupResponse
type LookupResponse struct {
	UserID string
	User   string
	Groups []string

	Email         string
	EmailVerified bool

	// one of these will be set based on the LookupRequest
	Password string
	Token    string
}
type Store
type Store interface {
	// Lookup retrieves the provided user's password and groups.
	Lookup(req LookupRequest) (resp LookupResponse, err error)
}

Store defines an abstraction for loading user credentials.

func OpenCSV
func OpenCSV(ctx context.Context, fileName string) (Store, error)

OpenCSV attempts to open the provided csv file and return a parsed index based on the contents.

type UsernamePassword
type UsernamePassword struct {
	Username string `json:"username" usage:"the username to login with"`
	Password string `json:"password" usage:"the password associated with the username"`
}

UsernamePassword is used to authenticate a user using a username and password.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBadRequest = errors.New("bad lookup request")

ErrBadRequest is returned when a lookup request does not contain a required field.

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when a credential is not found.

Functions

func Basic

func Basic(store Store) auth.HandlerFunc

Basic implements a basic access authentication handler function.

func Bearer

func Bearer(store Store) auth.HandlerFunc

Bearer returns a handler func that translates bearer tokens into user information.

func Handler

func Handler(ctx context.Context, cfg Config) (auth.HandlerFunc, error)

Handler returns the appropriate handler based on the provided configuration.

func Static

func Static(username, password string, groups ...string) auth.HandlerFunc

Static returns an auth.HandlerFunc that uses a static username/password for the system.

Types

type AccessToken

type AccessToken struct {
	Token string `json:"token" usage:"the access token used to authenticate requests"`
}

AccessToken is used to authenticate a user using a bearer token.

type ClientConfig

type ClientConfig struct {
	UsernamePassword
	AccessToken
}

ClientConfig defines the options available to a client.

func (ClientConfig) Token

func (c ClientConfig) Token() (*oauth2.Token, error)

type Config

type Config struct {
	PasswordFile   string           `json:"password_file" usage:"path to the csv file containing usernames and passwords"`
	TokenFile      string           `json:"token_file" usage:"path to the csv file containing tokens"`
	StaticUsername string           `json:"static_username" usage:"provide a static username to authenticate the user" hidden:"true"`
	StaticPassword string           `json:"static_password" usage:"provide a static password to authenticate the user" hidden:"true"`
	StaticGroups   *cli.StringSlice `json:"static_groups" usage:"provide a static set of groups to assign to the user" hidden:"true"`
}

Config defines the options available to a server.

type LazyStore

type LazyStore struct {
	Provider func() (Store, error)
	// contains filtered or unexported fields
}

LazyStore provides a convenient way to lazily load an underlying store.

func (*LazyStore) Lookup

func (c *LazyStore) Lookup(req LookupRequest) (resp LookupResponse, err error)

type LookupRequest

type LookupRequest struct {
	User  string
	Token string
}

type LookupResponse

type LookupResponse struct {
	UserID string
	User   string
	Groups []string

	Email         string
	EmailVerified bool

	// one of these will be set based on the LookupRequest
	Password string
	Token    string
}

type Store

type Store interface {
	// Lookup retrieves the provided user's password and groups.
	Lookup(req LookupRequest) (resp LookupResponse, err error)
}

Store defines an abstraction for loading user credentials.

func OpenCSV

func OpenCSV(ctx context.Context, fileName string) (Store, error)

OpenCSV attempts to open the provided csv file and return a parsed index based on the contents.

type UsernamePassword

type UsernamePassword struct {
	Username string `json:"username" usage:"the username to login with"`
	Password string `json:"password" usage:"the password associated with the username"`
}

UsernamePassword is used to authenticate a user using a username and password.

Jump to

Keyboard shortcuts

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