2021-09-11 11:35:15 +00:00
|
|
|
/*
|
2023-05-13 11:27:19 +00:00
|
|
|
Copyright 2020-2023 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-07-18 18:37:04 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2021-09-11 11:35:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2023-07-24 19:09:35 +00:00
|
|
|
"bytes"
|
|
|
|
"html/template"
|
2021-09-11 11:35:15 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-07-24 19:09:35 +00:00
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
2021-09-11 11:35:15 +00:00
|
|
|
"github.com/ory/hydra-client-go/client/admin"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-07-24 19:09:35 +00:00
|
|
|
|
|
|
|
"code.cacert.org/cacert/oidc_idp/internal/services"
|
|
|
|
"code.cacert.org/cacert/oidc_idp/ui"
|
2021-09-11 11:35:15 +00:00
|
|
|
)
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
type LogoutHandler struct {
|
|
|
|
adminClient admin.ClientService
|
2021-09-11 11:35:15 +00:00
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func (h *LogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
const Ten = 10 * time.Second
|
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
challenge := r.URL.Query().Get("logout_challenge")
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithField("challenge", challenge).Debug("received logout challenge")
|
2021-09-11 11:35:15 +00:00
|
|
|
|
|
|
|
logoutRequest, err := h.adminClient.GetLogoutRequest(
|
2023-05-13 11:27:19 +00:00
|
|
|
admin.NewGetLogoutRequestParams().WithLogoutChallenge(challenge).WithTimeout(Ten))
|
2021-09-11 11:35:15 +00:00
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithError(err).Error("error getting logout requests")
|
2021-09-11 11:35:15 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithField("logout_request", logoutRequest.Payload).Debug("received logout request")
|
2021-09-11 11:35:15 +00:00
|
|
|
|
|
|
|
acceptLogoutRequest, err := h.adminClient.AcceptLogoutRequest(
|
|
|
|
admin.NewAcceptLogoutRequestParams().WithLogoutChallenge(challenge))
|
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithError(err).Error("accept logout request failed")
|
2021-09-11 11:35:15 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Location", *acceptLogoutRequest.GetPayload().RedirectTo)
|
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func NewLogoutHandler(logger *log.Logger, adminClient admin.ClientService) *LogoutHandler {
|
|
|
|
return &LogoutHandler{
|
2021-09-11 11:35:15 +00:00
|
|
|
logger: logger,
|
2023-05-13 11:27:19 +00:00
|
|
|
adminClient: adminClient,
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
type LogoutSuccessHandler struct {
|
2023-07-24 19:09:35 +00:00
|
|
|
bundle *i18n.Bundle
|
|
|
|
logger *log.Logger
|
|
|
|
messageCatalog *services.MessageCatalog
|
|
|
|
template *template.Template
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 19:09:35 +00:00
|
|
|
func (h *LogoutSuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accept := r.Header.Get("Accept-Language")
|
|
|
|
localizer := i18n.NewLocalizer(h.bundle, accept)
|
|
|
|
|
|
|
|
rendered := bytes.NewBuffer(make([]byte, 0))
|
|
|
|
|
|
|
|
err := h.template.Lookup("base").Execute(rendered, map[string]interface{}{
|
|
|
|
"Title": h.messageCatalog.LookupMessage("LogoutSuccessfulTitle", nil, localizer),
|
|
|
|
"Explanation": template.HTML(h.messageCatalog.LookupMarkdownMessage( //nolint:gosec
|
|
|
|
"LogoutSuccessfulText",
|
|
|
|
nil,
|
|
|
|
localizer,
|
|
|
|
)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.logger.WithError(err).Error("template rendering failed")
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = w.Write(rendered.Bytes())
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 19:09:35 +00:00
|
|
|
func NewLogoutSuccessHandler(
|
|
|
|
logger *log.Logger,
|
|
|
|
bundle *i18n.Bundle,
|
|
|
|
messageCatalog *services.MessageCatalog,
|
|
|
|
) *LogoutSuccessHandler {
|
|
|
|
return &LogoutSuccessHandler{
|
|
|
|
bundle: bundle,
|
|
|
|
logger: logger,
|
|
|
|
messageCatalog: messageCatalog,
|
|
|
|
template: template.Must(template.ParseFS(ui.Templates,
|
|
|
|
"templates/base.gohtml",
|
|
|
|
"templates/logout_successful.gohtml",
|
|
|
|
)),
|
|
|
|
}
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|