8 "github.com/justinas/alice"
9 middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/header"
14func NewRequestHeaderInjector(headers []options.Header) (alice.Constructor, error) {
15 headerInjector, err := newRequestHeaderInjector(headers)
17 return nil, fmt.Errorf("error building request header injector: %v", err)
20 strip := newStripHeaders(headers)
22 return alice.New(strip, headerInjector).Then, nil
24 return headerInjector, nil
27func newStripHeaders(headers []options.Header) alice.Constructor {
28 headersToStrip := []string{}
29 for _, header := range headers {
30 if !header.PreserveRequestValue {
31 headersToStrip = append(headersToStrip, header.Name)
35 if len(headersToStrip) == 0 {
39 return func(next http.Handler) http.Handler {
40 return stripHeaders(headersToStrip, next)
44func flattenHeaders(headers http.Header) {
45 for name, values := range headers {
47 headers.Set(name, strings.Join(values, ","))
52func stripHeaders(headers []string, next http.Handler) http.Handler {
53 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
54 for _, header := range headers {
55 req.Header.Del(header)
57 next.ServeHTTP(rw, req)
61func newRequestHeaderInjector(headers []options.Header) (alice.Constructor, error) {
62 injector, err := header.NewInjector(headers)
64 return nil, fmt.Errorf("error building request injector: %v", err)
67 return func(next http.Handler) http.Handler {
68 return injectRequestHeaders(injector, next)
72func injectRequestHeaders(injector header.Injector, next http.Handler) http.Handler {
73 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
74 scope := middlewareapi.GetRequestScope(req)
76 // If scope is nil, this will panic.
77 // A scope should always be injected before this handler is called.
78 injector.Inject(req.Header, scope.Session)
79 flattenHeaders(req.Header)
80 next.ServeHTTP(rw, req)
84func NewResponseHeaderInjector(headers []options.Header) (alice.Constructor, error) {
85 headerInjector, err := newResponseHeaderInjector(headers)
87 return nil, fmt.Errorf("error building response header injector: %v", err)
90 return headerInjector, nil
93func newResponseHeaderInjector(headers []options.Header) (alice.Constructor, error) {
94 injector, err := header.NewInjector(headers)
96 return nil, fmt.Errorf("error building response injector: %v", err)
99 return func(next http.Handler) http.Handler {
100 return injectResponseHeaders(injector, next)
104func injectResponseHeaders(injector header.Injector, next http.Handler) http.Handler {
105 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
106 scope := middlewareapi.GetRequestScope(req)
108 // If scope is nil, this will panic.
109 // A scope should always be injected before this handler is called.
110 injector.Inject(rw.Header(), scope.Session)
111 flattenHeaders(rw.Header())
112 next.ServeHTTP(rw, req)
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