Refactor organization of scope aware request utils

Reorganized the structure of the Request Utils due to their widespread use
resulting in circular imports issues (mostly because of middleware & logger).
This commit is contained in:
Nick Meves
2021-01-16 13:55:48 -08:00
parent b625de9490
commit 6fb3274ca3
20 changed files with 357 additions and 185 deletions
-37
View File
@@ -4,9 +4,6 @@ import (
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/middleware"
)
func GetCertPool(paths []string) (*x509.CertPool, error) {
@@ -26,37 +23,3 @@ func GetCertPool(paths []string) (*x509.CertPool, error) {
}
return pool, nil
}
// GetRequestProto return the request host header or X-Forwarded-Proto if present
func GetRequestProto(req *http.Request) string {
proto := req.Header.Get("X-Forwarded-Proto")
if !isProxied(req) || proto == "" {
proto = req.URL.Scheme
}
return proto
}
// GetRequestHost return the request host header or X-Forwarded-Host if present
// and reverse proxy mode is enabled.
func GetRequestHost(req *http.Request) string {
host := req.Header.Get("X-Forwarded-Host")
if !isProxied(req) || host == "" {
host = req.Host
}
return host
}
// GetRequestURI return the request host header or X-Forwarded-Uri if present
func GetRequestURI(req *http.Request) string {
uri := req.Header.Get("X-Forwarded-Uri")
if !isProxied(req) || uri == "" {
// Use RequestURI to preserve ?query
uri = req.URL.RequestURI()
}
return uri
}
func isProxied(req *http.Request) bool {
scope := middleware.GetRequestScope(req)
return scope.ReverseProxy
}