10 "github.com/mbland/hmacauth"
11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
12 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
16 // SignatureHeader is the name of the request header containing the GAP Signature
18 SignatureHeader = "GAP-Signature"
24// SignatureHeaders contains the headers to be signed by the hmac algorithm
26var SignatureHeaders = []string{
34 "X-Forwarded-Preferred-User",
35 "X-Forwarded-Access-Token",
40// newHTTPUpstreamProxy creates a new httpUpstreamProxy that can serve requests
41// to a single upstream host.
42func newHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *options.SignatureData, errorHandler ProxyErrorHandler) http.Handler {
43 // Set path to empty so that request paths start at the server root
46 // Create a ReverseProxy
47 proxy := newReverseProxy(u, upstream, errorHandler)
49 // Set up a WebSocket proxy if required
50 var wsProxy http.Handler
51 if upstream.ProxyWebSockets == nil || *upstream.ProxyWebSockets {
52 wsProxy = newWebSocketReverseProxy(u, upstream.InsecureSkipTLSVerify)
55 var auth hmacauth.HmacAuth
57 auth = hmacauth.NewHmacAuth(sigData.Hash, []byte(sigData.Key), SignatureHeader, SignatureHeaders)
60 return &httpUpstreamProxy{
61 upstream: upstream.ID,
68// httpUpstreamProxy represents a single HTTP(S) upstream proxy
69type httpUpstreamProxy struct {
72 wsHandler http.Handler
73 auth hmacauth.HmacAuth
76// ServeHTTP proxies requests to the upstream provider while signing the
78func (h *httpUpstreamProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
79 scope := middleware.GetRequestScope(req)
80 // If scope is nil, this will panic.
81 // A scope should always be injected before this handler is called.
82 scope.Upstream = h.upstream
84 // TODO (@NickMeves) - Deprecate GAP-Signature & remove GAP-Auth
86 req.Header.Set("GAP-Auth", rw.Header().Get("GAP-Auth"))
87 h.auth.SignRequest(req)
89 if h.wsHandler != nil && strings.EqualFold(req.Header.Get("Connection"), "upgrade") && req.Header.Get("Upgrade") == "websocket" {
90 h.wsHandler.ServeHTTP(rw, req)
92 h.handler.ServeHTTP(rw, req)
96// newReverseProxy creates a new reverse proxy for proxying requests to upstream
97// servers based on the upstream configuration provided.
98// The proxy should render an error page if there are failures connecting to the
100func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler ProxyErrorHandler) http.Handler {
101 proxy := httputil.NewSingleHostReverseProxy(target)
103 // Configure options on the SingleHostReverseProxy
104 if upstream.FlushInterval != nil {
105 proxy.FlushInterval = upstream.FlushInterval.Duration()
107 proxy.FlushInterval = options.DefaultUpstreamFlushInterval
110 // InsecureSkipVerify is a configurable option we allow
112 if upstream.InsecureSkipTLSVerify {
113 proxy.Transport = &http.Transport{
114 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
118 // Ensure we always pass the original request path
119 setProxyDirector(proxy)
121 if upstream.PassHostHeader != nil && !*upstream.PassHostHeader {
122 setProxyUpstreamHostHeader(proxy, target)
125 // Set the error handler so that upstream connection failures render the
126 // error page instead of sending a empty response
127 if errorHandler != nil {
128 proxy.ErrorHandler = errorHandler
133// setProxyUpstreamHostHeader sets the proxy.Director so that upstream requests
134// receive a host header matching the target URL.
135func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
136 director := proxy.Director
137 proxy.Director = func(req *http.Request) {
139 req.Host = target.Host
143// setProxyDirector sets the proxy.Director so that request URIs are escaped
144// when proxying to usptream servers.
145func setProxyDirector(proxy *httputil.ReverseProxy) {
146 director := proxy.Director
147 proxy.Director = func(req *http.Request) {
149 // use RequestURI so that we aren't unescaping encoded slashes in the request path
150 req.URL.Opaque = req.RequestURI
151 req.URL.RawQuery = ""
152 req.URL.ForceQuery = false
156// newWebSocketReverseProxy creates a new reverse proxy for proxying websocket connections.
157func newWebSocketReverseProxy(u *url.URL, skipTLSVerify bool) http.Handler {
158 wsProxy := httputil.NewSingleHostReverseProxy(u)
161 wsProxy.Transport = &http.Transport{
162 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list