38 lines
848 B
Go
38 lines
848 B
Go
|
//go:build nullauth
|
||
|
|
||
|
package auth
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type nullProvider struct{}
|
||
|
|
||
|
func NewNull() (AuthProvider, error) {
|
||
|
return nullProvider{}, nil
|
||
|
}
|
||
|
|
||
|
func (nullProvider) Middleware() func(http.Handler) http.Handler {
|
||
|
return func(next http.Handler) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
nullAuth(next, w, r)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func nullAuth(next http.Handler, w http.ResponseWriter, r *http.Request) {
|
||
|
user, _, ok := r.BasicAuth()
|
||
|
if !ok {
|
||
|
w.Header().Add("WWW-Authenticate", `Basic realm="Please provide your system credentials", charset="UTF-8"`)
|
||
|
http.Error(w, "HTTP Basic auth is required", http.StatusUnauthorized)
|
||
|
return
|
||
|
}
|
||
|
authCtx := AuthContext{
|
||
|
AuthMethod: "null",
|
||
|
UserName: user,
|
||
|
}
|
||
|
ctx := NewContext(r.Context(), &authCtx)
|
||
|
r = r.WithContext(ctx)
|
||
|
next.ServeHTTP(w, r)
|
||
|
}
|