LocalAuthentication

This commit is contained in:
sbouaram 2023-04-26 10:57:49 +02:00
parent d0d30ac8ab
commit ada858a738
2 changed files with 128 additions and 11 deletions

View File

@ -47,24 +47,24 @@ func (psql PostgresqlStorage) GetAccount(id string) (*Account, error) {
if isUUIDv4(id) {
stmtAccounts, err := psql.DbConnection.Prepare("SELECT id, namespace, data, metadata FROM accounts WHERE id = $1")
if err != nil {
return account, fmt.Errorf("psql connection failed")
return nil, fmt.Errorf("psql connection failed")
}
defer stmtAccounts.Close()
err = stmtAccounts.QueryRow(id).Scan(&account.ID, &account.Namespace, &data, &metadata)
if err != nil {
return account, fmt.Errorf("psql select account query failed")
return nil, fmt.Errorf("psql select account query failed")
}
err = json.Unmarshal(data, &account.Data)
if err != nil {
return account, err
return nil, err
}
err = json.Unmarshal(metadata, &account.Metadata)
if err != nil {
return account, err
return nil, err
}
stmtAccount_auth, err := psql.DbConnection.Prepare("SELECT username, password, email, email_validation, phone_number, phone_number_validation FROM account_auth WHERE account_id= $1")
if err != nil {
return account, fmt.Errorf("psql connection failed")
return nil, fmt.Errorf("psql connection failed")
}
defer stmtAccount_auth.Close()
err = stmtAccount_auth.QueryRow(id).Scan(&account.Authentication.Local.Username,
@ -74,22 +74,110 @@ func (psql PostgresqlStorage) GetAccount(id string) (*Account, error) {
&account.Authentication.Local.PhoneNumber,
&phoneValidation)
if err != nil {
return account, fmt.Errorf("psql select account auth query failed")
return nil, fmt.Errorf("psql select account auth query failed")
}
err = json.Unmarshal(emailValidation, &account.Authentication.Local.EmailValidation)
if err != nil {
return account, err
return nil, err
}
err = json.Unmarshal(phoneValidation, &account.Authentication.Local.PhoneNumberValidation)
if err != nil {
return account, err
return nil, err
}
}
return account, nil
}
func (PostgresqlStorage) LocalAuthentication(namespace string, username string, email string, phone_number string) (*Account, error) {
return nil, fmt.Errorf("")
func (psql PostgresqlStorage) LocalAuthentication(namespace string, username string, email string, phone_number string) (*Account, error) {
account := &Account{}
var (
data, metadata, emailValidation, phoneValidation []byte
)
if username != "" {
usernameStmt, err := psql.DbConnection.Prepare("SELECT id, namespace, data, metadata, username, password, email, email_validation, phone_number, phone_number_validation FROM accounts INNER JOIN account_auth ON accounts.id = account_auth.account_id WHERE namespace = $1 AND username = $2;")
if err != nil {
return nil, err
}
err = usernameStmt.QueryRow(namespace, username).Scan(&account.ID, &account.Namespace, &data, &metadata, &account.Authentication.Local.Username,
&account.Authentication.Local.Password, &account.Authentication.Local.Email, &emailValidation, &account.Authentication.Local.PhoneNumber, &phoneValidation)
if err != nil {
return nil, err
}
err = json.Unmarshal(metadata, &account.Metadata)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &account.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(emailValidation, &account.Authentication.Local.EmailValidation)
if err != nil {
return nil, err
}
err = json.Unmarshal(phoneValidation, &account.Authentication.Local.PhoneNumberValidation)
if err != nil {
return nil, err
}
return account, nil
} else if email != "" {
account.Authentication.Local.Email = email
emailStmt, err := psql.DbConnection.Prepare("SELECT id, namespace, data, metadata, username, password, email_validation, phone_number, phone_number_validation FROM accounts INNER JOIN account_auth ON accounts.id = account_auth.account_id WHERE namespace = $1 AND email = $2;")
if err != nil {
return nil, err
}
err = emailStmt.QueryRow(namespace, email).Scan(&account.ID, &account.Namespace, &data, &metadata, &account.Authentication.Local.Username,
&account.Authentication.Local.Password, &emailValidation, &account.Authentication.Local.PhoneNumber, &phoneValidation)
if err != nil {
return nil, err
}
err = json.Unmarshal(metadata, &account.Metadata)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &account.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(emailValidation, &account.Authentication.Local.EmailValidation)
if err != nil {
return nil, err
}
err = json.Unmarshal(phoneValidation, &account.Authentication.Local.PhoneNumberValidation)
if err != nil {
return nil, err
}
return account, nil
} else if phone_number != "" {
account.Authentication.Local.PhoneNumber = phone_number
phoneStmt, err := psql.DbConnection.Prepare("SELECT id, namespace, data, metadata, username, password, email, email_validation, phone_number_validation FROM accounts INNER JOIN account_auth ON accounts.id = account_auth.account_id WHERE namespace = $1 AND phone_number = $2;")
if err != nil {
return nil, err
}
err = phoneStmt.QueryRow(namespace, phone_number).Scan(&account.ID, &account.Namespace, &data, &metadata, &account.Authentication.Local.Username,
&account.Authentication.Local.Password, &account.Authentication.Local.Email, &emailValidation, &phoneValidation)
if err != nil {
return nil, err
}
err = json.Unmarshal(metadata, &account.Metadata)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &account.Data)
if err != nil {
return nil, err
}
err = json.Unmarshal(emailValidation, &account.Authentication.Local.EmailValidation)
if err != nil {
return nil, err
}
err = json.Unmarshal(phoneValidation, &account.Authentication.Local.PhoneNumberValidation)
if err != nil {
return nil, err
}
return account, nil
}
return account, nil
}
func (PostgresqlStorage) GetAccounts(namespaces []string) ([]Account, error) {

View File

@ -23,6 +23,8 @@ func init() {
func TestNewPostgresqlStorage(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
storage.DbConnection.Exec("Delete from accounts;")
storage.DbConnection.Exec("Delete from account_auth;")
if err != nil {
t.Errorf("error creating new PostgreSQL storage: %v", err)
}
@ -144,7 +146,7 @@ func TestPostgresqlStorage_CreateAccount(t *testing.T) {
}
account := Account{
ID: Id,
Namespace: "test_namespace",
Namespace: "namespace",
Authentication: AccountAuth{
Local: localAuth,
},
@ -232,3 +234,30 @@ func TestPostgresqlStorage_UpdateAccount(t *testing.T) {
t.Errorf("failed")
}
}
func TestPostgresqlStorage_LocalAuthentication(t *testing.T) {
db, err := NewPostgresqlStorage(cfg)
if err != nil {
t.Errorf("failed to create new psql connection")
}
accountByUsername, err := db.LocalAuthentication("test_namespace", "testuser", "", "")
if err != nil {
t.Errorf("Failed LocalAuthentication based on username and namespace")
}
fmt.Println(accountByUsername)
accountByEmail, err := db.LocalAuthentication("test_namespace", "", "test@test.com", "")
if err != nil {
t.Errorf("Failed LocalAuthentication based on username and namespace")
}
fmt.Println(accountByEmail)
accountByPhone, err := db.LocalAuthentication("test_namespace", "", "", "1234567890")
if err != nil {
t.Errorf("Failed LocalAuthentication based on username and namespace")
}
fmt.Println(accountByPhone)
accountByAll, err := db.LocalAuthentication("test_namespace", "testuser", "test@test.com", "1234567890")
if err != nil {
t.Errorf("Failed LocalAuthentication based on username and namespace")
}
fmt.Println(accountByAll)
}