2019-04-25 04:52:08 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-13 15:38:12 +07:00
|
|
|
"net/url"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
2021-04-13 15:38:12 +07:00
|
|
|
type logoutBody struct {
|
|
|
|
TargetURL string `json:"targetURL"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type logoutResponseBody struct {
|
|
|
|
SafeTargetURL bool `json:"safeTargetURL"`
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
// LogoutPOST is the handler logging out the user attached to the given cookie.
|
|
|
|
func LogoutPOST(ctx *middlewares.AutheliaCtx) {
|
2021-04-13 15:38:12 +07:00
|
|
|
body := logoutBody{}
|
|
|
|
responseBody := logoutResponseBody{SafeTargetURL: false}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-04-13 15:38:12 +07:00
|
|
|
err := ctx.ParseBody(&body)
|
|
|
|
if err != nil {
|
2021-09-17 12:53:40 +07:00
|
|
|
ctx.Error(fmt.Errorf("unable to parse body during logout: %s", err), messageOperationFailed)
|
2021-04-13 15:38:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ctx.Providers.SessionProvider.DestroySession(ctx.RequestCtx)
|
2019-04-25 04:52:08 +07:00
|
|
|
if err != nil {
|
2021-09-17 12:53:40 +07:00
|
|
|
ctx.Error(fmt.Errorf("unable to destroy session during logout: %s", err), messageOperationFailed)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2021-04-13 15:38:12 +07:00
|
|
|
redirectionURL, err := url.Parse(body.TargetURL)
|
|
|
|
if err == nil {
|
2022-08-07 18:13:56 +07:00
|
|
|
responseBody.SafeTargetURL = utils.URLDomainHasSuffix(*redirectionURL, ctx.Configuration.Session.Domain)
|
2021-04-13 15:38:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if body.TargetURL != "" {
|
|
|
|
ctx.Logger.Debugf("Logout target url is %s, safe %t", body.TargetURL, responseBody.SafeTargetURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ctx.SetJSONBody(responseBody)
|
|
|
|
if err != nil {
|
2021-09-17 12:53:40 +07:00
|
|
|
ctx.Error(fmt.Errorf("unable to set body during logout: %s", err), messageOperationFailed)
|
2021-04-13 15:38:12 +07:00
|
|
|
}
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|