Перевести API на PostgreSQL и сохраняемые Bearer-сессии
This commit is contained in:
+44
-26
@@ -1,10 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@@ -15,44 +21,56 @@ type User struct {
|
||||
}
|
||||
|
||||
type userStore struct {
|
||||
mu sync.RWMutex
|
||||
byID map[string]User
|
||||
byUsername map[string]string
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func newUserStore() *userStore {
|
||||
return &userStore{
|
||||
byID: make(map[string]User),
|
||||
byUsername: make(map[string]string),
|
||||
}
|
||||
func newUserStore(db *pgxpool.Pool) *userStore {
|
||||
return &userStore{db: db}
|
||||
}
|
||||
|
||||
func (s *userStore) create(username string, displayName string) (User, bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
func derivePassword(password string, salt []byte) ([]byte, error) {
|
||||
return scrypt.Key([]byte(password), salt, 32768, 8, 1, 32)
|
||||
}
|
||||
|
||||
if _, exists := s.byUsername[username]; exists {
|
||||
return User{}, false
|
||||
func (s *userStore) create(ctx context.Context, username, displayName, password string) (User, error) {
|
||||
salt := make([]byte, 32)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
hash, err := derivePassword(password, salt)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
user := User{
|
||||
ID: uuid.New().String(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
s.byID[user.ID] = user
|
||||
s.byUsername[user.Username] = user.ID
|
||||
|
||||
return user, true
|
||||
_, err = s.db.Exec(ctx, "INSERT INTO users(id,username,display_name,password_hash,password_salt,created_at) VALUES($1,$2,$3,$4,$5,$6)", user.ID, user.Username, user.DisplayName, hash, salt, user.CreatedAt)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *userStore) getByID(id string) (User, bool) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
func (s *userStore) getByID(ctx context.Context, id string) (User, error) {
|
||||
var user User
|
||||
err := s.db.QueryRow(ctx, "SELECT id,username,display_name,created_at FROM users WHERE id=$1", id).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
|
||||
return user, err
|
||||
}
|
||||
|
||||
user, exists := s.byID[id]
|
||||
|
||||
return user, exists
|
||||
func (s *userStore) verifyPassword(ctx context.Context, username, password string) (User, bool, error) {
|
||||
var user User
|
||||
var hash, salt []byte
|
||||
err := s.db.QueryRow(ctx, "SELECT id,username,display_name,created_at,password_hash,password_salt FROM users WHERE username=$1", username).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash, &salt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return User{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return User{}, false, err
|
||||
}
|
||||
actual, err := derivePassword(password, salt)
|
||||
if err != nil {
|
||||
return User{}, false, err
|
||||
}
|
||||
return user, subtle.ConstantTimeCompare(hash, actual) == 1, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user