basicAuth.go 929 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package dynamicproxy
  2. import (
  3. "errors"
  4. "net/http"
  5. "imuslab.com/zoraxy/mod/auth"
  6. )
  7. /*
  8. BasicAuth.go
  9. This file handles the basic auth on proxy endpoints
  10. if RequireBasicAuth is set to true
  11. */
  12. func handleBasicAuthRouting(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
  13. u, p, ok := r.BasicAuth()
  14. if !ok {
  15. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  16. w.WriteHeader(401)
  17. return errors.New("unauthorized")
  18. }
  19. //Check for the credentials to see if there is one matching
  20. hashedPassword := auth.Hash(p)
  21. matchingFound := false
  22. for _, cred := range pe.BasicAuthCredentials {
  23. if u == cred.Username && hashedPassword == cred.PasswordHash {
  24. matchingFound = true
  25. break
  26. }
  27. }
  28. if !matchingFound {
  29. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  30. w.WriteHeader(401)
  31. return errors.New("unauthorized")
  32. }
  33. return nil
  34. }