| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"log/slog" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
|
| 9 |
"github.com/go-chi/chi/v5" |
| 10 |
"github.com/rs/cors" |
| 11 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 12 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 13 |
"github.com/zitadel/schema" |
| 14 |
) |
| 15 |
|
| 16 |
// RegisterServer registers an implementation of Server. |
| 17 |
// The resulting handler takes care of routing and request parsing, |
| 18 |
// with some basic validation of required fields. |
| 19 |
// The routes can be customized with [WithEndpoints]. |
| 20 |
// |
| 21 |
// EXPERIMENTAL: may change until v4 |
| 22 |
func RegisterServer(server Server, endpoints Endpoints, options ...ServerOption) http.Handler { |
| 23 |
decoder := schema.NewDecoder() |
| 24 |
decoder.IgnoreUnknownKeys(true) |
| 25 |
|
| 26 |
ws := &webServer{ |
| 27 |
router: chi.NewRouter(), |
| 28 |
server: server, |
| 29 |
endpoints: endpoints, |
| 30 |
decoder: decoder, |
| 31 |
corsOpts: &defaultCORSOptions, |
| 32 |
} |
| 33 |
|
| 34 |
for _, option := range options { |
| 35 |
option(ws) |
| 36 |
} |
| 37 |
|
| 38 |
ws.createRouter() |
| 39 |
ws.handler = ws.router |
| 40 |
if ws.corsOpts != nil { |
| 41 |
ws.handler = cors.New(*ws.corsOpts).Handler(ws.router) |
| 42 |
} |
| 43 |
return ws |
| 44 |
} |
| 45 |
|
| 46 |
type ServerOption func(s *webServer) |
| 47 |
|
| 48 |
// WithHTTPMiddleware sets the passed middleware chain to the root of |
| 49 |
// the Server's router. |
| 50 |
func WithHTTPMiddleware(m ...func(http.Handler) http.Handler) ServerOption { |
| 51 |
return func(s *webServer) { |
| 52 |
s.router.Use(m...) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// WithSetRouter allows customization or the Server's router. |
| 57 |
func WithSetRouter(set func(chi.Router)) ServerOption { |
| 58 |
return func(s *webServer) { |
| 59 |
set(s.router) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// WithDecoder overrides the default decoder, |
| 64 |
// which is a [schema.Decoder] with IgnoreUnknownKeys set to true. |
| 65 |
func WithDecoder(decoder httphelper.Decoder) ServerOption { |
| 66 |
return func(s *webServer) { |
| 67 |
s.decoder = decoder |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// WithServerCORSOptions sets the CORS policy for the Server's router. |
| 72 |
func WithServerCORSOptions(opts *cors.Options) ServerOption { |
| 73 |
return func(s *webServer) { |
| 74 |
s.corsOpts = opts |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// WithFallbackLogger is retained for source compatibility. |
| 79 |
// Deprecated: use [slog.SetDefault]. This option has no effect. |
| 80 |
func WithFallbackLogger(*slog.Logger) ServerOption { |
| 81 |
return func(*webServer) { |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
type webServer struct { |
| 86 |
server Server |
| 87 |
router *chi.Mux |
| 88 |
handler http.Handler |
| 89 |
endpoints Endpoints |
| 90 |
decoder httphelper.Decoder |
| 91 |
corsOpts *cors.Options |
| 92 |
} |
| 93 |
|
| 94 |
func (s *webServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 95 |
s.handler.ServeHTTP(w, r) |
| 96 |
} |
| 97 |
|
| 98 |
func (s *webServer) createRouter() { |
| 99 |
s.router.HandleFunc(healthEndpoint, simpleHandler(s, s.server.Health)) |
| 100 |
s.router.HandleFunc(readinessEndpoint, simpleHandler(s, s.server.Ready)) |
| 101 |
s.router.HandleFunc(oidc.DiscoveryEndpoint, simpleHandler(s, s.server.Discovery)) |
| 102 |
|
| 103 |
s.endpointRoute(s.endpoints.Authorization, s.authorizeHandler) |
| 104 |
s.endpointRoute(s.endpoints.DeviceAuthorization, s.withClient(s.deviceAuthorizationHandler)) |
| 105 |
s.endpointRoute(s.endpoints.Token, s.tokensHandler) |
| 106 |
s.endpointRoute(s.endpoints.Introspection, s.introspectionHandler) |
| 107 |
s.endpointRoute(s.endpoints.Userinfo, s.userInfoHandler) |
| 108 |
s.endpointRoute(s.endpoints.Revocation, s.withClient(s.revocationHandler)) |
| 109 |
s.endpointRoute(s.endpoints.EndSession, s.endSessionHandler) |
| 110 |
s.endpointRoute(s.endpoints.JwksURI, simpleHandler(s, s.server.Keys)) |
| 111 |
} |
| 112 |
|
| 113 |
func (s *webServer) endpointRoute(e *Endpoint, hf http.HandlerFunc) { |
| 114 |
if e != nil { |
| 115 |
traceHandler := func(w http.ResponseWriter, r *http.Request) { |
| 116 |
ctx, span := Tracer.Start(r.Context(), e.Relative()) |
| 117 |
r = r.WithContext(ctx) |
| 118 |
hf(w, r) |
| 119 |
defer span.End() |
| 120 |
} |
| 121 |
s.router.HandleFunc(e.Relative(), traceHandler) |
| 122 |
slog.Info("registered route", "endpoint", e.Relative()) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
type clientHandler func(w http.ResponseWriter, r *http.Request, client Client) |
| 127 |
|
| 128 |
func (s *webServer) withClient(handler clientHandler) http.HandlerFunc { |
| 129 |
return func(w http.ResponseWriter, r *http.Request) { |
| 130 |
ctx, span := Tracer.Start(r.Context(), r.URL.Path) |
| 131 |
defer span.End() |
| 132 |
r = r.WithContext(ctx) |
| 133 |
|
| 134 |
client, err := s.verifyRequestClient(r) |
| 135 |
if err != nil { |
| 136 |
WriteError(w, r, err, nil) |
| 137 |
return |
| 138 |
} |
| 139 |
if grantType := oidc.GrantType(r.Form.Get("grant_type")); grantType != "" { |
| 140 |
if !ValidateGrantType(client, grantType) { |
| 141 |
WriteError(w, r, oidc.ErrUnauthorizedClient().WithDescription("grant_type %q not allowed", grantType), nil) |
| 142 |
return |
| 143 |
} |
| 144 |
} |
| 145 |
handler(w, r, client) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func (s *webServer) verifyRequestClient(r *http.Request) (_ Client, err error) { |
| 150 |
cc, err := s.parseClientCredentials(r) |
| 151 |
if err != nil { |
| 152 |
return nil, err |
| 153 |
} |
| 154 |
return s.server.VerifyClient(r.Context(), &Request[ClientCredentials]{ |
| 155 |
Method: r.Method, |
| 156 |
URL: r.URL, |
| 157 |
Header: r.Header, |
| 158 |
Form: r.Form, |
| 159 |
Data: cc, |
| 160 |
}) |
| 161 |
} |
| 162 |
|
| 163 |
func (s *webServer) parseClientCredentials(r *http.Request) (_ *ClientCredentials, err error) { |
| 164 |
if err := r.ParseForm(); err != nil { |
| 165 |
return nil, oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err) |
| 166 |
} |
| 167 |
cc := new(ClientCredentials) |
| 168 |
if err = s.decoder.Decode(cc, r.Form); err != nil { |
| 169 |
return nil, oidc.ErrInvalidRequest().WithDescription("error decoding form").WithParent(err) |
| 170 |
} |
| 171 |
|
| 172 |
// https://datatracker.ietf.org/doc/html/rfc6749#section-2.3 |
| 173 |
// The client MUST NOT use more than one authentication method in each request. |
| 174 |
assertionAuthExists := cc.ClientAssertion != "" && cc.ClientAssertionType != "" |
| 175 |
secretAuthExists := cc.ClientSecret != "" && cc.ClientID != "" |
| 176 |
if assertionAuthExists && secretAuthExists { |
| 177 |
return nil, oidc.ErrInvalidRequest().WithDescription("client authentication must not use more than one method") |
| 178 |
} |
| 179 |
|
| 180 |
// Basic auth takes precedence, so if set it overwrites the form data. |
| 181 |
if clientID, clientSecret, ok := r.BasicAuth(); ok { |
| 182 |
if assertionAuthExists || secretAuthExists { |
| 183 |
return nil, oidc.ErrInvalidRequest().WithDescription("client authentication must not use more than one method") |
| 184 |
} |
| 185 |
cc.ClientID, err = url.QueryUnescape(clientID) |
| 186 |
if err != nil { |
| 187 |
return nil, oidc.ErrInvalidClient().WithDescription("invalid basic auth header").WithParent(err) |
| 188 |
} |
| 189 |
cc.ClientSecret, err = url.QueryUnescape(clientSecret) |
| 190 |
if err != nil { |
| 191 |
return nil, oidc.ErrInvalidClient().WithDescription("invalid basic auth header").WithParent(err) |
| 192 |
} |
| 193 |
} |
| 194 |
if cc.ClientID == "" && cc.ClientAssertion == "" { |
| 195 |
return nil, oidc.ErrInvalidRequest().WithDescription("client_id or client_assertion must be provided") |
| 196 |
} |
| 197 |
if cc.ClientAssertion != "" && cc.ClientAssertionType != oidc.ClientAssertionTypeJWTAssertion { |
| 198 |
return nil, oidc.ErrInvalidRequest().WithDescription("invalid client_assertion_type %s", cc.ClientAssertionType) |
| 199 |
} |
| 200 |
return cc, nil |
| 201 |
} |
| 202 |
|
| 203 |
func (s *webServer) authorizeHandler(w http.ResponseWriter, r *http.Request) { |
| 204 |
request, err := decodeRequest[oidc.AuthRequest](s.decoder, r, false) |
| 205 |
if err != nil { |
| 206 |
WriteError(w, r, err, nil) |
| 207 |
return |
| 208 |
} |
| 209 |
redirect, err := s.authorize(r.Context(), newRequest(r, request)) |
| 210 |
if err != nil { |
| 211 |
WriteError(w, r, err, nil) |
| 212 |
return |
| 213 |
} |
| 214 |
redirect.writeOut(w, r) |
| 215 |
} |
| 216 |
|
| 217 |
func (s *webServer) authorize(ctx context.Context, r *Request[oidc.AuthRequest]) (_ *Redirect, err error) { |
| 218 |
cr, err := s.server.VerifyAuthRequest(ctx, r) |
| 219 |
if err != nil { |
| 220 |
return nil, err |
| 221 |
} |
| 222 |
authReq := cr.Data |
| 223 |
if authReq.RedirectURI == "" { |
| 224 |
return nil, ErrAuthReqMissingRedirectURI |
| 225 |
} |
| 226 |
authReq.MaxAge, err = ValidateAuthReqPrompt(authReq.Prompt, authReq.MaxAge) |
| 227 |
if err != nil { |
| 228 |
return nil, err |
| 229 |
} |
| 230 |
authReq.Scopes, err = ValidateAuthReqScopes(cr.Client, authReq.Scopes) |
| 231 |
if err != nil { |
| 232 |
return nil, err |
| 233 |
} |
| 234 |
if err := ValidateAuthReqRedirectURI(cr.Client, authReq.RedirectURI, authReq.ResponseType); err != nil { |
| 235 |
return nil, err |
| 236 |
} |
| 237 |
if err := ValidateAuthReqResponseType(cr.Client, authReq.ResponseType); err != nil { |
| 238 |
return nil, err |
| 239 |
} |
| 240 |
return s.server.Authorize(ctx, cr) |
| 241 |
} |
| 242 |
|
| 243 |
func (s *webServer) deviceAuthorizationHandler(w http.ResponseWriter, r *http.Request, client Client) { |
| 244 |
request, err := decodeRequest[oidc.DeviceAuthorizationRequest](s.decoder, r, false) |
| 245 |
if err != nil { |
| 246 |
WriteError(w, r, err, nil) |
| 247 |
return |
| 248 |
} |
| 249 |
resp, err := s.server.DeviceAuthorization(r.Context(), newClientRequest(r, request, client)) |
| 250 |
if err != nil { |
| 251 |
WriteError(w, r, err, nil) |
| 252 |
return |
| 253 |
} |
| 254 |
resp.writeOut(w) |
| 255 |
} |
| 256 |
|
| 257 |
func (s *webServer) tokensHandler(w http.ResponseWriter, r *http.Request) { |
| 258 |
if err := r.ParseForm(); err != nil { |
| 259 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err), nil) |
| 260 |
return |
| 261 |
} |
| 262 |
|
| 263 |
switch grantType := oidc.GrantType(r.Form.Get("grant_type")); grantType { |
| 264 |
case oidc.GrantTypeCode: |
| 265 |
s.withClient(s.codeExchangeHandler)(w, r) |
| 266 |
case oidc.GrantTypeRefreshToken: |
| 267 |
s.withClient(s.refreshTokenHandler)(w, r) |
| 268 |
case oidc.GrantTypeClientCredentials: |
| 269 |
s.withClient(s.clientCredentialsHandler)(w, r) |
| 270 |
case oidc.GrantTypeBearer: |
| 271 |
s.jwtProfileHandler(w, r) |
| 272 |
case oidc.GrantTypeTokenExchange: |
| 273 |
s.withClient(s.tokenExchangeHandler)(w, r) |
| 274 |
case oidc.GrantTypeDeviceCode: |
| 275 |
s.withClient(s.deviceTokenHandler)(w, r) |
| 276 |
case "": |
| 277 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("grant_type missing"), nil) |
| 278 |
default: |
| 279 |
WriteError(w, r, unimplementedGrantError(grantType), nil) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
func (s *webServer) jwtProfileHandler(w http.ResponseWriter, r *http.Request) { |
| 284 |
request, err := decodeRequest[oidc.JWTProfileGrantRequest](s.decoder, r, false) |
| 285 |
if err != nil { |
| 286 |
WriteError(w, r, err, nil) |
| 287 |
return |
| 288 |
} |
| 289 |
if request.Assertion == "" { |
| 290 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("assertion missing"), nil) |
| 291 |
return |
| 292 |
} |
| 293 |
resp, err := s.server.JWTProfile(r.Context(), newRequest(r, request)) |
| 294 |
if err != nil { |
| 295 |
WriteError(w, r, err, nil) |
| 296 |
return |
| 297 |
} |
| 298 |
resp.writeOut(w) |
| 299 |
} |
| 300 |
|
| 301 |
func (s *webServer) codeExchangeHandler(w http.ResponseWriter, r *http.Request, client Client) { |
| 302 |
request, err := decodeRequest[oidc.AccessTokenRequest](s.decoder, r, false) |
| 303 |
if err != nil { |
| 304 |
WriteError(w, r, err, nil) |
| 305 |
return |
| 306 |
} |
| 307 |
if request.Code == "" { |
| 308 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("code missing"), nil) |
| 309 |
return |
| 310 |
} |
| 311 |
if request.RedirectURI == "" { |
| 312 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("redirect_uri missing"), nil) |
| 313 |
return |
| 314 |
} |
| 315 |
resp, err := s.server.CodeExchange(r.Context(), newClientRequest(r, request, client)) |
| 316 |
if err != nil { |
| 317 |
WriteError(w, r, err, nil) |
| 318 |
return |
| 319 |
} |
| 320 |
resp.writeOut(w) |
| 321 |
} |
| 322 |
|
| 323 |
func (s *webServer) refreshTokenHandler(w http.ResponseWriter, r *http.Request, client Client) { |
| 324 |
request, err := decodeRequest[oidc.RefreshTokenRequest](s.decoder, r, false) |
| 325 |
if err != nil { |
| 326 |
WriteError(w, r, err, nil) |
| 327 |
return |
| 328 |
} |
| 329 |
if request.RefreshToken == "" { |
| 330 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("refresh_token missing"), nil) |
| 331 |
return |
| 332 |
} |
| 333 |
resp, err := s.server.RefreshToken(r.Context(), newClientRequest(r, request, client)) |
| 334 |
if err != nil { |
| 335 |
WriteError(w, r, err, nil) |
| 336 |
return |
| 337 |
} |
| 338 |
resp.writeOut(w) |
| 339 |
} |
| 340 |
|
| 341 |
func (s *webServer) tokenExchangeHandler(w http.ResponseWriter, r *http.Request, client Client) { |
| 342 |
request, err := decodeRequest[oidc.TokenExchangeRequest](s.decoder, r, false) |
| 343 |
if err != nil { |
| 344 |
WriteError(w, r, err, nil) |
| 345 |
return |
| 346 |
} |
| 347 |
if request.SubjectToken == "" { |
| 348 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("subject_token missing"), nil) |
| 349 |
return |
| 350 |
} |
| 351 |
if request.SubjectTokenType == "" { |
| 352 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("subject_token_type missing"), nil) |
| 353 |
return |
| 354 |
} |
| 355 |
if !request.SubjectTokenType.IsSupported() { |
| 356 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("subject_token_type is not supported"), nil) |
| 357 |
return |
| 358 |
} |
| 359 |
if request.RequestedTokenType != "" && !request.RequestedTokenType.IsSupported() { |
| 360 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("requested_token_type is not supported"), nil) |
| 361 |
return |
| 362 |
} |
| 363 |
if request.ActorTokenType != "" && !request.ActorTokenType.IsSupported() { |
| 364 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("actor_token_type is not supported"), nil) |
| 365 |
return |
| 366 |
} |
| 367 |
resp, err := s.server.TokenExchange(r.Context(), newClientRequest(r, request, client)) |
| 368 |
if err != nil { |
| 369 |
WriteError(w, r, err, nil) |
| 370 |
return |
| 371 |
} |
| 372 |
resp.writeOut(w) |
| 373 |
} |
| 374 |
|
| 375 |
func (s *webServer) clientCredentialsHandler(w http.ResponseWriter, r *http.Request, client Client) { |
| 376 |
if client.AuthMethod() == oidc.AuthMethodNone { |
| 377 |
WriteError(w, r, oidc.ErrInvalidClient().WithDescription("client must be authenticated"), nil) |
| 378 |
return |
| 379 |
} |
| 380 |
|
| 381 |
request, err := decodeRequest[oidc.ClientCredentialsRequest](s.decoder, r, false) |
| 382 |
if err != nil { |
| 383 |
WriteError(w, r, err, nil) |
| 384 |
return |
| 385 |
} |
| 386 |
resp, err := s.server.ClientCredentialsExchange(r.Context(), newClientRequest(r, request, client)) |
| 387 |
if err != nil { |
| 388 |
WriteError(w, r, err, nil) |
| 389 |
return |
| 390 |
} |
| 391 |
resp.writeOut(w) |
| 392 |
} |
| 393 |
|
| 394 |
func (s *webServer) deviceTokenHandler(w http.ResponseWriter, r *http.Request, client Client) { |
| 395 |
request, err := decodeRequest[oidc.DeviceAccessTokenRequest](s.decoder, r, false) |
| 396 |
if err != nil { |
| 397 |
WriteError(w, r, err, nil) |
| 398 |
return |
| 399 |
} |
| 400 |
if request.DeviceCode == "" { |
| 401 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("device_code missing"), nil) |
| 402 |
return |
| 403 |
} |
| 404 |
resp, err := s.server.DeviceToken(r.Context(), newClientRequest(r, request, client)) |
| 405 |
if err != nil { |
| 406 |
WriteError(w, r, err, nil) |
| 407 |
return |
| 408 |
} |
| 409 |
resp.writeOut(w) |
| 410 |
} |
| 411 |
|
| 412 |
func (s *webServer) introspectionHandler(w http.ResponseWriter, r *http.Request) { |
| 413 |
cc, err := s.parseClientCredentials(r) |
| 414 |
if err != nil { |
| 415 |
WriteError(w, r, err, nil) |
| 416 |
return |
| 417 |
} |
| 418 |
if cc.ClientSecret == "" && cc.ClientAssertion == "" { |
| 419 |
WriteError(w, r, oidc.ErrInvalidClient().WithDescription("client must be authenticated"), nil) |
| 420 |
return |
| 421 |
} |
| 422 |
request, err := decodeRequest[oidc.IntrospectionRequest](s.decoder, r, false) |
| 423 |
if err != nil { |
| 424 |
WriteError(w, r, err, nil) |
| 425 |
return |
| 426 |
} |
| 427 |
if request.Token == "" { |
| 428 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("token missing"), nil) |
| 429 |
return |
| 430 |
} |
| 431 |
resp, err := s.server.Introspect(r.Context(), newRequest(r, &IntrospectionRequest{cc, request})) |
| 432 |
if err != nil { |
| 433 |
WriteError(w, r, err, nil) |
| 434 |
return |
| 435 |
} |
| 436 |
resp.writeOut(w) |
| 437 |
} |
| 438 |
|
| 439 |
func (s *webServer) userInfoHandler(w http.ResponseWriter, r *http.Request) { |
| 440 |
request, err := decodeRequest[oidc.UserInfoRequest](s.decoder, r, false) |
| 441 |
if err != nil { |
| 442 |
WriteError(w, r, err, nil) |
| 443 |
return |
| 444 |
} |
| 445 |
if token, err := getAccessToken(r); err == nil { |
| 446 |
request.AccessToken = token |
| 447 |
} |
| 448 |
if request.AccessToken == "" { |
| 449 |
err = NewStatusError( |
| 450 |
oidc.ErrInvalidRequest().WithDescription("access token missing"), |
| 451 |
http.StatusUnauthorized, |
| 452 |
) |
| 453 |
WriteError(w, r, err, nil) |
| 454 |
return |
| 455 |
} |
| 456 |
resp, err := s.server.UserInfo(r.Context(), newRequest(r, request)) |
| 457 |
if err != nil { |
| 458 |
WriteError(w, r, err, nil) |
| 459 |
return |
| 460 |
} |
| 461 |
resp.writeOut(w) |
| 462 |
} |
| 463 |
|
| 464 |
func (s *webServer) revocationHandler(w http.ResponseWriter, r *http.Request, client Client) { |
| 465 |
request, err := decodeRequest[oidc.RevocationRequest](s.decoder, r, false) |
| 466 |
if err != nil { |
| 467 |
WriteError(w, r, err, nil) |
| 468 |
return |
| 469 |
} |
| 470 |
if request.Token == "" { |
| 471 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("token missing"), nil) |
| 472 |
return |
| 473 |
} |
| 474 |
resp, err := s.server.Revocation(r.Context(), newClientRequest(r, request, client)) |
| 475 |
if err != nil { |
| 476 |
WriteError(w, r, err, nil) |
| 477 |
return |
| 478 |
} |
| 479 |
resp.writeOut(w) |
| 480 |
} |
| 481 |
|
| 482 |
func (s *webServer) endSessionHandler(w http.ResponseWriter, r *http.Request) { |
| 483 |
request, err := decodeRequest[oidc.EndSessionRequest](s.decoder, r, false) |
| 484 |
if err != nil { |
| 485 |
WriteError(w, r, err, nil) |
| 486 |
return |
| 487 |
} |
| 488 |
resp, err := s.server.EndSession(r.Context(), newRequest(r, request)) |
| 489 |
if err != nil { |
| 490 |
WriteError(w, r, err, nil) |
| 491 |
return |
| 492 |
} |
| 493 |
resp.writeOut(w, r) |
| 494 |
} |
| 495 |
|
| 496 |
func simpleHandler(s *webServer, method func(context.Context, *Request[struct{}]) (*Response, error)) http.HandlerFunc { |
| 497 |
return func(w http.ResponseWriter, r *http.Request) { |
| 498 |
if err := r.ParseForm(); err != nil { |
| 499 |
WriteError(w, r, oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err), nil) |
| 500 |
return |
| 501 |
} |
| 502 |
resp, err := method(r.Context(), newRequest(r, &struct{}{})) |
| 503 |
if err != nil { |
| 504 |
WriteError(w, r, err, nil) |
| 505 |
return |
| 506 |
} |
| 507 |
resp.writeOut(w) |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
func decodeRequest[R any](decoder httphelper.Decoder, r *http.Request, postOnly bool) (*R, error) { |
| 512 |
dst := new(R) |
| 513 |
if err := r.ParseForm(); err != nil { |
| 514 |
return nil, oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err) |
| 515 |
} |
| 516 |
form := r.Form |
| 517 |
if postOnly { |
| 518 |
form = r.PostForm |
| 519 |
} |
| 520 |
if err := decoder.Decode(dst, form); err != nil { |
| 521 |
return nil, oidc.ErrInvalidRequest().WithDescription("error decoding form").WithParent(err) |
| 522 |
} |
| 523 |
return dst, nil |
| 524 |
} |