diff --git a/internal/api/external.go b/internal/api/external.go index 957a327946..51ff1d4fde 100644 --- a/internal/api/external.go +++ b/internal/api/external.go @@ -216,7 +216,7 @@ func (a *API) internalExternalProviderCallback(w http.ResponseWriter, r *http.Re err = db.Transaction(func(tx *storage.Connection) error { var terr error if targetUser != nil { - if user, terr = a.linkIdentityToUser(r, ctx, tx, userData, providerType); terr != nil { + if user, terr = a.linkIdentityToUser(r, ctx, tx, userData, providerType, emailOptional); terr != nil { return terr } } else if inviteToken != "" { diff --git a/internal/api/identity.go b/internal/api/identity.go index 40136a8fbb..9ca0cd09b8 100644 --- a/internal/api/identity.go +++ b/internal/api/identity.go @@ -170,7 +170,7 @@ func (a *API) LinkIdentity(w http.ResponseWriter, r *http.Request) error { return nil } -func (a *API) linkIdentityToUser(r *http.Request, ctx context.Context, tx *storage.Connection, userData *provider.UserProvidedData, providerType string) (*models.User, error) { +func (a *API) linkIdentityToUser(r *http.Request, ctx context.Context, tx *storage.Connection, userData *provider.UserProvidedData, providerType string, emailOptional bool) (*models.User, error) { targetUser := getTargetUser(ctx) identity, terr := models.FindIdentityByIdAndProvider(tx, userData.Metadata.Subject, providerType) if terr != nil { @@ -203,7 +203,12 @@ func (a *API) linkIdentityToUser(r *http.Request, ctx context.Context, tx *stora } return nil, terr } - if !userData.Metadata.EmailVerified { + // When the provider is email_optional and linking left the user without + // an email (e.g. a custom OIDC identity with only the openid scope), + // there's no address to confirm, so this must behave like the sign-in + // path and skip the confirmation requirement. + hasEmail := !emailOptional || targetUser.GetEmail() != "" + if hasEmail && !userData.Metadata.EmailVerified { if terr := a.sendConfirmation(r, tx, targetUser, models.ImplicitFlow); terr != nil { return nil, terr } diff --git a/internal/api/identity_test.go b/internal/api/identity_test.go index d56f3714e1..b8ec3beb0c 100644 --- a/internal/api/identity_test.go +++ b/internal/api/identity_test.go @@ -92,7 +92,7 @@ func (ts *IdentityTestSuite) TestLinkIdentityToUser() { // request is just used as a placeholder in the function r := httptest.NewRequest(http.MethodGet, "/identities", nil) r.RemoteAddr = "192.0.2.1:1234" - u, err = ts.API.linkIdentityToUser(r, ctx, ts.API.db, testValidUserData, "test") + u, err = ts.API.linkIdentityToUser(r, ctx, ts.API.db, testValidUserData, "test", false) require.NoError(ts.T(), err) // load associated identities for the user @@ -121,7 +121,7 @@ func (ts *IdentityTestSuite) TestLinkIdentityToUser() { Subject: u.ID.String(), }, } - u, err = ts.API.linkIdentityToUser(r, ctx, ts.API.db, testExistingUserData, "email") + u, err = ts.API.linkIdentityToUser(r, ctx, ts.API.db, testExistingUserData, "email", false) require.ErrorIs(ts.T(), err, apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeIdentityAlreadyExists, "Identity is already linked")) require.Nil(ts.T(), u) @@ -130,6 +130,32 @@ func (ts *IdentityTestSuite) TestLinkIdentityToUser() { require.Len(ts.T(), logs, 1, "an already-linked identity must not emit another audit log") } +func (ts *IdentityTestSuite) TestLinkIdentityToUserEmailOptionalWithoutEmail() { + // An anonymous user has no email, and a custom OIDC identity with + // email_optional (e.g. one using only the openid scope) carries none + // either, so there is nothing to confirm and linking must succeed + // instead of trying to send a confirmation email to an empty address. + u, err := models.NewUser("", "", "", ts.Config.JWT.Aud, nil) + require.NoError(ts.T(), err, "Error creating anonymous test user model") + u.IsAnonymous = true + require.NoError(ts.T(), ts.API.db.Create(u), "Error saving new anonymous test user") + + ctx := withTargetUser(context.Background(), u) + r := httptest.NewRequest(http.MethodGet, "/identities", nil) + r.RemoteAddr = "192.0.2.1:1234" + + testUserData := &provider.UserProvidedData{ + Metadata: &provider.Claims{ + Subject: "line_subject_no_email", + }, + } + linkedUser, err := ts.API.linkIdentityToUser(r, ctx, ts.API.db, testUserData, "custom:line", true) + require.NoError(ts.T(), err) + require.NotNil(ts.T(), linkedUser) + require.False(ts.T(), linkedUser.IsAnonymous, "linking an email-less identity should convert an anonymous user") + require.True(ts.T(), linkedUser.IsConfirmed()) +} + func (ts *IdentityTestSuite) TestUnlinkIdentityError() { manualLinkingEnabled := ts.Config.Security.ManualLinkingEnabled ts.Config.Security.ManualLinkingEnabled = true @@ -438,7 +464,7 @@ func (ts *IdentityTestSuite) TestLinkIdentitySendsNotificationEmailEnabled() { }, } r := httptest.NewRequest(http.MethodGet, "/identities", nil) - u, err = ts.API.linkIdentityToUser(r, ctx, ts.API.db, testValidUserData, "google") + u, err = ts.API.linkIdentityToUser(r, ctx, ts.API.db, testValidUserData, "google", false) require.NoError(ts.T(), err) // Assert that identity linked notification email was sent @@ -469,7 +495,7 @@ func (ts *IdentityTestSuite) TestLinkIdentitySendsNotificationEmailDisabled() { }, } r := httptest.NewRequest(http.MethodGet, "/identities", nil) - _, err = ts.API.linkIdentityToUser(r, ctx, ts.API.db, testValidUserData, "facebook") + _, err = ts.API.linkIdentityToUser(r, ctx, ts.API.db, testValidUserData, "facebook", false) require.NoError(ts.T(), err) // Assert that identity linked notification email was not sent diff --git a/internal/api/token_oidc.go b/internal/api/token_oidc.go index cb9ab439fc..4f472bfb19 100644 --- a/internal/api/token_oidc.go +++ b/internal/api/token_oidc.go @@ -334,7 +334,7 @@ func (a *API) IdTokenGrant(ctx context.Context, w http.ResponseWriter, r *http.R var decision models.AccountLinkingDecision if params.LinkIdentity { - user, terr = a.linkIdentityToUser(r, ctx, tx, userData, providerType) + user, terr = a.linkIdentityToUser(r, ctx, tx, userData, providerType, emailOptional) } else { decision, user, terr = a.createAccountFromExternalIdentity(tx, r, userData, providerType, emailOptional) }