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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use super::provider::Backup;
use crate::web::types::github::{Code, Gist, OAuth, OAuthErrorCode};
use crate::{get_packages, Package};
use async_trait::async_trait;
use serde_json::json;
use std::error::Error;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::exit;
use std::thread;
pub struct Api {
config_dir: PathBuf,
}
#[async_trait]
impl Backup for Api {
async fn new() -> Self {
let mut config_dir = dirs::config_dir().unwrap();
config_dir.push("cargo-backup");
if !config_dir.exists() {
fs::create_dir(&config_dir).unwrap();
}
Api { config_dir }
}
async fn login(self) -> Result<(), Box<dyn Error>> {
let mut auth_file = self.config_dir.clone();
auth_file.push("github.auth");
if auth_file.exists() {
return Ok(());
}
let device_login: Code = ureq::post(&format!(
"https://github.com/login/device/code?client_id={}&scope={}",
"65102f4f3d896bfc9c1a", "gist"
))
.set("Accept", "application/json")
.call()
.expect("Failed to send request")
.into_json()?;
println!("Open the following URL in your browser and enter the code.");
println!("{}", device_login.verification_uri);
println!("{}", device_login.user_code);
let mut has_token = false;
let mut auth: Option<OAuth> = None;
while !has_token {
thread::sleep(std::time::Duration::from_secs(u64::from(
device_login.interval,
)));
let poll_req: OAuth = ureq::post(&format!(
"https://github.com/login/oauth/access_token?client_id={}&grant_type={}&device_code={}",
"65102f4f3d896bfc9c1a", "urn:ietf:params:oauth:grant-type:device_code",
device_login.device_code
))
.set("Accept", "application/json")
.call()
.expect("Failed to send request")
.into_json()?;
match poll_req.error {
OAuthErrorCode::AuthorizationPending => {
}
OAuthErrorCode::SlowDown => todo!("Handle SlowDown"),
OAuthErrorCode::ExpiredToken => panic!("Token expired"),
OAuthErrorCode::UnsupportedGrantType => panic!("Unsupported grant type"),
OAuthErrorCode::IncorrectClientCredentials => {
panic!("Incorrect client credentials")
}
OAuthErrorCode::IncorrectDeviceCode => panic!("Incorrect device code"),
OAuthErrorCode::AccessDenied => panic!("Access denied"),
OAuthErrorCode::None => {
auth = Some(poll_req);
has_token = true;
}
}
}
assert!(auth.is_some());
let auth = auth.unwrap();
write_auth(&auth, &self.config_dir);
Ok(())
}
async fn fetch_backup(self) -> Result<Vec<Package>, Box<dyn Error>> {
let auth = read_auth(&self.config_dir);
if auth.gist_id.is_none() {
println!("Set the gist id via 'set-id' command before pulling");
exit(0);
}
let gist: Gist = ureq::get(&format!(
"https://api.github.com/gists/{}",
auth.gist_id.unwrap()
))
.set("Accept", "application/json")
.set("Authorization", &format!("token {}", auth.access_token))
.set("Content-Type", "application/json")
.set(
"User-Agent",
&format!("CargoBackup/{}", env!("CARGO_PKG_VERSION")),
)
.set("Authorization", &format!("token {}", auth.access_token))
.call()?
.into_json()?;
let mut packages: Vec<Package> = Vec::new();
for file in gist.files.values() {
if file.filename == "backup.json" {
packages = serde_json::from_str(file.content.as_ref().unwrap())?;
}
}
Ok(packages)
}
async fn push_backup(self) -> Result<(), Box<dyn Error>> {
let mut config = self.config_dir.clone();
config.push("github.auth");
let mut auth: OAuth =
serde_json::from_reader(std::fs::File::open(config).unwrap()).unwrap();
let packages = get_packages();
if auth.gist_id.is_some() {
let result = ureq::patch(&format!(
"https://api.github.com/gists/{}",
auth.gist_id.unwrap()
))
.set("Accept", "application/json")
.set("Authorization", &format!("token {}", auth.access_token))
.set("Content-Type", "application/json")
.set(
"User-Agent",
&format!("CargoBackup/{}", env!("CARGO_PKG_VERSION")),
)
.set("Authorization", &format!("token {}", auth.access_token))
.send_json(&json!({
"description": "Cargo Package Backup (Created by CargoBackup)",
"public": false,
"files": {
"backup.json": {
"content": serde_json::to_string(&packages).unwrap()
}
}
}))?;
if result.status() == 200 {
println!("Successfully updated backup");
} else {
println!("Failed to update backup");
}
} else {
let new_gist = ureq::post("https://api.github.com/gists")
.set("Accept", "application/json")
.set("Content-Type", "application/json")
.set(
"User-Agent",
&format!("CargoBackup/{}", env!("CARGO_PKG_VERSION")),
)
.set(
"Authorization",
&format!("{} {}", &auth.token_type, &auth.access_token),
)
.send_json(&json!({
"description": "Cargo Package Backup (Created by CargoBackup)",
"public": false,
"files": {
"backup.json": {
"filename": "backup.json",
"content": serde_json::to_string(&packages).unwrap()
}
}
}))?;
let new_gist: Gist = new_gist.into_json()?;
let gist_id = new_gist.id;
auth.gist_id = Some(gist_id.clone());
write_auth(&auth, &self.config_dir);
println!("Created gist {} ", gist_id);
}
Ok(())
}
}
fn read_auth(config_dir: &Path) -> OAuth {
let mut config: PathBuf = config_dir.to_path_buf();
config.push("github.auth");
let auth: OAuth = serde_json::from_reader(std::fs::File::open(config).unwrap()).unwrap();
auth
}
fn write_auth(auth: &OAuth, dir: &PathBuf) {
let mut auth_file = dir.clone();
if !dir.exists() {
fs::create_dir(dir).unwrap();
}
auth_file.push("github.auth");
let mut auth_file = std::fs::File::create(auth_file).unwrap();
auth_file
.write_all(serde_json::to_string(&auth).unwrap().as_bytes())
.unwrap();
}