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
use error::*;
use reqwest;
use serde_json;
use std::time::SystemTime;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Token {
pub expires_in: u64,
pub scope: String,
pub token_type: String,
pub access_token: String,
pub user_id: String,
pub refresh_token: String,
pub session_id: String,
#[serde(default = "cur_date")]
pub updated_at: u64,
}
fn cur_date() -> u64 {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("System time is before Unix Epoch")
.as_secs()
}
impl Token {
pub fn from_response(response: &str) -> Result<Token> {
println!("{}", response);
Ok(serde_json::from_str(response)?)
}
pub fn from_login_code(code: &str) -> Result<Token> {
let mut res = reqwest::get(&("https://auth.gog.com/token?client_id=46899977096215655&client_secret=9d85c43b1482497dbbce61f6e4aa173a433796eeae2ca8c5f6129f2dc4de46d9&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fembed.gog.com%2Fon_login_success%3Forigin%3Dclient&layout=client2&code=".to_string()+&code+""))?;
Token::from_response(&res.text()?)
}
pub fn is_expired(&self) -> bool {
self.updated_at + self.expires_in - cur_date() <= 0
}
pub fn refresh(&self) -> Result<Token> {
let mut res = reqwest::get(&("https://auth.gog.com/token?client_id=46899977096215655&client_secret=9d85c43b1482497dbbce61f6e4aa173a433796eeae2ca8c5f6129f2dc4de46d9&grant_type=refresh_token&redirect_uri=https://embed.gog.com/on_login_success?origin=client&refresh_token=".to_string()+&self.refresh_token))?;
Ok(serde_json::from_str(&res.text()?)?)
}
}