Skip to content

Commit fc68656

Browse files
committed
Revert to original string-based JSON encoding approach
1 parent 28ddf6e commit fc68656

1 file changed

Lines changed: 28 additions & 105 deletions

File tree

iRAM-Plus/AnisetteDataHelper.swift

Lines changed: 28 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,6 @@ import CryptoKit
1111
import StosSign_API_NoCertificate
1212
import StosSign_Auth
1313

14-
// Temporary struct for encoding Anisette data with correct types
15-
struct TempAnisetteData: Codable {
16-
var deviceSerialNumber: String
17-
var machineID: String?
18-
var oneTimePassword: String?
19-
var routingInfo: String?
20-
var deviceDescription: String?
21-
var localUserID: String?
22-
var deviceUniqueIdentifier: String?
23-
var date: Date?
24-
var locale: Locale?
25-
var timeZone: TimeZone?
26-
27-
enum CodingKeys: String, CodingKey {
28-
case deviceSerialNumber, machineID, oneTimePassword, routingInfo
29-
case deviceDescription, localUserID, deviceUniqueIdentifier
30-
case date, locale, timeZone
31-
}
32-
33-
init(deviceSerialNumber: String) {
34-
self.deviceSerialNumber = deviceSerialNumber
35-
}
36-
37-
init(from decoder: Decoder) throws {
38-
let container = try decoder.container(keyedBy: CodingKeys.self)
39-
deviceSerialNumber = try container.decode(String.self, forKey: .deviceSerialNumber)
40-
machineID = try container.decodeIfPresent(String.self, forKey: .machineID)
41-
oneTimePassword = try container.decodeIfPresent(String.self, forKey: .oneTimePassword)
42-
routingInfo = try container.decodeIfPresent(String.self, forKey: .routingInfo)
43-
deviceDescription = try container.decodeIfPresent(String.self, forKey: .deviceDescription)
44-
localUserID = try container.decodeIfPresent(String.self, forKey: .localUserID)
45-
deviceUniqueIdentifier = try container.decodeIfPresent(String.self, forKey: .deviceUniqueIdentifier)
46-
date = try container.decodeIfPresent(Date.self, forKey: .date)
47-
48-
if let localeIdentifier = try container.decodeIfPresent(String.self, forKey: .locale) {
49-
locale = Locale(identifier: localeIdentifier)
50-
} else {
51-
locale = nil
52-
}
53-
54-
if let timeZoneAbbreviation = try container.decodeIfPresent(String.self, forKey: .timeZone) {
55-
timeZone = TimeZone(abbreviation: timeZoneAbbreviation)
56-
} else {
57-
timeZone = nil
58-
}
59-
}
60-
61-
func encode(to encoder: Encoder) throws {
62-
var container = encoder.container(keyedBy: CodingKeys.self)
63-
try container.encode(deviceSerialNumber, forKey: .deviceSerialNumber)
64-
try container.encodeIfPresent(machineID, forKey: .machineID)
65-
try container.encodeIfPresent(oneTimePassword, forKey: .oneTimePassword)
66-
try container.encodeIfPresent(routingInfo, forKey: .routingInfo)
67-
try container.encodeIfPresent(deviceDescription, forKey: .deviceDescription)
68-
try container.encodeIfPresent(localUserID, forKey: .localUserID)
69-
try container.encodeIfPresent(deviceUniqueIdentifier, forKey: .deviceUniqueIdentifier)
70-
try container.encodeIfPresent(date, forKey: .date)
71-
try container.encodeIfPresent(locale?.identifier, forKey: .locale)
72-
try container.encodeIfPresent(timeZone?.abbreviation(), forKey: .timeZone)
73-
}
74-
}
75-
7614
final class AnisetteDataHelper
7715
{
7816
var socket: URLSessionWebSocketTask?
@@ -138,70 +76,55 @@ final class AnisetteDataHelper
13876

13977
// try to read out a dictionary
14078
// for some reason serial number isn't needed but it doesn't work unless it has a value
141-
var tempData = TempAnisetteData(deviceSerialNumber: "0")
142-
143-
if let machineID = json["X-Apple-I-MD-M"] { tempData.machineID = machineID }
144-
if let oneTimePassword = json["X-Apple-I-MD"] { tempData.oneTimePassword = oneTimePassword }
145-
if let routingInfo = json["X-Apple-I-MD-RINFO"] { tempData.routingInfo = routingInfo }
79+
var formattedJSON: [String: String] = ["deviceSerialNumber": "0"]
80+
if let machineID = json["X-Apple-I-MD-M"] { formattedJSON["machineID"] = machineID }
81+
if let oneTimePassword = json["X-Apple-I-MD"] { formattedJSON["oneTimePassword"] = oneTimePassword }
82+
if let routingInfo = json["X-Apple-I-MD-RINFO"] { formattedJSON["routingInfo"] = routingInfo }
14683

14784
if v3 {
148-
tempData.deviceDescription = self.clientInfo!
149-
tempData.localUserID = self.mdLu!
150-
tempData.deviceUniqueIdentifier = self.deviceId!
85+
formattedJSON["deviceDescription"] = self.clientInfo!
86+
formattedJSON["localUserID"] = self.mdLu!
87+
formattedJSON["deviceUniqueIdentifier"] = self.deviceId!
15188

152-
// Generate date stuff on client - use proper types
153-
tempData.date = Date()
154-
tempData.locale = Locale.current
155-
tempData.timeZone = TimeZone.current
89+
// Generate date stuff on client
90+
let formatter = DateFormatter()
91+
formatter.locale = Locale(identifier: "en_US_POSIX")
92+
formatter.calendar = Calendar(identifier: .gregorian)
93+
formatter.timeZone = TimeZone.current
94+
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
95+
let dateString = formatter.string(from: Date())
96+
formattedJSON["date"] = dateString
97+
formattedJSON["locale"] = Locale.current.identifier
98+
formattedJSON["timeZone"] = TimeZone.current.abbreviation()
15699
} else {
157-
if let deviceDescription = json["X-MMe-Client-Info"] { tempData.deviceDescription = deviceDescription }
158-
if let localUserID = json["X-Apple-I-MD-LU"] { tempData.localUserID = localUserID }
159-
if let deviceUniqueIdentifier = json["X-Mme-Device-Id"] { tempData.deviceUniqueIdentifier = deviceUniqueIdentifier }
100+
if let deviceDescription = json["X-MMe-Client-Info"] { formattedJSON["deviceDescription"] = deviceDescription }
101+
if let localUserID = json["X-Apple-I-MD-LU"] { formattedJSON["localUserID"] = localUserID }
102+
if let deviceUniqueIdentifier = json["X-Mme-Device-Id"] { formattedJSON["deviceUniqueIdentifier"] = deviceUniqueIdentifier }
160103

161-
// Parse date string to Date object
162-
if let dateString = json["X-Apple-I-Client-Time"] {
163-
let formatter = DateFormatter()
164-
formatter.locale = Locale(identifier: "en_US_POSIX")
165-
formatter.calendar = Calendar(identifier: .gregorian)
166-
formatter.timeZone = TimeZone(identifier: "UTC")
167-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
168-
if let date = formatter.date(from: dateString) {
169-
tempData.date = date
170-
}
171-
}
172-
173-
// Parse locale string to Locale object
174-
if let localeString = json["X-Apple-Locale"] {
175-
tempData.locale = Locale(identifier: localeString)
176-
}
177-
178-
// Parse timezone string to TimeZone object
179-
if let timeZoneString = json["X-Apple-I-TimeZone"] {
180-
tempData.timeZone = TimeZone(abbreviation: timeZoneString)
181-
}
104+
if let date = json["X-Apple-I-Client-Time"] { formattedJSON["date"] = date }
105+
if let locale = json["X-Apple-Locale"] { formattedJSON["locale"] = locale }
106+
if let timeZone = json["X-Apple-I-TimeZone"] { formattedJSON["timeZone"] = timeZone }
182107
}
183108

184109
if let response = response,
185110
let version = response.value(forHTTPHeaderField: "Implementation-Version") {
186111
self.printOut("Implementation-Version: \(version)")
187112
} else { self.printOut("No Implementation-Version header") }
188113

189-
self.printOut("Anisette used: \(tempData)")
114+
self.printOut("Anisette used: \(formattedJSON)")
190115
self.printOut("Original JSON: \(json)")
191116
do {
192-
let jsonData = try JSONEncoder().encode(tempData)
193-
self.printOut("Encoded JSON data: \(String(data: jsonData, encoding: .utf8) ?? "unable to encode")")
117+
let jsonData = try JSONEncoder().encode(formattedJSON)
194118
let anisette = try JSONDecoder().decode(AnisetteData.self, from: jsonData)
195119

196120
self.printOut("Anisette is valid!")
197121
return anisette
198122
} catch {
199-
self.printOut("Anisette is invalid!!!! Error: \(error)")
200-
self.printOut("Decoding error details: \(error.localizedDescription)")
123+
self.printOut("Anisette is invalid!!!!")
201124
if v3 {
202-
throw "Invalid anisette (the returned data may not have all the required fields). Error: \(error.localizedDescription)"
125+
throw "Invalid anisette (the returned data may not have all the required fields)"
203126
} else {
204-
throw "Invalid anisette (the returned data may not have all the required fields). Error: \(error.localizedDescription)"
127+
throw "Invalid anisette (the returned data may not have all the required fields)"
205128
}
206129
}
207130
} else {

0 commit comments

Comments
 (0)