git.iko.soy/typeable/oauth2-proxy
on reference-generator help sign in
39 lines 1.1 kB history raw
1package middleware
3import (
4 "context"
5 "net/http"
7 "github.com/justinas/alice"
8 middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
9)
11type scopeKey string
13// requestScopeKey uses a typed string to reduce likelihood of clasing
14// with other context keys
15const requestScopeKey scopeKey = "request-scope"
17func NewScope() alice.Constructor {
18 return addScope
21// addScope injects a new request scope into the request context.
22func addScope(next http.Handler) http.Handler {
23 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
24 scope := &middlewareapi.RequestScope{}
25 contextWithScope := context.WithValue(req.Context(), requestScopeKey, scope)
26 requestWithScope := req.WithContext(contextWithScope)
27 next.ServeHTTP(rw, requestWithScope)
28 })
31// GetRequestScope returns the current request scope from the given request
32func GetRequestScope(req *http.Request) *middlewareapi.RequestScope {
33 scope := req.Context().Value(requestScopeKey)
34 if scope == nil {
35 return nil
36 }
38 return scope.(*middlewareapi.RequestScope)