basicauth

package module
v0.0.0-...-3b2305e Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2025 License: Apache-2.0 Imports: 4 Imported by: 3

README

basicauth

HTTP basic access authentication

Example:

var up *basicauth.UserPass

func makeHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if e, ok := recover().(error); ok {
				http.Error(w, e.Error(), http.StatusInternalServerError)
				return
			}
		}()
		if up.IsAllowed(r) {
			fn(w, r)
		} else {
			basicauth.SendUnauthorized(w, r, "montorrent")
		}
	}
}

func main() {
	var err error
	up, err = basicauth.New("foo:bar")
	if err != nil {
		log.Fatal(err)
	}

	http.Handle("/", makeHandler(someHandler))
	http.ListenAndServe(*host, nil)
}

Documentation

Overview

Package basicauth provides HTTP basic access authentication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UserPass

type UserPass struct {
	U string // username
	P string // password
}

func NewUserPass

func NewUserPass(userpass string) (*UserPass, error)

NewUserPass takes a username:password string and returns a *UserPass.

type UserPassHandler

type UserPassHandler struct {
	UP *UserPass
	H  http.Handler
	HF func(http.ResponseWriter, *http.Request)
}

func (*UserPassHandler) ServeHTTP

func (uph *UserPassHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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