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"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/lestrrat-go/jwx/jwk"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
2023-07-30 14:48:26 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"code.cacert.org/cacert/oidc-demo-app/ui"
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:53:26 +00:00
|
|
|
"code.cacert.org/cacert/oidc-demo-app/internal/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
|
2023-07-30 14:48:26 +00:00
|
|
|
logger *log.Logger
|
2021-09-11 11:37:31 +00:00
|
|
|
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 (
|
2023-07-30 14:48:26 +00:00
|
|
|
accessToken string
|
|
|
|
refreshToken string
|
|
|
|
idToken string
|
|
|
|
ok bool
|
2023-07-29 15:46:33 +00:00
|
|
|
)
|
|
|
|
|
2023-07-30 14:48:26 +00:00
|
|
|
if accessToken, ok = session.Values[sessionKeyAccessToken].(string); ok {
|
|
|
|
h.logger.WithField("access_token", accessToken).Info("found access token in session")
|
|
|
|
}
|
|
|
|
|
|
|
|
if refreshToken, ok = session.Values[refreshToken].(string); ok {
|
|
|
|
h.logger.WithField("refresh_token", refreshToken).Info("found refresh token in session")
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-30 14:48:26 +00:00
|
|
|
expires := oidcToken.Expiration()
|
|
|
|
|
|
|
|
h.logger.WithField("expires", expires).Info("id token expires at")
|
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
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(),
|
2023-07-30 14:48:26 +00:00
|
|
|
"AuthenticatedAs": msgLookup("AuthenticatedAs", map[string]interface{}{
|
|
|
|
"Name": oidcToken.Name(),
|
|
|
|
"Email": oidcToken.Email(),
|
|
|
|
}, localizer),
|
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(
|
2023-07-30 14:48:26 +00:00
|
|
|
logger *log.Logger,
|
2023-07-29 15:46:33 +00:00
|
|
|
bundle *i18n.Bundle,
|
|
|
|
catalog *services.MessageCatalog,
|
|
|
|
oidcInfo *services.OIDCInformation,
|
|
|
|
publicURL string,
|
|
|
|
) (*IndexHandler, error) {
|
|
|
|
indexTemplate, err := template.ParseFS(
|
2023-07-30 14:48:26 +00:00
|
|
|
ui.Templates,
|
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{
|
2023-07-30 14:48:26 +00:00
|
|
|
logger: logger,
|
2023-07-29 15:46:33 +00:00
|
|
|
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
|
|
|
|
}
|