|
1
|
use { |
|
2
|
crate::{ |
|
3
|
cli::{AuthCommand, GitCredentialOperation}, |
|
4
|
config, |
|
5
|
context::Ctx, |
|
6
|
credential, ui, |
|
7
|
}, |
|
8
|
anyhow::{anyhow, Context, Result}, |
|
9
|
sigit_si_api::{account::incomplete_reason, models::AccountStatus}, |
|
10
|
std::io::{self, Write}, |
|
11
|
}; |
|
12
|
|
|
13
|
pub async fn run(ctx: &Ctx, command: AuthCommand) -> Result<()> { |
|
14
|
match command { |
|
15
|
AuthCommand::Login { email } => login(ctx, email).await, |
|
16
|
AuthCommand::Logout => logout(ctx).await, |
|
17
|
AuthCommand::Status => status(ctx).await, |
|
18
|
AuthCommand::Signup { email } => signup(ctx, email).await, |
|
19
|
AuthCommand::Resend { email } => resend(ctx, email).await, |
|
20
|
AuthCommand::ResetPassword { email } => reset_password(ctx, email).await, |
|
21
|
AuthCommand::DeleteAccount { yes } => delete_account(ctx, yes).await, |
|
22
|
AuthCommand::SetupGit => setup_git(ctx), |
|
23
|
AuthCommand::GitCredential { operation } => git_credential(ctx, operation).await, |
|
24
|
} |
|
25
|
} |
|
26
|
|
|
27
|
fn setup_git(ctx: &Ctx) -> Result<()> { |
|
28
|
let exe = std::env::current_exe().context("Could not locate the `si` executable.")?; |
|
29
|
let base_url = ctx.client.base_url(); |
|
30
|
credential::setup_global_helper(base_url, &exe)?; |
|
31
|
ui::success(format!( |
|
32
|
"Git will now sign in to {} through `si`.", |
|
33
|
ui::highlight(base_url) |
|
34
|
)); |
|
35
|
Ok(()) |
|
36
|
} |
|
37
|
|
|
38
|
/// Answer git's credential protocol. `store` and `erase` do nothing, since the |
|
39
|
/// password is minted on demand and never kept. Anything this helper can't |
|
40
|
/// answer for stays silent so git moves on to the next helper. |
|
41
|
async fn git_credential(ctx: &Ctx, operation: GitCredentialOperation) -> Result<()> { |
|
42
|
if !matches!(operation, GitCredentialOperation::Get) { |
|
43
|
return Ok(()); |
|
44
|
} |
|
45
|
|
|
46
|
let request = credential::parse_request(io::stdin().lock()); |
|
47
|
if ctx.client.token().is_none() || !credential::is_for_host(&request, ctx.client.base_url()) { |
|
48
|
return Ok(()); |
|
49
|
} |
|
50
|
|
|
51
|
let minted = ctx.client.git_credential().await?; |
|
52
|
let mut stdout = io::stdout().lock(); |
|
53
|
stdout.write_all(credential::response(&minted.username, &minted.git_token).as_bytes())?; |
|
54
|
stdout.flush()?; |
|
55
|
Ok(()) |
|
56
|
} |
|
57
|
|
|
58
|
fn email_or_prompt(email: Option<String>) -> Result<String> { |
|
59
|
match email { |
|
60
|
Some(email) => Ok(email), |
|
61
|
None => ui::prompt::text("Email"), |
|
62
|
} |
|
63
|
} |
|
64
|
|
|
65
|
async fn signup(ctx: &Ctx, email: Option<String>) -> Result<()> { |
|
66
|
let email = email_or_prompt(email)?; |
|
67
|
let password = ui::prompt::new_password("Password")?; |
|
68
|
|
|
69
|
let result = ctx.client.sign_up(&email, &password).await?; |
|
70
|
ui::emit(ctx.json, &result, |result| { |
|
71
|
let message = result["message"] |
|
72
|
.as_str() |
|
73
|
.unwrap_or("Signed up. Check your email to confirm your account."); |
|
74
|
ui::success(message); |
|
75
|
ui::note("Once it is confirmed, run `si auth login`."); |
|
76
|
}) |
|
77
|
} |
|
78
|
|
|
79
|
async fn resend(ctx: &Ctx, email: Option<String>) -> Result<()> { |
|
80
|
let email = email_or_prompt(email)?; |
|
81
|
ctx.client.resend_confirmation(&email).await?; |
|
82
|
ui::success(format!( |
|
83
|
"If {} has an account that is not confirmed yet, a new email is on its way.", |
|
84
|
ui::highlight(&email) |
|
85
|
)); |
|
86
|
Ok(()) |
|
87
|
} |
|
88
|
|
|
89
|
async fn reset_password(ctx: &Ctx, email: Option<String>) -> Result<()> { |
|
90
|
let email = email_or_prompt(email)?; |
|
91
|
ctx.client.reset_password(&email).await?; |
|
92
|
ui::success(format!( |
|
93
|
"If {} has an account, reset instructions are on their way.", |
|
94
|
ui::highlight(&email) |
|
95
|
)); |
|
96
|
Ok(()) |
|
97
|
} |
|
98
|
|
|
99
|
async fn delete_account(ctx: &Ctx, yes: bool) -> Result<()> { |
|
100
|
ctx.require_token()?; |
|
101
|
|
|
102
|
if !yes { |
|
103
|
let user = ctx.client.user().await?; |
|
104
|
ui::note(format!( |
|
105
|
"This permanently deletes the account {} and everything in it.", |
|
106
|
user.email |
|
107
|
)); |
|
108
|
let typed = ui::prompt::text(&format!("Type {} to confirm", user.email))?; |
|
109
|
if !typed.trim().eq_ignore_ascii_case(&user.email) { |
|
110
|
ui::note("Cancelled."); |
|
111
|
return Ok(()); |
|
112
|
} |
|
113
|
} |
|
114
|
|
|
115
|
ctx.client.delete_account().await?; |
|
116
|
config::clear_token(ctx.environment)?; |
|
117
|
ui::success("Your account was deleted."); |
|
118
|
Ok(()) |
|
119
|
} |
|
120
|
|
|
121
|
async fn login(ctx: &Ctx, email: Option<String>) -> Result<()> { |
|
122
|
let email = match email { |
|
123
|
Some(email) => email, |
|
124
|
None => ui::prompt::text("Email")?, |
|
125
|
}; |
|
126
|
let password = ui::prompt::password("Password")?; |
|
127
|
|
|
128
|
match ctx.client.sign_in(&email, &password).await? { |
|
129
|
AccountStatus::Ready { access_token } => { |
|
130
|
let path = config::write_token(ctx.environment, &access_token)?; |
|
131
|
ui::success(format!( |
|
132
|
"Signed in as {}. Token saved to {}.", |
|
133
|
ui::highlight(&email), |
|
134
|
path.display() |
|
135
|
)); |
|
136
|
Ok(()) |
|
137
|
} |
|
138
|
AccountStatus::NotFound => Err(anyhow!( |
|
139
|
"No sigit.si account found for {email}. Sign up at https://sigit.si/auth/signup." |
|
140
|
)), |
|
141
|
AccountStatus::Incomplete { status } => Err(anyhow!("{}", incomplete_reason(status))), |
|
142
|
} |
|
143
|
} |
|
144
|
|
|
145
|
async fn logout(ctx: &Ctx) -> Result<()> { |
|
146
|
// A token file surviving from a previous, now-unauthenticated session |
|
147
|
// shouldn't block sign-out — clear it locally either way. |
|
148
|
if ctx.client.token().is_some() { |
|
149
|
let _ = ctx.client.sign_out().await; |
|
150
|
} |
|
151
|
config::clear_token(ctx.environment)?; |
|
152
|
ui::success("Signed out."); |
|
153
|
Ok(()) |
|
154
|
} |
|
155
|
|
|
156
|
async fn status(ctx: &Ctx) -> Result<()> { |
|
157
|
if ctx.client.token().is_none() { |
|
158
|
println!("Not signed in to {}. Run `si auth login`.", ctx.environment); |
|
159
|
return Ok(()); |
|
160
|
} |
|
161
|
|
|
162
|
match ctx.client.user().await { |
|
163
|
Ok(user) => ui::emit(ctx.json, &user, |user| { |
|
164
|
println!( |
|
165
|
"Signed in to {} as {} <{}>.", |
|
166
|
ctx.environment, |
|
167
|
ui::highlight(&user.username), |
|
168
|
user.email |
|
169
|
); |
|
170
|
}), |
|
171
|
Err(e) if e.is_auth_failure() => { |
|
172
|
config::clear_token(ctx.environment)?; |
|
173
|
Err(anyhow!( |
|
174
|
"Your session has expired. Run `si auth login` to sign in again." |
|
175
|
)) |
|
176
|
} |
|
177
|
Err(e) => Err(e.into()), |
|
178
|
} |
|
179
|
} |