2019-04-24 21:52:08 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-13 08:38:12 +00:00
|
|
|
"net/url"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2021-04-13 08:38:12 +00:00
|
|
|
type logoutBody struct {
|
|
|
|
TargetURL string `json:"targetURL"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type logoutResponseBody struct {
|
|
|
|
SafeTargetURL bool `json:"safeTargetURL"`
|
|
|
|
}
|
|
|
|
|
2022-04-08 04:13:47 +00:00
|
|
|
// LogoutPOST is the handler logging out the user attached to the given cookie.
|
|
|
|
func LogoutPOST(ctx *middlewares.AutheliaCtx) {
|
2023-04-23 10:59:15 +00:00
|
|
|
var (
|
|
|
|
body logoutBody
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2021-04-13 08:38:12 +00:00
|
|
|
responseBody := logoutResponseBody{SafeTargetURL: false}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2023-04-23 10:59:15 +00:00
|
|
|
if err = ctx.ParseBody(&body); err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("unable to parse body during logout: %w", err), messageOperationFailed)
|
|
|
|
|
|
|
|
return
|
2021-04-13 08:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 10:59:15 +00:00
|
|
|
if err = ctx.DestroySession(); err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("unable to destroy session during logout: %w", err), messageOperationFailed)
|
|
|
|
|
|
|
|
return
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 01:51:02 +00:00
|
|
|
redirectionURL, err := url.ParseRequestURI(body.TargetURL)
|
2021-04-13 08:38:12 +00:00
|
|
|
if err == nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
responseBody.SafeTargetURL = ctx.IsSafeRedirectionTargetURI(redirectionURL)
|
2021-04-13 08:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if body.TargetURL != "" {
|
|
|
|
ctx.Logger.Debugf("Logout target url is %s, safe %t", body.TargetURL, responseBody.SafeTargetURL)
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:59:15 +00:00
|
|
|
if err = ctx.SetJSONBody(responseBody); err != nil {
|
|
|
|
ctx.Error(fmt.Errorf("unable to set body during logout: %w", err), messageOperationFailed)
|
2021-04-13 08:38:12 +00:00
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|