mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
fix(ea): retry rejected EA licence token requests (#2440)
* fix(ea): retry licence requests EA rejects for a transient token failure * test(ea): cover bounded licence retries * fix(ea): retry only token field rejections
This commit is contained in:
1 parent
afd2d91662
commit
8ee50e5b38
2 files changed
+54
-1
No files matched your search
@@ -175,13 +175,29 @@ object EaOoa {
|
||||
return head + EaCrypto.licenseEncrypt(licenseXml(l).toByteArray())
|
||||
}
|
||||
|
||||
private const val AUTH_ATTEMPTS = 4
|
||||
|
||||
internal fun tokenRejected(body: String) =
|
||||
body.contains("cause=\"AUTHENTICATION_FAILED\"") && body.contains("field=\"ea_eadmtoken\"")
|
||||
|
||||
internal fun retryTokenRejected(request: () -> EaReply): EaReply {
|
||||
var reply = request()
|
||||
var attempt = 1
|
||||
while (!reply.ok && attempt < AUTH_ATTEMPTS && tokenRejected(reply.text)) {
|
||||
attempt++
|
||||
reply = request()
|
||||
}
|
||||
return reply
|
||||
}
|
||||
|
||||
fun request(token: String, contentId: String, machineHash: String, requestToken: String? = null, requestType: String? = null): License {
|
||||
val nonce = EaCrypto.hex(ByteArray(4).also(SecureRandom()::nextBytes))
|
||||
val params = mutableListOf("contentId" to contentId, "machineHash" to machineHash, "nonce" to nonce, "ea_eadmtoken" to token)
|
||||
if (requestToken != null) params += "requestToken" to requestToken
|
||||
if (requestType != null) params += "requestType" to requestType
|
||||
val url = LICENSES + "?" + params.joinToString("&") { (k, v) -> k + "=" + URLEncoder.encode(v, "UTF-8") }
|
||||
val reply = EaHttp.request(url, headers = mapOf("X-Requester-Id" to "Origin Online Activation", "User-Agent" to "EACTransaction"))
|
||||
val headers = mapOf("X-Requester-Id" to "Origin Online Activation", "User-Agent" to "EACTransaction")
|
||||
val reply = retryTokenRejected { EaHttp.request(url, headers = headers) }
|
||||
if (!reply.ok) throw IllegalStateException("EA refused the licence (${reply.status}): ${reply.text.replace(Regex("(eadmToken value=\")[^\"]*"), "$1<redacted>\"").take(600)}")
|
||||
val signature = reply.header("x-signature") ?: throw IllegalStateException("EA's licence had no signature")
|
||||
return parseLicense(String(EaCrypto.licenseDecrypt(reply.body), Charsets.UTF_8), signature)
|
||||
|
||||
@@ -40,6 +40,43 @@ class EaTest {
|
||||
assertNull(EaCrypto.challengeSeed("9", answer))
|
||||
}
|
||||
|
||||
@Test fun onlyTokenRejectionsAreRetried() {
|
||||
val rejected = EaReply(409, "<error code=\"VALIDATION_FAILED\"><failure cause=\"AUTHENTICATION_FAILED\" field=\"ea_eadmtoken\"/></error>".toByteArray(), emptyMap())
|
||||
val notEntitled = EaReply(409, "<error code=\"VALIDATION_FAILED\"><failure cause=\"NOT_ENTITLED\" field=\"userId\" value=\"1\"/></error>".toByteArray(), emptyMap())
|
||||
var authCalls = 0
|
||||
val success = EaReply(200, ByteArray(0), emptyMap())
|
||||
val result = EaOoa.retryTokenRejected {
|
||||
authCalls++
|
||||
if (authCalls == 1) rejected else success
|
||||
}
|
||||
assertEquals(2, authCalls)
|
||||
assertEquals(200, result.status)
|
||||
|
||||
var entitlementCalls = 0
|
||||
val refused = EaOoa.retryTokenRejected {
|
||||
entitlementCalls++
|
||||
notEntitled
|
||||
}
|
||||
assertEquals(1, entitlementCalls)
|
||||
assertEquals(409, refused.status)
|
||||
|
||||
val otherAuthFailure = EaReply(409, "<error code=\"VALIDATION_FAILED\"><failure cause=\"AUTHENTICATION_FAILED\" field=\"machineHash\"/></error>".toByteArray(), emptyMap())
|
||||
var otherAuthCalls = 0
|
||||
val otherRefused = EaOoa.retryTokenRejected {
|
||||
otherAuthCalls++
|
||||
otherAuthFailure
|
||||
}
|
||||
assertEquals(1, otherAuthCalls)
|
||||
assertEquals(409, otherRefused.status)
|
||||
|
||||
var failedCalls = 0
|
||||
EaOoa.retryTokenRejected {
|
||||
failedCalls++
|
||||
rejected
|
||||
}
|
||||
assertEquals(4, failedCalls)
|
||||
}
|
||||
|
||||
@Test fun licenceRoundTripsThroughTheFileFormat() {
|
||||
val xml = "<?xml version=\"1.0\"?><License xmlns=\"http://ea.com/license\"><CipherKey>k</CipherKey><MachineHash>h</MachineHash>" +
|
||||
"<ContentId>1234</ContentId><UserId>9</UserId><GameToken>tok</GameToken><GrantTime>g</GrantTime><StartTime>s</StartTime><Nonce>n</Nonce></License>"
|
||||
|
||||
Reference in new issue
Block a user