request / response

a blog about the web, Go, and building things

(written by Matt Silverlock)

httpauth - Basic Auth Middleware For Go


httpauth is a HTTP Basic Authentication middleware for Go.

I originally designed it for the Goji micro-framework, but it’s compatible with vanilla net/http. We can thank Go’s http.Handler interface for that, but I’d recommend Alice to minimise the function wrapping if you’re particularly framework adverse.

package main


func main() {

    goji.Use(httpauth.SimpleBasicAuth("dave", "password"))
    // myHandler requires HTTP Basic Auth to access
    goji.Get("/thing", myHandler)

As always, note that HTTP Basic Auth credentials are sent over the wire in plain-text, so serve your application over HTTPS (TLS) using Go’s built-in ListenAndServeTLS or nginx up front.

Full examples are in the README, and I’m open to any pull requests.

© 2017 Matt Silverlock | His other site | Code snippets are MIT licensed | Built with Jekyll