| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"log/slog" |
| 7 |
"net/http" |
| 8 |
"net/url" |
| 9 |
"path" |
| 10 |
|
| 11 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 12 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 13 |
) |
| 14 |
|
| 15 |
type SessionEnder interface { |
| 16 |
Decoder() httphelper.Decoder |
| 17 |
Storage() Storage |
| 18 |
IDTokenHintVerifier(context.Context) *IDTokenHintVerifier |
| 19 |
DefaultLogoutRedirectURI() string |
| 20 |
// Deprecated: configure logging with slog.SetDefault. |
| 21 |
Logger() *slog.Logger |
| 22 |
} |
| 23 |
|
| 24 |
func endSessionHandler(ender SessionEnder) func(http.ResponseWriter, *http.Request) { |
| 25 |
return func(w http.ResponseWriter, r *http.Request) { |
| 26 |
EndSession(w, r, ender) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func EndSession(w http.ResponseWriter, r *http.Request, ender SessionEnder) { |
| 31 |
ctx, span := Tracer.Start(r.Context(), "EndSession") |
| 32 |
defer span.End() |
| 33 |
r = r.WithContext(ctx) |
| 34 |
|
| 35 |
req, err := ParseEndSessionRequest(r, ender.Decoder()) |
| 36 |
if err != nil { |
| 37 |
http.Error(w, err.Error(), http.StatusInternalServerError) |
| 38 |
return |
| 39 |
} |
| 40 |
session, err := ValidateEndSessionRequest(r.Context(), req, ender) |
| 41 |
if err != nil { |
| 42 |
RequestError(w, r, err, nil) |
| 43 |
return |
| 44 |
} |
| 45 |
redirect := session.RedirectURI |
| 46 |
if fromRequest, ok := ender.Storage().(CanTerminateSessionFromRequest); ok { |
| 47 |
redirect, err = fromRequest.TerminateSessionFromRequest(r.Context(), session) |
| 48 |
} else { |
| 49 |
err = ender.Storage().TerminateSession(r.Context(), session.UserID, session.ClientID) |
| 50 |
} |
| 51 |
if err != nil { |
| 52 |
RequestError(w, r, oidc.DefaultToServerError(err, "error terminating session"), nil) |
| 53 |
return |
| 54 |
} |
| 55 |
http.Redirect(w, r, redirect, http.StatusFound) |
| 56 |
} |
| 57 |
|
| 58 |
func ParseEndSessionRequest(r *http.Request, decoder httphelper.Decoder) (*oidc.EndSessionRequest, error) { |
| 59 |
err := r.ParseForm() |
| 60 |
if err != nil { |
| 61 |
return nil, oidc.ErrInvalidRequest().WithDescription("error parsing form").WithParent(err) |
| 62 |
} |
| 63 |
req := new(oidc.EndSessionRequest) |
| 64 |
err = decoder.Decode(req, r.Form) |
| 65 |
if err != nil { |
| 66 |
return nil, oidc.ErrInvalidRequest().WithDescription("error decoding form").WithParent(err) |
| 67 |
} |
| 68 |
return req, nil |
| 69 |
} |
| 70 |
|
| 71 |
func ValidateEndSessionRequest(ctx context.Context, req *oidc.EndSessionRequest, ender SessionEnder) (*EndSessionRequest, error) { |
| 72 |
ctx, span := Tracer.Start(ctx, "ValidateEndSessionRequest") |
| 73 |
defer span.End() |
| 74 |
|
| 75 |
session := &EndSessionRequest{ |
| 76 |
RedirectURI: ender.DefaultLogoutRedirectURI(), |
| 77 |
LogoutHint: req.LogoutHint, |
| 78 |
UILocales: req.UILocales, |
| 79 |
} |
| 80 |
if req.IdTokenHint != "" { |
| 81 |
claims, err := VerifyIDTokenHint[*oidc.IDTokenClaims](ctx, req.IdTokenHint, ender.IDTokenHintVerifier(ctx)) |
| 82 |
if err != nil && !errors.As(err, &IDTokenHintExpiredError{}) { |
| 83 |
return nil, oidc.ErrInvalidRequest().WithDescription("id_token_hint invalid").WithParent(err) |
| 84 |
} |
| 85 |
session.UserID = claims.GetSubject() |
| 86 |
session.IDTokenHintClaims = claims |
| 87 |
if req.ClientID != "" && req.ClientID != claims.GetAuthorizedParty() { |
| 88 |
return nil, oidc.ErrInvalidRequest().WithDescription("client_id does not match azp of id_token_hint") |
| 89 |
} |
| 90 |
req.ClientID = claims.GetAuthorizedParty() |
| 91 |
} |
| 92 |
if req.ClientID != "" { |
| 93 |
client, err := ender.Storage().GetClientByClientID(ctx, req.ClientID) |
| 94 |
if err != nil { |
| 95 |
return nil, oidc.DefaultToServerError(err, "") |
| 96 |
} |
| 97 |
session.ClientID = client.GetID() |
| 98 |
if req.PostLogoutRedirectURI != "" { |
| 99 |
if err := ValidateEndSessionPostLogoutRedirectURI(req.PostLogoutRedirectURI, client); err != nil { |
| 100 |
return nil, err |
| 101 |
} |
| 102 |
session.RedirectURI = req.PostLogoutRedirectURI |
| 103 |
} |
| 104 |
} |
| 105 |
if req.State != "" { |
| 106 |
redirect, err := url.Parse(session.RedirectURI) |
| 107 |
if err != nil { |
| 108 |
return nil, oidc.DefaultToServerError(err, "") |
| 109 |
} |
| 110 |
session.RedirectURI = mergeQueryParams(redirect, url.Values{"state": {req.State}}) |
| 111 |
} |
| 112 |
return session, nil |
| 113 |
} |
| 114 |
|
| 115 |
func ValidateEndSessionPostLogoutRedirectURI(postLogoutRedirectURI string, client Client) error { |
| 116 |
for _, uri := range client.PostLogoutRedirectURIs() { |
| 117 |
if uri == postLogoutRedirectURI { |
| 118 |
return nil |
| 119 |
} |
| 120 |
} |
| 121 |
// For native clients, RFC 8252 section 7.3 allows the use of dynamic ports |
| 122 |
// on the loopback interface (127.0.0.1 / ::1 / localhost). Match the incoming |
| 123 |
// post_logout_redirect_uri against registered loopback URIs while ignoring the port. |
| 124 |
if client.ApplicationType() == ApplicationTypeNative { |
| 125 |
if parsedURL, isLoopback := HTTPLoopbackOrLocalhost(postLogoutRedirectURI); isLoopback { |
| 126 |
for _, uri := range client.PostLogoutRedirectURIs() { |
| 127 |
registeredURL, ok := HTTPLoopbackOrLocalhost(uri) |
| 128 |
if ok && equalURI(parsedURL, registeredURL) { |
| 129 |
return nil |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
if globClient, ok := client.(HasRedirectGlobs); ok { |
| 135 |
for _, uriGlob := range globClient.PostLogoutRedirectURIGlobs() { |
| 136 |
isMatch, err := path.Match(uriGlob, postLogoutRedirectURI) |
| 137 |
if err != nil { |
| 138 |
return oidc.ErrServerError().WithParent(err) |
| 139 |
} |
| 140 |
if isMatch { |
| 141 |
return nil |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
return oidc.ErrInvalidRequest().WithDescription("post_logout_redirect_uri invalid") |
| 146 |
} |