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.

209 lines
5.1 KiB
Go

10 months ago
package api
import (
9 months ago
"bytes"
"crypto/rand"
"database/sql"
10 months ago
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"os"
9 months ago
"regexp"
"strconv"
"time"
10 months ago
"github.com/Flashfyre/pokerogue-server/db"
9 months ago
"golang.org/x/crypto/argon2"
10 months ago
)
9 months ago
const (
argonTime = 1
argonMemory = 256 * 1024
argonThreads = 4
9 months ago
argonKeyLength = 32
)
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 (s *Server) handleAccountInfo(w http.ResponseWriter, r *http.Request) {
username, err := getUsernameFromRequest(r)
10 months ago
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
6 months ago
uuid, err := getUuidFromRequest(r) // lazy
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var latestSaveTime 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(latestSaveTime) {
latestSaveTime = stat.ModTime()
latestSaveId = id
}
}
response, err := json.Marshal(AccountInfoResponse{Username: username, LastSessionSlot: latestSaveId})
10 months ago
if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return
}
w.Write(response)
}
type AccountRegisterRequest GenericAuthRequest
6 months ago
// /account/register - register account
func (s *Server) handleAccountRegister(w http.ResponseWriter, r *http.Request) {
10 months ago
var request AccountRegisterRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return
}
if !isValidUsername(request.Username) {
9 months ago
http.Error(w, "invalid username", http.StatusBadRequest)
return
}
if len(request.Password) < 6 {
http.Error(w, "invalid password", http.StatusBadRequest)
return
}
uuid := make([]byte, 16)
_, err = rand.Read(uuid)
if err != nil {
http.Error(w, fmt.Sprintf("failed to generate uuid: %s", err), http.StatusInternalServerError)
return
}
salt := make([]byte, 16)
_, err = rand.Read(salt)
if err != nil {
http.Error(w, fmt.Sprintf("failed to generate salt: %s", err), http.StatusInternalServerError)
return
}
err = db.AddAccountRecord(uuid, request.Username, argon2.IDKey([]byte(request.Password), salt, argonTime, argonMemory, argonThreads, argonKeyLength), salt)
if err != nil {
http.Error(w, fmt.Sprintf("failed to add account record: %s", err), http.StatusInternalServerError)
9 months ago
return
}
w.WriteHeader(http.StatusOK)
10 months ago
}
type AccountLoginRequest GenericAuthRequest
type AccountLoginResponse GenericAuthResponse
6 months ago
// /account/login - log into account
func (s *Server) handleAccountLogin(w http.ResponseWriter, r *http.Request) {
9 months ago
var request AccountLoginRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
return
}
if !isValidUsername(request.Username) {
9 months ago
http.Error(w, "invalid username", http.StatusBadRequest)
return
}
if len(request.Password) < 6 {
http.Error(w, "invalid password", http.StatusBadRequest)
return
}
key, salt, err := db.FetchAccountKeySaltFromUsername(request.Username)
9 months ago
if err != nil {
if err == sql.ErrNoRows {
http.Error(w, "account doesn't exist", http.StatusBadRequest)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !bytes.Equal(key, argon2.IDKey([]byte(request.Password), salt, argonTime, argonMemory, argonThreads, argonKeyLength)) {
http.Error(w, "password doesn't match", http.StatusBadRequest)
return
}
token := make([]byte, 32)
9 months ago
_, err = rand.Read(token)
if err != nil {
http.Error(w, fmt.Sprintf("failed to generate token: %s", err), http.StatusInternalServerError)
return
}
10 months ago
9 months ago
err = db.AddAccountSession(request.Username, token)
if err != nil {
http.Error(w, "failed to add account session", http.StatusInternalServerError)
return
}
response, err := json.Marshal(AccountLoginResponse{Token: base64.StdEncoding.EncodeToString(token)})
if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return
}
w.Write(response)
10 months ago
}
// /account/logout - log out of account
6 months ago
func (s *Server) handleAccountLogout(w http.ResponseWriter, r *http.Request) {
9 months ago
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to decode token: %s", err), http.StatusBadRequest)
return
}
if len(token) != 32 {
http.Error(w, "invalid token", http.StatusBadRequest)
return
}
err = db.RemoveSessionFromToken(token)
if err != nil {
if err == sql.ErrNoRows {
http.Error(w, "token not found", http.StatusBadRequest)
return
}
http.Error(w, "failed to remove account session", http.StatusInternalServerError)
return
}
10 months ago
9 months ago
w.WriteHeader(http.StatusOK)
10 months ago
}