2022-06-27 17:28:39 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2022-06-29 06:06:43 +02:00
|
|
|
"errors"
|
2022-07-22 03:19:24 +02:00
|
|
|
"strings"
|
2022-06-27 17:28:39 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-06-29 06:06:43 +02:00
|
|
|
"github.com/mattn/go-sqlite3"
|
|
|
|
|
2022-07-12 04:10:19 +02:00
|
|
|
"lbryio/lbry-id/auth"
|
2022-06-27 17:28:39 +02:00
|
|
|
)
|
|
|
|
|
2022-07-22 03:19:24 +02:00
|
|
|
func expectAccountMatch(t *testing.T, s *Store, normEmail auth.NormalizedEmail, expectedEmail auth.Email, password auth.Password, seed auth.ClientSaltSeed) {
|
2022-07-13 18:32:48 +02:00
|
|
|
var key auth.KDFKey
|
2022-07-15 21:36:11 +02:00
|
|
|
var salt auth.ServerSalt
|
2022-07-22 03:19:24 +02:00
|
|
|
var email auth.Email
|
2022-07-13 18:32:48 +02:00
|
|
|
|
|
|
|
err := s.db.QueryRow(
|
2022-07-22 03:19:24 +02:00
|
|
|
`SELECT key, server_salt, email from accounts WHERE normalized_email=? AND client_salt_seed=?`,
|
|
|
|
normEmail, seed,
|
|
|
|
).Scan(&key, &salt, &email)
|
2022-06-27 17:28:39 +02:00
|
|
|
if err != nil {
|
2022-07-22 03:19:24 +02:00
|
|
|
t.Fatalf("Error finding account for: %s %s - %+v", normEmail, password, err)
|
2022-06-27 17:28:39 +02:00
|
|
|
}
|
2022-06-27 21:59:56 +02:00
|
|
|
|
2022-07-13 18:32:48 +02:00
|
|
|
match, err := password.Check(key, salt)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error checking password for: %s %s - %+v", email, password, err)
|
|
|
|
}
|
|
|
|
if !match {
|
2022-07-22 03:19:24 +02:00
|
|
|
t.Fatalf("Password incorrect for: %s %s", email, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
if email != expectedEmail {
|
|
|
|
t.Fatalf("Email case not as expected. Want: %s Got: %s", email, expectedEmail)
|
2022-06-27 21:59:56 +02:00
|
|
|
}
|
2022-06-27 17:28:39 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 03:19:24 +02:00
|
|
|
func expectAccountNotExists(t *testing.T, s *Store, normEmail auth.NormalizedEmail) {
|
2022-06-27 21:59:56 +02:00
|
|
|
rows, err := s.db.Query(
|
2022-07-22 03:19:24 +02:00
|
|
|
`SELECT 1 from accounts WHERE normalized_email=?`,
|
|
|
|
normEmail,
|
2022-06-27 21:59:56 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-07-22 03:19:24 +02:00
|
|
|
t.Fatalf("Error finding account for: %s - %+v", normEmail, err)
|
2022-06-27 17:28:39 +02:00
|
|
|
}
|
2022-06-27 21:59:56 +02:00
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
2022-07-22 03:19:24 +02:00
|
|
|
t.Fatalf("Expected no account for: %s", normEmail)
|
2022-06-27 21:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// found nothing, we're good
|
2022-06-27 17:28:39 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 18:32:48 +02:00
|
|
|
// Test CreateAccount
|
2022-06-27 17:28:39 +02:00
|
|
|
// Try CreateAccount twice with the same email and different password, error the second time
|
|
|
|
func TestStoreCreateAccount(t *testing.T) {
|
|
|
|
s, sqliteTmpFile := StoreTestInit(t)
|
|
|
|
defer StoreTestCleanup(sqliteTmpFile)
|
|
|
|
|
2022-07-22 03:19:24 +02:00
|
|
|
email, normEmail := auth.Email("Abc@Example.Com"), auth.NormalizedEmail("abc@example.com")
|
|
|
|
password, seed := auth.Password("123"), auth.ClientSaltSeed("abcd1234abcd1234")
|
2022-06-27 17:28:39 +02:00
|
|
|
|
|
|
|
// Get an account, come back empty
|
2022-07-22 03:19:24 +02:00
|
|
|
expectAccountNotExists(t, &s, normEmail)
|
2022-06-27 17:28:39 +02:00
|
|
|
|
|
|
|
// Create an account
|
2022-07-15 21:36:11 +02:00
|
|
|
if err := s.CreateAccount(email, password, seed); err != nil {
|
2022-06-27 17:28:39 +02:00
|
|
|
t.Fatalf("Unexpected error in CreateAccount: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get and confirm the account we just put in
|
2022-07-22 03:19:24 +02:00
|
|
|
expectAccountMatch(t, &s, normEmail, email, password, seed)
|
2022-06-27 17:28:39 +02:00
|
|
|
|
|
|
|
newPassword := auth.Password("xyz")
|
|
|
|
|
|
|
|
// Try to create a new account with the same email and different password,
|
|
|
|
// fail because email already exists
|
2022-07-15 21:36:11 +02:00
|
|
|
if err := s.CreateAccount(email, newPassword, seed); err != ErrDuplicateAccount {
|
2022-06-27 17:28:39 +02:00
|
|
|
t.Fatalf(`CreateAccount err: wanted "%+v", got "%+v"`, ErrDuplicateAccount, err)
|
|
|
|
}
|
|
|
|
|
2022-07-22 03:19:24 +02:00
|
|
|
differentCaseEmail := auth.Email("aBC@examplE.CoM")
|
|
|
|
|
|
|
|
// Try to create a new account with the same email different capitalization.
|
|
|
|
// fail because email already exists
|
|
|
|
if err := s.CreateAccount(differentCaseEmail, password, seed); err != ErrDuplicateAccount {
|
|
|
|
t.Fatalf(`CreateAccount err (for case insensitivity check): wanted "%+v", got "%+v"`, ErrDuplicateAccount, err)
|
|
|
|
}
|
|
|
|
|
2022-07-13 18:32:48 +02:00
|
|
|
// Get the email and same *first* password we successfully put in
|
2022-07-22 03:19:24 +02:00
|
|
|
expectAccountMatch(t, &s, normEmail, email, password, seed)
|
2022-06-27 17:28:39 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 18:32:48 +02:00
|
|
|
// Test GetUserId for nonexisting email
|
|
|
|
func TestStoreGetUserIdAccountNotExists(t *testing.T) {
|
2022-06-27 17:28:39 +02:00
|
|
|
s, sqliteTmpFile := StoreTestInit(t)
|
|
|
|
defer StoreTestCleanup(sqliteTmpFile)
|
|
|
|
|
|
|
|
email, password := auth.Email("abc@example.com"), auth.Password("123")
|
|
|
|
|
|
|
|
// Check that there's no user id for email and password first
|
2022-07-01 00:44:59 +02:00
|
|
|
if userId, err := s.GetUserId(email, password); err != ErrWrongCredentials || userId != 0 {
|
2022-07-13 18:32:48 +02:00
|
|
|
t.Fatalf(`GetUserId error for nonexistant account: wanted "%+v", got "%+v. userId: %v"`, ErrWrongCredentials, err, userId)
|
2022-06-27 17:28:39 +02:00
|
|
|
}
|
2022-07-13 18:32:48 +02:00
|
|
|
}
|
2022-06-27 17:28:39 +02:00
|
|
|
|
2022-07-13 18:32:48 +02:00
|
|
|
// Test GetUserId for existing account, with the correct and incorrect password
|
|
|
|
func TestStoreGetUserIdAccountExists(t *testing.T) {
|
|
|
|
s, sqliteTmpFile := StoreTestInit(t)
|
|
|
|
defer StoreTestCleanup(sqliteTmpFile)
|
|
|
|
|
2022-07-15 21:36:11 +02:00
|
|
|
createdUserId, email, password, _ := makeTestUser(t, &s)
|
2022-06-27 17:28:39 +02:00
|
|
|
|
2022-07-22 03:19:24 +02:00
|
|
|
// Check that the userId is correct for the email, irrespective of the case of
|
|
|
|
// the characters in the email.
|
|
|
|
lowerEmail := auth.Email(strings.ToLower(string(email)))
|
|
|
|
upperEmail := auth.Email(strings.ToUpper(string(email)))
|
|
|
|
|
2022-06-27 17:28:39 +02:00
|
|
|
// Check that there's now a user id for the email and password
|
2022-07-22 03:19:24 +02:00
|
|
|
if userId, err := s.GetUserId(lowerEmail, password); err != nil || userId != createdUserId {
|
|
|
|
t.Fatalf("Unexpected error in GetUserId: err: %+v userId: %v", err, userId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that there's now a user id for the email and password
|
|
|
|
if userId, err := s.GetUserId(upperEmail, password); err != nil || userId != createdUserId {
|
2022-06-27 17:28:39 +02:00
|
|
|
t.Fatalf("Unexpected error in GetUserId: err: %+v userId: %v", err, userId)
|
|
|
|
}
|
2022-07-13 18:32:48 +02:00
|
|
|
|
|
|
|
// Check that it won't return if the wrong password is given
|
|
|
|
if userId, err := s.GetUserId(email, password+auth.Password("_wrong")); err != ErrWrongCredentials || userId != 0 {
|
|
|
|
t.Fatalf(`GetUserId error for wrong password: wanted "%+v", got "%+v. userId: %v"`, ErrWrongCredentials, err, userId)
|
|
|
|
}
|
2022-06-27 17:28:39 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 17:36:58 +02:00
|
|
|
func TestStoreAccountEmptyFields(t *testing.T) {
|
|
|
|
// Make sure expiration doesn't get set if sanitization fails
|
2022-06-29 06:06:43 +02:00
|
|
|
tt := []struct {
|
2022-07-15 21:36:11 +02:00
|
|
|
name string
|
|
|
|
email auth.Email
|
|
|
|
clientSaltSeed auth.ClientSaltSeed
|
|
|
|
password auth.Password
|
2022-06-29 06:06:43 +02:00
|
|
|
}{
|
|
|
|
{
|
2022-07-15 21:36:11 +02:00
|
|
|
name: "missing email",
|
|
|
|
email: "",
|
|
|
|
clientSaltSeed: "abcd1234abcd1234",
|
|
|
|
password: "xyz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing client salt seed",
|
|
|
|
email: "a@example.com",
|
|
|
|
clientSaltSeed: "",
|
|
|
|
password: "xyz",
|
2022-06-29 06:06:43 +02:00
|
|
|
},
|
2022-07-13 18:32:48 +02:00
|
|
|
// Not testing empty key and salt because they get generated to something
|
2022-07-15 21:36:11 +02:00
|
|
|
// non-empty in the method, even if email is empty
|
2022-06-29 06:06:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
s, sqliteTmpFile := StoreTestInit(t)
|
|
|
|
defer StoreTestCleanup(sqliteTmpFile)
|
|
|
|
|
|
|
|
var sqliteErr sqlite3.Error
|
|
|
|
|
2022-07-15 21:36:11 +02:00
|
|
|
err := s.CreateAccount(tc.email, tc.password, tc.clientSaltSeed)
|
2022-06-29 06:06:43 +02:00
|
|
|
if errors.As(err, &sqliteErr) {
|
|
|
|
if errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintCheck) {
|
|
|
|
return // We got the error we expected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("Expected check constraint error for empty field. Got %+v", err)
|
|
|
|
})
|
|
|
|
}
|
2022-06-27 17:28:39 +02:00
|
|
|
}
|
2022-07-15 21:36:11 +02:00
|
|
|
|
|
|
|
// Test GetClientSaltSeed for existing account
|
|
|
|
func TestStoreGetClientSaltSeedAccountSuccess(t *testing.T) {
|
|
|
|
s, sqliteTmpFile := StoreTestInit(t)
|
|
|
|
defer StoreTestCleanup(sqliteTmpFile)
|
|
|
|
|
|
|
|
_, email, _, createdSeed := makeTestUser(t, &s)
|
|
|
|
|
2022-07-22 03:19:24 +02:00
|
|
|
// Check that the seed is correct for the email, irrespective of the case of
|
|
|
|
// the characters in the email.
|
|
|
|
lowerEmail := auth.Email(strings.ToLower(string(email)))
|
|
|
|
upperEmail := auth.Email(strings.ToUpper(string(email)))
|
|
|
|
|
|
|
|
if seed, err := s.GetClientSaltSeed(lowerEmail); err != nil || seed != createdSeed {
|
|
|
|
t.Fatalf("Unexpected error in GetClientSaltSeed: err: %+v seed: %v", err, seed)
|
|
|
|
}
|
|
|
|
if seed, err := s.GetClientSaltSeed(upperEmail); err != nil || seed != createdSeed {
|
2022-07-15 21:36:11 +02:00
|
|
|
t.Fatalf("Unexpected error in GetClientSaltSeed: err: %+v seed: %v", err, seed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test GetClientSaltSeed for nonexisting email
|
|
|
|
func TestStoreGetClientSaltSeedAccountNotExists(t *testing.T) {
|
|
|
|
s, sqliteTmpFile := StoreTestInit(t)
|
|
|
|
defer StoreTestCleanup(sqliteTmpFile)
|
|
|
|
|
|
|
|
email := auth.Email("abc@example.com")
|
|
|
|
|
|
|
|
// Check that there's no user id for email and password first
|
|
|
|
if seed, err := s.GetClientSaltSeed(email); err != ErrWrongCredentials || seed != "" {
|
|
|
|
t.Fatalf(`GetClientSaltSeed error for nonexistant account: wanted "%+v", got "%+v. seed: %v"`, ErrWrongCredentials, err, seed)
|
|
|
|
}
|
|
|
|
}
|