Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Renew Steam access tokens only through the CM

Leonardo Lobato4ee009a412f6
diff --git a/src/steam/backend/auth_client.cpp b/src/steam/backend/auth_client.cpp
index 64c5c7f..fe50ed9 100644
--- a/src/steam/backend/auth_client.cpp
+++ b/src/steam/backend/auth_client.cpp
@@ -119,7 +119,7 @@ AuthClient::AuthClient(AuthClientHooks hooks) : hooks_(std::move(hooks)) {}
AuthError AuthClient::Post(const char* method, const std::vector<std::uint8_t>& message,
const std::atomic<bool>& cancel, std::uint32_t timeout_ms,
- bool restoring, std::vector<std::uint8_t>* body, bool get) {
+ std::vector<std::uint8_t>* body, bool get) {
body->clear();
if (cancel.load()) return AuthError::kCanceled;
if (!timeout_ms) return AuthError::kExpired;
@@ -152,7 +152,7 @@ AuthError AuthClient::Post(const char* method, const std::vector<std::uint8_t>&
};
if (!response.ok()) return report(AuthError::kNetwork, 0);
if (response.status_code == 401 || response.status_code == 403)
- return report(restoring ? AuthError::kCredentialsRevoked : AuthError::kRejected, 0);
+ return report(AuthError::kRejected, 0);
if (response.status_code == 429 || response.status_code >= 500) return report(AuthError::kNetwork, 0);
if (response.status_code != 200 || response.body.size() > kBodyLimit) return report(AuthError::kProtocol, 0);
int result = 1; bool seen = false;
@@ -170,8 +170,6 @@ AuthError AuthClient::Post(const char* method, const std::vector<std::uint8_t>&
// Match the reference's absent-header success policy; malformed/duplicate
// headers fail closed instead of silently becoming success.
if (result != 1) {
- if (restoring && (result == 5 || result == 15 || result == 26 || result == 27))
- return report(AuthError::kCredentialsRevoked, result);
if (result == 9 || result == 27) return report(AuthError::kExpired, result);
return report(AuthError::kRejected, result);
}
@@ -190,7 +188,7 @@ AuthResult AuthClient::AuthenticateQr(const std::atomic<bool>& cancel,
if (hooks_.machine_id) begin.device_details.machine_id = hooks_.machine_id();
std::vector<std::uint8_t> request, response;
if (!auth::Encode(begin, &request).ok()) return Failure(AuthError::kProtocol);
- auto error = Post("BeginAuthSessionViaQR", request, cancel, Remaining(started,hooks_), false, &response);
+ auto error = Post("BeginAuthSessionViaQR", request, cancel, Remaining(started,hooks_), &response);
if (error != AuthError::kNone) return Failure(error);
if (!Remaining(started,hooks_)) return Failure(AuthError::kExpired);
auth::BeginAuthSessionViaQrResponse session;
@@ -226,7 +224,7 @@ AuthError AuthClient::Poll(std::uint64_t* client_id, const std::vector<std::uint
poll.client_id = *client_id; poll.request_id = request_id;
std::vector<std::uint8_t> request, response;
if (!auth::Encode(poll,&request).ok()) return AuthError::kProtocol;
- const auto error = Post("PollAuthSessionStatus",request,cancel,timeout_ms,false,&response);
+ const auto error = Post("PollAuthSessionStatus",request,cancel,timeout_ms,&response);
if (error != AuthError::kNone) return error;
if (!auth::Decode(response,approval).ok()) return AuthError::kProtocol;
if (approval->new_client_id) {
@@ -252,7 +250,9 @@ AuthResult AuthClient::Approved(auth::PollAuthSessionStatusResponse approval, st
StoredSession stored{claims->steam_id, approval.account_name, std::move(approval.refresh_token),
std::move(approval.access_token), std::nullopt};
if (stored.access_token.empty()) {
- auto renewed = Renew(stored,cancel,Remaining(started,hooks_,lifetime_ms),false);
+ auto renewed = Renew(stored,cancel);
+ // A refused new token fails this sign-in only; a saved session it would have replaced stays.
+ if (renewed.error == AuthError::kCredentialsRevoked) renewed.error = AuthError::kRejected;
if (cancel.load()) return Failure(AuthError::kCanceled);
if (!Remaining(started,hooks_,lifetime_ms)) return Failure(AuthError::kExpired);
return signed_in(std::move(renewed));
@@ -281,7 +281,7 @@ AuthResult AuthClient::AuthenticateCredentials(Credentials& credentials, const s
key_request.account_name = credentials.account_name;
std::vector<std::uint8_t> request, response;
if (!auth::Encode(key_request, &request).ok()) return Failure(AuthError::kProtocol);
- auto error = Post("GetPasswordRSAPublicKey", request, cancel, remaining(), false, &response, true);
+ auto error = Post("GetPasswordRSAPublicKey", request, cancel, remaining(), &response, true);
if (error != AuthError::kNone) return Failure(refused(error));
auth::GetPasswordRsaPublicKeyResponse key;
if (!auth::Decode(response, &key).ok() || !key.timestamp) return Failure(AuthError::kProtocol);
@@ -295,7 +295,7 @@ AuthResult AuthClient::AuthenticateCredentials(Credentials& credentials, const s
if (!encrypted) return Failure(AuthError::kProtocol);
begin.encrypted_password = std::move(*encrypted);
if (!auth::Encode(begin, &request).ok()) return Failure(AuthError::kProtocol);
- error = Post("BeginAuthSessionViaCredentials", request, cancel, remaining(), false, &response);
+ error = Post("BeginAuthSessionViaCredentials", request, cancel, remaining(), &response);
if (error != AuthError::kNone) return Failure(refused(error));
if (!remaining()) return Failure(AuthError::kSignInTimedOut);
auth::BeginAuthSessionViaCredentialsResponse session;
@@ -356,7 +356,7 @@ AuthResult AuthClient::AuthenticateCredentials(Credentials& credentials, const s
std::vector<std::uint8_t> code_request;
if (usable && !auth::Encode(update, &code_request).ok()) return Failure(AuthError::kProtocol);
ZeroizeSecret(update.code);
- error = usable ? Post("UpdateAuthSessionWithSteamGuardCode", code_request, cancel, remaining(), false, &response)
+ error = usable ? Post("UpdateAuthSessionWithSteamGuardCode", code_request, cancel, remaining(), &response)
: AuthError::kRejected;
if (usable) std::fill(code_request.begin(), code_request.end(), std::uint8_t{0});
// DuplicateRequest (29): Steam already took this code.
@@ -395,23 +395,11 @@ AuthResult AuthClient::AuthenticateCredentials(Credentials& credentials, const s
return Failure(AuthError::kCanceled);
}
-AuthResult AuthClient::Renew(const StoredSession& session, const std::atomic<bool>& cancel,
- std::uint32_t timeout_ms, bool restoring) {
- auth::GenerateAccessTokenForAppRequest req;
- req.refresh_token = session.refresh_token; req.steam_id = session.steam_id;
- std::vector<std::uint8_t> request, response;
- if (!auth::Encode(req,&request).ok()) return Failure(AuthError::kProtocol);
- const auto error = Post("GenerateAccessTokenForApp",request,cancel,timeout_ms,restoring,&response);
- if (error != AuthError::kNone) return Failure(error);
- auth::GenerateAccessTokenForAppResponse renewed;
- if (!auth::Decode(response,&renewed).ok()) return Failure(AuthError::kProtocol);
- const auto now = hooks_.unix_seconds();
- if (!ValidToken(renewed.access_token,session.steam_id,now) ||
- (renewed.refresh_token && !ValidToken(*renewed.refresh_token,session.steam_id,now))) return Failure(AuthError::kProtocol);
- auto updated = session;
- updated.access_token = std::move(renewed.access_token);
- if (renewed.refresh_token) updated.refresh_token = std::move(*renewed.refresh_token);
- return {AuthError::kNone,std::move(updated)};
+AuthResult AuthClient::Renew(const StoredSession& session, const std::atomic<bool>& cancel) {
+ // As the Steam client: a SteamClient refresh token goes only to an authenticated CM session. Sent to the
+ // Web API it was refused, and every saved sign-in was gone from the first renewal, about a day after it.
+ if (!hooks_.cm_renew) return Failure(AuthError::kProtocol);
+ return hooks_.cm_renew(session, cancel);
}
AuthResult AuthClient::Restored(const char* path, const StoredSession& before, AuthResult result) {
@@ -430,16 +418,7 @@ AuthResult AuthClient::Restore(const StoredSession& session,const std::atomic<bo
if (const auto claims = InspectTokenClaims(session.access_token);
claims && claims->steam_id == session.steam_id && claims->expires_at_unix_seconds > now + 60)
return Restored("stored", session, {AuthError::kNone, session});
- auto renewed = Renew(session,cancel,kRequestTimeoutMs,true);
- // The Web API refuses SteamClient refresh tokens with AccessDenied (15), and some days with
- // AccountLogonDenied (63), although the CM accepts them; only a refused CM logon then means the
- // credentials are gone.
- const bool web_refused = (renewed.error == AuthError::kCredentialsRevoked && last_eresult_ == 15) ||
- (renewed.error == AuthError::kRejected && last_eresult_ == 63);
- if (web_refused && hooks_.cm_renew && !cancel.load()) {
- // Its outcome, with Steam's result code, is in steam-cm.log (owner=auth).
- return Restored("cm", session, hooks_.cm_renew(session, cancel));
- }
- return Restored("web", session, std::move(renewed));
+ // Its outcome, with Steam's result code, is in steam-cm.log (owner=auth).
+ return Restored("cm", session, Renew(session, cancel));
}
} // namespace playden::steam
diff --git a/src/steam/backend/auth_client.h b/src/steam/backend/auth_client.h
index 7a7837f..bbd5067 100644
--- a/src/steam/backend/auth_client.h
+++ b/src/steam/backend/auth_client.h
@@ -18,10 +18,10 @@ struct AuthClientHooks {
std::function<bool(std::uint32_t, const std::atomic<bool>&)> wait;
// Optional sanitized failure report: method name, HTTP status and X-EResult number only.
std::function<void(const char* method, long http_status, int eresult)> failure;
- // Optional renewal through an authenticated CM session, used when the Web API refuses a
- // SteamClient refresh token with AccessDenied. Returns the updated session or an error.
+ // Renews the access token through an authenticated CM session, the only place the refresh token is sent
+ // after sign-in. Returns the updated session or an error; without it no token can be renewed.
std::function<AuthResult(const StoredSession&, const std::atomic<bool>&)> cm_renew;
- // Optional report of a successful restore: which path (stored, web, cm), and the refresh token's
+ // Optional report of a successful restore: which path (stored, cm), and the refresh token's
// fingerprint before and after (they differ when Steam rotated it).
std::function<void(const char* path, const std::string& before, const std::string& after)> restored;
// Optional: the machine ID the CM logon sends, so a sign-in names the same device (empty: none sent).
@@ -62,7 +62,7 @@ class AuthClient final : public AuthBackend {
// get: the message goes in the query string (GetPasswordRSAPublicKey); otherwise it is the form body.
AuthError Post(const char* method, const std::vector<std::uint8_t>& message,
const std::atomic<bool>& cancel, std::uint32_t timeout_ms,
- bool restoring, std::vector<std::uint8_t>* body, bool get = false);
+ std::vector<std::uint8_t>* body, bool get = false);
// One PollAuthSessionStatus round. Updates client_id when Steam replaces it.
AuthError Poll(std::uint64_t* client_id, const std::vector<std::uint8_t>& request_id,
const std::atomic<bool>& cancel, std::uint32_t timeout_ms,
@@ -70,8 +70,7 @@ class AuthClient final : public AuthBackend {
// Checks the approved tokens and renews a missing access token.
AuthResult Approved(auth::PollAuthSessionStatusResponse approval, std::uint64_t started,
std::uint64_t lifetime_ms, const std::atomic<bool>& cancel);
- AuthResult Renew(const StoredSession& session, const std::atomic<bool>& cancel,
- std::uint32_t timeout_ms, bool restoring);
+ AuthResult Renew(const StoredSession& session, const std::atomic<bool>& cancel);
AuthResult Restored(const char* path, const StoredSession& before, AuthResult result);
AuthClientHooks hooks_;
int last_eresult_ = 1; // X-EResult of the most recent refused Post, for renewal fallback
diff --git a/tests/auth_client_test.cpp b/tests/auth_client_test.cpp
index ba2e774..41c3437 100644
--- a/tests/auth_client_test.cpp
+++ b/tests/auth_client_test.cpp
@@ -43,10 +43,6 @@ HttpResponse Success(std::string refresh = Token(), std::string access = Token()
proto::Writer w; w.AddString(3, refresh); w.AddString(4, access); w.AddString(6, name);
return Reply(w);
}
-HttpResponse Renew(std::string access = Token(), std::string refresh = "") {
- proto::Writer w; w.AddString(1, access); if (!refresh.empty()) w.AddString(2, refresh);
- return Reply(w);
-}
struct Fixture {
std::uint64_t clock = 0;
std::atomic<bool> cancel{false};
@@ -189,9 +185,14 @@ void HeadersAndTokens() {
assert(c.AuthenticateQr(f.cancel, f.Callback()).error == AuthError::kProtocol);
}
{
- Fixture f; auto begin = Begin(); begin.headers.clear(); f.replies.push_back(begin); f.replies.push_back(Success(Token(), "")); f.replies.push_back(Renew());
- AuthClient c(f.Hooks()); auto r = c.AuthenticateQr(f.cancel, f.Callback());
- assert(r.error == AuthError::kNone && !r.session->access_token.empty() && f.requests.size() == 3);
+ // An approval without an access token gets one through the CM, not the Web API.
+ Fixture f; auto begin = Begin(); begin.headers.clear(); f.replies.push_back(begin); f.replies.push_back(Success(Token(), ""));
+ auto hooks = f.Hooks(); int cm_calls = 0;
+ hooks.cm_renew = [&](const StoredSession& s, const std::atomic<bool>&) {
+ ++cm_calls; auto renewed = s; renewed.access_token = Token(); return AuthResult{AuthError::kNone, renewed};
+ };
+ AuthClient c(hooks); auto r = c.AuthenticateQr(f.cancel, f.Callback());
+ assert(r.error == AuthError::kNone && !r.session->access_token.empty() && cm_calls == 1 && f.requests.size() == 2);
}
}
void FailedReplacementKeepsSavedSession() {
@@ -211,8 +212,12 @@ void FailedReplacementKeepsSavedSession() {
}
} repository;
Fixture f; f.replies.push_back(Begin()); f.replies.push_back(Success(Token("76561198000000002"),""));
- proto::Writer rejected; f.replies.push_back(Reply(rejected,15));
- AuthClient client(f.Hooks()); SteamService service(client,repository); service.BeginQrSignIn();
+ auto hooks = f.Hooks();
+ // What RenewThroughCm reports when the CM refuses the logon.
+ hooks.cm_renew = [](const StoredSession&, const std::atomic<bool>&) {
+ return AuthResult{AuthError::kCredentialsRevoked, std::nullopt};
+ };
+ AuthClient client(hooks); SteamService service(client,repository); service.BeginQrSignIn();
bool failed = false;
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(3);
while (!failed && std::chrono::steady_clock::now() < deadline) {
@@ -284,59 +289,34 @@ void Restoration() {
assert(r.error == AuthError::kNone && r.session->access_token == saved.access_token && f.requests.empty());
}
{
- // The Web API refuses a SteamClient refresh token with AccessDenied: renew through the CM.
- Fixture f; proto::Writer w; f.replies.push_back(Reply(w,15)); int cm_calls = 0;
+ // An expired access token is renewed through the CM only: the refresh token never goes to the Web API.
+ Fixture f; int cm_calls = 0; const auto rotation = Token("76561198000000001",2100000000);
auto hooks = f.Hooks();
hooks.cm_renew = [&](const StoredSession& s, const std::atomic<bool>&) {
- ++cm_calls; auto renewed = s; renewed.access_token = Token(); return AuthResult{AuthError::kNone, renewed};
+ ++cm_calls; auto renewed = s; renewed.access_token = Token(); renewed.refresh_token = rotation;
+ return AuthResult{AuthError::kNone, renewed};
};
+ std::string path;
+ hooks.restored = [&](const char* p, const std::string&, const std::string&) { path = p; };
AuthClient c(hooks); auto r = c.Restore(Stored(),f.cancel);
- assert(r.error == AuthError::kNone && cm_calls == 1 && r.session->access_token == Token());
+ assert(r.error == AuthError::kNone && cm_calls == 1 && r.session->refresh_token == rotation);
+ assert(r.session->cell_id == 7 && path == "cm" && f.requests.empty());
}
- {
- // Some days the Web API answers AccountLogonDenied (63) instead: also renew through the CM (#51).
- Fixture f; proto::Writer w; f.replies.push_back(Reply(w,63)); int cm_calls = 0;
- auto hooks = f.Hooks();
- hooks.cm_renew = [&](const StoredSession& s, const std::atomic<bool>&) {
- ++cm_calls; auto renewed = s; renewed.access_token = Token(); return AuthResult{AuthError::kNone, renewed};
- };
- AuthClient c(hooks); auto r = c.Restore(Stored(),f.cancel);
- assert(r.error == AuthError::kNone && cm_calls == 1 && r.session->access_token == Token());
- }
- {
- // Any other refusal keeps its meaning; the CM is not consulted.
- Fixture f; proto::Writer w; f.replies.push_back(Reply(w,5)); int cm_calls = 0;
- auto hooks = f.Hooks();
- hooks.cm_renew = [&](const StoredSession&, const std::atomic<bool>&) { ++cm_calls; return AuthResult{}; };
+ for (AuthError error : {AuthError::kCredentialsRevoked, AuthError::kNetwork, AuthError::kCanceled}) {
+ Fixture f; auto hooks = f.Hooks();
+ hooks.cm_renew = [&](const StoredSession&, const std::atomic<bool>&) { return AuthResult{error, std::nullopt}; };
AuthClient c(hooks);
- assert(c.Restore(Stored(),f.cancel).error == AuthError::kCredentialsRevoked && cm_calls == 0);
- }
- {
- Fixture f; const auto rotation = Token("76561198000000001",2100000000); f.replies.push_back(Renew(Token(), rotation));
- AuthClient c(f.Hooks()); auto r = c.Restore(Stored(),f.cancel);
- assert(r.error == AuthError::kNone && r.session->refresh_token == rotation && r.session->cell_id == 7);
- assert(f.requests[0].url.find("GenerateAccessTokenForApp") != std::string::npos);
+ assert(c.Restore(Stored(),f.cancel).error == error && f.requests.empty());
}
{
- Fixture f; f.replies.push_back(Renew(Token(),Token("76561198000000002"))); AuthClient c(f.Hooks());
- assert(c.Restore(Stored(),f.cancel).error == AuthError::kProtocol);
- }
- {
- Fixture f; f.replies.push_back(Renew()); f.cancel_on_response = true; AuthClient c(f.Hooks());
- assert(c.Restore(Stored(),f.cancel).error == AuthError::kCanceled);
- }
- for (int eresult : {5,15,26,27}) {
- Fixture f; proto::Writer w; f.replies.push_back(Reply(w,eresult)); AuthClient c(f.Hooks());
- assert(c.Restore(Stored(),f.cancel).error == AuthError::kCredentialsRevoked);
+ // Without a CM there is nothing to renew through.
+ Fixture f; AuthClient c(f.Hooks());
+ assert(c.Restore(Stored(),f.cancel).error == AuthError::kProtocol && f.requests.empty());
}
{
Fixture f; auto saved = Stored(); saved.steam_id++; AuthClient c(f.Hooks());
assert(c.Restore(saved,f.cancel).error == AuthError::kCredentialsRevoked && f.requests.empty());
}
- {
- Fixture f; auto r = Renew(); r.status_code = 503; f.replies.push_back(r); AuthClient c(f.Hooks());
- assert(c.Restore(Stored(),f.cancel).error == AuthError::kNetwork);
- }
}
}
// Password sign-in. The modulus is synthetic: any odd 2048-bit value encrypts.