Skip to content

Latest commit

 

History

History
35 lines (24 loc) · 770 Bytes

File metadata and controls

35 lines (24 loc) · 770 Bytes

Simple HTTP Basic Authentication in Go

Travis

This is an implementation of HTTP Basic authentication in Go.

Usage

package main

import (
	"log"
	"net/http"

	auth "github.com/bywan/go-http-basic-auth"
)

func myValidator(username, password string) bool {
	// Here is your validator logic
	return username == "foo" && password == "bar"
}

func main() {
	authenticator := auth.NewAuthenticator(myValidator, "myRealm")
	appHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(204)
	})

	http.Handle("/", authenticator.Wrap(appHandler))
	log.Fatal(http.ListenAndServe(":3000", nil))
}