Authentication

Rafał Lorenz edited this page Feb 23, 2018 · 4 revisions
  1. Basic Authentication

Basic Authentication

package main

import (
	"fmt"
	"log"
	"net/http"

        "github.com/vardius/gorouter"
)

func BasicAuth(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
        requiredUser := "gordon"
        requiredPassword := "secret!"
        
        // Get the Basic Authentication credentials
        user, password, hasAuth := r.BasicAuth()
        
        if hasAuth && user == requiredUser && password == requiredPassword {
            return nil;
        } else {
            w.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
            http.Error(w,
                http.StatusText(http.StatusUnauthorized),
                http.StatusUnauthorized,
            )
        }
		next.ServeHTTP(w, r)
	}

	return http.HandlerFunc(fn)
}

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Not protected!\n")
}

func Protected(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Protected!\n")
}

func main() {
	router := gorouter.New()
	router.GET("/", http.HandlerFunc(Index))	
	router.GET("/protected", http.HandlerFunc(Protected))

	router.USE("GET", "/protected", BasicAuth)

	log.Fatal(http.ListenAndServe(":8080", router))
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.