2022-07-16 05:56:12 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
_ "embed"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
)
|
|
|
|
|
|
|
|
// go:embed schema.sql
|
|
|
|
var schema string
|
|
|
|
|
|
|
|
func EnsureSchema() {
|
|
|
|
// TODO look into tern
|
|
|
|
}
|
|
|
|
|
|
|
|
func connect() (*pgxpool.Pool, error) {
|
|
|
|
// TODO read dburl from environment
|
|
|
|
conn, err := pgxpool.Connect(context.Background(), "postgres://vilmibm:vilmibm@localhost:5432/hermeticum")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2022-07-16 06:54:18 +00:00
|
|
|
func CreateAccount(name, password string) (*Account, error) {
|
2022-07-16 05:56:12 +00:00
|
|
|
conn, err := connect()
|
|
|
|
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-16 06:54:18 +00:00
|
|
|
func ValidateCredentials(name, password string) (*Account, error) {
|
2022-07-16 05:56:12 +00:00
|
|
|
a, err := GetAccount(name)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
type Account struct {
|
2022-07-16 06:54:18 +00:00
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Pwhash string
|
2022-07-16 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetAccount(name string) (*Account, error) {
|
|
|
|
conn, err := connect()
|
|
|
|
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-16 06:54:18 +00:00
|
|
|
func StartSession(a Account) (sessionID string, err error) {
|
2022-07-16 05:56:12 +00:00
|
|
|
var conn *pgxpool.Pool
|
|
|
|
conn, err = connect()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionID = uuid.New().String()
|
|
|
|
|
2022-07-16 06:54:18 +00:00
|
|
|
_, err = conn.Exec(context.Background(), "INSERT INTO sessions (session_id, account) VALUES ( $1, $2 )", sessionID, a.ID)
|
2022-07-16 05:56:12 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|