|
| 1 | +// Package discovery serves the OIDC discovery document |
| 2 | +// (/.well-known/openid-configuration) and the JWKS endpoint (/keys). |
| 3 | +package discovery |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "log/slog" |
| 10 | + "net/http" |
| 11 | + "sort" |
| 12 | + "strconv" |
| 13 | + "sync" |
| 14 | + "time" |
| 15 | + |
| 16 | + jose "github.com/go-jose/go-jose/v4" |
| 17 | + |
| 18 | + "github.com/dexidp/dex/server/router" |
| 19 | + "github.com/dexidp/dex/server/signer" |
| 20 | +) |
| 21 | + |
| 22 | +// Config holds the discovery handler's dependencies. AbsURL builds an absolute |
| 23 | +// URL under the issuer; RenderError renders an HTML error page. Both are |
| 24 | +// supplied by the server so the handler does not depend on the whole Server. |
| 25 | +type Config struct { |
| 26 | + Issuer string |
| 27 | + AbsURL func(...string) string |
| 28 | + RenderError func(*http.Request, http.ResponseWriter, int, string) |
| 29 | + Signer signer.Signer |
| 30 | + Logger *slog.Logger |
| 31 | + ResponseTypes map[string]bool |
| 32 | + GrantTypes []string |
| 33 | + PKCEMethods []string |
| 34 | + SessionsEnabled bool |
| 35 | +} |
| 36 | + |
| 37 | +// Handler serves the discovery document and the JWKS. |
| 38 | +type Handler struct { |
| 39 | + Config |
| 40 | + |
| 41 | + docOnce sync.Once |
| 42 | + docData []byte |
| 43 | + docErr error |
| 44 | +} |
| 45 | + |
| 46 | +// New returns a discovery handler. |
| 47 | +func New(c Config) *Handler { |
| 48 | + return &Handler{Config: c} |
| 49 | +} |
| 50 | + |
| 51 | +// Mount registers the discovery routes. |
| 52 | +func (h *Handler) Mount(m router.Mux) { |
| 53 | + m.HandleCORS("/.well-known/openid-configuration", h.serveDocument) |
| 54 | + m.HandleCORS("/keys", h.Keys) |
| 55 | +} |
| 56 | + |
| 57 | +// Document is the OIDC discovery document. |
| 58 | +type Document struct { |
| 59 | + Issuer string `json:"issuer"` |
| 60 | + Auth string `json:"authorization_endpoint"` |
| 61 | + Token string `json:"token_endpoint"` |
| 62 | + Keys string `json:"jwks_uri"` |
| 63 | + UserInfo string `json:"userinfo_endpoint"` |
| 64 | + DeviceEndpoint string `json:"device_authorization_endpoint"` |
| 65 | + Introspect string `json:"introspection_endpoint"` |
| 66 | + EndSession string `json:"end_session_endpoint,omitempty"` |
| 67 | + GrantTypes []string `json:"grant_types_supported"` |
| 68 | + ResponseTypes []string `json:"response_types_supported"` |
| 69 | + Subjects []string `json:"subject_types_supported"` |
| 70 | + IDTokenAlgs []string `json:"id_token_signing_alg_values_supported"` |
| 71 | + CodeChallengeAlgs []string `json:"code_challenge_methods_supported"` |
| 72 | + Scopes []string `json:"scopes_supported"` |
| 73 | + AuthMethods []string `json:"token_endpoint_auth_methods_supported"` |
| 74 | + Claims []string `json:"claims_supported"` |
| 75 | +} |
| 76 | + |
| 77 | +// Keys serves the JSON Web Key Set. |
| 78 | +func (h *Handler) Keys(w http.ResponseWriter, r *http.Request) { |
| 79 | + ctx := r.Context() |
| 80 | + // TODO(ericchiang): Cache this. |
| 81 | + keys, err := h.Signer.ValidationKeys(ctx) |
| 82 | + if err != nil { |
| 83 | + h.Logger.ErrorContext(ctx, "failed to get keys", "err", err) |
| 84 | + h.RenderError(r, w, http.StatusInternalServerError, "Internal server error.") |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + if len(keys) == 0 { |
| 89 | + h.Logger.ErrorContext(ctx, "no public keys found.") |
| 90 | + h.RenderError(r, w, http.StatusInternalServerError, "Internal server error.") |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + jwks := jose.JSONWebKeySet{ |
| 95 | + Keys: make([]jose.JSONWebKey, len(keys)), |
| 96 | + } |
| 97 | + for i, key := range keys { |
| 98 | + jwks.Keys[i] = *key |
| 99 | + } |
| 100 | + |
| 101 | + data, err := json.MarshalIndent(jwks, "", " ") |
| 102 | + if err != nil { |
| 103 | + h.Logger.ErrorContext(ctx, "failed to marshal discovery data", "err", err) |
| 104 | + h.RenderError(r, w, http.StatusInternalServerError, "Internal server error.") |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + // We don't have NextRotation info from Signer interface easily, |
| 109 | + // so we'll just set a reasonable default cache time. |
| 110 | + maxAge := time.Minute * 10 |
| 111 | + |
| 112 | + w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, must-revalidate", int(maxAge.Seconds()))) |
| 113 | + w.Header().Set("Content-Type", "application/json") |
| 114 | + w.Header().Set("Content-Length", strconv.Itoa(len(data))) |
| 115 | + w.Write(data) |
| 116 | +} |
| 117 | + |
| 118 | +// serveDocument serves the discovery document, marshaling it once on first use. |
| 119 | +func (h *Handler) serveDocument(w http.ResponseWriter, r *http.Request) { |
| 120 | + h.docOnce.Do(func() { |
| 121 | + h.docData, h.docErr = json.MarshalIndent(h.Construct(r.Context()), "", " ") |
| 122 | + }) |
| 123 | + if h.docErr != nil { |
| 124 | + h.Logger.ErrorContext(r.Context(), "failed to marshal discovery data", "err", h.docErr) |
| 125 | + h.RenderError(r, w, http.StatusInternalServerError, "Internal server error.") |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + w.Header().Set("Content-Type", "application/json") |
| 130 | + w.Header().Set("Content-Length", strconv.Itoa(len(h.docData))) |
| 131 | + w.Write(h.docData) |
| 132 | +} |
| 133 | + |
| 134 | +// Construct builds the discovery document from the current configuration. |
| 135 | +func (h *Handler) Construct(ctx context.Context) Document { |
| 136 | + d := Document{ |
| 137 | + Issuer: h.Issuer, |
| 138 | + Auth: h.AbsURL("/auth"), |
| 139 | + Token: h.AbsURL("/token"), |
| 140 | + Keys: h.AbsURL("/keys"), |
| 141 | + UserInfo: h.AbsURL("/userinfo"), |
| 142 | + DeviceEndpoint: h.AbsURL("/device/code"), |
| 143 | + Introspect: h.AbsURL("/token/introspect"), |
| 144 | + Subjects: []string{"public"}, |
| 145 | + IDTokenAlgs: []string{string(jose.RS256)}, |
| 146 | + CodeChallengeAlgs: h.PKCEMethods, |
| 147 | + Scopes: []string{"openid", "email", "groups", "profile", "offline_access"}, |
| 148 | + AuthMethods: []string{"client_secret_basic", "client_secret_post"}, |
| 149 | + Claims: []string{ |
| 150 | + "iss", "sub", "aud", "iat", "exp", "email", "email_verified", |
| 151 | + "locale", "name", "preferred_username", "at_hash", |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + // Determine signing algorithm from signer. |
| 156 | + signingAlg, err := h.Signer.Algorithm(ctx) |
| 157 | + if err != nil { |
| 158 | + h.Logger.Error("failed to get signing algorithm", "err", err) |
| 159 | + } else { |
| 160 | + d.IDTokenAlgs = []string{string(signingAlg)} |
| 161 | + } |
| 162 | + |
| 163 | + for responseType := range h.ResponseTypes { |
| 164 | + d.ResponseTypes = append(d.ResponseTypes, responseType) |
| 165 | + } |
| 166 | + sort.Strings(d.ResponseTypes) |
| 167 | + |
| 168 | + d.GrantTypes = h.GrantTypes |
| 169 | + |
| 170 | + if h.SessionsEnabled { |
| 171 | + d.EndSession = h.AbsURL("/logout") |
| 172 | + } |
| 173 | + |
| 174 | + return d |
| 175 | +} |
0 commit comments