Add HTTP Basic authentication to the agent's API. Update OpenAPI specification to include security requirements and modify the server handler to enforce authentication. Introduce default admin credentials for access control.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"net/http"
|
||||
|
||||
"tea.chunkbyte.com/kato/go-worm/lib/config"
|
||||
)
|
||||
|
||||
func BasicAuth(next http.Handler) http.Handler {
|
||||
user := []byte(config.AuthUser)
|
||||
pass := []byte(config.AuthPass)
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/openapi", "/openapi.json":
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
u, p, ok := r.BasicAuth()
|
||||
if !ok ||
|
||||
subtle.ConstantTimeCompare([]byte(u), user) != 1 ||
|
||||
subtle.ConstantTimeCompare([]byte(p), pass) != 1 {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="win64_mp"`)
|
||||
WriteError(w, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user