-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathindex_auth.rs
More file actions
181 lines (153 loc) · 4.54 KB
/
Copy pathindex_auth.rs
File metadata and controls
181 lines (153 loc) · 4.54 KB
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
#[cfg(not(target_os = "android"))]
use cli_clipboard::ClipboardProvider;
use reqwest::header::USER_AGENT;
use serde::Deserialize;
use serde_json::json;
use crate::{
config::Config, done, fatal, index, info, logging::ask_value, server::ApiResponse, warn,
NiceUnwrap,
};
#[derive(Debug, Deserialize)]
struct LoginAttempt {
uuid: String,
interval: i32,
uri: String,
code: String,
}
#[cfg(not(target_os = "android"))]
pub fn copy_token(token: &str) {
if let Ok(mut ctx) = cli_clipboard::ClipboardContext::new() {
if ctx.set_contents(token.to_string()).is_ok() {
done!("Token has been copied to your clipboard");
}
}
}
#[cfg(target_os = "android")]
pub fn copy_token(token: &str) {
if terminal_clipboard::set_string(token).is_ok() {
done!("Token has been copied to your clipboard");
}
}
pub fn login(config: &mut Config, token: Option<String>, github_token: Option<String>) {
if let Some(token) = token {
config.index_token = Some(token);
config.save();
done!("Successfully logged in with the provided token");
return;
}
if config.index_token.is_some() {
warn!("You are already logged in");
let token = config.index_token.clone().unwrap();
info!("Your token is: {}", token);
return;
}
let client = reqwest::blocking::Client::new();
if let Some(github_token) = github_token {
let response = client
.post(index::get_index_url("/v1/login/github/token", config))
.header(USER_AGENT, "GeodeCli")
.json(&json!({ "token": github_token }))
.send()
.nice_unwrap("Unable to connect to Geode Index");
let parsed: ApiResponse<String> = match response.status().as_u16() {
400 => fatal!("Invalid Github Token"),
200 => response
.json()
.nice_unwrap("Unable to parse login response"),
_ => fatal!("Unable to connect to Geode Index"),
};
config.index_token = Some(parsed.payload);
config.save();
done!("Successfully logged in via Github token");
return;
}
let response: reqwest::blocking::Response = client
.post(index::get_index_url("/v1/login/github", config))
.header(USER_AGENT, "GeodeCli")
.json(&{})
.send()
.nice_unwrap("Unable to connect to Geode Index");
if !response.status().is_success() {
fatal!("Unable to connect to Geode Index");
}
let parsed = response
.json::<ApiResponse<LoginAttempt>>()
.nice_unwrap("Unable to parse login response");
let login_data = parsed.payload;
info!("You will need to complete the login process in your web browser");
info!("Go to: {} and enter the login code", &login_data.uri);
info!("Your login code is: {}", &login_data.code);
copy_token(&login_data.code);
if let Err(msg) = open::that(&login_data.uri) {
warn!("Unable to open browser: {}", msg);
warn!("Go to the URL manually: {}", &login_data.uri);
}
loop {
info!("Checking login status");
if let Some(token) = poll_login(&client, &login_data.uuid, config) {
config.index_token = Some(token);
config.save();
done!("Login successful");
break;
}
std::thread::sleep(std::time::Duration::from_secs(login_data.interval as u64));
}
}
fn poll_login(
client: &reqwest::blocking::Client,
uuid: &str,
config: &mut Config,
) -> Option<String> {
let response = client
.post(index::get_index_url("/v1/login/github/poll", config))
.json(&json!({ "uuid": uuid }))
.header(USER_AGENT, "GeodeCLI")
.send()
.nice_unwrap("Unable to connect to Geode Index");
if !response.status().is_success() {
return None;
}
let parsed = response
.json::<ApiResponse<String>>()
.nice_unwrap("Unable to parse login response");
Some(parsed.payload)
}
pub fn invalidate(config: &mut Config) {
if config.index_token.is_none() {
warn!("You are not logged in");
return;
}
let response = ask_confirm(
"Do you want to log out of all devices?",
false
);
if response {
invalidate_index_tokens(config);
config.index_token = None;
config.save();
done!("All tokens for the current account have been invalidated successfully");
} else {
done!("Operation canceled");
}
}
fn invalidate_index_tokens(config: &mut Config) {
if config.index_token.is_none() {
fatal!("You are not logged in");
}
let token = config.index_token.clone().unwrap();
let client = reqwest::blocking::Client::new();
let response = client
.delete(index::get_index_url("/v1/me/tokens", config))
.header(USER_AGENT, "GeodeCLI")
.bearer_auth(token)
.send()
.nice_unwrap("Unable to connect to Geode Index");
if response.status() == 401 {
config.index_token = None;
config.save();
fatal!("Invalid token. Please login again.");
}
if !response.status().is_success() {
fatal!("Unable to invalidate token");
}
}