Merge pull request #5 from kesselborn/feature/basic-auth-support

Implement simple basic auth for the exporter
This commit is contained in:
Matt T. Proud 2013-01-09 03:51:09 -08:00
commit 872a959b31
1 changed files with 27 additions and 0 deletions

View File

@ -9,6 +9,7 @@ the LICENSE file.
package registry package registry
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"github.com/matttproud/golang_instrumentation/maths" "github.com/matttproud/golang_instrumentation/maths"
"github.com/matttproud/golang_instrumentation/metrics" "github.com/matttproud/golang_instrumentation/metrics"
@ -23,6 +24,7 @@ const (
jsonContentType = "application/json" jsonContentType = "application/json"
contentType = "Content-Type" contentType = "Content-Type"
jsonSuffix = ".json" jsonSuffix = ".json"
authorization = "Authorization"
) )
/* /*
@ -117,6 +119,31 @@ func (r *Registry) Register(name string, metric metrics.Metric) {
} }
} }
func (register *Registry) YieldBasicAuthExporter(username, password string) http.HandlerFunc {
exporter := register.YieldExporter()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authenticated := false
if auth := r.Header.Get(authorization); auth != "" {
base64Encoded := strings.SplitAfter(auth, " ")[1]
decoded, err := base64.URLEncoding.DecodeString(base64Encoded)
if err == nil {
usernamePassword := strings.Split(string(decoded), ":")
if usernamePassword[0] == username && usernamePassword[1] == password {
authenticated = true
}
}
}
if authenticated {
exporter.ServeHTTP(w, r)
} else {
http.Error(w, "access forbidden", 403)
}
})
}
/* /*
Create a http.HandlerFunc that is tied to r Registry such that requests Create a http.HandlerFunc that is tied to r Registry such that requests
against it generate a representation of the housed metrics. against it generate a representation of the housed metrics.