|
|
@ -176,8 +176,10 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
switch r.PathValue("action") {
|
|
|
|
case "GET":
|
|
|
|
default:
|
|
|
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case "get":
|
|
|
|
save, err := savedata.GetSession(uuid, slot)
|
|
|
|
save, err := savedata.GetSession(uuid, slot)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
@ -190,7 +192,7 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
writeJSON(w, r, save)
|
|
|
|
writeJSON(w, r, save)
|
|
|
|
case "PUT":
|
|
|
|
case "update":
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -204,38 +206,8 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
case "DELETE":
|
|
|
|
case "clear":
|
|
|
|
err := savedata.DeleteSession(uuid, slot)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const legacyClientSessionId = "LEGACY_CLIENT"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME UNFINISHED!!!
|
|
|
|
|
|
|
|
/*func clearSessionData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
uuid, err := uuidFromRequest(r)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusUnauthorized)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var slot int
|
|
|
|
|
|
|
|
if r.URL.Query().Has("slot") {
|
|
|
|
|
|
|
|
slot, err = strconv.Atoi(r.URL.Query().Get("slot"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var save any
|
|
|
|
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
var session defs.SessionSaveData
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&session)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -243,69 +215,39 @@ const legacyClientSessionId = "LEGACY_CLIENT"
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
save = session
|
|
|
|
seed, err := db.GetDailyRunSeed()
|
|
|
|
|
|
|
|
|
|
|
|
var active bool
|
|
|
|
|
|
|
|
active, err = db.IsActiveSession(uuid, legacyClientSessionId) // TODO: unfinished, read token from query
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var trainerId, secretId int
|
|
|
|
|
|
|
|
if r.URL.Query().Has("trainerId") && r.URL.Query().Has("secretId") {
|
|
|
|
|
|
|
|
trainerId, err = strconv.Atoi(r.URL.Query().Get("trainerId"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
secretId, err = strconv.Atoi(r.URL.Query().Get("secretId"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusBadRequest)
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
storedTrainerId, storedSecretId, err := db.FetchTrainerIds(uuid)
|
|
|
|
resp, err := savedata.Clear(uuid, slot, seed, session)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if storedTrainerId > 0 || storedSecretId > 0 {
|
|
|
|
writeJSON(w, r, resp)
|
|
|
|
if trainerId != storedTrainerId || secretId != storedSecretId {
|
|
|
|
case "newclear":
|
|
|
|
httpError(w, r, fmt.Errorf("session out of date: stored trainer or secret ID does not match"), http.StatusBadRequest)
|
|
|
|
resp, err := savedata.NewClear(uuid, slot)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
err = db.UpdateTrainerIds(trainerId, secretId, uuid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("unable to update trainer ID: %s", err), http.StatusInternalServerError)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to read new clear: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !active {
|
|
|
|
|
|
|
|
save = savedata.ClearResponse{Error: "session out of date: not active"}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var seed string
|
|
|
|
writeJSON(w, r, resp)
|
|
|
|
seed, err = db.GetDailyRunSeed()
|
|
|
|
case "delete":
|
|
|
|
|
|
|
|
err := savedata.DeleteSession(uuid, slot)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response, err := savedata.Clear(uuid, slot, seed, save.(defs.SessionSaveData))
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonResponse(w, r, response)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
const legacyClientSessionId = "LEGACY_CLIENT"
|
|
|
|
|
|
|
|
|
|
|
|
func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
|
|
|
|
uuid, err := uuidFromRequest(r)
|
|
|
|
uuid, err := uuidFromRequest(r)
|
|
|
@ -527,118 +469,110 @@ type SystemVerifyRequest struct {
|
|
|
|
|
|
|
|
|
|
|
|
type SystemVerifyResponse struct {
|
|
|
|
type SystemVerifyResponse struct {
|
|
|
|
Valid bool `json:"valid"`
|
|
|
|
Valid bool `json:"valid"`
|
|
|
|
SystemData *defs.SystemSaveData `json:"systemData"`
|
|
|
|
SystemData defs.SystemSaveData `json:"systemData"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func handleSystemVerify(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func handleSystem(w http.ResponseWriter, r *http.Request) {
|
|
|
|
uuid, err := uuidFromRequest(r)
|
|
|
|
uuid, err := uuidFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusUnauthorized)
|
|
|
|
httpError(w, r, err, http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var input SystemVerifyRequest
|
|
|
|
if !r.URL.Query().Has("clientSessionId") {
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&input)
|
|
|
|
httpError(w, r, fmt.Errorf("missing clientSessionId"), http.StatusBadRequest)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var active bool
|
|
|
|
active, err := db.IsActiveSession(uuid, r.URL.Query().Get("clientSessionId"))
|
|
|
|
active, err = db.IsActiveSession(uuid, input.ClientSessionId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response := SystemVerifyResponse{
|
|
|
|
switch r.PathValue("action") {
|
|
|
|
Valid: active,
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case "get":
|
|
|
|
// not valid, send server state
|
|
|
|
|
|
|
|
if !active {
|
|
|
|
if !active {
|
|
|
|
err = db.UpdateActiveSession(uuid, input.ClientSessionId)
|
|
|
|
err = db.UpdateActiveSession(uuid, r.URL.Query().Get("clientSessionId"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var storedSaveData defs.SystemSaveData
|
|
|
|
save, err := savedata.GetSystem(uuid)
|
|
|
|
storedSaveData, err = db.ReadSystemSaveData(uuid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to read session save data: %s", err), http.StatusInternalServerError)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response.SystemData = &storedSaveData
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = db.UpdateAccountLastActivity(uuid)
|
|
|
|
writeJSON(w, r, save)
|
|
|
|
if err != nil {
|
|
|
|
case "update":
|
|
|
|
httpError(w, r, fmt.Errorf("failed to update account last activity: %s", err), http.StatusInternalServerError)
|
|
|
|
if !active {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("session out of date: not active"), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
writeJSON(w, r, response)
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
}
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&system)
|
|
|
|
|
|
|
|
|
|
|
|
func handleSystem(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
uuid, err := uuidFromRequest(r)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusUnauthorized)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !r.URL.Query().Has("clientSessionId") {
|
|
|
|
err = savedata.PutSystem(uuid, system)
|
|
|
|
httpError(w, r, fmt.Errorf("missing clientSessionId"), http.StatusBadRequest)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to put system data: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = db.UpdateActiveSession(uuid, r.URL.Query().Get("clientSessionId"))
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
|
|
|
case "verify":
|
|
|
|
|
|
|
|
var input SystemVerifyRequest
|
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&input)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
response := SystemVerifyResponse{
|
|
|
|
case "GET":
|
|
|
|
Valid: active,
|
|
|
|
save, err := savedata.GetSystem(uuid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// not valid, send server state
|
|
|
|
|
|
|
|
if !active {
|
|
|
|
|
|
|
|
err = db.UpdateActiveSession(uuid, input.ClientSessionId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: apply vouchers
|
|
|
|
var storedSaveData defs.SystemSaveData
|
|
|
|
|
|
|
|
storedSaveData, err = db.ReadSystemSaveData(uuid)
|
|
|
|
writeJSON(w, r, save)
|
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&system)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
|
|
|
httpError(w, r, fmt.Errorf("failed to read session save data: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = savedata.PutSystem(uuid, system)
|
|
|
|
response.SystemData = storedSaveData
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
httpError(w, r, fmt.Errorf("failed to put system data: %s", err), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
writeJSON(w, r, response)
|
|
|
|
case "DELETE":
|
|
|
|
case "delete":
|
|
|
|
err := savedata.DeleteSystem(uuid)
|
|
|
|
err := savedata.DeleteSystem(uuid)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
httpError(w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|