forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternalauthparam.go
More file actions
40 lines (33 loc) · 1.02 KB
/
externalauthparam.go
File metadata and controls
40 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package httpmw
import (
"context"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/coder/coder/v2/coderd/externalauth"
"github.com/coder/coder/v2/coderd/httpapi"
)
type externalAuthParamContextKey struct{}
func ExternalAuthParam(r *http.Request) *externalauth.Config {
config, ok := r.Context().Value(externalAuthParamContextKey{}).(*externalauth.Config)
if !ok {
panic("developer error: external auth param middleware not provided")
}
return config
}
func ExtractExternalAuthParam(configs []*externalauth.Config) func(next http.Handler) http.Handler {
configByID := make(map[string]*externalauth.Config)
for _, c := range configs {
configByID[c.ID] = c
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
config, ok := configByID[chi.URLParam(r, "externalauth")]
if !ok {
httpapi.ResourceNotFound(w)
return
}
r = r.WithContext(context.WithValue(r.Context(), externalAuthParamContextKey{}, config))
next.ServeHTTP(w, r)
})
}
}