zenazn/goji — pull request #24 by elithrar (2014-05-20 12:27:03+00:00)
Title: HTTP Basic Authentication middleware (inc. simple tests!)

Hi,

On my to implementing some CSRF middleware I figured I'd push out some HTTP Basic Authentication middleware (plus I wanted to validate the approach we discussed in https://github.com/zenazn/goji/issues/23).

A quick example:

```go
// Establish our authentication settings
authOpts := &middleware.AuthOptions{
Realm: "Restricted",
User: "matt",
Password: "if-its-http-its-plaintext",
}

// Add the middleware to the stack.
goji.Use(middleware.BasicAuth(authOpts))

// Your router is now 'protected' with HTTP Basic Auth.
goji.Get("/", myHandler)
```

There's some minimal tests included, to which I'll add a few more cases if you're happy with the overall approach. `AuthOptions` also allows a package user to pass in a custom `UnauthorizedHandler` if they wish to serve their own custom 401 page.