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
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/middlewares"
|
2021-04-13 08:38:12 +00:00
|
|
|
"github.com/authelia/authelia/internal/utils"
|
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"`
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
// LogoutPost is the handler logging out the user attached to the given cookie.
|
|
|
|
func LogoutPost(ctx *middlewares.AutheliaCtx) {
|
2021-04-13 08:38:12 +00:00
|
|
|
body := logoutBody{}
|
|
|
|
responseBody := logoutResponseBody{SafeTargetURL: false}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2021-04-13 08:38:12 +00:00
|
|
|
ctx.Logger.Tracef("Attempting to decode body")
|
|
|
|
|
|
|
|
err := ctx.ParseBody(&body)
|
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("Unable to parse body during logout: %s", err), messageOperationFailed)
|
2021-04-13 08:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Logger.Tracef("Attempting to destroy session")
|
|
|
|
|
|
|
|
err = ctx.Providers.SessionProvider.DestroySession(ctx.RequestCtx)
|
2019-04-24 21:52:08 +00:00
|
|
|
if err != nil {
|
2021-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("Unable to destroy session during logout: %s", err), messageOperationFailed)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 08:38:12 +00:00
|
|
|
redirectionURL, err := url.Parse(body.TargetURL)
|
|
|
|
if err == nil {
|
|
|
|
responseBody.SafeTargetURL = utils.IsRedirectionSafe(*redirectionURL, ctx.Configuration.Session.Domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-22 03:52:37 +00:00
|
|
|
ctx.Error(fmt.Errorf("Unable to set body during logout: %s", err), messageOperationFailed)
|
2021-04-13 08:38:12 +00:00
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|