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
|
|
|
"html/template"
|
2021-09-11 11:35:15 +00:00
|
|
|
"net/http"
|
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
client "github.com/ory/hydra-client-go/v2"
|
2021-09-11 11:35:15 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-07-29 20:00:53 +00:00
|
|
|
|
|
|
|
"code.cacert.org/cacert/oidc-idp/internal/services"
|
2021-09-11 11:35:15 +00:00
|
|
|
)
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
type LogoutHandler struct {
|
2023-08-03 21:49:57 +00:00
|
|
|
adminClient client.OAuth2Api
|
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) {
|
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
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
logoutRequest, response, err := h.adminClient.GetOAuth2LogoutRequest(
|
|
|
|
r.Context(),
|
|
|
|
).LogoutChallenge(challenge).Execute()
|
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-08-03 21:49:57 +00:00
|
|
|
defer func() { _ = response.Body.Close() }()
|
|
|
|
|
|
|
|
h.logger.WithFields(
|
|
|
|
log.Fields{"response": response.Status, "logout_request": logoutRequest},
|
|
|
|
).Debug("got response for GetOAuth2LogoutRequest")
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
acceptLogoutRequest, response, err := h.adminClient.AcceptOAuth2LogoutRequest(
|
|
|
|
r.Context(),
|
|
|
|
).LogoutChallenge(challenge).Execute()
|
2021-09-11 11:35:15 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
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())
|
2021-09-11 11:35:15 +00:00
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
func NewLogout(logger *log.Logger, adminClient client.OAuth2Api) *LogoutHandler {
|
2023-05-13 11:27:19 +00:00
|
|
|
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-08-07 13:15:45 +00:00
|
|
|
logger *log.Logger
|
|
|
|
trans *services.I18NService
|
|
|
|
templates TemplateCache
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
localizer := getLocalizer(h.trans, r)
|
2023-07-24 19:09:35 +00:00
|
|
|
|
2023-08-07 13:15:45 +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,
|
|
|
|
)),
|
|
|
|
})
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
func NewLogoutSuccess(
|
2023-07-24 19:09:35 +00:00
|
|
|
logger *log.Logger,
|
2023-08-07 13:15:45 +00:00
|
|
|
templateCache TemplateCache,
|
|
|
|
trans *services.I18NService,
|
2023-07-24 19:09:35 +00:00
|
|
|
) *LogoutSuccessHandler {
|
2023-08-07 13:15:45 +00:00
|
|
|
return &LogoutSuccessHandler{logger: logger, trans: trans, templates: templateCache}
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|