questionable services

Technical writings about computing infrastructure, HTTP & security.

(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

import(
    "net/http"
    "github.com/goji/httpauth"
    "github.com/zenazn/goji"
)

func main() {

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

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.


© 2023 Matt Silverlock | Mastodon | Code snippets are MIT licensed | Built with Jekyll