2024-04-29 14:26:46 -07:00
|
|
|
/*
|
|
|
|
Copyright (C) 2024 Pagefault Games
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2024-04-29 12:32:58 -07:00
|
|
|
|
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-29 12:22:27 -07:00
|
|
|
"github.com/pagefaultgames/rogueserver/api/account"
|
|
|
|
"github.com/pagefaultgames/rogueserver/api/daily"
|
|
|
|
"github.com/pagefaultgames/rogueserver/db"
|
2023-12-31 13:12:20 -08:00
|
|
|
)
|
|
|
|
|
2024-05-11 05:06:47 -07:00
|
|
|
func Init(mux *http.ServeMux) error {
|
|
|
|
if err := scheduleStatRefresh(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := daily.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-24 17:05:57 -07:00
|
|
|
|
|
|
|
// 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)
|
2024-04-24 17:05:57 -07:00
|
|
|
|
|
|
|
// game
|
2024-04-25 12:30:12 -07:00
|
|
|
mux.HandleFunc("GET /game/titlestats", handleGameTitleStats)
|
|
|
|
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
|
2024-04-24 17:05:57 -07:00
|
|
|
|
|
|
|
// 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)
|
2024-05-10 15:07:14 -07:00
|
|
|
mux.HandleFunc("GET /savedata/newclear", handleNewClear)
|
2024-04-24 17:05:57 -07:00
|
|
|
|
2024-05-11 18:34:08 -07:00
|
|
|
// new session
|
2024-05-11 18:42:35 -07:00
|
|
|
mux.HandleFunc("POST /savedata/updateall", handleSaveData2)
|
2024-05-11 18:34:08 -07:00
|
|
|
|
2024-04-24 17:05:57 -07:00
|
|
|
// 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)
|
2024-05-11 05:06:47 -07:00
|
|
|
return nil
|
2024-04-14 17:03:53 -07:00
|
|
|
}
|
|
|
|
|
2024-04-25 00:50:35 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-25 00:50:35 -07:00
|
|
|
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)
|
2024-05-10 12:49:26 -07:00
|
|
|
}
|