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-29 19:56:19 +00:00
|
|
|
"bytes"
|
2021-09-11 11:35:15 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-07-29 20:00:53 +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
|
|
|
)
|
|
|
|
|
|
|
|
type errorKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
errorBucketKey errorKey = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type ErrorDetails struct {
|
|
|
|
ErrorMessage string
|
|
|
|
ErrorDetails []string
|
|
|
|
ErrorCode string
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorBucket struct {
|
2023-08-07 13:15:45 +00:00
|
|
|
logger *log.Logger
|
|
|
|
trans *services.I18NService
|
|
|
|
errorDetails *ErrorDetails
|
|
|
|
templates TemplateCache
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ErrorBucket) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if b.errorDetails != nil {
|
2023-08-07 13:15:45 +00:00
|
|
|
localizer := getLocalizer(b.trans, r)
|
|
|
|
|
|
|
|
b.templates.render(b.logger, w, Error, map[string]interface{}{
|
|
|
|
"Title": b.trans.LookupMessage(
|
2021-09-11 11:35:15 +00:00
|
|
|
"ErrorTitle",
|
|
|
|
nil,
|
|
|
|
localizer,
|
|
|
|
),
|
|
|
|
"details": b.errorDetails,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetErrorBucket(r *http.Request) *ErrorBucket {
|
2023-05-13 11:27:19 +00:00
|
|
|
if bucket, ok := r.Context().Value(errorBucketKey).(*ErrorBucket); ok {
|
|
|
|
return bucket
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
// AddError can be called to add error details from your application's handler.
|
2021-09-11 11:35:15 +00:00
|
|
|
func (b *ErrorBucket) AddError(details *ErrorDetails) {
|
|
|
|
b.errorDetails = details
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
2023-05-13 11:27:19 +00:00
|
|
|
errorBucket *ErrorBucket
|
|
|
|
statusCode int
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *errorResponseWriter) WriteHeader(code int) {
|
|
|
|
w.statusCode = code
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
if code >= http.StatusBadRequest {
|
2021-09-11 11:35:15 +00:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
w.errorBucket.AddError(&ErrorDetails{ErrorCode: "HTTP error"})
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
w.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *errorResponseWriter) Write(content []byte) (int, error) {
|
2023-05-13 11:27:19 +00:00
|
|
|
if w.statusCode >= http.StatusBadRequest {
|
|
|
|
if w.errorBucket.errorDetails.ErrorDetails == nil {
|
|
|
|
w.errorBucket.errorDetails.ErrorDetails = make([]string, 0)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
w.errorBucket.errorDetails.ErrorDetails = append(
|
|
|
|
w.errorBucket.errorDetails.ErrorDetails, string(content),
|
|
|
|
)
|
|
|
|
|
|
|
|
return len(content), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
code, err := w.ResponseWriter.Write(content)
|
|
|
|
if err != nil {
|
|
|
|
return code, fmt.Errorf("error writing response: %w", err)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
return code, nil
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func ErrorHandling(
|
|
|
|
logger *log.Logger,
|
2023-08-07 13:15:45 +00:00
|
|
|
templateCache TemplateCache,
|
|
|
|
trans *services.I18NService,
|
2023-05-13 11:27:19 +00:00
|
|
|
) (func(http.Handler) http.Handler, error) {
|
2021-09-11 11:35:15 +00:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
errorBucket := &ErrorBucket{
|
2023-08-07 13:15:45 +00:00
|
|
|
logger: logger,
|
|
|
|
trans: trans,
|
|
|
|
templates: templateCache,
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
next.ServeHTTP(
|
2023-05-13 11:27:19 +00:00
|
|
|
&errorResponseWriter{w, errorBucket, http.StatusOK},
|
|
|
|
r.WithContext(context.WithValue(r.Context(), errorBucketKey, errorBucket)),
|
2021-09-11 11:35:15 +00:00
|
|
|
)
|
|
|
|
errorBucket.serveHTTP(w, r)
|
|
|
|
})
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
type ErrorHandler struct {
|
2023-08-07 13:15:45 +00:00
|
|
|
logger *log.Logger
|
|
|
|
trans *services.I18NService
|
|
|
|
template *template.Template
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 19:56:19 +00:00
|
|
|
func (h *ErrorHandler) 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-29 19:56:19 +00:00
|
|
|
|
|
|
|
errorName := r.URL.Query().Get("error")
|
|
|
|
errorDescription := r.URL.Query().Get("error_description")
|
|
|
|
|
|
|
|
h.logger.WithFields(log.Fields{
|
|
|
|
"error_name": errorName,
|
|
|
|
"error_description": errorDescription,
|
|
|
|
}).Debug("error from Hydra")
|
|
|
|
|
|
|
|
rendered := bytes.NewBuffer(make([]byte, 0))
|
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
msg := h.trans.LookupMessage
|
|
|
|
msgMarkdown := h.trans.LookupMarkdownMessage
|
2023-07-29 19:56:19 +00:00
|
|
|
|
|
|
|
err := h.template.Lookup("base").Execute(rendered, map[string]interface{}{
|
2023-07-29 20:00:53 +00:00
|
|
|
"Title": msg("AuthServerErrorTitle", nil, localizer),
|
|
|
|
"Explanation": template.HTML( //nolint:gosec
|
|
|
|
msgMarkdown("AuthServerErrorExplanation", nil, localizer),
|
|
|
|
),
|
2023-07-29 19:56:19 +00:00
|
|
|
"ErrorMessage": errorDescription,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.logger.WithError(err).Error("template rendering failed")
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Pragma", "no-cache")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
|
|
|
|
_, _ = w.Write(rendered.Bytes())
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
func NewErrorHandler(logger *log.Logger, trans *services.I18NService) *ErrorHandler {
|
2023-07-29 19:56:19 +00:00
|
|
|
return &ErrorHandler{
|
2023-08-07 13:15:45 +00:00
|
|
|
logger: logger,
|
|
|
|
trans: trans,
|
2023-07-29 19:56:19 +00:00
|
|
|
template: template.Must(template.ParseFS(
|
|
|
|
ui.Templates,
|
|
|
|
"templates/base.gohtml",
|
|
|
|
"templates/hydra_error.gohtml"),
|
|
|
|
),
|
|
|
|
}
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|