Show why personal OIDC logins are refused

Personal Google and Microsoft accounts were rejected with a raw
JSON unauthorized response after the OIDC callback. Redirect to a
dedicated auth page that explains the enterprise-account
requirement, and check enterprise eligibility before xms_edov so
Microsoft consumer accounts get the same clear error.

Signed-off-by: Cursor Agent <cursoragent@cursor.com>

Co-authored-by: Bryan FRIMIN <bryan@frimin.fr>
This commit is contained in:
Bryan Frimin
2026-07-24 21:16:11 +00:00
committed by Cursor Agent
parent b8c3fb086e
commit d1814d7051
5 changed files with 148 additions and 14 deletions

View File

@@ -165,6 +165,30 @@ func (c *idTokenClaims) isEmailDomainOwnerVerified() bool {
return false
}
// validateIDTokenClaims enforces provider-specific account requirements.
// Enterprise eligibility is checked before email-verification claims so
// personal Google/Microsoft accounts surface ErrPersonalAccountNotAllowed
// rather than a generic email-verification failure (e.g. missing xms_edov).
func validateIDTokenClaims(info *providerInfo, claims *idTokenClaims) error {
if claims.Email == "" {
return NewMissingEmailClaimError()
}
if !info.enterpriseChecker(claims) {
return NewPersonalAccountNotAllowedError()
}
if !info.trustProviderEmail && !claims.isEmailVerified() {
return NewEmailNotVerifiedError()
}
if info.requireEmailDomainOwnerVerified && !claims.isEmailDomainOwnerVerified() {
return NewEmailNotVerifiedError()
}
return nil
}
var (
googleEndpoint = oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/v2/auth",
@@ -423,20 +447,8 @@ func (s *Service) HandleCallback(
return nil, "", nil, fmt.Errorf("cannot verify id token: %w", err)
}
if claims.Email == "" {
return nil, "", nil, NewMissingEmailClaimError()
}
if !info.trustProviderEmail && !claims.isEmailVerified() {
return nil, "", nil, NewEmailNotVerifiedError()
}
if info.requireEmailDomainOwnerVerified && !claims.isEmailDomainOwnerVerified() {
return nil, "", nil, NewEmailNotVerifiedError()
}
if !info.enterpriseChecker(claims) {
return nil, "", nil, NewPersonalAccountNotAllowedError()
if err := validateIDTokenClaims(info, claims); err != nil {
return nil, "", nil, err
}
email, err := mail.ParseAddr(claims.Email)

View File

@@ -21,6 +21,7 @@
package oidc
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
@@ -89,3 +90,65 @@ func TestIsEmailDomainOwnerVerified(t *testing.T) {
)
}
}
func TestValidateIDTokenClaims_PersonalAccounts(t *testing.T) {
t.Parallel()
s := newTestService(t)
t.Run("rejects Google personal account without hosted domain", func(t *testing.T) {
t.Parallel()
err := validateIDTokenClaims(
s.providers[coredata.OIDCProviderGoogle],
&idTokenClaims{
Email: "user@gmail.com",
EmailVerified: true,
},
)
_, ok := errors.AsType[*ErrPersonalAccountNotAllowed](err)
assert.True(t, ok, "got %T: %v", err, err)
})
t.Run("rejects Microsoft personal account before xms_edov check", func(t *testing.T) {
t.Parallel()
err := validateIDTokenClaims(
s.providers[coredata.OIDCProviderMicrosoft],
&idTokenClaims{
Issuer: "https://login.microsoftonline.com/" + microsoftConsumerTenantID + "/v2.0",
Email: "user@outlook.com",
},
)
_, ok := errors.AsType[*ErrPersonalAccountNotAllowed](err)
assert.True(t, ok, "got %T: %v", err, err)
})
t.Run("accepts Google Workspace account with hosted domain", func(t *testing.T) {
t.Parallel()
err := validateIDTokenClaims(
s.providers[coredata.OIDCProviderGoogle],
&idTokenClaims{
Email: "user@acme.com",
EmailVerified: true,
HostedDomain: "acme.com",
},
)
assert.NoError(t, err)
})
t.Run("rejects Microsoft enterprise account missing xms_edov", func(t *testing.T) {
t.Parallel()
err := validateIDTokenClaims(
s.providers[coredata.OIDCProviderMicrosoft],
&idTokenClaims{
Issuer: "https://login.microsoftonline.com/tenant-id/v2.0",
Email: "user@acme.com",
},
)
_, ok := errors.AsType[*ErrEmailNotVerified](err)
assert.True(t, ok, "got %T: %v", err, err)
})
}

View File

@@ -32,6 +32,7 @@ import (
"go.probo.inc/probo/pkg/coredata"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/iam"
"go.probo.inc/probo/pkg/iam/oidc"
"go.probo.inc/probo/pkg/mail"
"go.probo.inc/probo/pkg/saferedirect"
"go.probo.inc/probo/pkg/securecookie"
@@ -130,6 +131,13 @@ func (h *OIDCHandler) CallbackHandler(w http.ResponseWriter, r *http.Request) {
identity, continueURL, organizationID, err := h.iam.OIDCService.HandleCallback(ctx, provider, stateParam, code)
if err != nil {
if _, ok := errors.AsType[*oidc.ErrPersonalAccountNotAllowed](err); ok {
h.logger.WarnCtx(ctx, "OIDC login rejected: personal account not allowed")
http.Redirect(w, r, "/auth/personal-account-not-allowed", http.StatusFound)
return
}
h.logger.ErrorCtx(ctx, "cannot handle OIDC callback", log.Error(err))
httpserver.RenderError(w, http.StatusUnauthorized, errors.New("authentication failed"))