Skip to content

Commit

Permalink
fix(login): prevent init mail on idp registration (#7895)
Browse files Browse the repository at this point in the history
  • Loading branch information
livio-a committed May 3, 2024
1 parent b72ecf6 commit 9008941
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func (repo *AuthRequestRepo) AutoRegisterExternalUser(ctx context.Context, regis
}
}
human := command.AddHumanFromDomain(registerUser, metadatas, request, externalIDP)
err = repo.Command.AddUserHuman(ctx, resourceOwner, human, true, repo.UserCodeAlg)
err = repo.Command.AddUserHuman(ctx, resourceOwner, human, false, repo.UserCodeAlg)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions internal/command/user_human.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (c *Commands) addHumanCommandEmail(ctx context.Context, filter preparation.
if human.Email.ReturnCode {
human.EmailCode = &emailCode.Plain
}
return append(cmds, user.NewHumanEmailCodeAddedEventV2(ctx, &a.Aggregate, emailCode.Crypted, emailCode.Expiry, human.Email.URLTemplate, human.Email.ReturnCode)), nil
return append(cmds, user.NewHumanEmailCodeAddedEventV2(ctx, &a.Aggregate, emailCode.Crypted, emailCode.Expiry, human.Email.URLTemplate, human.Email.ReturnCode, human.AuthRequestID)), nil
}
return cmds, nil
}
Expand Down Expand Up @@ -411,10 +411,9 @@ func (h *AddHuman) ensureDisplayName() {
// and / or
// - have no authentication method (password / passwordless)
func (h *AddHuman) shouldAddInitCode() bool {
return !h.ExternalIDP &&
!h.Email.Verified ||
!h.Passwordless &&
h.Password == ""
return len(h.Links) == 0 &&
(!h.Email.Verified ||
(!h.Passwordless && h.Password == ""))
}

// Deprecated: use commands.AddUserHuman
Expand Down
2 changes: 2 additions & 0 deletions internal/command/user_human_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ func TestCommandSide_AddHuman(t *testing.T) {
1*time.Hour,
"https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}",
false,
"",
),
),
),
Expand Down Expand Up @@ -591,6 +592,7 @@ func TestCommandSide_AddHuman(t *testing.T) {
1*time.Hour,
"",
true,
"",
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion internal/command/user_v2_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func generateCodeCommand(ctx context.Context, agg *eventstore.Aggregate, gen cry
return nil, "", err
}

cmd := user.NewHumanEmailCodeAddedEventV2(ctx, agg, value, gen.Expiry(), urlTmpl, returnCode)
cmd := user.NewHumanEmailCodeAddedEventV2(ctx, agg, value, gen.Expiry(), urlTmpl, returnCode, "")
if returnCode {
return cmd, plain, nil
}
Expand Down
32 changes: 16 additions & 16 deletions internal/command/user_v2_email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func TestCommands_ResendUserEmailCode(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down Expand Up @@ -577,7 +577,7 @@ func TestCommands_ResendUserEmailCodeURLTemplate(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down Expand Up @@ -696,7 +696,7 @@ func TestCommands_ResendUserEmailReturnCode(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down Expand Up @@ -1070,7 +1070,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down Expand Up @@ -1126,7 +1126,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", true,
"", true, "",
),
),
),
Expand Down Expand Up @@ -1183,7 +1183,7 @@ func TestCommands_changeUserEmailWithGenerator(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}", false,
"https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}", false, "",
),
),
),
Expand Down Expand Up @@ -1308,7 +1308,7 @@ func TestCommands_resendUserEmailCodeWithGeneratorEvents(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down Expand Up @@ -1352,7 +1352,7 @@ func TestCommands_resendUserEmailCodeWithGeneratorEvents(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand All @@ -1366,7 +1366,7 @@ func TestCommands_resendUserEmailCodeWithGeneratorEvents(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down Expand Up @@ -1416,7 +1416,7 @@ func TestCommands_resendUserEmailCodeWithGeneratorEvents(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand All @@ -1430,7 +1430,7 @@ func TestCommands_resendUserEmailCodeWithGeneratorEvents(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", true,
"", true, "",
),
),
),
Expand Down Expand Up @@ -1481,7 +1481,7 @@ func TestCommands_resendUserEmailCodeWithGeneratorEvents(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand All @@ -1495,7 +1495,7 @@ func TestCommands_resendUserEmailCodeWithGeneratorEvents(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}", false,
"https://example.com/email/verify?userID={{.UserID}}&code={{.Code}}&orgID={{.OrgID}}", false, "",
),
),
),
Expand Down Expand Up @@ -1642,7 +1642,7 @@ func TestCommands_VerifyUserEmail(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down Expand Up @@ -1757,7 +1757,7 @@ func TestCommands_verifyUserEmailWithGenerator(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down Expand Up @@ -1804,7 +1804,7 @@ func TestCommands_verifyUserEmailWithGenerator(t *testing.T) {
Crypted: []byte("a"),
},
time.Hour*1,
"", false,
"", false, "",
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion internal/command/user_v2_human.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (c *Commands) changeUserEmail(ctx context.Context, cmds []eventstore.Comman
if err != nil {
return cmds, code, err
}
cmds = append(cmds, user.NewHumanEmailCodeAddedEventV2(ctx, &wm.Aggregate().Aggregate, cryptoCode.Crypted, cryptoCode.Expiry, email.URLTemplate, email.ReturnCode))
cmds = append(cmds, user.NewHumanEmailCodeAddedEventV2(ctx, &wm.Aggregate().Aggregate, cryptoCode.Crypted, cryptoCode.Expiry, email.URLTemplate, email.ReturnCode, ""))
if email.ReturnCode {
code = &cryptoCode.Plain
}
Expand Down
Loading

0 comments on commit 9008941

Please sign in to comment.