-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal.rs
More file actions
174 lines (150 loc) · 4.23 KB
/
local.rs
File metadata and controls
174 lines (150 loc) · 4.23 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
use anyhow::{Context, anyhow};
use axum::http::StatusCode;
use mogh_auth_client::api::login::{
JwtOrTwoFactor, JwtResponse, LoginLocalUser, SignUpLocalUser,
};
use mogh_error::{AddStatusCode, AddStatusCodeError};
use mogh_rate_limit::WithFailureRateLimit;
use mogh_resolver::Resolve;
use tracing::{info, instrument};
use crate::{AuthImpl, api::login::LoginArgs, session::Session};
pub async fn sign_up_local_user<I: AuthImpl + ?Sized>(
auth: &I,
username: String,
password: &str,
) -> mogh_error::Result<JwtResponse> {
if !auth.local_auth_enabled() {
return Err(
anyhow!("Local auth is not enabled")
.status_code(StatusCode::UNAUTHORIZED),
);
}
let no_users_exist = auth.no_users_exist().await?;
if auth.local_registration_disabled() && !no_users_exist {
return Err(
anyhow!("User registration is disabled")
.status_code(StatusCode::UNAUTHORIZED),
);
}
auth.validate_username(&username)?;
auth.validate_password(&password)?;
let hashed_password =
bcrypt::hash(password.as_bytes(), auth.local_auth_bcrypt_cost())?;
let user_id = auth
.sign_up_local_user(
username.clone(),
hashed_password,
no_users_exist,
)
.await?;
info!(user_id, username, "New user registration (Local)");
auth.jwt_provider().encode_sub(&user_id).map_err(Into::into)
}
impl Resolve<LoginArgs> for SignUpLocalUser {
#[instrument("SignUpLocalUser", skip_all, fields(ip = ip.to_string()))]
async fn resolve(
self,
LoginArgs { auth, ip, .. }: &LoginArgs,
) -> Result<Self::Response, Self::Error> {
sign_up_local_user(auth.as_ref(), self.username, &self.password)
.with_failure_rate_limit_using_ip(
auth.general_rate_limiter(),
&ip,
)
.await
}
}
pub async fn login_local_user<I: AuthImpl + ?Sized>(
auth: &I,
session: &Session,
username: String,
password: &str,
) -> mogh_error::Result<JwtOrTwoFactor> {
if !auth.local_auth_enabled() {
return Err(
anyhow!("Local auth is not enabled")
.status_code(StatusCode::UNAUTHORIZED),
);
}
auth.validate_username(&username)?;
let user = auth
.find_user_with_username(username)
.await?
.context("Invalid login credentials")
.status_code(StatusCode::UNAUTHORIZED)?;
let hashed_password = user
.hashed_password()
.context("Invalid login credentials")
.status_code(StatusCode::UNAUTHORIZED)?;
let verified = bcrypt::verify(password, hashed_password)
.context("Invalid login credentials")
.status_code(StatusCode::UNAUTHORIZED)?;
if !verified {
return Err(
anyhow!("Invalid login credentials")
.status_code(StatusCode::UNAUTHORIZED),
);
}
let res = match (user.passkey(), user.totp_secret()) {
// Passkey 2FA
(Some(passkey), _) => {
let provider = auth.passkey_provider().context(
"No passkey provider available, possibly invalid 'host' config.",
)?;
let (response, state) = provider
.start_passkey_authentication(passkey)
.context("Failed to start passkey authentication flow")?;
session.insert_passkey_login(user.id(), &state).await?;
info!(
user_id = user.id(),
username = user.username(),
"Passkey 2FA flow initiated"
);
JwtOrTwoFactor::Passkey(response)
}
// TOTP 2FA
(None, Some(_)) => {
session.insert_totp_login_user_id(user.id()).await?;
info!(
user_id = user.id(),
username = user.username(),
"TOTP 2FA flow initiated"
);
JwtOrTwoFactor::Totp {}
}
(None, None) => {
info!(
user_id = user.id(),
username = user.username(),
"User logged in"
);
JwtOrTwoFactor::Jwt(auth.jwt_provider().encode_sub(user.id())?)
}
};
Ok(res)
}
impl Resolve<LoginArgs> for LoginLocalUser {
#[instrument(
"LoginLocalUser",
skip_all,
fields(
ip = ip.to_string(),
)
)]
async fn resolve(
self,
LoginArgs { auth, session, ip }: &LoginArgs,
) -> Result<Self::Response, Self::Error> {
login_local_user(
auth.as_ref(),
&session,
self.username,
&self.password,
)
.with_failure_rate_limit_using_ip(
auth.local_login_rate_limiter(),
&ip,
)
.await
}
}