fix parsing for add authenticator response

This commit is contained in:
Carson McManus 2021-08-09 22:00:49 -04:00
parent 691d927050
commit ad2cdd2a7e
2 changed files with 29 additions and 0 deletions

View file

@ -0,0 +1 @@
{"response":{"shared_secret":"wGwZx=sX5MmTxi6QgA3Gi","serial_number":"72016503753671","revocation_code":"R123456","uri":"otpauth://totp/Steam:hydrastar2?secret=JRX7DZIF4JNA3QE3UMS4BDACDISZTRWA&issuer=Steam","server_time":"1628559846","account_name":"hydrastar2","token_gid":"fe12390348285d7f4","identity_secret":"soo58ouTUV+5=KhRKDVK","secret_1":"Me7ngFQsY9R=x3EQyOU","status":1}}

View file

@ -513,6 +513,7 @@ pub struct AddAuthenticatorResponse {
/// URI for QR code generation /// URI for QR code generation
pub uri: String, pub uri: String,
/// Current server time /// Current server time
#[serde(deserialize_with = "parse_json_string_as_number")]
pub server_time: u64, pub server_time: u64,
/// Account name to display on token client /// Account name to display on token client
pub account_name: String, pub account_name: String,
@ -552,3 +553,30 @@ pub struct FinalizeAddAuthenticatorResponse {
pub want_more: bool, pub want_more: bool,
pub success: bool, pub success: bool,
} }
fn parse_json_string_as_number<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
// for some reason, deserializing to &str doesn't work but this does.
let s: String = Deserialize::deserialize(deserializer)?;
Ok(s.parse().unwrap())
}
#[test]
fn test_parse_add_auth_response() {
let result = serde_json::from_str::<SteamApiResponse<AddAuthenticatorResponse>>(include_str!(
"fixtures/api-responses/add-authenticator-1.json"
));
assert!(
matches!(result, Ok(_)),
"got error: {}",
result.unwrap_err()
);
let resp = result.unwrap().response;
assert_eq!(resp.server_time, 1628559846);
assert_eq!(resp.shared_secret, "wGwZx=sX5MmTxi6QgA3Gi");
assert_eq!(resp.revocation_code, "R123456");
}