You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.3 KiB
Go

10 months ago
package api
import (
9 months ago
"bytes"
"crypto/rand"
"database/sql"
10 months ago
"encoding/base64"
"fmt"
"os"
9 months ago
"regexp"
"strconv"
"time"
10 months ago
"github.com/Flashfyre/pokerogue-server/db"
)
9 months ago
const (
UUIDSize = 16
TokenSize = 32
9 months ago
)
var isValidUsername = regexp.MustCompile(`^\w{1,16}$`).MatchString
9 months ago
type AccountInfoResponse struct {
Username string `json:"username"`
LastSessionSlot int `json:"lastSessionSlot"`
10 months ago
}
6 months ago
// /account/info - get account info
func handleAccountInfo(username string, uuid []byte) (AccountInfoResponse, error) {
var latestSave time.Time
latestSaveID := -1
for id := range sessionSlotCount {
fileName := "session"
if id != 0 {
fileName += strconv.Itoa(id)
}
stat, err := os.Stat(fmt.Sprintf("userdata/%x/%s.pzs", uuid, fileName))
if err != nil {
continue
}
if stat.ModTime().After(latestSave) {
latestSave = stat.ModTime()
latestSaveID = id
}
}
return AccountInfoResponse{Username: username, LastSessionSlot: latestSaveID}, nil
10 months ago
}
type AccountRegisterRequest GenericAuthRequest
6 months ago
// /account/register - register account
func handleAccountRegister(request AccountRegisterRequest) error {
if !isValidUsername(request.Username) {
return fmt.Errorf("invalid username")
10 months ago
}
if len(request.Password) < 6 {
return fmt.Errorf("invalid password")
9 months ago
}
uuid := make([]byte, UUIDSize)
_, err := rand.Read(uuid)
9 months ago
if err != nil {
return fmt.Errorf("failed to generate uuid: %s", err)
9 months ago
}
salt := make([]byte, ArgonSaltSize)
9 months ago
_, err = rand.Read(salt)
if err != nil {
return fmt.Errorf(fmt.Sprintf("failed to generate salt: %s", err))
9 months ago
}
err = db.AddAccountRecord(uuid, request.Username, deriveArgon2IDKey([]byte(request.Password), salt), salt)
9 months ago
if err != nil {
return fmt.Errorf("failed to add account record: %s", err)
9 months ago
}
return nil
10 months ago
}
type AccountLoginRequest GenericAuthRequest
type AccountLoginResponse GenericAuthResponse
6 months ago
// /account/login - log into account
func handleAccountLogin(request AccountLoginRequest) (AccountLoginResponse, error) {
if !isValidUsername(request.Username) {
return AccountLoginResponse{}, fmt.Errorf("invalid username")
9 months ago
}
if len(request.Password) < 6 {
return AccountLoginResponse{}, fmt.Errorf("invalid password")
9 months ago
}
key, salt, err := db.FetchAccountKeySaltFromUsername(request.Username)
9 months ago
if err != nil {
if err == sql.ErrNoRows {
return AccountLoginResponse{}, fmt.Errorf("account doesn't exist")
9 months ago
}
return AccountLoginResponse{}, err
9 months ago
}
if !bytes.Equal(key, deriveArgon2IDKey([]byte(request.Password), salt)) {
return AccountLoginResponse{}, fmt.Errorf("password doesn't match")
9 months ago
}
token := make([]byte, TokenSize)
9 months ago
_, err = rand.Read(token)
if err != nil {
return AccountLoginResponse{}, fmt.Errorf("failed to generate token: %s", err)
9 months ago
}
err = db.AddAccountSession(request.Username, token)
9 months ago
if err != nil {
return AccountLoginResponse{}, fmt.Errorf("failed to add account session")
9 months ago
}
return AccountLoginResponse{Token: base64.StdEncoding.EncodeToString(token)}, nil
10 months ago
}
// /account/logout - log out of account
func handleAccountLogout(token []byte) error {
if len(token) != TokenSize {
return fmt.Errorf("invalid token")
9 months ago
}
err := db.RemoveSessionFromToken(token)
9 months ago
if err != nil {
if err == sql.ErrNoRows {
return fmt.Errorf("token not found")
9 months ago
}
return fmt.Errorf("failed to remove account session")
9 months ago
}
10 months ago
return nil
10 months ago
}