This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
forked from dustin-decker/saml-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseproxyhost.go
102 lines (88 loc) · 2.64 KB
/
reverseproxyhost.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
log "github.com/sirupsen/logrus"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type (
ReverseProxyHost struct {
ReverseProxy *echo.Echo
}
)
type HostConfig struct {
ServiceRootURL string `yaml:"service_root_url"`
IdpMetadataURL string `yaml:"idp_metadata_url"`
Targets []string `yaml:"targets"`
NoCache bool `yaml:"no_cache"`
AllowIDPInitiated bool `yaml:"allow_idp_initiated"`
AddAttributesAsHeaders []string `yaml:"add_attributes_as_headers"`
}
func NewReverseProxyDirector(balancer middleware.ProxyBalancer) func(req *http.Request) {
director := func(req *http.Request) {
target := balancer.Next().URL
targetQuery := target.RawQuery
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
req.Host = target.Host
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
return director
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
func (s *Server) NewReverseProxyHost(config HostConfig) *ReverseProxyHost {
var targets []*middleware.ProxyTarget
// Parse target URLs
for _, target := range config.Targets {
url, err := url.Parse(target)
if err != nil {
log.WithFields(log.Fields{
"target": target,
"error": err.Error()}).Fatal("could not parse host target URL")
}
proxyTarget := middleware.ProxyTarget{URL: url}
targets = append(targets, &proxyTarget)
}
samlSP := s.NewSamlSP(config)
proxyHost := echo.New()
proxyHost.Use(middleware.Logger())
proxyHost.Use(middleware.Recover())
if config.NoCache {
// Remove auth'd upstream hosts' client caching headers (session expiration)
proxyHost.Use(NoCache())
}
director := NewReverseProxyDirector(middleware.NewRoundRobinBalancer(targets))
reverseProxy := &httputil.ReverseProxy{Director: director}
// This endpoint handles SAML auth flow
proxyHost.Any("/saml/*", echo.WrapHandler(samlSP))
proxyHost.Any("/*",
echo.WrapHandler(reverseProxy),
echo.WrapMiddleware(samlSP.RequireAccount),
samlHeaders(config),
)
return &ReverseProxyHost{
ReverseProxy: proxyHost,
}
}