git.iko.soy/typeable/oauth2-proxy
on remove-gap-headers help sign in
102 lines 2.9 kB history raw
1package middleware
3import (
4 "fmt"
5 "net/http"
7 "github.com/justinas/alice"
8 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/header"
12func NewRequestHeaderInjector(headers []options.Header) (alice.Constructor, error) {
13 headerInjector, err := newRequestHeaderInjector(headers)
14 if err != nil {
15 return nil, fmt.Errorf("error building request header injector: %v", err)
16 }
18 strip := newStripHeaders(headers)
19 if strip != nil {
20 return alice.New(strip, headerInjector).Then, nil
21 }
22 return headerInjector, nil
25func newStripHeaders(headers []options.Header) alice.Constructor {
26 headersToStrip := []string{}
27 for _, header := range headers {
28 if !header.PreserveRequestValue {
29 headersToStrip = append(headersToStrip, header.Name)
30 }
31 }
33 if len(headersToStrip) == 0 {
34 return nil
35 }
37 return func(next http.Handler) http.Handler {
38 return stripHeaders(headersToStrip, next)
39 }
42func stripHeaders(headers []string, next http.Handler) http.Handler {
43 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
44 for _, header := range headers {
45 req.Header.Del(header)
46 }
47 next.ServeHTTP(rw, req)
48 })
51func newRequestHeaderInjector(headers []options.Header) (alice.Constructor, error) {
52 injector, err := header.NewInjector(headers)
53 if err != nil {
54 return nil, fmt.Errorf("error building request injector: %v", err)
55 }
57 return func(next http.Handler) http.Handler {
58 return injectRequestHeaders(injector, next)
59 }, nil
62func injectRequestHeaders(injector header.Injector, next http.Handler) http.Handler {
63 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
64 scope := GetRequestScope(req)
66 // If scope is nil, this will panic.
67 // A scope should always be injected before this handler is called.
68 injector.Inject(req.Header, scope.Session)
69 next.ServeHTTP(rw, req)
70 })
73func NewResponseHeaderInjector(headers []options.Header) (alice.Constructor, error) {
74 headerInjector, err := newResponseHeaderInjector(headers)
75 if err != nil {
76 return nil, fmt.Errorf("error building response header injector: %v", err)
77 }
79 return headerInjector, nil
82func newResponseHeaderInjector(headers []options.Header) (alice.Constructor, error) {
83 injector, err := header.NewInjector(headers)
84 if err != nil {
85 return nil, fmt.Errorf("error building response injector: %v", err)
86 }
88 return func(next http.Handler) http.Handler {
89 return injectResponseHeaders(injector, next)
90 }, nil
93func injectResponseHeaders(injector header.Injector, next http.Handler) http.Handler {
94 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
95 scope := GetRequestScope(req)
97 // If scope is nil, this will panic.
98 // A scope should always be injected before this handler is called.
99 injector.Inject(rw.Header(), scope.Session)
100 next.ServeHTTP(rw, req)
101 })