feat: sign in to IMVU for Access Pass products (#4)
Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #4.
This commit is contained in:
1 parent
dd8205602c
commit
145ebf25c1
11 files changed
+1001
-24
No files matched your search
@@ -0,0 +1,150 @@
|
||||
package imvutest
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// User is a fake IMVU account.
|
||||
type User struct {
|
||||
ID int64
|
||||
Username string
|
||||
Password string
|
||||
AccessPass bool
|
||||
}
|
||||
|
||||
// Product is a fake IMVU product. Access Pass ones are hidden from the signed out and from accounts without it.
|
||||
type Product struct {
|
||||
ID int64
|
||||
Name string
|
||||
CreatorID int64
|
||||
AccessPass bool
|
||||
}
|
||||
|
||||
// API is a fake of IMVU's REST API: signing in (a session cookie), the signed-in account, users, products and shop
|
||||
// listings, each as the viewer may see them.
|
||||
type API struct {
|
||||
*httptest.Server
|
||||
mu sync.Mutex
|
||||
Users []User
|
||||
Products []Product
|
||||
SignIns int // sign-ins tried
|
||||
sessions map[string]int64 // session cookie → account
|
||||
}
|
||||
|
||||
// NewAPI starts a fake API.
|
||||
func NewAPI(t testing.TB) *API {
|
||||
a := &API{sessions: map[string]int64{}}
|
||||
a.Server = httptest.NewServer(http.HandlerFunc(a.serve))
|
||||
t.Cleanup(a.Close)
|
||||
return a
|
||||
}
|
||||
|
||||
// EndSessions ends every session, as IMVU does after a while.
|
||||
func (a *API) EndSessions() {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
a.sessions = map[string]int64{}
|
||||
}
|
||||
|
||||
func (a *API) user(f func(User) bool) *User {
|
||||
for i := range a.Users {
|
||||
if f(a.Users[i]) {
|
||||
return &a.Users[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *API) serve(w http.ResponseWriter, r *http.Request) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
var viewer *User
|
||||
if c, err := r.Cookie("osCsid"); err == nil {
|
||||
if id, ok := a.sessions[c.Value]; ok {
|
||||
viewer = a.user(func(u User) bool { return u.ID == id })
|
||||
}
|
||||
}
|
||||
self := a.URL + r.URL.Path
|
||||
one := func(data map[string]any) {
|
||||
json.NewEncoder(w).Encode(map[string]any{"status": "success", "id": self, "denormalized": map[string]any{self: map[string]any{"data": data}}})
|
||||
}
|
||||
fail := func(status int, code, message string) {
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(map[string]any{"status": "failure", "error": code, "message": message})
|
||||
}
|
||||
visible := func(p Product) bool { return !p.AccessPass || (viewer != nil && viewer.AccessPass) }
|
||||
productData := func(p Product) map[string]any {
|
||||
rating := "GA"
|
||||
if p.AccessPass {
|
||||
rating = "AP"
|
||||
}
|
||||
return map[string]any{"product_id": p.ID, "product_name": p.Name, "creator_cid": p.CreatorID, "creator_name": "creator" + strconv.FormatInt(p.CreatorID, 10), "rating": rating, "is_purchasable": true}
|
||||
}
|
||||
|
||||
switch {
|
||||
case r.URL.Path == "/login" && r.Method == http.MethodPost:
|
||||
a.SignIns++
|
||||
var body struct{ Username, Password string }
|
||||
json.NewDecoder(r.Body).Decode(&body)
|
||||
u := a.user(func(u User) bool { return strings.EqualFold(u.Username, body.Username) && u.Password == body.Password })
|
||||
if u == nil {
|
||||
fail(http.StatusUnauthorized, "LOGIN-002", "Invalid username or password")
|
||||
return
|
||||
}
|
||||
b := make([]byte, 16)
|
||||
rand.Read(b)
|
||||
token := hex.EncodeToString(b)
|
||||
a.sessions[token] = u.ID
|
||||
http.SetCookie(w, &http.Cookie{Name: "osCsid", Value: token, Path: "/"})
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
one(map[string]any{"user": map[string]any{"id": a.URL + "/user/user-" + strconv.FormatInt(u.ID, 10)}})
|
||||
case r.URL.Path == "/login/me":
|
||||
if viewer == nil {
|
||||
fail(http.StatusUnauthorized, "AUTHENTICATION-005", "Not signed in")
|
||||
return
|
||||
}
|
||||
one(map[string]any{"user": map[string]any{"id": a.URL + "/user/user-" + strconv.FormatInt(viewer.ID, 10)}, "session_id": "x"})
|
||||
case strings.HasPrefix(r.URL.Path, "/user/user-"):
|
||||
id, _ := strconv.ParseInt(strings.TrimPrefix(r.URL.Path, "/user/user-"), 10, 64)
|
||||
u := a.user(func(u User) bool { return u.ID == id })
|
||||
if u == nil {
|
||||
fail(http.StatusNotFound, "NOT-FOUND", "No such user")
|
||||
return
|
||||
}
|
||||
// IMVU shows Access Pass to the account itself only.
|
||||
one(map[string]any{"legacy_cid": u.ID, "username": u.Username, "is_ap": viewer != nil && viewer.ID == u.ID && u.AccessPass})
|
||||
case strings.HasPrefix(r.URL.Path, "/product/product-"):
|
||||
id, _ := strconv.ParseInt(strings.TrimPrefix(r.URL.Path, "/product/product-"), 10, 64)
|
||||
for _, p := range a.Products {
|
||||
if p.ID == id && visible(p) {
|
||||
one(productData(p))
|
||||
return
|
||||
}
|
||||
}
|
||||
fail(http.StatusUnauthorized, "AUTHENTICATION-005", "Not allowed") // as IMVU answers hidden and missing alike
|
||||
case r.URL.Path == "/product":
|
||||
creator := a.user(func(u User) bool { return strings.EqualFold(u.Username, r.URL.Query().Get("creator")) })
|
||||
self = a.URL + r.URL.RequestURI()
|
||||
den := map[string]any{}
|
||||
items := []string{}
|
||||
for _, p := range a.Products {
|
||||
if creator != nil && p.CreatorID == creator.ID && visible(p) {
|
||||
ref := a.URL + "/product/product-" + strconv.FormatInt(p.ID, 10)
|
||||
items = append(items, ref)
|
||||
den[ref] = map[string]any{"data": productData(p)}
|
||||
}
|
||||
}
|
||||
den[self] = map[string]any{"data": map[string]any{"items": items, "total_count": len(items)}, "relations": []any{}}
|
||||
json.NewEncoder(w).Encode(map[string]any{"status": "success", "id": self, "denormalized": den})
|
||||
default:
|
||||
fail(http.StatusNotFound, "NOT-FOUND", "No such thing")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user