Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/api/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
9 changes: 7 additions & 2 deletions internal/api/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
34 changes: 30 additions & 4 deletions internal/api/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/api/token_oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down