rogueserver/api/common.go

90 lines
2.4 KiB
Go
Raw Normal View History

2023-12-31 13:12:20 -08:00
package api
import (
"encoding/base64"
"fmt"
2024-04-24 17:23:20 -07:00
"log"
2023-12-31 13:12:20 -08:00
"net/http"
2024-04-15 00:24:45 -07:00
"github.com/pagefaultgames/pokerogue-server/api/account"
"github.com/pagefaultgames/pokerogue-server/api/daily"
2024-04-14 16:25:36 -07:00
"github.com/pagefaultgames/pokerogue-server/db"
2023-12-31 13:12:20 -08:00
)
func Init(mux *http.ServeMux) {
scheduleStatRefresh()
daily.Init()
// account
2024-04-25 12:30:12 -07:00
mux.HandleFunc("GET /account/info", handleAccountInfo)
mux.HandleFunc("POST /account/register", handleAccountRegister)
mux.HandleFunc("POST /account/login", handleAccountLogin)
2024-04-28 14:27:58 -07:00
mux.HandleFunc("POST /account/changepw", handleAccountChangePW)
2024-04-25 12:30:12 -07:00
mux.HandleFunc("GET /account/logout", handleAccountLogout)
// game
2024-04-25 12:30:12 -07:00
mux.HandleFunc("GET /game/playercount", handleGamePlayerCount)
mux.HandleFunc("GET /game/titlestats", handleGameTitleStats)
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
// savedata
2024-04-25 12:30:12 -07:00
mux.HandleFunc("GET /savedata/get", handleSaveData)
mux.HandleFunc("POST /savedata/update", handleSaveData)
mux.HandleFunc("GET /savedata/delete", handleSaveData)
mux.HandleFunc("POST /savedata/clear", handleSaveData)
// daily
2024-04-25 12:30:12 -07:00
mux.HandleFunc("GET /daily/seed", handleDailySeed)
mux.HandleFunc("GET /daily/rankings", handleDailyRankings)
mux.HandleFunc("GET /daily/rankingpagecount", handleDailyRankingPageCount)
}
func tokenFromRequest(r *http.Request) ([]byte, error) {
2024-04-01 19:54:55 -07:00
if r.Header.Get("Authorization") == "" {
2024-04-21 13:52:26 -07:00
return nil, fmt.Errorf("missing token")
2023-12-31 13:12:20 -08:00
}
2024-04-01 19:54:55 -07:00
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-21 13:52:26 -07:00
return nil, fmt.Errorf("failed to decode token: %s", err)
2023-12-31 13:12:20 -08:00
}
2024-04-21 15:57:27 -07:00
2024-04-15 00:24:45 -07:00
if len(token) != account.TokenSize {
2024-04-21 13:52:26 -07:00
return nil, fmt.Errorf("invalid token length: got %d, expected %d", len(token), account.TokenSize)
}
return token, nil
}
func usernameFromRequest(r *http.Request) (string, error) {
token, err := tokenFromRequest(r)
2024-04-21 13:52:26 -07:00
if err != nil {
return "", err
2023-12-31 13:12:20 -08:00
}
2024-03-18 16:55:02 -07:00
username, err := db.FetchUsernameFromToken(token)
2023-12-31 13:12:20 -08:00
if err != nil {
return "", fmt.Errorf("failed to validate token: %s", err)
}
return username, nil
}
func uuidFromRequest(r *http.Request) ([]byte, error) {
token, err := tokenFromRequest(r)
2023-12-31 13:12:20 -08:00
if err != nil {
2024-04-21 13:52:26 -07:00
return nil, err
2023-12-31 13:12:20 -08:00
}
2024-04-08 15:15:09 -07:00
uuid, err := db.FetchUUIDFromToken(token)
2023-12-31 13:12:20 -08:00
if err != nil {
return nil, fmt.Errorf("failed to validate token: %s", err)
}
return uuid, nil
}
2024-04-24 17:23:20 -07:00
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
log.Printf("%s: %s\n", r.URL.Path, err)
http.Error(w, err.Error(), code)
}