https://github.com/mpl/basicauth
HTTP basic access authentication
https://github.com/mpl/basicauth
Last synced: about 1 year ago
JSON representation
HTTP basic access authentication
- Host: GitHub
- URL: https://github.com/mpl/basicauth
- Owner: mpl
- License: apache-2.0
- Created: 2015-03-01T16:28:10.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-03-21T11:19:25.000Z (over 3 years ago)
- Last Synced: 2025-03-18T03:21:08.283Z (over 1 year ago)
- Language: Go
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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)
}