2021-09-11 11:37:31 +00:00
|
|
|
/*
|
2024-05-12 08:20:06 +00:00
|
|
|
Copyright CAcert Inc.
|
2023-07-29 15:46:33 +00:00
|
|
|
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 (
|
|
|
|
"encoding/base64"
|
2023-07-29 15:46:33 +00:00
|
|
|
"fmt"
|
2024-05-12 10:02:27 +00:00
|
|
|
"log/slog"
|
2021-09-11 11:37:31 +00:00
|
|
|
"net/http"
|
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
2023-07-29 15:46:33 +00:00
|
|
|
"golang.org/x/oauth2"
|
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
|
|
|
const (
|
|
|
|
oauth2RedirectStateLength = 8
|
2023-08-03 14:46:28 +00:00
|
|
|
sessionName = "resource_app"
|
2023-07-29 15:46:33 +00:00
|
|
|
)
|
|
|
|
|
2024-05-12 10:02:27 +00:00
|
|
|
func Authenticate(logger *slog.Logger, oauth2Config *oauth2.Config) func(http.Handler) http.Handler {
|
2021-09-11 11:37:31 +00:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-08-03 14:46:28 +00:00
|
|
|
session, err := GetSession(r)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
2024-05-12 10:02:27 +00:00
|
|
|
logger.ErrorContext(r.Context(), "failed to get session", "error", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), 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
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
if _, ok := session.Values[services.SessionIDToken]; ok {
|
2021-09-11 11:37:31 +00:00
|
|
|
next.ServeHTTP(w, r)
|
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
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
session.Values[services.SessionRedirectTarget] = r.URL.String()
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
if err = session.Save(r, w); err != nil {
|
2024-05-12 10:02:27 +00:00
|
|
|
logger.ErrorContext(r.Context(), "failed to save session", "error", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), 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
|
|
|
|
2024-05-12 10:02:27 +00:00
|
|
|
state, err := services.GenerateKey(oauth2RedirectStateLength)
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorContext(
|
|
|
|
r.Context(), "failed to generate state for starting OIDC flow", "error", err,
|
|
|
|
)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
authURL := oauth2Config.AuthCodeURL(base64.URLEncoding.EncodeToString(state))
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2023-08-03 21:54:03 +00:00
|
|
|
w.Header().Set("Location", authURL)
|
2021-09-11 11:37:31 +00:00
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
func GetSession(r *http.Request) (*sessions.Session, error) {
|
|
|
|
session, err := services.GetSessionStore().Get(r, sessionName)
|
2024-05-12 10:02:27 +00:00
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get session")
|
2023-07-29 15:46:33 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type I18NHandler interface {
|
|
|
|
GetBundle() *i18n.Bundle
|
|
|
|
GetCatalog() *services.MessageCatalog
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLocalizer(h I18NHandler, r *http.Request) *i18n.Localizer {
|
|
|
|
accept := r.Header.Get("Accept-Language")
|
|
|
|
localizer := i18n.NewLocalizer(h.GetBundle(), accept)
|
|
|
|
|
|
|
|
return localizer
|
|
|
|
}
|
|
|
|
|
|
|
|
func BaseTemplateData(h I18NHandler, localizer *i18n.Localizer) map[string]interface{} {
|
|
|
|
msg := h.GetCatalog().LookupMessage
|
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"WelcomeNavLabel": msg("IndexNavLabel", nil, localizer),
|
|
|
|
"ProtectedNavLabel": msg("ProtectedNavLabel", nil, localizer),
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
return data
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|