basicAuth.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 (h *ProxyHandler) handleBasicAuthRouting(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
  13. proxyType := "vdir-auth"
  14. if pe.ProxyType == ProxyType_Subdomain {
  15. proxyType = "subd-auth"
  16. }
  17. u, p, ok := r.BasicAuth()
  18. if !ok {
  19. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  20. w.WriteHeader(401)
  21. return errors.New("unauthorized")
  22. }
  23. //Check for the credentials to see if there is one matching
  24. hashedPassword := auth.Hash(p)
  25. matchingFound := false
  26. for _, cred := range pe.BasicAuthCredentials {
  27. if u == cred.Username && hashedPassword == cred.PasswordHash {
  28. matchingFound = true
  29. break
  30. }
  31. }
  32. if !matchingFound {
  33. h.logRequest(r, false, 401, proxyType, pe.Domain)
  34. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  35. w.WriteHeader(401)
  36. return errors.New("unauthorized")
  37. }
  38. return nil
  39. }