Skip to content

Commit 27324f8

Browse files
committed
Github Token Login
1 parent 6f81a64 commit 27324f8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/index.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ pub enum Index {
3131
/// Existing access token to use
3232
#[clap(long)]
3333
token: Option<String>,
34+
35+
#[clap(long, conflicts_with = "token")]
36+
github_token: Option<String>
3437
},
3538

3639
/// Invalidate all existing access tokens (logout)
@@ -358,7 +361,7 @@ pub fn subcommand(cmd: Index) {
358361
);
359362
done!("Mod installed");
360363
}
361-
Index::Login { token } => index_auth::login(config, token),
364+
Index::Login { token, github_token } => index_auth::login(config, token, github_token),
362365
Index::Invalidate => index_auth::invalidate(config),
363366
Index::Url { url } => {
364367
if let Some(u) = url {

src/index_auth.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn copy_token(token: &str) {
3333
}
3434
}
3535

36-
pub fn login(config: &mut Config, token: Option<String>) {
36+
pub fn login(config: &mut Config, token: Option<String>, github_token: Option<String>) {
3737
if let Some(token) = token {
3838
config.index_token = Some(token);
3939
config.save();
@@ -50,6 +50,25 @@ pub fn login(config: &mut Config, token: Option<String>) {
5050

5151
let client = reqwest::blocking::Client::new();
5252

53+
if let Some(github_token) = github_token {
54+
let response = client
55+
.post(index::get_index_url("/v1/login/github/token", config))
56+
.header(USER_AGENT, "GeodeCli")
57+
.json(&json!({ "token": github_token }))
58+
.send()
59+
.nice_unwrap("Unable to connect to Geode Index");
60+
61+
let parsed: ApiResponse<String> = match response.status().as_u16() {
62+
400 => fatal!("Invalid Github Token"),
63+
200 => response.json().nice_unwrap("Unable to parse login response"),
64+
_ => fatal!("Unable to connect to Geode Index"),
65+
};
66+
67+
config.index_token = Some(parsed.payload);
68+
config.save();
69+
done!("Successfully logged in via Github token");
70+
}
71+
5372
let response: reqwest::blocking::Response = client
5473
.post(index::get_index_url("/v1/login/github", config))
5574
.header(USER_AGENT, "GeodeCli")

0 commit comments

Comments
 (0)