2021-01-27 22:22:54 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020, 2021 Jan Dittberner
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2021-01-01 09:20:49 +01:00
|
|
|
"context"
|
2020-12-31 13:19:21 +01:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2020-12-31 19:11:06 +01:00
|
|
|
"github.com/lestrrat-go/jwx/jwk"
|
2021-01-01 09:20:49 +01:00
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
2020-12-31 19:11:06 +01:00
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
"git.cacert.org/oidc_login/app/services"
|
2021-01-01 09:20:49 +01:00
|
|
|
commonServices "git.cacert.org/oidc_login/common/services"
|
2020-12-31 13:19:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type indexHandler struct {
|
2021-01-01 09:20:49 +01:00
|
|
|
bundle *i18n.Bundle
|
|
|
|
indexTemplate *template.Template
|
2021-06-26 13:14:23 +02:00
|
|
|
keySet jwk.Set
|
2021-01-01 09:20:49 +01:00
|
|
|
logoutUrl string
|
|
|
|
messageCatalog *commonServices.MessageCatalog
|
|
|
|
serverAddr string
|
2020-12-31 13:19:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *indexHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
if request.Method != http.MethodGet {
|
|
|
|
http.Error(writer, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if request.URL.Path != "/" {
|
|
|
|
http.NotFound(writer, request)
|
|
|
|
return
|
|
|
|
}
|
2021-01-01 09:20:49 +01:00
|
|
|
accept := request.Header.Get("Accept-Language")
|
|
|
|
localizer := i18n.NewLocalizer(h.bundle, accept)
|
2020-12-31 13:19:21 +01:00
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
session, err := services.GetSessionStore().Get(request, sessionName)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logoutUrl, err := url.Parse(h.logoutUrl)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2020-12-31 19:11:06 +01:00
|
|
|
var idToken string
|
2020-12-31 13:19:21 +01:00
|
|
|
var ok bool
|
2020-12-31 19:11:06 +01:00
|
|
|
if idToken, ok = session.Values[sessionKeyIdToken].(string); ok {
|
2020-12-31 13:19:21 +01:00
|
|
|
logoutUrl.RawQuery = url.Values{
|
|
|
|
"id_token_hint": []string{idToken},
|
|
|
|
"post_logout_redirect_uri": []string{fmt.Sprintf("https://%s/after-logout", h.serverAddr)},
|
|
|
|
}.Encode()
|
2020-12-31 19:11:06 +01:00
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
oidcToken, err := ParseIdToken(idToken, h.keySet)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2020-12-31 13:19:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Add("Content-Type", "text/html")
|
2021-01-01 09:20:49 +01:00
|
|
|
err = h.indexTemplate.Lookup("base").Execute(writer, map[string]interface{}{
|
|
|
|
"Title": h.messageCatalog.LookupMessage("IndexTitle", nil, localizer),
|
|
|
|
"Greeting": h.messageCatalog.LookupMessage("IndexGreeting", map[string]interface{}{
|
|
|
|
"User": oidcToken.Name(),
|
|
|
|
}, localizer),
|
|
|
|
"IntroductionText": h.messageCatalog.LookupMessage("IndexIntroductionText", nil, localizer),
|
|
|
|
"LogoutLabel": h.messageCatalog.LookupMessage("LogoutLabel", nil, localizer),
|
|
|
|
"LogoutURL": logoutUrl.String(),
|
2020-12-31 13:19:21 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 09:20:49 +01:00
|
|
|
func NewIndexHandler(ctx context.Context, serverAddr string) (*indexHandler, error) {
|
|
|
|
indexTemplate, err := template.ParseFiles(
|
|
|
|
"templates/app/base.gohtml", "templates/app/index.gohtml")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &indexHandler{
|
|
|
|
bundle: commonServices.GetI18nBundle(ctx),
|
|
|
|
indexTemplate: indexTemplate,
|
|
|
|
keySet: commonServices.GetJwkSet(ctx),
|
|
|
|
logoutUrl: commonServices.GetOidcConfig(ctx).EndSessionEndpoint,
|
|
|
|
messageCatalog: commonServices.GetMessageCatalog(ctx),
|
|
|
|
serverAddr: serverAddr,
|
|
|
|
}, nil
|
2020-12-31 13:19:21 +01:00
|
|
|
}
|