basic

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: Apache-2.0 Imports: 17 Imported by: 2

Documentation

Overview

Package basic provides HTTP Basic authentication middleware and utilities.

Copyright 2019 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Imported from https://golang.org/src/cmd/go/internal/auth/netrc.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCredentials

func AddCredentials(req *http.Request) (added bool)

AddCredentials fills in the user's credentials for req, if any. The return value reports whether any matching credentials were found. This function uses the default package-level NetrcProvider for backward compatibility.

func AddCredentialsWithProvider

func AddCredentialsWithProvider(req *http.Request, provider *NetrcProvider) (added bool)

AddCredentialsWithProvider fills in the user's credentials for req using the specified provider. The return value reports whether any matching credentials were found.

Types

type AuthMap

type AuthMap map[string]BasicAuthPair

AuthMap is a user and password pair

func LoadBasicAuthFromFile

func LoadBasicAuthFromFile(filePath string) (AuthMap, error)

func LoadBasicAuthFromFileOrEmpty added in v0.9.0

func LoadBasicAuthFromFileOrEmpty(filePath string) (AuthMap, error)

LoadBasicAuthFromFileOrEmpty behaves like LoadBasicAuthFromFile, except a missing file returns an empty AuthMap and a nil error instead of the underlying os.Open error. Other errors (bad permissions, unreadable file) are returned unchanged. Useful for callers that treat "no credentials file yet" as a valid, empty starting state rather than a failure.

func LoadBasicAuthFromReader

func LoadBasicAuthFromReader(reader io.Reader) AuthMap

func LoadBasicAuthFromScanner

func LoadBasicAuthFromScanner(scanner *bufio.Scanner) AuthMap

func (AuthMap) AddUserWithHashedPassword

func (m AuthMap) AddUserWithHashedPassword(user, hashedPassword string)

AddUserWithHashedPassword if user already exists it will over ride it

func (AuthMap) AddUserWithPlainPassword

func (m AuthMap) AddUserWithPlainPassword(user, password string)

AddUserWithPlainPassword if user already exists it will over ride it

func (AuthMap) Authenticate

func (m AuthMap) Authenticate(user, password string) (bool, error)

Authenticate returns true if the user exists and the password is correct

func (AuthMap) UserExists

func (m AuthMap) UserExists(user string) bool

UserExists returns true if the user exists

type BasicAuthPair

type BasicAuthPair struct {
	User           string
	HashedPassword string
}

func NewBasicAuthPairWithPlainPassword

func NewBasicAuthPairWithPlainPassword(user, password string) (BasicAuthPair, error)

func (BasicAuthPair) VerifyPassword

func (p BasicAuthPair) VerifyPassword(password string) (bool, error)

type ClientAuth

type ClientAuth struct {
	Config ClientConfig
	// contains filtered or unexported fields
}

func (*ClientAuth) AddAuth

func (a *ClientAuth) AddAuth(req *http.Request) error

func (*ClientAuth) HTTPClient

func (a *ClientAuth) HTTPClient() *http.Client

type ClientConfig

type ClientConfig struct {
	// https://everything.curl.dev/usingcurl/netrc
	//
	// machine connect.lab.dioad.net
	// login blah
	// password blah
	NetRCFile string `mapstructure:"netrc-file"`
	User      string `mapstructure:"user"`
	Password  string `mapstructure:"password"`
}

type Handler

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

Handler implements basic authentication for HTTP servers. The credential map can be replaced at any time via SetAuthMap, safely concurrent with in-flight AuthRequest calls - Handler does not need to be rebuilt to pick up new or changed credentials.

func NewHandler

func NewHandler(cfg ServerConfig) (*Handler, error)

NewHandler creates a new Basic authentication handler from the provided configuration.

func NewHandlerWithMap

func NewHandlerWithMap(cfg ServerConfig, authMap AuthMap) (*Handler, error)

NewHandlerWithMap creates a new Basic authentication handler using the provided AuthMap and configuration (for Realm, used in the WWW-Authenticate challenge header). Call h.SetAuthMap later to replace the credentials the returned Handler authenticates against, e.g. after a credentials file changes on disk.

func (*Handler) AuthMap added in v0.9.0

func (h *Handler) AuthMap() AuthMap

AuthMap returns the credentials Handler currently authenticates against. Safe to call concurrently with SetAuthMap and AuthRequest.

func (*Handler) AuthRequest

func (h *Handler) AuthRequest(r *http.Request) (stdctx.Context, error)

AuthRequest authenticates an HTTP request using Basic authentication.

func (*Handler) SetAuthMap added in v0.9.0

func (h *Handler) SetAuthMap(m AuthMap)

SetAuthMap atomically replaces the credentials Handler authenticates against. Safe to call concurrently with in-flight AuthRequest calls.

func (*Handler) Wrap

func (h *Handler) Wrap(handler http.Handler) http.Handler

Wrap wraps an HTTP handler with Basic authentication middleware.

type NetrcProvider

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

NetrcProvider manages netrc credentials and their loading. It encapsulates the state for loading and parsing netrc files, allowing for multiple independent instances with different configurations. This resolves the global state issue that made testing difficult.

func NewNetrcProviderFromContent

func NewNetrcProviderFromContent(content string) *NetrcProvider

NewNetrcProviderFromContent creates a NetrcProvider initialized with the given netrc content. This is useful for testing or when netrc data comes from a non-standard source.

type RoundTripper

type RoundTripper struct {
	Username string
	Password string
	Base     http.RoundTripper
}

func (*RoundTripper) RoundTrip

func (t *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

type ServerConfig

type ServerConfig struct {
	AllowInsecureHTTP bool     `mapstructure:"allow-insecure-http"`
	HTPasswdFile      string   `mapstructure:"htpasswd-file"`
	Users             []string `mapstructure:"users"`
	Realm             string   `mapstructure:"realm"`
}

Jump to

Keyboard shortcuts

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