MCPcopy Index your code
hub / github.com/coder/coder / RequireAPIKeyOrProvisionerDaemonAuth

Function RequireAPIKeyOrProvisionerDaemonAuth

coderd/httpmw/actor.go:73–95  ·  view source on GitHub ↗

RequireAPIKeyOrProvisionerDaemonAuth is middleware that should be inserted after optional ExtractAPIKey and ExtractProvisionerDaemonAuthenticated middlewares to ensure one of the two authentication methods is provided. If both are provided, an error is returned to avoid misuse.

()

Source from the content-addressed store, hash-verified

71//
72// If both are provided, an error is returned to avoid misuse.
73func RequireAPIKeyOrProvisionerDaemonAuth() func(http.Handler) http.Handler {
74 return func(next http.Handler) http.Handler {
75 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
76 _, hasAPIKey := APIKeyOptional(r)
77 hasProvisionerDaemon := ProvisionerDaemonAuthenticated(r)
78
79 if hasAPIKey && hasProvisionerDaemon {
80 httpapi.Write(r.Context(), w, http.StatusBadRequest, codersdk.Response{
81 Message: "API key and external provisioner authentication provided, but only one is allowed",
82 })
83 return
84 }
85 if !hasAPIKey && !hasProvisionerDaemon {
86 httpapi.Write(r.Context(), w, http.StatusUnauthorized, codersdk.Response{
87 Message: "API key or external provisioner authentication required, but none provided",
88 })
89 return
90 }
91
92 next.ServeHTTP(w, r)
93 })
94 }
95}

Callers 1

NewFunction · 0.92

Calls 5

WriteFunction · 0.92
APIKeyOptionalFunction · 0.85
ContextMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected