Skip to content
Merged
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
4 changes: 2 additions & 2 deletions app/services/sqlstore/postgres/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func getVerificationByKey(ctx context.Context, q *query.GetVerificationByKey) er
return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
verification := dbEntities.EmailVerification{}

query := "SELECT id, email, name, key, code, created_at, verified_at, expires_at, kind, user_id, attempts FROM email_verifications WHERE key = $1 AND kind = $2 LIMIT 1"
err := trx.Get(&verification, query, q.Key, q.Kind)
query := "SELECT id, email, name, key, code, created_at, verified_at, expires_at, kind, user_id, attempts FROM email_verifications WHERE key = $1 AND kind = $2 AND tenant_id = $3 LIMIT 1"
err := trx.Get(&verification, query, q.Key, q.Kind, tenant.ID)
if err != nil {
return errors.Wrap(err, "failed to get email verification by its key")
}
Expand Down
28 changes: 28 additions & 0 deletions app/services/sqlstore/postgres/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,34 @@ func TestTenantStorage_SaveFindSet_ChangeEmailVerificationKey(t *testing.T) {
Expect(getKey.Result.ExpiresAt).TemporarilySimilar(getKey.Result.CreatedAt.Add(15*time.Minute), 1*time.Second)
}

func TestTenantStorage_VerificationKey_IsTenantScoped(t *testing.T) {
SetupDatabaseTest(t)
defer TeardownDatabaseTest()

//Save new Key on the demo tenant
err := bus.Dispatch(demoTenantCtx, &cmd.SaveVerificationKey{
Key: "s3cr3tk3y",
Duration: 15 * time.Minute,
Request: &actions.CreateTenant{
Email: "jon.snow@got.com",
Name: "Jon Snow",
},
})
Expect(err).IsNil()

//Same key must NOT be retrievable from a different tenant
getKeyFromOtherTenant := &query.GetVerificationByKey{Kind: enum.EmailVerificationKindSignUp, Key: "s3cr3tk3y"}
err = bus.Dispatch(avengersTenantCtx, getKeyFromOtherTenant)
Expect(errors.Cause(err)).Equals(app.ErrNotFound)
Expect(getKeyFromOtherTenant.Result).IsNil()

//But it is retrievable from the tenant it belongs to
getKey := &query.GetVerificationByKey{Kind: enum.EmailVerificationKindSignUp, Key: "s3cr3tk3y"}
err = bus.Dispatch(demoTenantCtx, getKey)
Expect(err).IsNil()
Expect(getKey.Result.Email).Equals("jon.snow@got.com")
}

func TestTenantStorage_FindUnknownVerificationKey(t *testing.T) {
SetupDatabaseTest(t)
defer TeardownDatabaseTest()
Expand Down
Loading