2021-09-11 11:37:31 +00:00
|
|
|
/*
|
2023-07-29 15:46:33 +00:00
|
|
|
Copyright 2020-2023 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +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:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +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:37:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2023-07-29 15:46:33 +00:00
|
|
|
"io/fs"
|
2021-09-11 11:37:31 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/lestrrat-go/jwx/jwk"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
"code.cacert.org/cacert/oidc-demo-app/services"
|
2021-09-11 11:37:31 +00:00
|
|
|
)
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
type IndexHandler struct {
|
2021-09-11 11:37:31 +00:00
|
|
|
bundle *i18n.Bundle
|
|
|
|
indexTemplate *template.Template
|
|
|
|
keySet jwk.Set
|
2023-07-29 15:46:33 +00:00
|
|
|
logoutURL string
|
2021-09-11 11:37:31 +00:00
|
|
|
messageCatalog *services.MessageCatalog
|
2023-07-29 15:46:33 +00:00
|
|
|
publicURL string
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
func (h *IndexHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
2021-09-11 11:37:31 +00:00
|
|
|
if request.Method != http.MethodGet {
|
|
|
|
http.Error(writer, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
if request.URL.Path != "/" {
|
|
|
|
http.NotFound(writer, request)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
accept := request.Header.Get("Accept-Language")
|
|
|
|
localizer := i18n.NewLocalizer(h.bundle, accept)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
session, err := services.GetSessionStore().Get(request, sessionName)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
logoutURL, err := url.Parse(h.logoutURL)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
idToken string
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if idToken, ok = session.Values[sessionKeyIDToken].(string); ok {
|
|
|
|
logoutURL.RawQuery = url.Values{
|
2021-09-11 11:37:31 +00:00
|
|
|
"id_token_hint": []string{idToken},
|
2023-07-29 15:46:33 +00:00
|
|
|
"post_logout_redirect_uri": []string{fmt.Sprintf("%s/after-logout", h.publicURL)},
|
2021-09-11 11:37:31 +00:00
|
|
|
}.Encode()
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
oidcToken, err := ParseIDToken(idToken, h.keySet)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Add("Content-Type", "text/html")
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
msgLookup := h.messageCatalog.LookupMessage
|
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
err = h.indexTemplate.Lookup("base").Execute(writer, map[string]interface{}{
|
2023-07-29 15:46:33 +00:00
|
|
|
"Title": msgLookup("IndexTitle", nil, localizer),
|
|
|
|
"Greeting": msgLookup("IndexGreeting", map[string]interface{}{
|
2021-09-11 11:37:31 +00:00
|
|
|
"User": oidcToken.Name(),
|
|
|
|
}, localizer),
|
2023-07-29 15:46:33 +00:00
|
|
|
"IntroductionText": msgLookup("IndexIntroductionText", nil, localizer),
|
|
|
|
"LogoutLabel": msgLookup("LogoutLabel", nil, localizer),
|
|
|
|
"LogoutURL": logoutURL.String(),
|
2021-09-11 11:37:31 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
func NewIndexHandler(
|
|
|
|
bundle *i18n.Bundle,
|
|
|
|
catalog *services.MessageCatalog,
|
|
|
|
templateFS fs.FS,
|
|
|
|
oidcInfo *services.OIDCInformation,
|
|
|
|
publicURL string,
|
|
|
|
) (*IndexHandler, error) {
|
|
|
|
indexTemplate, err := template.ParseFS(
|
|
|
|
templateFS,
|
2021-09-11 11:37:31 +00:00
|
|
|
"templates/base.gohtml", "templates/index.gohtml")
|
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
return nil, fmt.Errorf("could not parse templates: %w", err)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
return &IndexHandler{
|
|
|
|
bundle: bundle,
|
2021-09-11 11:37:31 +00:00
|
|
|
indexTemplate: indexTemplate,
|
2023-07-29 15:46:33 +00:00
|
|
|
keySet: oidcInfo.KeySet,
|
|
|
|
logoutURL: oidcInfo.OIDCConfiguration.EndSessionEndpoint,
|
|
|
|
messageCatalog: catalog,
|
|
|
|
publicURL: publicURL,
|
2021-09-11 11:37:31 +00:00
|
|
|
}, nil
|
|
|
|
}
|