123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package dynamicproxy
- import (
- "errors"
- "net/http"
- "strings"
- "imuslab.com/zoraxy/mod/auth"
- )
- func handleAuthProviderRouting(sep *ProxyEndpoint, w http.ResponseWriter, r *http.Request, h *ProxyHandler) bool {
- if sep.AuthenticationProvider.AuthMethod == AuthMethodBasic {
- err := h.handleBasicAuthRouting(w, r, sep)
- if err != nil {
- h.Parent.Option.Logger.LogHTTPRequest(r, "host", 401)
- return true
- }
- } else if sep.AuthenticationProvider.AuthMethod == AuthMethodAuthelia {
- err := h.handleAutheliaAuth(w, r)
- if err != nil {
- h.Parent.Option.Logger.LogHTTPRequest(r, "host", 401)
- return true
- }
- }
-
- return false
- }
- func (h *ProxyHandler) handleBasicAuthRouting(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
- err := handleBasicAuth(w, r, pe)
- if err != nil {
- h.Parent.logRequest(r, false, 401, "host", r.URL.Hostname())
- }
- return err
- }
- func handleBasicAuth(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
- if len(pe.AuthenticationProvider.BasicAuthExceptionRules) > 0 {
-
- for _, exceptionRule := range pe.AuthenticationProvider.BasicAuthExceptionRules {
- if strings.HasPrefix(r.RequestURI, exceptionRule.PathPrefix) {
-
- return nil
- }
- }
- }
- u, p, ok := r.BasicAuth()
- if !ok {
- w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
- w.WriteHeader(401)
- return errors.New("unauthorized")
- }
-
- hashedPassword := auth.Hash(p)
- matchingFound := false
- for _, cred := range pe.AuthenticationProvider.BasicAuthCredentials {
- if u == cred.Username && hashedPassword == cred.PasswordHash {
- matchingFound = true
-
- r.Header.Set("X-Remote-User", u)
- break
- }
- }
- if !matchingFound {
- w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
- w.WriteHeader(401)
- return errors.New("unauthorized")
- }
- return nil
- }
- func (h *ProxyHandler) handleAutheliaAuth(w http.ResponseWriter, r *http.Request) error {
- return h.Parent.Option.AutheliaRouter.HandleAutheliaAuth(w, r)
- }
|