git.iko.soy/typeable/oauth2-proxy
on ea261ca help sign in
48 lines 1.3 kB history raw
1package upstream
3import (
4 "fmt"
5 "net/http"
7 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
8 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
9)
11const defaultStaticResponseCode = 200
13// newStaticResponseHandler creates a new staticResponseHandler that serves a
14// a static response code.
15func newStaticResponseHandler(upstream string, code *int) http.Handler {
16 return &staticResponseHandler{
17 code: derefStaticCode(code),
18 upstream: upstream,
19 }
22// staticResponseHandler responds with a static response with the given response code.
23type staticResponseHandler struct {
24 code int
25 upstream string
28// ServeHTTP serves a static response.
29func (s *staticResponseHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
30 scope := middleware.GetRequestScope(req)
31 // If scope is nil, this will panic.
32 // A scope should always be injected before this handler is called.
33 scope.Upstream = s.upstream
35 rw.WriteHeader(s.code)
36 _, err := fmt.Fprintf(rw, "Authenticated")
37 if err != nil {
38 logger.Errorf("Error writing static response: %v", err)
39 }
42// derefStaticCode returns the derefenced value, or the default if the value is nil
43func derefStaticCode(code *int) int {
44 if code != nil {
45 return *code
46 }
47 return defaultStaticResponseCode