2022-10-20 02:16:36 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/ory/fosite"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
|
|
|
"github.com/authelia/authelia/v4/internal/oidc"
|
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleOIDCAuthorizationConsentModeExplicit(ctx *middlewares.AutheliaCtx, issuer *url.URL, client *oidc.Client,
|
|
|
|
userSession session.UserSession, subject uuid.UUID,
|
|
|
|
rw http.ResponseWriter, r *http.Request, requester fosite.AuthorizeRequester) (consent *model.OAuth2ConsentSession, handled bool) {
|
|
|
|
var (
|
|
|
|
consentID uuid.UUID
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
bytesConsentID := ctx.QueryArgs().PeekBytes(qryArgConsentID)
|
|
|
|
|
|
|
|
switch len(bytesConsentID) {
|
|
|
|
case 0:
|
|
|
|
return handleOIDCAuthorizationConsentGenerate(ctx, issuer, client, userSession, subject, rw, r, requester)
|
|
|
|
default:
|
2022-10-31 23:31:13 +00:00
|
|
|
if consentID, err = uuid.ParseBytes(bytesConsentID); err != nil {
|
2022-10-20 02:16:36 +00:00
|
|
|
ctx.Logger.Errorf(logFmtErrConsentParseChallengeID, requester.GetID(), client.GetID(), client.Consent, bytesConsentID, err)
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
ctx.Providers.OpenIDConnect.WriteAuthorizeError(ctx, rw, requester, oidc.ErrConsentMalformedChallengeID)
|
2022-10-20 02:16:36 +00:00
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleOIDCAuthorizationConsentModeExplicitWithID(ctx, issuer, client, userSession, subject, consentID, rw, r, requester)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleOIDCAuthorizationConsentModeExplicitWithID(ctx *middlewares.AutheliaCtx, issuer *url.URL, client *oidc.Client,
|
|
|
|
userSession session.UserSession, subject uuid.UUID, consentID uuid.UUID,
|
|
|
|
rw http.ResponseWriter, r *http.Request, requester fosite.AuthorizeRequester) (consent *model.OAuth2ConsentSession, handled bool) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if consentID.ID() == 0 {
|
|
|
|
ctx.Logger.Errorf(logFmtErrConsentZeroID, requester.GetID(), client.GetID(), client.Consent)
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
ctx.Providers.OpenIDConnect.WriteAuthorizeError(ctx, rw, requester, oidc.ErrConsentCouldNotLookup)
|
2022-10-20 02:16:36 +00:00
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if consent, err = ctx.Providers.StorageProvider.LoadOAuth2ConsentSessionByChallengeID(ctx, consentID); err != nil {
|
|
|
|
ctx.Logger.Errorf(logFmtErrConsentLookupLoadingSession, requester.GetID(), client.GetID(), client.Consent, consentID, err)
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
ctx.Providers.OpenIDConnect.WriteAuthorizeError(ctx, rw, requester, oidc.ErrConsentCouldNotLookup)
|
2022-10-20 02:16:36 +00:00
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if subject.ID() != consent.Subject.UUID.ID() {
|
|
|
|
ctx.Logger.Errorf(logFmtErrConsentSessionSubjectNotAuthorized, requester.GetID(), client.GetID(), client.Consent, consent.ChallengeID, userSession.Username, subject, consent.Subject.UUID)
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
ctx.Providers.OpenIDConnect.WriteAuthorizeError(ctx, rw, requester, oidc.ErrConsentCouldNotLookup)
|
2022-10-20 02:16:36 +00:00
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !consent.CanGrant() {
|
|
|
|
ctx.Logger.Errorf(logFmtErrConsentCantGrant, requester.GetID(), client.GetID(), client.Consent, consent.ChallengeID, "explicit")
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
ctx.Providers.OpenIDConnect.WriteAuthorizeError(ctx, rw, requester, oidc.ErrConsentCouldNotPerform)
|
2022-10-20 02:16:36 +00:00
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !consent.IsAuthorized() {
|
|
|
|
if consent.Responded() {
|
|
|
|
ctx.Logger.Errorf(logFmtErrConsentCantGrantRejected, requester.GetID(), client.GetID(), client.Consent, consent.ChallengeID)
|
|
|
|
|
2022-11-19 06:42:03 +00:00
|
|
|
ctx.Providers.OpenIDConnect.WriteAuthorizeError(ctx, rw, requester, fosite.ErrAccessDenied)
|
2022-10-20 02:16:36 +00:00
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOIDCAuthorizationConsentRedirect(ctx, issuer, consent, client, userSession, rw, r, requester)
|
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return consent, false
|
|
|
|
}
|