2022-07-16 05:56:12 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
_ "embed"
|
|
|
|
"errors"
|
2022-07-28 01:45:21 +00:00
|
|
|
"log"
|
2022-07-16 05:56:12 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
// go:embed schema.sql
|
|
|
|
var schema string
|
|
|
|
|
2022-07-28 01:45:21 +00:00
|
|
|
type Account struct {
|
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Pwhash string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DB interface {
|
|
|
|
// EnsureSchema() TODO look into tern
|
|
|
|
CreateAccount(string, string) (*Account, error)
|
|
|
|
ValidateCredentials(string, string) (*Account, error)
|
|
|
|
GetAccount(string) (*Account, error)
|
|
|
|
StartSession(Account) (string, error)
|
|
|
|
EndSession(string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type pgDB struct {
|
|
|
|
pool *pgxpool.Pool
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 01:45:21 +00:00
|
|
|
func NewDB(connURL string) (DB, error) {
|
|
|
|
pool, err := pgxpool.Connect(context.Background(), connURL)
|
2022-07-16 05:56:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-28 01:45:21 +00:00
|
|
|
pgdb := &pgDB{
|
|
|
|
pool: pool,
|
|
|
|
}
|
2022-07-16 05:56:12 +00:00
|
|
|
|
2022-07-28 01:45:21 +00:00
|
|
|
return pgdb, nil
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 01:45:21 +00:00
|
|
|
func (db *pgDB) CreateAccount(name, password string) (*Account, error) {
|
|
|
|
conn, err := db.pool.Acquire(context.Background())
|
2022-07-16 05:56:12 +00:00
|
|
|
if err != nil {
|
2022-07-16 06:54:18 +00:00
|
|
|
return nil, err
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 06:54:18 +00:00
|
|
|
_, err = conn.Exec(context.Background(),
|
|
|
|
"INSERT INTO accounts (name, pwhash) VALUES ( $1, $2 )", name, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-16 06:59:52 +00:00
|
|
|
row := conn.QueryRow(context.Background(), "SELECT id,name,pwhash FROM accounts WHERE name = $1", name)
|
2022-07-16 06:54:18 +00:00
|
|
|
a := &Account{}
|
|
|
|
err = row.Scan(&a.ID, &a.Name, &a.Pwhash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-16 05:56:12 +00:00
|
|
|
|
|
|
|
// TODO handle and cleanup unqiue violations
|
|
|
|
|
2022-07-16 06:54:18 +00:00
|
|
|
return a, err
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 01:45:21 +00:00
|
|
|
func (db *pgDB) ValidateCredentials(name, password string) (*Account, error) {
|
|
|
|
a, err := db.GetAccount(name)
|
2022-07-16 05:56:12 +00:00
|
|
|
if err != nil {
|
2022-07-16 06:54:18 +00:00
|
|
|
return nil, err
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO hashing lol
|
|
|
|
|
2022-07-16 06:54:18 +00:00
|
|
|
if a.Pwhash != password {
|
|
|
|
return nil, errors.New("invalid credentials")
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 06:54:18 +00:00
|
|
|
return a, nil
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 01:45:21 +00:00
|
|
|
func (db *pgDB) GetAccount(name string) (*Account, error) {
|
|
|
|
conn, err := db.pool.Acquire(context.Background())
|
2022-07-16 05:56:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-16 06:54:18 +00:00
|
|
|
row := conn.QueryRow(context.Background(), "SELECT id, name, pwhash FROM accounts WHERE name = $1", name)
|
2022-07-16 05:56:12 +00:00
|
|
|
|
|
|
|
a := &Account{}
|
|
|
|
|
2022-07-16 06:54:18 +00:00
|
|
|
err = row.Scan(&a.ID, &a.Name, &a.Pwhash)
|
2022-07-16 05:56:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 01:45:21 +00:00
|
|
|
func (db *pgDB) StartSession(a Account) (sessionID string, err error) {
|
|
|
|
conn, err := db.pool.Acquire(context.Background())
|
2022-07-16 05:56:12 +00:00
|
|
|
if err != nil {
|
2022-07-28 01:45:21 +00:00
|
|
|
return "", err
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sessionID = uuid.New().String()
|
|
|
|
|
2022-07-28 02:05:48 +00:00
|
|
|
_, err = conn.Exec(context.Background(), "INSERT INTO sessions (id, account) VALUES ( $1, $2 )", sessionID, a.ID)
|
2022-07-16 05:56:12 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-07-28 01:45:27 +00:00
|
|
|
|
|
|
|
func (db *pgDB) EndSession(sid string) error {
|
|
|
|
if sid == "" {
|
|
|
|
log.Println("db.EndSession called with empty session id")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := db.pool.Acquire(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-28 02:05:48 +00:00
|
|
|
_, err = conn.Exec(context.Background(), "DELETE FROM sessions WHERE id = $1", sid)
|
2022-07-28 01:45:27 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|