Jan Dittberner
26bfcc225e
- switch to structured logging - use JSON formatter - support log level and formatter configuration
125 lines
3.6 KiB
Go
125 lines
3.6 KiB
Go
/*
|
|
Copyright 2020-2023 CAcert Inc.
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
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
|
|
|
|
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 (
|
|
"bytes"
|
|
"html/template"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
"github.com/ory/hydra-client-go/client/admin"
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"code.cacert.org/cacert/oidc_idp/internal/services"
|
|
"code.cacert.org/cacert/oidc_idp/ui"
|
|
)
|
|
|
|
type LogoutHandler struct {
|
|
adminClient admin.ClientService
|
|
logger *log.Logger
|
|
}
|
|
|
|
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(
|
|
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)
|
|
|
|
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)
|
|
}
|
|
|
|
func NewLogoutHandler(logger *log.Logger, adminClient admin.ClientService) *LogoutHandler {
|
|
return &LogoutHandler{
|
|
logger: logger,
|
|
adminClient: adminClient,
|
|
}
|
|
}
|
|
|
|
type LogoutSuccessHandler struct {
|
|
bundle *i18n.Bundle
|
|
logger *log.Logger
|
|
messageCatalog *services.MessageCatalog
|
|
template *template.Template
|
|
}
|
|
|
|
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())
|
|
}
|
|
|
|
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",
|
|
)),
|
|
}
|
|
}
|