oidc-idp/internal/handlers/logout.go

126 lines
3.6 KiB
Go
Raw Normal View History

/*
2023-05-13 11:27:19 +00:00
Copyright 2020-2023 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
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
https://www.apache.org/licenses/LICENSE-2.0
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.
*/
package handlers
import (
2023-07-24 19:09:35 +00:00
"bytes"
"html/template"
"net/http"
"time"
2023-07-24 19:09:35 +00:00
"github.com/nicksnyder/go-i18n/v2/i18n"
"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"
)
2023-05-13 11:27:19 +00:00
type LogoutHandler struct {
adminClient admin.ClientService
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
challenge := r.URL.Query().Get("logout_challenge")
h.logger.WithField("challenge", challenge).Debug("received logout challenge")
logoutRequest, err := h.adminClient.GetLogoutRequest(
2023-05-13 11:27:19 +00:00
admin.NewGetLogoutRequestParams().WithLogoutChallenge(challenge).WithTimeout(Ten))
if err != nil {
h.logger.WithError(err).Error("error getting logout requests")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
2023-05-13 11:27:19 +00:00
return
}
h.logger.WithField("logout_request", logoutRequest.Payload).Debug("received logout request")
acceptLogoutRequest, err := h.adminClient.AcceptLogoutRequest(
admin.NewAcceptLogoutRequestParams().WithLogoutChallenge(challenge))
if err != nil {
h.logger.WithError(err).Error("accept logout request failed")
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{
logger: logger,
2023-05-13 11:27:19 +00:00
adminClient: adminClient,
}
}
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
}
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())
}
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",
)),
}
}