1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaFile {
pub account_name: String,
pub device_id: String,
pub identity_secret: String,
pub revocation_code: String,
pub secret_1: String,
pub serial_number: u64,
pub server_time: u64,
pub shared_secret: String,
pub status: u64,
pub token_gid: String,
pub uri: String,
}
impl MaFile {
/// load a mafile from a string
///
/// ```rust
/// use libr2fa::steam::MaFile;
///
/// let mafile = MaFile::from_string(r#"{
/// "account_name": "test",
/// "device_id": "test",
/// "identity_secret": "test",
/// "revocation_code": "test",
/// "secret_1": "test",
/// "serial_number": 0,
/// "server_time": 0,
/// "shared_secret": "1Yl+tt/6w2dZEG51M8P6oc2x/cY=",
/// "status": 0,
/// "token_gid": "test",
/// "uri": "test"
/// }"#);
///
/// assert!(mafile.is_ok());
///
/// let mafile = mafile.unwrap();
///
/// assert_eq!(mafile.account_name, "test");
/// assert_eq!(mafile.device_id, "test");
/// assert_eq!(mafile.identity_secret, "test");
/// assert_eq!(mafile.revocation_code, "test");
/// assert_eq!(mafile.secret_1, "test");
/// assert_eq!(mafile.serial_number, 0);
/// assert_eq!(mafile.server_time, 0);
/// assert_eq!(mafile.shared_secret, "1Yl+tt/6w2dZEG51M8P6oc2x/cY=");
/// assert_eq!(mafile.status, 0);
/// assert_eq!(mafile.token_gid, "test");
/// assert_eq!(mafile.uri, "test");
/// ```
pub fn from_string(s: &str) -> Result<Self, crate::Error> {
let mafile = serde_json::from_str(s);
if let Err(e) = mafile {
return Err(crate::Error::SteamSerdeError(
"Error in convert json to mafile".to_string(),
s.to_string(),
e.to_string(),
));
}
Ok(mafile.unwrap())
}
/// load a mafile from a file
///
/// ```rust
/// use libr2fa::steam::MaFile;
///
/// let mafile = MaFile::from_file("./public/mafile_test.mafile");
///
/// assert!(mafile.is_ok());
///
/// let mafile = mafile.unwrap();
///
/// assert_eq!(mafile.account_name, "test");
/// assert_eq!(mafile.device_id, "test");
/// assert_eq!(mafile.identity_secret, "test");
/// assert_eq!(mafile.revocation_code, "test");
/// assert_eq!(mafile.secret_1, "test");
/// assert_eq!(mafile.serial_number, 0);
/// assert_eq!(mafile.server_time, 0);
/// assert_eq!(mafile.shared_secret, "1Yl+tt/6w2dZEG51M8P6oc2x/cY=");
/// assert_eq!(mafile.status, 0);
/// assert_eq!(mafile.token_gid, "test");
/// assert_eq!(mafile.uri, "test");
/// ```
///
/// // ./public/mafile_test.mafile
/// ```json
/// {
/// "account_name": "test",
/// "device_id": "test",
/// "identity_secret": "test",
/// "revocation_code": "test",
/// "secret_1": "test",
/// "serial_number": 0,
/// "server_time": 0,
/// "shared_secret": "1Yl+tt/6w2dZEG51M8P6oc2x/cY=",
/// "status": 0,
/// "token_gid": "test",
/// "uri": "test"
/// }
/// ```
pub fn from_file(path: &str) -> Result<Self, crate::Error> {
let s = std::fs::read_to_string(path);
if let Err(e) = s {
return Err(crate::Error::IOError(
"Error in read mafile".to_string(),
path.to_string(),
e.to_string(),
));
}
Self::from_string(&s.unwrap())
}
/// save a mafile to a string
///
/// ```rust
/// use libr2fa::steam::MaFile;
///
/// let mafile = MaFile::from_string(r#"{
/// "account_name": "test",
/// "device_id": "test",
/// "identity_secret": "test",
/// "revocation_code": "test",
/// "secret_1": "test",
/// "serial_number": 0,
/// "server_time": 0,
/// "shared_secret": "1Yl+tt/6w2dZEG51M8P6oc2x/cY=",
/// "status": 0,
/// "token_gid": "test",
/// "uri": "test"
/// }"#);
///
/// assert!(mafile.is_ok());
///
/// let mafile = mafile.unwrap();
///
/// let s = mafile.to_string();
///
/// assert!(s.is_ok());
///
/// let s = s.unwrap();
///
/// println!("{}", s);
/// ```
pub fn to_string(&self) -> Result<String, crate::Error> {
let s = serde_json::to_string(self);
if let Err(e) = s {
return Err(crate::Error::SteamSerdeError(
"Error in convert mafile to json".to_string(),
"".to_string(),
e.to_string(),
));
}
Ok(s.unwrap())
}
/// save a mafile to a file
///
/// ```rust
/// use libr2fa::steam::MaFile;
///
/// let mafile = MaFile::from_string(r#"{
/// "account_name": "test",
/// "device_id": "test",
/// "identity_secret": "test",
/// "revocation_code": "test",
/// "secret_1": "test",
/// "serial_number": 0,
/// "server_time": 0,
/// "shared_secret": "1Yl+tt/6w2dZEG51M8P6oc2x/cY=",
/// "status": 0,
/// "token_gid": "test",
/// "uri": "test"
/// }"#);
///
/// assert!(mafile.is_ok());
///
/// let mafile = mafile.unwrap();
///
/// let res = mafile.to_file("./public/mafile_save_test.mafile");
///
/// assert!(res.is_ok());
/// ```
pub fn to_file(&self, path: &str) -> Result<(), crate::Error> {
let s = self.to_string()?;
let res = std::fs::write(path, s);
if let Err(e) = res {
return Err(crate::Error::IOError(
"Error in write mafile".to_string(),
path.to_string(),
e.to_string(),
));
}
Ok(())
}
}