1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-01-24 05:26:55 +02:00

34 lines
937 B
Go
Raw Normal View History

2020-07-04 18:41:58 +01:00
package middleware
import (
"net/http"
"github.com/google/uuid"
2020-07-04 18:41:58 +01:00
"github.com/justinas/alice"
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
2020-07-04 18:41:58 +01:00
)
func NewScope(reverseProxy bool, idHeader string) alice.Constructor {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
scope := &middlewareapi.RequestScope{
ReverseProxy: reverseProxy,
RequestID: genRequestID(req, idHeader),
}
req = middlewareapi.AddRequestScope(req, scope)
next.ServeHTTP(rw, req)
})
}
2020-07-04 18:41:58 +01:00
}
// genRequestID sets a request-wide ID for use in logging or error pages.
// If a RequestID header is set, it uses that. Otherwise, it generates a random
// UUID for the lifespan of the request.
func genRequestID(req *http.Request, idHeader string) string {
rid := req.Header.Get(idHeader)
if rid != "" {
return rid
}
return uuid.New().String()
}