1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-12-09 23:21:49 +02:00
Files
oauth2-proxy/pkg/upstream/static.go
Jan Larwig ceb9a387b1 deref everything... but why?
Signed-off-by: Jan Larwig <jan@larwig.com>
2025-11-16 22:38:54 +01:00

40 lines
1.1 KiB
Go

package upstream
import (
"fmt"
"net/http"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/util/ptr"
)
// newStaticResponseHandler creates a new staticResponseHandler that serves a
// a static response code.
func newStaticResponseHandler(upstream string, code *int) http.Handler {
return &staticResponseHandler{
code: ptr.Deref(code, 200),
upstream: upstream,
}
}
// staticResponseHandler responds with a static response with the given response code.
type staticResponseHandler struct {
code int
upstream string
}
// ServeHTTP serves a static response.
func (s *staticResponseHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
scope := middleware.GetRequestScope(req)
// If scope is nil, this will panic.
// A scope should always be injected before this handler is called.
scope.Upstream = s.upstream
rw.WriteHeader(s.code)
_, err := fmt.Fprintf(rw, "Authenticated")
if err != nil {
logger.Errorf("Error writing static response: %v", err)
}
}