github.com/weaviate/weaviate@v1.24.6/adapters/handlers/rest/clusterapi/auth.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package clusterapi 13 14 import ( 15 "net/http" 16 17 "github.com/weaviate/weaviate/usecases/cluster" 18 ) 19 20 type auth interface { 21 handleFunc(handler http.HandlerFunc) http.HandlerFunc 22 } 23 24 type basicAuthHandler struct { 25 basicAuth cluster.BasicAuth 26 } 27 28 func NewBasicAuthHandler(authConfig cluster.AuthConfig) auth { 29 return &basicAuthHandler{authConfig.BasicAuth} 30 } 31 32 func (h *basicAuthHandler) handleFunc(handler http.HandlerFunc) http.HandlerFunc { 33 if !h.basicAuth.Enabled() { 34 return handler 35 } 36 return func(w http.ResponseWriter, r *http.Request) { 37 u, p, ok := r.BasicAuth() 38 if ok && u == h.basicAuth.Username && p == h.basicAuth.Password { 39 handler(w, r) 40 return 41 } 42 // unauthorized request, send 401 43 w.WriteHeader(401) 44 } 45 } 46 47 type noopAuthHandler struct{} 48 49 func NewNoopAuthHandler() auth { 50 return &noopAuthHandler{} 51 } 52 53 func (h *noopAuthHandler) handleFunc(handler http.HandlerFunc) http.HandlerFunc { 54 return handler 55 }