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.
rogueserver/api/account.go

147 lines
3.4 KiB
Go

package api
import (
1 year ago
"bytes"
"crypto/rand"
"database/sql"
"encoding/base64"
"fmt"
"os"
1 year ago
"regexp"
"strconv"
"time"
"github.com/Flashfyre/pokerogue-server/db"
1 year ago
"golang.org/x/crypto/argon2"
)
1 year ago
const (
UUIDSize = 16
ArgonTime = 1
ArgonMemory = 256 * 1024
ArgonThreads = 4
ArgonKeySize = 32
ArgonSaltSize = 16
1 year ago
)
var isValidUsername = regexp.MustCompile(`^\w{1,16}$`).MatchString
1 year ago
type AccountInfoResponse struct {
Username string `json:"username"`
LastSessionSlot int `json:"lastSessionSlot"`
}
9 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
}
type AccountRegisterRequest GenericAuthRequest
9 months ago
// /account/register - register account
func handleAccountRegister(username, password string) error {
if !isValidUsername(username) {
return fmt.Errorf("invalid username")
}
if len(password) < 6 {
return fmt.Errorf("invalid password")
1 year ago
}
uuid := make([]byte, UUIDSize)
_, err := rand.Read(uuid)
1 year ago
if err != nil {
return fmt.Errorf("failed to generate uuid: %s", err)
1 year ago
}
salt := make([]byte, ArgonSaltSize)
1 year ago
_, err = rand.Read(salt)
if err != nil {
return fmt.Errorf(fmt.Sprintf("failed to generate salt: %s", err))
1 year ago
}
err = db.AddAccountRecord(uuid, username, argon2.IDKey([]byte(password), salt, ArgonTime, ArgonMemory, ArgonThreads, ArgonKeySize), salt)
1 year ago
if err != nil {
return fmt.Errorf("failed to add account record: %s", err)
1 year ago
}
return nil
}
type AccountLoginRequest GenericAuthRequest
type AccountLoginResponse GenericAuthResponse
9 months ago
// /account/login - log into account
func handleAccountLogin(username, password string) (AccountLoginResponse, error) {
if !isValidUsername(username) {
return AccountLoginResponse{}, fmt.Errorf("invalid username")
1 year ago
}
if len(password) < 6 {
return AccountLoginResponse{}, fmt.Errorf("invalid password")
1 year ago
}
key, salt, err := db.FetchAccountKeySaltFromUsername(username)
1 year ago
if err != nil {
if err == sql.ErrNoRows {
return AccountLoginResponse{}, fmt.Errorf("account doesn't exist")
1 year ago
}
return AccountLoginResponse{}, err
1 year ago
}
if !bytes.Equal(key, argon2.IDKey([]byte(password), salt, ArgonTime, ArgonMemory, ArgonThreads, ArgonKeySize)) {
return AccountLoginResponse{}, fmt.Errorf("password doesn't match")
1 year ago
}
token := make([]byte, 32)
1 year ago
_, err = rand.Read(token)
if err != nil {
return AccountLoginResponse{}, fmt.Errorf("failed to generate token: %s", err)
1 year ago
}
err = db.AddAccountSession(username, token)
1 year ago
if err != nil {
return AccountLoginResponse{}, fmt.Errorf("failed to add account session")
1 year ago
}
return AccountLoginResponse{Token: base64.StdEncoding.EncodeToString(token)}, nil
}
// /account/logout - log out of account
func handleAccountLogout(token []byte) error {
1 year ago
if len(token) != 32 {
return fmt.Errorf("invalid token")
1 year ago
}
err := db.RemoveSessionFromToken(token)
1 year ago
if err != nil {
if err == sql.ErrNoRows {
return fmt.Errorf("token not found")
1 year ago
}
return fmt.Errorf("failed to remove account session")
1 year ago
}
return nil
}