From 3b2820cbe3b691da4195a4a42968af8f97615567 Mon Sep 17 00:00:00 2001 From: Dmitry Buryanov <54956408+theburyat@users.noreply.github.com> Date: Mon, 18 Sep 2023 16:47:49 +0300 Subject: [PATCH] feat: make redirect_uri really optional in logout route (#2342) --- controllers/account.go | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/controllers/account.go b/controllers/account.go index 664ee8ee..9856dc43 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -309,27 +309,32 @@ func (c *ApiController) Logout() { return } - if application.IsRedirectUriValid(redirectUri) { - if user == "" { - user = util.GetId(token.Organization, token.User) - } + if user == "" { + user = util.GetId(token.Organization, token.User) + } - c.ClearUserSession() - // TODO https://github.com/casdoor/casdoor/pull/1494#discussion_r1095675265 - owner, username := util.GetOwnerAndNameFromId(user) + c.ClearUserSession() + // TODO https://github.com/casdoor/casdoor/pull/1494#discussion_r1095675265 + owner, username := util.GetOwnerAndNameFromId(user) - _, err := object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID()) - if err != nil { - c.ResponseError(err.Error()) + _, err = object.DeleteSessionId(util.GetSessionId(owner, username, object.CasdoorApplication), c.Ctx.Input.CruSession.SessionID()) + if err != nil { + c.ResponseError(err.Error()) + return + } + + util.LogInfo(c.Ctx, "API: [%s] logged out", user) + + if redirectUri == "" { + c.ResponseOk() + return + } else { + if application.IsRedirectUriValid(redirectUri) { + c.Ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?state=%s", strings.TrimRight(redirectUri, "/"), state)) + } else { + c.ResponseError(fmt.Sprintf(c.T("token:Redirect URI: %s doesn't exist in the allowed Redirect URI list"), redirectUri)) return } - - util.LogInfo(c.Ctx, "API: [%s] logged out", user) - - c.Ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?state=%s", strings.TrimRight(redirectUri, "/"), state)) - } else { - c.ResponseError(fmt.Sprintf(c.T("token:Redirect URI: %s doesn't exist in the allowed Redirect URI list"), redirectUri)) - return } } }