Skip to content

Instantly share code, notes, and snippets.

@codegangsta
Created November 13, 2013 01:33
Show Gist options
  • Save codegangsta/7442073 to your computer and use it in GitHub Desktop.
Save codegangsta/7442073 to your computer and use it in GitHub Desktop.
package auth
import (
"encoding/base64"
"net/http"
)
func Basic(username string, password string) http.HandlerFunc {
var siteAuth = base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
return func(res http.ResponseWriter, req *http.Request) {
auth := req.Header.Get("Authorization")
if auth != "Basic "+siteAuth {
res.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
http.Error(res, "Not Authorized", http.StatusUnauthorized)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment