-
Notifications
You must be signed in to change notification settings - Fork 2
/
oidc.go
33 lines (29 loc) · 813 Bytes
/
oidc.go
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
package main
import (
"context"
"log"
"net/http"
"github.com/coreos/go-oidc"
)
type OIDCDiscoveryConfig struct {
log *log.Logger
verifier *oidc.IDTokenVerifier
}
func OIDCDiscoveryAuth(cfg *OIDCDiscoveryConfig, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rawToken := getToken(r)
if rawToken == "" {
cfg.log.Print("no token found in request")
http.Error(w, http.StatusText(401)+": no token", 401)
return
}
token, err := cfg.verifier.Verify(r.Context(), rawToken)
if err != nil {
cfg.log.Print("token verification failed:", err)
http.Error(w, http.StatusText(401)+": "+err.Error(), 401)
return
}
ctx := context.WithValue(r.Context(), "subject", token.Subject)
next.ServeHTTP(w, r.WithContext(ctx))
})
}