create support early auth functions
This commit is contained in:
+30
-21
@@ -2,9 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"errors"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
@@ -18,10 +19,10 @@ func InitDatabase(ctx context.Context) {
|
||||
|
||||
_, err = conn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(20) UNIQUE NOT NULL,
|
||||
pass_hash VARCHAR(255) NOT NULL,
|
||||
color VARCHAR(3) NOT NULL
|
||||
Id SERIAL PRIMARY KEY,
|
||||
Name VARCHAR(20) UNIQUE NOT NULL,
|
||||
pass_hash VARCHAR(60) NOT NULL,
|
||||
Color VARCHAR(3) NOT NULL
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
@@ -31,44 +32,52 @@ func InitDatabase(ctx context.Context) {
|
||||
}
|
||||
|
||||
func AddNewUser(ctx context.Context, user User) (uint, error) {
|
||||
if len(user.name) == 0 || len(user.name) > 20 {
|
||||
var id uint
|
||||
var err error
|
||||
|
||||
if len(user.Name) == 0 || len(user.Name) > 20 {
|
||||
return 0, errors.New("username bad length")
|
||||
}
|
||||
if user.isPasswordHashed {
|
||||
if len(user.password) != 255 {
|
||||
if user.IsPasswordHashed {
|
||||
if len(user.Password) != 60 {
|
||||
return 0, errors.New("password bad length")
|
||||
}
|
||||
} else {
|
||||
// TODO
|
||||
var hashed []byte
|
||||
hashed, err = bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
user.Password = string(hashed)
|
||||
}
|
||||
if len(user.color) != 1 && len(user.color) != 3 {
|
||||
if len(user.Color) != 1 && len(user.Color) != 3 {
|
||||
return 0, errors.New("color invalid")
|
||||
}
|
||||
var id uint
|
||||
err := dbConnection.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, color)
|
||||
err = dbConnection.QueryRow(ctx, `
|
||||
INSERT INTO users (Name, pass_hash, Color)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id
|
||||
`, user.name, user.isPasswordHashed, user.color).Scan(&id)
|
||||
RETURNING Id
|
||||
`, user.Name, user.IsPasswordHashed, user.Color).Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
func CheckPassword(ctx context.Context, id string, hash string) bool {
|
||||
func CheckPassword(ctx context.Context, id string, plainPassword string) bool {
|
||||
var controlHash string
|
||||
err := dbConnection.QueryRow(ctx, "SELECT pass_hash FROM users WHERE id = $1", id).Scan(&controlHash)
|
||||
err := dbConnection.QueryRow(ctx, "SELECT pass_hash FROM users WHERE Id = $1", id).Scan(&controlHash)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return subtle.ConstantTimeCompare([]byte(controlHash), []byte(hash)) == 1
|
||||
|
||||
return bcrypt.CompareHashAndPassword([]byte(controlHash), []byte(plainPassword)) == nil
|
||||
}
|
||||
|
||||
func GetUserData(ctx context.Context, id string) (User, error) {
|
||||
var user User
|
||||
err := dbConnection.QueryRow(ctx, "SELECT id, name, pass_hash, color FROM users WHERE id = $1", id).
|
||||
Scan(&user.id, &user.name, &user.password, &user.color)
|
||||
err := dbConnection.QueryRow(ctx, "SELECT Id, Name, pass_hash, Color FROM users WHERE Id = $1", id).
|
||||
Scan(&user.Id, &user.Name, &user.Password, &user.Color)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
user.isPasswordHashed = true
|
||||
user.IsPasswordHashed = true
|
||||
return user, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user