2021-09-11 11:35:15 +00:00
|
|
|
/*
|
2023-05-13 11:27:19 +00:00
|
|
|
Copyright 2020-2023 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +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:35:15 +00:00
|
|
|
|
2023-07-18 18:37:04 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +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:35:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-05-13 11:27:19 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-09-11 11:35:15 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-playground/form/v4"
|
|
|
|
"github.com/gorilla/csrf"
|
2023-08-03 21:49:57 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2021-09-11 11:35:15 +00:00
|
|
|
"github.com/lestrrat-go/jwx/jwt/openid"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
2023-08-03 21:49:57 +00:00
|
|
|
client "github.com/ory/hydra-client-go/v2"
|
2021-09-11 11:35:15 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-07-29 19:15:11 +00:00
|
|
|
"code.cacert.org/cacert/oidc-idp/internal/models"
|
|
|
|
"code.cacert.org/cacert/oidc-idp/internal/services"
|
2021-09-11 11:35:15 +00:00
|
|
|
)
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
type ConsentHandler struct {
|
2023-08-07 13:15:45 +00:00
|
|
|
logger *log.Logger
|
|
|
|
trans *services.I18NService
|
2024-05-11 20:27:22 +00:00
|
|
|
adminClient client.OAuth2API
|
2023-08-07 13:15:45 +00:00
|
|
|
templates TemplateCache
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConsentInformation struct {
|
|
|
|
GrantedScopes []string `form:"scope"`
|
|
|
|
SelectedClaims []string `form:"claims"`
|
2023-08-07 15:38:57 +00:00
|
|
|
ConsentAction string `form:"consent"`
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserInfo struct {
|
|
|
|
Email string
|
|
|
|
EmailVerified bool
|
|
|
|
CommonName string
|
|
|
|
}
|
|
|
|
|
|
|
|
var supportedScopes, supportedClaims map[string]*i18n.Message
|
|
|
|
|
|
|
|
const (
|
|
|
|
ScopeOpenID = "openid"
|
|
|
|
ScopeOffline = "offline"
|
|
|
|
ScopeOfflineAccess = "offline_access"
|
|
|
|
ScopeProfile = "profile"
|
|
|
|
ScopeEmail = "email"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
supportedScopes = make(map[string]*i18n.Message)
|
|
|
|
supportedScopes[ScopeOpenID] = &i18n.Message{
|
|
|
|
ID: "Scope-openid-Description",
|
|
|
|
Other: "Request information about your identity.",
|
|
|
|
}
|
|
|
|
supportedScopes[ScopeOffline] = &i18n.Message{
|
|
|
|
ID: "Scope-offline-Description",
|
|
|
|
Other: "Keep access to your information until you revoke the permission.",
|
|
|
|
}
|
|
|
|
supportedScopes[ScopeOfflineAccess] = supportedScopes[ScopeOffline]
|
|
|
|
supportedScopes[ScopeProfile] = &i18n.Message{
|
|
|
|
ID: "Scope-profile-Description",
|
|
|
|
Other: "Access your user profile information (your name).",
|
|
|
|
}
|
|
|
|
supportedScopes[ScopeEmail] = &i18n.Message{
|
|
|
|
ID: "Scope-email-Description",
|
|
|
|
Other: "Access your email address.",
|
|
|
|
}
|
|
|
|
|
|
|
|
supportedClaims = make(map[string]*i18n.Message)
|
|
|
|
supportedClaims[openid.SubjectKey] = nil
|
|
|
|
supportedClaims[openid.EmailKey] = nil
|
|
|
|
supportedClaims[openid.EmailVerifiedKey] = nil
|
|
|
|
supportedClaims[openid.GivenNameKey] = nil
|
|
|
|
supportedClaims[openid.FamilyNameKey] = nil
|
|
|
|
supportedClaims[openid.MiddleNameKey] = nil
|
|
|
|
supportedClaims[openid.NameKey] = nil
|
|
|
|
supportedClaims[openid.BirthdateKey] = nil
|
|
|
|
supportedClaims[openid.ZoneinfoKey] = nil
|
|
|
|
supportedClaims[openid.LocaleKey] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *UserInfo) GetFullName() string {
|
|
|
|
return i.CommonName
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func (h *ConsentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-09-11 11:35:15 +00:00
|
|
|
challenge := r.URL.Query().Get("consent_challenge")
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithField("consent_challenge", challenge).Debug("received consent challenge")
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
localizer := getLocalizer(h.trans, r)
|
2021-09-11 11:35:15 +00:00
|
|
|
|
|
|
|
// retrieve consent information
|
|
|
|
consentData, requestedClaims, err := h.getRequestedConsentInformation(challenge, r)
|
|
|
|
if err != nil {
|
|
|
|
// error is already handled in getRequestConsentInformation
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
session, err := GetSession(r)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.WithError(err).Error("could get session for request")
|
|
|
|
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2023-08-03 21:49:57 +00:00
|
|
|
h.handleGet(w, r, consentData, requestedClaims, session, challenge, localizer)
|
|
|
|
case http.MethodPost:
|
|
|
|
h.handlePost(w, r, consentData, requestedClaims, session, challenge)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ConsentHandler) handleGet(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
consentData *client.OAuth2ConsentRequest,
|
|
|
|
requestedClaims *models.OIDCClaimsRequest,
|
|
|
|
session *sessions.Session,
|
|
|
|
challenge string,
|
|
|
|
localizer *i18n.Localizer,
|
|
|
|
) {
|
|
|
|
// Hydra has a previous session for this user and client
|
|
|
|
if consentData.GetSkip() {
|
|
|
|
consentRequest, err := h.handleExistingConsent(consentData, requestedClaims, session)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.WithError(err).Error("could not handle existing consent")
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
err = h.acceptConsent(w, r, challenge, consentRequest)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.WithError(err).Error("could not accept consent")
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
h.renderConsentForm(w, r, consentData, requestedClaims, localizer)
|
2023-08-03 21:49:57 +00:00
|
|
|
}
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
func (h *ConsentHandler) handlePost(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
consentData *client.OAuth2ConsentRequest,
|
|
|
|
requestedClaims *models.OIDCClaimsRequest,
|
|
|
|
session *sessions.Session,
|
|
|
|
challenge string,
|
|
|
|
) {
|
|
|
|
var consentInfo ConsentInformation
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
// validate input
|
|
|
|
decoder := form.NewDecoder()
|
|
|
|
if err := decoder.Decode(&consentInfo, r.Form); err != nil {
|
|
|
|
h.logger.WithError(err).Error("could not decode consent form")
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-07 15:38:57 +00:00
|
|
|
if consentInfo.ConsentAction == "consent" {
|
2023-08-03 21:49:57 +00:00
|
|
|
consentRequest, err := h.rememberNewConsent(consentData, consentInfo, requestedClaims, session)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.WithError(err).Error("could not accept consent")
|
|
|
|
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2023-05-13 11:27:19 +00:00
|
|
|
}
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
err = h.acceptConsent(w, r, challenge, consentRequest)
|
2023-05-13 11:27:19 +00:00
|
|
|
if err != nil {
|
2023-08-03 21:49:57 +00:00
|
|
|
h.logger.WithError(err).Error("could not accept consent")
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
consentRequest, response, err := h.adminClient.RejectOAuth2ConsentRequest(
|
|
|
|
r.Context(),
|
|
|
|
).ConsentChallenge(challenge).Execute()
|
|
|
|
if err != nil {
|
|
|
|
h.logger.WithError(err).Error("reject consent request failed")
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-08-03 21:49:57 +00:00
|
|
|
|
|
|
|
defer func() { _ = response.Body.Close() }()
|
|
|
|
|
|
|
|
h.logger.WithFields(
|
|
|
|
log.Fields{"response": response.Status, "reject_consent_request": consentRequest},
|
|
|
|
).Debug("received response for RejectOAuth2ConsentRequest")
|
|
|
|
|
|
|
|
w.Header().Add("Location", consentRequest.GetRedirectTo())
|
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ConsentHandler) rememberNewConsent(
|
|
|
|
consentData *client.OAuth2ConsentRequest,
|
|
|
|
consentInfo ConsentInformation,
|
|
|
|
requestedClaims *models.OIDCClaimsRequest,
|
|
|
|
session *sessions.Session,
|
|
|
|
) (*client.AcceptOAuth2ConsentRequest, error) {
|
|
|
|
sessionData, err := h.getSessionData(
|
|
|
|
consentData.GetRequestedScope(),
|
|
|
|
consentInfo.GrantedScopes,
|
|
|
|
consentInfo.SelectedClaims,
|
|
|
|
requestedClaims,
|
|
|
|
session,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get session data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request := client.NewAcceptOAuth2ConsentRequestWithDefaults()
|
|
|
|
request.SetRemember(true)
|
|
|
|
request.SetHandledAt(time.Now())
|
|
|
|
request.SetGrantAccessTokenAudience(consentData.RequestedAccessTokenAudience)
|
|
|
|
request.SetGrantScope(consentInfo.GrantedScopes)
|
|
|
|
request.SetRememberFor(0)
|
|
|
|
request.SetSession(*sessionData)
|
|
|
|
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ConsentHandler) acceptConsent(
|
|
|
|
w http.ResponseWriter, r *http.Request, challenge string, request *client.AcceptOAuth2ConsentRequest,
|
|
|
|
) error {
|
|
|
|
oAuth2RedirectTo, response, err := h.adminClient.AcceptOAuth2ConsentRequest(
|
|
|
|
r.Context(),
|
|
|
|
).ConsentChallenge(challenge).AcceptOAuth2ConsentRequest(*request).Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("accept consent request failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = response.Body.Close() }()
|
|
|
|
|
|
|
|
h.logger.WithFields(
|
|
|
|
log.Fields{"response": response.Status, "redirect_to": oAuth2RedirectTo},
|
|
|
|
).Debug("received response for AcceptOAuth2ConsentRequest")
|
|
|
|
|
|
|
|
w.Header().Add("Location", oAuth2RedirectTo.GetRedirectTo())
|
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ConsentHandler) handleExistingConsent(
|
|
|
|
data *client.OAuth2ConsentRequest,
|
|
|
|
claims *models.OIDCClaimsRequest,
|
|
|
|
session *sessions.Session,
|
|
|
|
) (*client.AcceptOAuth2ConsentRequest, error) {
|
|
|
|
sessionData, err := h.getSessionData(data.GetRequestedScope(),
|
|
|
|
data.GetRequestedScope(), []string{}, claims, session)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get session data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request := client.NewAcceptOAuth2ConsentRequestWithDefaults()
|
|
|
|
request.SetGrantScope(data.RequestedScope)
|
|
|
|
request.SetHandledAt(time.Now())
|
|
|
|
request.SetGrantAccessTokenAudience(data.RequestedAccessTokenAudience)
|
|
|
|
request.SetSession(*sessionData)
|
|
|
|
|
|
|
|
return request, nil
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func (h *ConsentHandler) getRequestedConsentInformation(challenge string, r *http.Request) (
|
2023-08-03 21:49:57 +00:00
|
|
|
*client.OAuth2ConsentRequest,
|
2023-07-29 19:15:11 +00:00
|
|
|
*models.OIDCClaimsRequest,
|
2021-09-11 11:35:15 +00:00
|
|
|
error,
|
|
|
|
) {
|
2023-08-03 21:49:57 +00:00
|
|
|
consentRequest, response, err := h.adminClient.GetOAuth2ConsentRequest(
|
|
|
|
r.Context(),
|
|
|
|
).ConsentChallenge(challenge).Execute()
|
2021-09-11 11:35:15 +00:00
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithError(err).Error("error getting consent information")
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
if errorBucket := GetErrorBucket(r); errorBucket != nil {
|
|
|
|
errorDetails := &ErrorDetails{
|
|
|
|
ErrorMessage: "could not get consent details",
|
|
|
|
ErrorDetails: []string{http.StatusText(http.StatusInternalServerError)},
|
|
|
|
}
|
|
|
|
|
|
|
|
errorBucket.AddError(errorDetails)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
return nil, nil, fmt.Errorf("error getting consent information: %w", err)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
defer func() { _ = response.Body.Close() }()
|
|
|
|
|
|
|
|
h.logger.WithFields(
|
|
|
|
log.Fields{"response": response.Status, "consent_request": consentRequest},
|
|
|
|
).Debug("response for GetOAuth2ConsentRequest")
|
|
|
|
|
2023-07-29 19:15:11 +00:00
|
|
|
var requestedClaims models.OIDCClaimsRequest
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
requestURLStr := consentRequest.GetRequestUrl()
|
|
|
|
|
|
|
|
requestURL, err := url.Parse(requestURLStr)
|
2021-09-11 11:35:15 +00:00
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithError(err).WithField(
|
2023-08-03 21:49:57 +00:00
|
|
|
"request_url", requestURLStr,
|
2023-07-29 18:32:02 +00:00
|
|
|
).Warn("could not parse original request URL")
|
2021-09-11 11:35:15 +00:00
|
|
|
} else {
|
2023-05-13 11:27:19 +00:00
|
|
|
claimsParameter := requestURL.Query().Get("claims")
|
2021-09-11 11:35:15 +00:00
|
|
|
if claimsParameter != "" {
|
|
|
|
decoder := json.NewDecoder(strings.NewReader(claimsParameter))
|
|
|
|
err := decoder.Decode(&requestedClaims)
|
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithError(err).WithField(
|
|
|
|
"claims_parameter", claimsParameter,
|
|
|
|
).Warn("ignoring claims request parameter that could not be decoded")
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
return consentRequest, &requestedClaims, nil
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func (h *ConsentHandler) renderConsentForm(
|
2021-09-11 11:35:15 +00:00
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
2023-08-03 21:49:57 +00:00
|
|
|
consentRequest *client.OAuth2ConsentRequest,
|
2023-07-29 19:15:11 +00:00
|
|
|
claims *models.OIDCClaimsRequest,
|
2021-09-11 11:35:15 +00:00
|
|
|
localizer *i18n.Localizer,
|
2023-08-07 13:15:45 +00:00
|
|
|
) {
|
2023-08-07 15:38:57 +00:00
|
|
|
trans := h.trans.LookupMessage
|
|
|
|
transMarkdown := func(id string, params map[string]interface{}, localizer *i18n.Localizer) template.HTML {
|
|
|
|
return template.HTML( //nolint:gosec
|
|
|
|
h.trans.LookupMarkdownMessage(id, params, localizer),
|
|
|
|
)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// render consent form
|
2023-08-03 21:49:57 +00:00
|
|
|
oAuth2Client := consentRequest.Client
|
2023-08-07 15:38:57 +00:00
|
|
|
clientLogoURI := oAuth2Client.GetLogoUri()
|
|
|
|
clientName := template.HTMLEscaper(oAuth2Client.GetClientName())
|
|
|
|
clientURI := oAuth2Client.GetClientUri()
|
|
|
|
|
2023-08-07 13:15:45 +00:00
|
|
|
h.templates.render(h.logger, w, Consent, map[string]interface{}{
|
2023-08-07 15:38:57 +00:00
|
|
|
"Title": trans("TitleRequestConsent", nil, localizer),
|
|
|
|
csrf.TemplateTag: csrf.TemplateField(r),
|
|
|
|
"errors": map[string]string{},
|
|
|
|
"LogoURI": clientLogoURI,
|
|
|
|
"ClientName": clientName,
|
|
|
|
"requestedScope": h.mapRequestedScope(consentRequest.RequestedScope, localizer),
|
|
|
|
"requestedClaims": h.mapRequestedClaims(claims, localizer),
|
|
|
|
"ButtonTitleConsent": trans("ButtonTitleConsent", nil, localizer),
|
|
|
|
"ButtonTitleDeny": trans("ButtonTitleDeny", nil, localizer),
|
|
|
|
"HasMoreInformation": clientURI != "",
|
|
|
|
"IntroMoreInformation": transMarkdown(
|
|
|
|
"IntroConsentMoreInformation", map[string]interface{}{
|
|
|
|
"client": clientName,
|
|
|
|
"clientLink": clientURI,
|
|
|
|
}, localizer),
|
|
|
|
"LabelConsent": transMarkdown("LabelConsent", nil, localizer),
|
|
|
|
"ClaimsInformation": transMarkdown(
|
|
|
|
"ClaimsInformation", nil, localizer),
|
|
|
|
"IntroConsentRequested": transMarkdown(
|
|
|
|
"IntroConsentRequested", map[string]interface{}{
|
|
|
|
"client": clientName,
|
|
|
|
}, localizer),
|
2021-09-11 11:35:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type scopeWithLabel struct {
|
|
|
|
Name string
|
|
|
|
Label string
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func (h *ConsentHandler) mapRequestedScope(
|
2023-08-03 21:49:57 +00:00
|
|
|
scope []string,
|
2023-05-13 11:27:19 +00:00
|
|
|
localizer *i18n.Localizer,
|
|
|
|
) []*scopeWithLabel {
|
2021-09-11 11:35:15 +00:00
|
|
|
result := make([]*scopeWithLabel, 0)
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
for _, scopeName := range scope {
|
|
|
|
if _, ok := supportedScopes[scopeName]; !ok {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithField("scope", scopeName).Warn("ignoring unsupported scope")
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
label, err := localizer.Localize(&i18n.LocalizeConfig{
|
|
|
|
DefaultMessage: supportedScopes[scopeName],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithError(err).WithField("scope", scopeName).Warn("could not localize scope label")
|
2021-09-11 11:35:15 +00:00
|
|
|
label = scopeName
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
result = append(result, &scopeWithLabel{Name: scopeName, Label: label})
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
type claimWithLabel struct {
|
|
|
|
Name string
|
|
|
|
Label string
|
|
|
|
Essential bool
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func (h *ConsentHandler) mapRequestedClaims(
|
2023-07-29 19:15:11 +00:00
|
|
|
claims *models.OIDCClaimsRequest,
|
2023-05-13 11:27:19 +00:00
|
|
|
localizer *i18n.Localizer,
|
|
|
|
) []*claimWithLabel {
|
2021-09-11 11:35:15 +00:00
|
|
|
result := make([]*claimWithLabel, 0)
|
|
|
|
known := make(map[string]bool)
|
|
|
|
|
2023-07-29 19:15:11 +00:00
|
|
|
for _, claimElement := range []*models.ClaimElement{claims.GetUserInfo(), claims.GetIDToken()} {
|
2021-09-11 11:35:15 +00:00
|
|
|
if claimElement != nil {
|
|
|
|
for k, v := range *claimElement {
|
|
|
|
if _, ok := supportedClaims[k]; !ok {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithField("claim", k).Warn("ignoring unsupported claim")
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
label, err := localizer.Localize(&i18n.LocalizeConfig{
|
|
|
|
DefaultMessage: supportedClaims[k],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithError(err).WithField("claim", k).Warn("could not localize claim label")
|
2021-09-11 11:35:15 +00:00
|
|
|
label = k
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
if !known[k] {
|
|
|
|
result = append(result, &claimWithLabel{
|
|
|
|
Name: k,
|
|
|
|
Label: label,
|
|
|
|
Essential: v.IsEssential(),
|
|
|
|
})
|
|
|
|
known[k] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func (h *ConsentHandler) getSessionData(
|
2023-08-03 21:49:57 +00:00
|
|
|
requestedScopes,
|
|
|
|
grantedScopes,
|
|
|
|
selectedClaims []string,
|
2023-07-29 19:15:11 +00:00
|
|
|
claims *models.OIDCClaimsRequest,
|
2023-08-03 21:49:57 +00:00
|
|
|
session *sessions.Session,
|
|
|
|
) (*client.AcceptOAuth2ConsentRequestSession, error) {
|
|
|
|
idTokenData := make(map[string]interface{})
|
|
|
|
|
|
|
|
if err := h.fillTokenData(
|
|
|
|
idTokenData,
|
|
|
|
requestedScopes,
|
|
|
|
grantedScopes,
|
|
|
|
claims,
|
|
|
|
selectedClaims,
|
|
|
|
session.Values,
|
|
|
|
); err != nil {
|
2023-05-13 11:27:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
consentSession := client.NewAcceptOAuth2ConsentRequestSession()
|
|
|
|
consentSession.SetIdToken(idTokenData)
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
return consentSession, nil
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func (h *ConsentHandler) fillTokenData(
|
2021-09-11 11:35:15 +00:00
|
|
|
m map[string]interface{},
|
2023-08-03 21:49:57 +00:00
|
|
|
requestedScopes []string,
|
|
|
|
grantedScopes []string,
|
2023-07-29 19:15:11 +00:00
|
|
|
claimsRequest *models.OIDCClaimsRequest,
|
2023-08-03 21:49:57 +00:00
|
|
|
selectedClaims []string,
|
|
|
|
sessionData map[interface{}]interface{},
|
2023-05-13 11:27:19 +00:00
|
|
|
) error {
|
2023-08-03 21:49:57 +00:00
|
|
|
for _, scope := range requestedScopes {
|
2021-09-11 11:35:15 +00:00
|
|
|
granted := false
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
for _, k := range grantedScopes {
|
2021-09-11 11:35:15 +00:00
|
|
|
if k == scope {
|
|
|
|
granted = true
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
if !granted {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
switch scope {
|
|
|
|
case ScopeEmail:
|
|
|
|
// email
|
|
|
|
// OPTIONAL. This scope value requests access to the email and
|
|
|
|
// email_verified Claims.
|
2023-08-03 21:49:57 +00:00
|
|
|
m[openid.EmailKey] = sessionData[services.SessionEmail]
|
|
|
|
m[openid.EmailVerifiedKey] = true
|
2021-09-11 11:35:15 +00:00
|
|
|
case ScopeProfile:
|
|
|
|
// profile
|
|
|
|
// OPTIONAL. This scope value requests access to the
|
|
|
|
// End-User's default profile Claims, which are: name,
|
|
|
|
// family_name, given_name, middle_name, nickname,
|
|
|
|
// preferred_username, profile, picture, website, gender,
|
|
|
|
// birthdate, zoneinfo, locale, and updated_at.
|
2023-08-03 21:49:57 +00:00
|
|
|
m[openid.NameKey] = sessionData[services.SessionFullName]
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
if userInfoClaims := claimsRequest.GetUserInfo(); userInfoClaims != nil {
|
2023-08-03 21:49:57 +00:00
|
|
|
err := h.parseUserInfoClaims(m, userInfoClaims, selectedClaims)
|
2023-05-13 11:27:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ConsentHandler) parseUserInfoClaims(
|
|
|
|
m map[string]interface{},
|
2023-07-29 19:15:11 +00:00
|
|
|
userInfoClaims *models.ClaimElement,
|
2023-08-03 21:49:57 +00:00
|
|
|
selectedClaims []string,
|
2023-05-13 11:27:19 +00:00
|
|
|
) error {
|
|
|
|
for claimName, claim := range *userInfoClaims {
|
|
|
|
granted := false
|
|
|
|
|
2023-08-03 21:49:57 +00:00
|
|
|
for _, k := range selectedClaims {
|
2023-05-13 11:27:19 +00:00
|
|
|
if k == claimName {
|
|
|
|
granted = true
|
|
|
|
|
|
|
|
break
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !granted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
wantedValue, err := claim.WantedValue()
|
|
|
|
if err != nil {
|
2023-07-29 19:15:11 +00:00
|
|
|
if !errors.Is(err, models.ErrNoValue) {
|
2023-05-13 11:27:19 +00:00
|
|
|
return fmt.Errorf("error handling claim: %w", err)
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
if wantedValue != "" {
|
|
|
|
m[claimName] = wantedValue
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if claim.IsEssential() {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithField("claim", claimName).Warn("handling for essential claim not implemented")
|
2023-05-13 11:27:19 +00:00
|
|
|
} else {
|
2023-07-29 18:32:02 +00:00
|
|
|
h.logger.WithField("claim", claimName).Warn("handling for claim not implemented")
|
2023-05-13 11:27:19 +00:00
|
|
|
}
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
return nil
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func NewConsentHandler(
|
2024-05-11 20:27:22 +00:00
|
|
|
logger *log.Logger, templateCache TemplateCache, trans *services.I18NService, adminClient client.OAuth2API,
|
2023-05-13 11:27:19 +00:00
|
|
|
) *ConsentHandler {
|
|
|
|
return &ConsentHandler{
|
2023-08-07 13:15:45 +00:00
|
|
|
logger: logger,
|
|
|
|
trans: trans,
|
|
|
|
adminClient: adminClient,
|
|
|
|
templates: templateCache,
|
2023-05-13 11:27:19 +00:00
|
|
|
}
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|