oidc-idp/internal/handlers/logout.go

112 lines
3.2 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
"html/template"
"net/http"
client "github.com/ory/hydra-client-go/v2"
log "github.com/sirupsen/logrus"
2023-07-29 20:00:53 +00:00
"code.cacert.org/cacert/oidc-idp/internal/services"
)
2023-05-13 11:27:19 +00:00
type LogoutHandler struct {
2024-05-11 20:27:22 +00:00
adminClient client.OAuth2API
logger *log.Logger
}
2023-05-13 11:27:19 +00:00
func (h *LogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
challenge := r.URL.Query().Get("logout_challenge")
h.logger.WithField("challenge", challenge).Debug("received logout challenge")
logoutRequest, response, err := h.adminClient.GetOAuth2LogoutRequest(
r.Context(),
).LogoutChallenge(challenge).Execute()
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
}
defer func() { _ = response.Body.Close() }()
h.logger.WithFields(
log.Fields{"response": response.Status, "logout_request": logoutRequest},
).Debug("got response for GetOAuth2LogoutRequest")
acceptLogoutRequest, response, err := h.adminClient.AcceptOAuth2LogoutRequest(
r.Context(),
).LogoutChallenge(challenge).Execute()
if err != nil {
h.logger.WithError(err).Error("accept logout request failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
defer func() { _ = response.Body.Close() }()
h.logger.WithFields(
log.Fields{"response": response.Status, "accept_logout_request": acceptLogoutRequest},
).Debug("got response for AcceptOAuth2LogoutRequest")
w.Header().Set("Location", acceptLogoutRequest.GetRedirectTo())
w.WriteHeader(http.StatusFound)
}
2024-05-11 20:27:22 +00:00
func NewLogout(logger *log.Logger, adminClient client.OAuth2API) *LogoutHandler {
2023-05-13 11:27:19 +00:00
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 {
logger *log.Logger
trans *services.I18NService
templates TemplateCache
}
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
}
localizer := getLocalizer(h.trans, r)
2023-07-24 19:09:35 +00:00
h.templates.render(h.logger, w, LogoutSuccessful, map[string]interface{}{
"Title": h.trans.LookupMessage("LogoutSuccessfulTitle", nil, localizer),
"Explanation": template.HTML(h.trans.LookupMarkdownMessage( //nolint:gosec
2023-07-24 19:09:35 +00:00
"LogoutSuccessfulText",
nil,
localizer,
)),
})
}
func NewLogoutSuccess(
2023-07-24 19:09:35 +00:00
logger *log.Logger,
templateCache TemplateCache,
trans *services.I18NService,
2023-07-24 19:09:35 +00:00
) *LogoutSuccessHandler {
return &LogoutSuccessHandler{logger: logger, trans: trans, templates: templateCache}
}