2022-10-20 16:59:57 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"GoWeb/app"
|
2023-09-03 20:45:12 +00:00
|
|
|
"log/slog"
|
2022-10-20 16:59:57 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2022-10-22 01:17:11 +00:00
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2022-10-20 16:59:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int64
|
|
|
|
Username string
|
|
|
|
Password string
|
2023-02-18 00:55:27 +00:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
2022-10-20 16:59:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
const userColumnsNoId = "\"Username\", \"Password\", \"CreatedAt\", \"UpdatedAt\""
|
|
|
|
const userColumns = "\"Id\", " + userColumnsNoId
|
|
|
|
const userTable = "public.\"User\""
|
|
|
|
|
|
|
|
const (
|
2023-04-08 02:27:14 +00:00
|
|
|
selectUserById = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Id\" = $1"
|
|
|
|
selectUserByUsername = "SELECT " + userColumns + " FROM " + userTable + " WHERE \"Username\" = $1"
|
|
|
|
insertUser = "INSERT INTO " + userTable + " (" + userColumnsNoId + ") VALUES ($1, $2, $3, $4) RETURNING \"Id\""
|
2023-03-05 21:46:43 +00:00
|
|
|
)
|
|
|
|
|
2022-12-22 03:22:11 +00:00
|
|
|
// GetCurrentUser finds the currently logged-in user by session cookie
|
2022-11-04 18:58:03 +00:00
|
|
|
func GetCurrentUser(app *app.App, r *http.Request) (User, error) {
|
|
|
|
cookie, err := r.Cookie("session")
|
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
2023-04-08 02:23:46 +00:00
|
|
|
session, err := GetSessionByAuthToken(app, cookie.Value)
|
2022-11-04 18:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
2023-04-08 02:23:46 +00:00
|
|
|
return GetUserById(app, session.UserId)
|
2022-11-04 18:58:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
// GetUserById finds a User table row in the database by id and returns a struct representing this row
|
2022-10-20 16:59:57 +00:00
|
|
|
func GetUserById(app *app.App, id int64) (User, error) {
|
|
|
|
user := User{}
|
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
err := app.Db.QueryRow(selectUserById, id).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
|
2022-10-20 16:59:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
2023-02-28 20:54:55 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
// GetUserByUsername finds a User table row in the database by username and returns a struct representing this row
|
2023-02-28 20:54:55 +00:00
|
|
|
func GetUserByUsername(app *app.App, username string) (User, error) {
|
|
|
|
user := User{}
|
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
err := app.Db.QueryRow(selectUserByUsername, username).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
|
2022-10-20 16:59:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
// CreateUser creates a User table row in the database
|
2022-10-20 16:59:57 +00:00
|
|
|
func CreateUser(app *app.App, username string, password string, createdAt time.Time, updatedAt time.Time) (User, error) {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
2023-09-03 20:45:12 +00:00
|
|
|
slog.Error("error hashing password: " + err.Error())
|
2022-10-20 16:59:57 +00:00
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastInsertId int64
|
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
err = app.Db.QueryRow(insertUser, username, string(hash), createdAt, updatedAt).Scan(&lastInsertId)
|
2022-10-20 16:59:57 +00:00
|
|
|
if err != nil {
|
2023-09-03 20:45:12 +00:00
|
|
|
slog.Error("error creating user row: " + err.Error())
|
2022-10-20 16:59:57 +00:00
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetUserById(app, lastInsertId)
|
|
|
|
}
|
|
|
|
|
2023-02-28 20:54:55 +00:00
|
|
|
// AuthenticateUser validates the password for the specified user
|
2023-04-06 13:56:48 +00:00
|
|
|
func AuthenticateUser(app *app.App, w http.ResponseWriter, username string, password string, remember bool) (Session, error) {
|
2023-02-28 20:54:55 +00:00
|
|
|
var user User
|
2022-10-20 16:59:57 +00:00
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
err := app.Db.QueryRow(selectUserByUsername, username).Scan(&user.Id, &user.Username, &user.Password, &user.CreatedAt, &user.UpdatedAt)
|
2022-10-20 16:59:57 +00:00
|
|
|
if err != nil {
|
2023-09-03 20:45:12 +00:00
|
|
|
slog.Info("user not found: " + username)
|
2023-02-28 20:54:55 +00:00
|
|
|
return Session{}, err
|
2022-10-20 16:59:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 20:54:55 +00:00
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
2022-10-20 16:59:57 +00:00
|
|
|
if err != nil { // Failed to validate password, doesn't match
|
2023-09-03 20:45:12 +00:00
|
|
|
slog.Info("incorrect password:" + username)
|
2023-02-28 20:54:55 +00:00
|
|
|
return Session{}, err
|
2022-10-20 16:59:57 +00:00
|
|
|
} else {
|
2023-04-06 13:56:48 +00:00
|
|
|
return CreateSession(app, w, user.Id, remember)
|
2022-11-06 16:50:54 +00:00
|
|
|
}
|
2022-10-22 02:04:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 21:46:43 +00:00
|
|
|
// LogoutUser deletes the session cookie and AuthToken from the database
|
2022-10-22 02:04:46 +00:00
|
|
|
func LogoutUser(app *app.App, w http.ResponseWriter, r *http.Request) {
|
|
|
|
cookie, err := r.Cookie("session")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-28 20:54:55 +00:00
|
|
|
err = DeleteSessionByAuthToken(app, w, cookie.Value)
|
2022-10-22 02:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|