Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Profile: the Game Pass plan from the store service (Ultimate...)

Rafael Abud Ferreirae7242619fee6
diff --git a/src/app/cli_main.cpp b/src/app/cli_main.cpp
index d6cb0a4..0442de8 100644
--- a/src/app/cli_main.cpp
+++ b/src/app/cli_main.cpp
@@ -565,6 +565,7 @@ int main(int argc, char** argv) {
if (cmd == "login") {
xcloud::GssvClient gssv;
rc = signIn(am, gssv) ? 0 : 1;
+ std::printf("subscription: %s\n", am.profile().subscription.empty() ? "(none or unknown)" : am.profile().subscription.c_str());
} else if (cmd == "titlehub" && argi < argc) {
// Debugging: titlehub's view of Xbox title ids (devices etc.).
xcloud::GssvClient gssv;
diff --git a/src/app/worker.cpp b/src/app/worker.cpp
index 019fdcf..2ef8ca1 100644
--- a/src/app/worker.cpp
+++ b/src/app/worker.cpp
@@ -520,6 +520,7 @@ void signInAndLoad(auth::AuthManager& am, xcloud::GssvClient& gssv) {
if (!am.saved()) // the next start would ask for a code again: said now, not then
platform::notify(ui::tr(ui::Str::SignInNotSaved), "the sign-in couldn't be saved (notification)");
g_ui->setProfile(am.profile().gamertag, am.profile().gamerpicUrl, am.profile().gamerscore);
+ if (!am.profile().subscription.empty()) g_ui->setSubscription(am.profile().subscription);
{
std::lock_guard<std::mutex> lock(g_argMutex);
g_xblAuth = am.profile().xblAuthorization;
@@ -549,7 +550,7 @@ void loadLibrary(xcloud::GssvClient& gssv) {
auto publish = [](const app::Library& lib) {
g_ui->setRows(lib.rows());
g_ui->setOwned(lib.owned(), lib.purchasable(), lib.ownedKnown());
- g_ui->setSubscription(lib.gamePassMember() ? "GamePass" : "");
+ g_ui->setMembership(lib.gamePassMember());
g_ui->setSearchPools(lib.gamePassSearchPool(), lib.librarySearchPool());
auto p = lib.progress();
g_ui->setLoading(p.active, p.done, p.total);
diff --git a/src/auth/auth_manager.cpp b/src/auth/auth_manager.cpp
index e3f49d6..904b0db 100644
--- a/src/auth/auth_manager.cpp
+++ b/src/auth/auth_manager.cpp
@@ -9,6 +9,7 @@
#include <algorithm>
#include <cerrno>
+#include <cstdlib>
#include <cstdio>
namespace xc::auth {
@@ -122,10 +123,60 @@ bool AuthManager::xboxChain(xcloud::GssvClient& gssv, std::string& err) {
XC_LOGW("profile XSTS: %s", perr.c_str());
}
+ fetchSubscription(user);
if (!xstsToken(user, rp::kGssv, gssvXsts_, err)) return false;
return gssv.login(gssvXsts_, err);
}
+std::string subscriptionPlan(const std::string& body) {
+ // The store's entitlements for the Game Pass products, highest plan
+ // first (an Ultimate account also has the lower ones).
+ static const std::pair<const char*, const char*> kPlans[] = {
+ {"CFQ7TTC0KHS0", "Ultimate"}, {"CFQ7TTC0P85B", "Premium"}, {"CFQ7TTC0KGQ8", "PC"}, {"CFQ7TTC0K5DJ", "Essential"}};
+ auto j = json::parse(body);
+ if (!j) return {};
+ const auto& entitlements = (*j)["entitlements"];
+ for (const auto& [id, plan] : kPlans) {
+ std::string lower = id;
+ for (auto& ch : lower) ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
+ for (const auto& key : {std::string(id), lower}) {
+ const auto& e = entitlements[key];
+ if (!e.isObject()) continue;
+ std::string status = e["status"].str();
+ if (status == "Active" || (status.empty() && e["isSatisfyingEntitlement"].asBool())) return plan;
+ }
+ }
+ return {};
+}
+
+void AuthManager::fetchSubscription(const XblToken& userToken) {
+ // What xbox.com shows on the profile: the store service's entitlements
+ // for the Game Pass products. Optional: a failure only leaves the plan
+ // unknown (the UI then shows "Game Pass" from the titles).
+ XblToken mp;
+ std::string err;
+ if (!xstsToken(userToken, rp::kMarketplace, mp, err)) {
+ XC_LOGW("subscription: store XSTS: %s", err.c_str());
+ return;
+ }
+ net::Request req;
+ req.method = "POST";
+ req.url = "https://emerald.xboxservices.com/xboxcomfd/entitlements/bulk?locale=en-US";
+ req.headers = {{"Authorization", mp.authorizationHeader()},
+ {"Content-Type", "application/json"},
+ {"X-MS-API-Version", "1.0"},
+ {"MS-CV", "PSBoxProfile.1"}};
+ req.body = R"({"ProductIds":["CFQ7TTC0KHS0","CFQ7TTC0P85B","CFQ7TTC0K5DJ","CFQ7TTC0KGQ8"]})";
+ auto r = net::perform(req);
+ if (!r.ok()) {
+ XC_LOGW("subscription: entitlements: %s", r.status ? ("HTTP " + std::to_string(r.status)).c_str() : r.error.c_str());
+ return;
+ }
+ if (std::getenv("XC_DUMP_SUBSCRIPTION")) std::printf("%s\n", r.body.c_str()); // diagnostics (CLI)
+ profile_.subscription = subscriptionPlan(r.body);
+ XC_LOGI("subscription: %s", profile_.subscription.empty() ? "none" : profile_.subscription.c_str());
+}
+
bool AuthManager::loginOffering(xcloud::GssvClient& other, std::string& err) const {
if (gssvXsts_.token.empty()) {
err = "not signed in";
diff --git a/src/auth/auth_manager.h b/src/auth/auth_manager.h
index 472b40f..20bdd1c 100644
--- a/src/auth/auth_manager.h
+++ b/src/auth/auth_manager.h
@@ -20,11 +20,17 @@ struct Profile {
std::string xuid;
std::string gamerpicUrl; // empty until fetched
std::string gamerscore; // digits; empty until fetched
+ // The Game Pass plan: "Ultimate", "Premium", "Essential", "PC"; empty
+ // when none or unknown (the store service didn't answer).
+ std::string subscription;
// "XBL3.0 x=...;token" for services on http://xboxlive.com (titlehub...).
// Memory only: never logged or saved.
std::string xblAuthorization;
};
+// The Game Pass plan in an entitlements answer ("Ultimate"...; empty: none).
+std::string subscriptionPlan(const std::string& entitlementsJson);
+
class AuthManager {
public:
// Called while waiting for the user to sign in on another device.
@@ -61,6 +67,7 @@ private:
bool deviceCodeSignIn(const DeviceCodeCallback& onCode, std::string& err, const std::atomic<bool>* cancel);
bool xboxChain(xcloud::GssvClient& gssv, std::string& err);
void fetchGamerpic(const XblToken& xsts);
+ void fetchSubscription(const XblToken& userToken);
std::string storePath_;
MsaClient msa_;
diff --git a/src/auth/xbox_live.h b/src/auth/xbox_live.h
index dd4c303..3ba9b25 100644
--- a/src/auth/xbox_live.h
+++ b/src/auth/xbox_live.h
@@ -22,6 +22,7 @@ struct XblToken {
namespace rp {
constexpr const char* kGssv = "http://gssv.xboxlive.com/";
constexpr const char* kXboxLive = "http://xboxlive.com";
+constexpr const char* kMarketplace = "http://mp.microsoft.com/"; // store / subscriptions
} // namespace rp
// `rpsPrefix` is "t=" for login.live.com tokens issued to a custom client id,
diff --git a/src/ui/app_ui.cpp b/src/ui/app_ui.cpp
index d87cd61..ba93bad 100644
--- a/src/ui/app_ui.cpp
+++ b/src/ui/app_ui.cpp
@@ -304,6 +304,13 @@ void AppUi::setSubscription(const std::string& plan) {
dirty_ = true;
}
+void AppUi::setMembership(bool member) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (!subscription_.empty() && subscription_ != "GamePass") return; // the plan is known
+ subscription_ = member ? "GamePass" : "";
+ dirty_ = true;
+}
+
void AppUi::setRows(std::vector<GameRow> rows) {
std::lock_guard<std::mutex> lock(mutex_);
allRows_ = std::move(rows);
diff --git a/src/ui/app_ui.h b/src/ui/app_ui.h
index 64cdfbf..c1d56a5 100644
--- a/src/ui/app_ui.h
+++ b/src/ui/app_ui.h
@@ -174,6 +174,9 @@ public:
// "Essential"...; "GamePass" for a member whose plan isn't known); empty
// for none (nothing shown).
void setSubscription(const std::string& plan);
+ // From the title list: a Game Pass member or not; only when the plan
+ // itself isn't known ("GamePass" or nothing).
+ void setMembership(bool member);
std::string gamertag() const; // for keeping it out of reports
std::vector<std::string> consoleNames() const; // the same: the user's names for their consoles
// "Report a problem": a QR code of `url` (the GitHub bug form, filled
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 0f71f9a..19a619a 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -18,6 +18,7 @@
#include "ui/stream_menu.h"
#include "ui/strings.h"
#include "util/json.h"
+#include "auth/auth_manager.h"
#include "auth/msa.h"
#include "ui/image_cache.h"
#include "xcloud/gssv.h"
@@ -756,6 +757,21 @@ static void testMicrosoftHost() {
CHECK(!net::microsoftHost(h));
}
+static void testSubscriptionPlan() {
+ // The store's entitlements: the highest active Game Pass plan.
+ auto entry = [](const char* id, const char* status) {
+ return std::string("\"") + id + "\":{\"productId\":\"" + id + "\",\"isOwned\":true,\"status\":\"" + status + "\"}";
+ };
+ std::string all = "{\"entitlements\":{" + entry("CFQ7TTC0K5DJ", "Active") + "," + entry("CFQ7TTC0KHS0", "Active") + "," +
+ entry("CFQ7TTC0P85B", "Active") + "}}";
+ CHECK(auth::subscriptionPlan(all) == "Ultimate");
+ CHECK(auth::subscriptionPlan("{\"entitlements\":{" + entry("CFQ7TTC0P85B", "Active") + "}}") == "Premium");
+ CHECK(auth::subscriptionPlan("{\"entitlements\":{" + entry("CFQ7TTC0KHS0", "Expired") + "," + entry("CFQ7TTC0K5DJ", "Active") + "}}") ==
+ "Essential");
+ CHECK(auth::subscriptionPlan("{\"entitlements\":{\"cfq7ttc0kgq8\":{\"isSatisfyingEntitlement\":true}}}") == "PC");
+ CHECK(auth::subscriptionPlan("{\"entitlements\":{}}").empty() && auth::subscriptionPlan("not json").empty());
+}
+
static void testHostPacer() {
// Five a second per Microsoft host; other hosts never wait; a 429 holds
// the host for its Retry-After (30 s at most).
@@ -825,6 +841,7 @@ int main() {
testPrivacyAndRobustness();
testStoreLeftovers();
testHostPacer();
+ testSubscriptionPlan();
testMicrosoftHost();
testAudioEncoder();
testAutoDeband();