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 (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2023-07-29 15:46:33 +00:00
|
|
|
"fmt"
|
2021-09-11 11:37:31 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
2021-09-11 11:37:31 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
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/models"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
func Authenticate(logger *log.Logger, oauth2Config *oauth2.Config, clientID string) 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 {
|
|
|
|
http.Error(w, 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
|
|
|
|
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 {
|
|
|
|
http.Error(w, 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 authURL *url.URL
|
|
|
|
|
|
|
|
if authURL, err = url.Parse(oauth2Config.Endpoint.AuthURL); err != nil {
|
2021-09-11 11:37:31 +00:00
|
|
|
http.Error(w, 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
|
|
|
|
|
|
|
queryValues := authURL.Query()
|
|
|
|
queryValues.Set("client_id", clientID)
|
2021-09-11 11:37:31 +00:00
|
|
|
queryValues.Set("response_type", "code")
|
2023-07-30 14:48:26 +00:00
|
|
|
queryValues.Set("scope", "openid profile email cacert_groups")
|
2023-07-29 15:46:33 +00:00
|
|
|
queryValues.Set("state", base64.URLEncoding.EncodeToString(services.GenerateKey(oauth2RedirectStateLength)))
|
2021-09-11 11:37:31 +00:00
|
|
|
queryValues.Set("claims", getRequestedClaims(logger))
|
2023-07-29 15:46:33 +00:00
|
|
|
authURL.RawQuery = queryValues.Encode()
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
w.Header().Set("Location", authURL.String())
|
2021-09-11 11:37:31 +00:00
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRequestedClaims(logger *log.Logger) string {
|
|
|
|
claims := make(models.OIDCClaimsRequest)
|
|
|
|
claims["userinfo"] = make(models.ClaimElement)
|
|
|
|
essentialItem := make(models.IndividualClaimRequest)
|
|
|
|
essentialItem["essential"] = true
|
2023-07-29 15:46:33 +00:00
|
|
|
claims["userinfo"]["https://auth.cacert.org/groups"] = &essentialItem
|
2021-09-11 11:37:31 +00:00
|
|
|
|
|
|
|
target := make([]byte, 0)
|
|
|
|
buf := bytes.NewBuffer(target)
|
|
|
|
enc := json.NewEncoder(buf)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
if err := enc.Encode(claims); err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
logger.WithError(err).Warn("could not encode claims request parameter")
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
func GetSession(r *http.Request) (*sessions.Session, error) {
|
|
|
|
session, err := services.GetSessionStore().Get(r, sessionName)
|
|
|
|
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
|
|
|
}
|