MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / BasicAuth

Function BasicAuth

pkg/webmiddleware/basic_auth.go:57–84  ·  view source on GitHub ↗

BasicAuth returns a middleware that authenticates users with Basic Auth.

(realm string, authenticator Authenticator)

Source from the content-addressed store, hash-verified

55
56// BasicAuth returns a middleware that authenticates users with Basic Auth.
57func BasicAuth(realm string, authenticator Authenticator) MiddlewareFunc {
58 if authenticator == nil {
59 authenticator = noopAuthenticator
60 }
61 writeUnauthorized := func(w http.ResponseWriter) {
62 w.Header().Add("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
63 w.WriteHeader(http.StatusUnauthorized)
64 }
65 return func(next http.Handler) http.Handler {
66 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67 username, password, ok := r.BasicAuth()
68 if !ok {
69 writeUnauthorized(w)
70 return
71 }
72 authenticated, err := authenticator.Authenticate(username, password)
73 if err != nil {
74 webhandlers.Error(w, r, err)
75 return
76 }
77 if !authenticated {
78 writeUnauthorized(w)
79 return
80 }
81 next.ServeHTTP(w, r)
82 })
83 }
84}

Callers 2

initWebMethod · 0.92
TestBasicAuthFunction · 0.85

Calls 5

ErrorFunction · 0.92
HeaderMethod · 0.80
AddMethod · 0.65
AuthenticateMethod · 0.65
ServeHTTPMethod · 0.45

Tested by 1

TestBasicAuthFunction · 0.68