2021-10-25 10:36:05 +02:00
|
|
|
use chrono::Utc;
|
2019-08-03 18:47:52 +02:00
|
|
|
use num_traits::FromPrimitive;
|
2021-11-07 18:53:39 +01:00
|
|
|
use rocket::serde::json::Json;
|
2020-07-14 18:00:09 +02:00
|
|
|
use rocket::{
|
2021-11-07 18:53:39 +01:00
|
|
|
form::{Form, FromForm},
|
2020-07-14 18:00:09 +02:00
|
|
|
Route,
|
|
|
|
};
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2020-07-14 18:00:09 +02:00
|
|
|
use crate::{
|
|
|
|
api::{
|
2022-03-20 18:51:24 +01:00
|
|
|
core::accounts::{PreloginData, _prelogin},
|
2020-07-14 18:00:09 +02:00
|
|
|
core::two_factor::{duo, email, email::EmailTokenData, yubikey},
|
2022-03-20 18:51:24 +01:00
|
|
|
ApiResult, EmptyResult, JsonResult, JsonUpcase,
|
2020-07-14 18:00:09 +02:00
|
|
|
},
|
|
|
|
auth::ClientIp,
|
|
|
|
db::{models::*, DbConn},
|
|
|
|
error::MapResult,
|
|
|
|
mail, util, CONFIG,
|
|
|
|
};
|
2018-07-13 15:58:50 +02:00
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
pub fn routes() -> Vec<Route> {
|
2022-03-20 18:51:24 +01:00
|
|
|
routes![login, prelogin]
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
#[post("/connect/token", data = "<data>")]
|
2021-11-16 17:07:55 +01:00
|
|
|
async fn login(data: Form<ConnectData>, conn: DbConn, ip: ClientIp) -> JsonResult {
|
2018-10-10 20:40:39 +02:00
|
|
|
let data: ConnectData = data.into_inner();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-12-16 20:00:16 +01:00
|
|
|
match data.grant_type.as_ref() {
|
|
|
|
"refresh_token" => {
|
|
|
|
_check_is_some(&data.refresh_token, "refresh_token cannot be blank")?;
|
2021-11-16 17:07:55 +01:00
|
|
|
_refresh_login(data, conn).await
|
2018-12-16 20:00:16 +01:00
|
|
|
}
|
|
|
|
"password" => {
|
|
|
|
_check_is_some(&data.client_id, "client_id cannot be blank")?;
|
|
|
|
_check_is_some(&data.password, "password cannot be blank")?;
|
|
|
|
_check_is_some(&data.scope, "scope cannot be blank")?;
|
|
|
|
_check_is_some(&data.username, "username cannot be blank")?;
|
|
|
|
|
|
|
|
_check_is_some(&data.device_identifier, "device_identifier cannot be blank")?;
|
|
|
|
_check_is_some(&data.device_name, "device_name cannot be blank")?;
|
|
|
|
_check_is_some(&data.device_type, "device_type cannot be blank")?;
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
_password_login(data, conn, &ip).await
|
2018-12-16 20:00:16 +01:00
|
|
|
}
|
2022-01-19 11:51:26 +01:00
|
|
|
"client_credentials" => {
|
|
|
|
_check_is_some(&data.client_id, "client_id cannot be blank")?;
|
|
|
|
_check_is_some(&data.client_secret, "client_secret cannot be blank")?;
|
|
|
|
_check_is_some(&data.scope, "scope cannot be blank")?;
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
_api_key_login(data, conn, &ip).await
|
2022-01-19 11:51:26 +01:00
|
|
|
}
|
2018-12-16 20:00:16 +01:00
|
|
|
t => err!("Invalid type", t),
|
2018-06-01 15:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
async fn _refresh_login(data: ConnectData, mut conn: DbConn) -> JsonResult {
|
2018-06-01 15:08:03 +02:00
|
|
|
// Extract token
|
2018-10-17 22:25:28 +02:00
|
|
|
let token = data.refresh_token.unwrap();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-01 15:08:03 +02:00
|
|
|
// Get device by refresh token
|
2022-05-20 23:39:47 +02:00
|
|
|
let mut device = Device::find_by_refresh_token(&token, &mut conn).await.map_res("Invalid refresh token")?;
|
2018-06-01 15:08:03 +02:00
|
|
|
|
2022-01-21 06:50:58 +01:00
|
|
|
let scope = "api offline_access";
|
|
|
|
let scope_vec = vec!["api".into(), "offline_access".into()];
|
|
|
|
|
|
|
|
// Common
|
2022-05-20 23:39:47 +02:00
|
|
|
let user = User::find_by_uuid(&device.user_uuid, &mut conn).await.unwrap();
|
|
|
|
let orgs = UserOrganization::find_confirmed_by_user(&user.uuid, &mut conn).await;
|
2022-01-21 06:50:58 +01:00
|
|
|
let (access_token, expires_in) = device.refresh_tokens(&user, orgs, scope_vec);
|
2022-05-20 23:39:47 +02:00
|
|
|
device.save(&mut conn).await?;
|
2022-01-21 06:50:58 +01:00
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
Ok(Json(json!({
|
|
|
|
"access_token": access_token,
|
|
|
|
"expires_in": expires_in,
|
|
|
|
"token_type": "Bearer",
|
|
|
|
"refresh_token": device.refresh_token,
|
2019-05-20 21:24:29 +02:00
|
|
|
"Key": user.akey,
|
2018-12-19 21:52:53 +01:00
|
|
|
"PrivateKey": user.private_key,
|
2020-08-04 15:12:04 +02:00
|
|
|
|
|
|
|
"Kdf": user.client_kdf_type,
|
|
|
|
"KdfIterations": user.client_kdf_iter,
|
|
|
|
"ResetMasterPassword": false, // TODO: according to official server seems something like: user.password_hash.is_empty(), but would need testing
|
2022-01-21 06:50:58 +01:00
|
|
|
"scope": scope,
|
2021-04-06 20:38:22 +02:00
|
|
|
"unofficialServer": true,
|
2018-12-19 21:52:53 +01:00
|
|
|
})))
|
2018-06-01 15:08:03 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
async fn _password_login(data: ConnectData, mut conn: DbConn, ip: &ClientIp) -> JsonResult {
|
2018-06-01 15:08:03 +02:00
|
|
|
// Validate scope
|
2018-10-17 22:25:28 +02:00
|
|
|
let scope = data.scope.as_ref().unwrap();
|
2018-06-01 15:08:03 +02:00
|
|
|
if scope != "api offline_access" {
|
|
|
|
err!("Scope not supported")
|
|
|
|
}
|
2022-01-21 06:50:58 +01:00
|
|
|
let scope_vec = vec!["api".into(), "offline_access".into()];
|
2018-06-01 15:08:03 +02:00
|
|
|
|
2021-12-22 21:48:49 +01:00
|
|
|
// Ratelimit the login
|
|
|
|
crate::ratelimit::check_limit_login(&ip.ip)?;
|
|
|
|
|
2018-06-01 15:08:03 +02:00
|
|
|
// Get the user
|
2022-03-03 21:00:10 +01:00
|
|
|
let username = data.username.as_ref().unwrap().trim();
|
2022-05-20 23:39:47 +02:00
|
|
|
let user = match User::find_by_mail(username, &mut conn).await {
|
2018-06-01 15:08:03 +02:00
|
|
|
Some(user) => user,
|
2021-04-06 22:54:42 +02:00
|
|
|
None => err!("Username or password is incorrect. Try again", format!("IP: {}. Username: {}.", ip.ip, username)),
|
2018-06-01 15:08:03 +02:00
|
|
|
};
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-01 15:08:03 +02:00
|
|
|
// Check password
|
2018-10-17 22:25:28 +02:00
|
|
|
let password = data.password.as_ref().unwrap();
|
2018-06-01 15:08:03 +02:00
|
|
|
if !user.check_valid_password(password) {
|
2021-04-06 22:54:42 +02:00
|
|
|
err!("Username or password is incorrect. Try again", format!("IP: {}. Username: {}.", ip.ip, username))
|
2018-06-01 15:08:03 +02:00
|
|
|
}
|
2018-07-12 21:46:50 +02:00
|
|
|
|
2020-11-30 23:12:56 +01:00
|
|
|
// Check if the user is disabled
|
|
|
|
if !user.enabled {
|
2021-04-06 22:54:42 +02:00
|
|
|
err!("This user has been disabled", format!("IP: {}. Username: {}.", ip.ip, username))
|
2020-11-30 23:12:56 +01:00
|
|
|
}
|
|
|
|
|
2021-10-25 10:36:05 +02:00
|
|
|
let now = Utc::now().naive_utc();
|
2020-07-08 06:30:18 +02:00
|
|
|
|
2019-12-06 22:12:41 +01:00
|
|
|
if user.verified_at.is_none() && CONFIG.mail_enabled() && CONFIG.signups_verify() {
|
2021-03-31 22:18:35 +02:00
|
|
|
if user.last_verifying_at.is_none()
|
|
|
|
|| now.signed_duration_since(user.last_verifying_at.unwrap()).num_seconds()
|
|
|
|
> CONFIG.signups_verify_resend_time() as i64
|
|
|
|
{
|
2019-11-25 06:28:49 +01:00
|
|
|
let resend_limit = CONFIG.signups_verify_resend_limit() as i32;
|
|
|
|
if resend_limit == 0 || user.login_verify_count < resend_limit {
|
|
|
|
// We want to send another email verification if we require signups to verify
|
|
|
|
// their email address, and we haven't sent them a reminder in a while...
|
|
|
|
let mut user = user;
|
|
|
|
user.last_verifying_at = Some(now);
|
2019-12-06 22:12:41 +01:00
|
|
|
user.login_verify_count += 1;
|
2019-11-25 06:28:49 +01:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
if let Err(e) = user.save(&mut conn).await {
|
2019-11-25 06:28:49 +01:00
|
|
|
error!("Error updating user: {:#?}", e);
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:57:37 +02:00
|
|
|
if let Err(e) = mail::send_verify_email(&user.email, &user.uuid).await {
|
2019-11-25 06:28:49 +01:00
|
|
|
error!("Error auto-sending email verification email: {:#?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We still want the login to fail until they actually verified the email address
|
2021-04-06 22:54:42 +02:00
|
|
|
err!("Please verify your email before trying again.", format!("IP: {}. Username: {}.", ip.ip, username))
|
2019-11-25 06:28:49 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
let (mut device, new_device) = get_device(&data, &mut conn, &user).await;
|
2019-07-22 08:26:24 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
let twofactor_token = twofactor_auth(&user.uuid, &data, &mut device, ip, &mut conn).await?;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2019-07-25 20:47:58 +02:00
|
|
|
if CONFIG.mail_enabled() && new_device {
|
2022-07-06 23:57:37 +02:00
|
|
|
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &now, &device.name).await {
|
2019-08-18 19:32:26 +02:00
|
|
|
error!("Error sending new device email: {:#?}", e);
|
2019-08-19 22:14:00 +02:00
|
|
|
|
|
|
|
if CONFIG.require_device_email() {
|
|
|
|
err!("Could not send login notification email. Please contact your administrator.")
|
|
|
|
}
|
2019-08-18 19:32:26 +02:00
|
|
|
}
|
2019-07-25 20:47:58 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 15:08:03 +02:00
|
|
|
// Common
|
2022-05-20 23:39:47 +02:00
|
|
|
let orgs = UserOrganization::find_confirmed_by_user(&user.uuid, &mut conn).await;
|
2022-01-21 06:50:58 +01:00
|
|
|
let (access_token, expires_in) = device.refresh_tokens(&user, orgs, scope_vec);
|
2022-05-20 23:39:47 +02:00
|
|
|
device.save(&mut conn).await?;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-06-01 15:08:03 +02:00
|
|
|
let mut result = json!({
|
2018-02-10 01:00:55 +01:00
|
|
|
"access_token": access_token,
|
|
|
|
"expires_in": expires_in,
|
|
|
|
"token_type": "Bearer",
|
|
|
|
"refresh_token": device.refresh_token,
|
2019-05-20 21:24:29 +02:00
|
|
|
"Key": user.akey,
|
2018-06-01 15:08:03 +02:00
|
|
|
"PrivateKey": user.private_key,
|
2019-08-04 16:56:39 +02:00
|
|
|
//"TwoFactorToken": "11122233333444555666777888999"
|
2021-03-31 22:18:35 +02:00
|
|
|
|
2020-08-04 15:12:04 +02:00
|
|
|
"Kdf": user.client_kdf_type,
|
|
|
|
"KdfIterations": user.client_kdf_iter,
|
|
|
|
"ResetMasterPassword": false,// TODO: Same as above
|
2022-01-21 06:50:58 +01:00
|
|
|
"scope": scope,
|
2021-04-06 20:38:22 +02:00
|
|
|
"unofficialServer": true,
|
2018-06-01 15:08:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(token) = twofactor_token {
|
|
|
|
result["TwoFactorToken"] = Value::String(token);
|
|
|
|
}
|
|
|
|
|
2018-12-11 21:20:06 +01:00
|
|
|
info!("User {} logged in successfully. IP: {}", username, ip.ip);
|
2018-06-01 15:08:03 +02:00
|
|
|
Ok(Json(result))
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
async fn _api_key_login(data: ConnectData, mut conn: DbConn, ip: &ClientIp) -> JsonResult {
|
2022-01-19 11:51:26 +01:00
|
|
|
// Validate scope
|
|
|
|
let scope = data.scope.as_ref().unwrap();
|
|
|
|
if scope != "api" {
|
|
|
|
err!("Scope not supported")
|
|
|
|
}
|
2022-01-21 06:50:58 +01:00
|
|
|
let scope_vec = vec!["api".into()];
|
2022-01-19 11:51:26 +01:00
|
|
|
|
|
|
|
// Ratelimit the login
|
|
|
|
crate::ratelimit::check_limit_login(&ip.ip)?;
|
|
|
|
|
|
|
|
// Get the user via the client_id
|
|
|
|
let client_id = data.client_id.as_ref().unwrap();
|
|
|
|
let user_uuid = match client_id.strip_prefix("user.") {
|
|
|
|
Some(uuid) => uuid,
|
|
|
|
None => err!("Malformed client_id", format!("IP: {}.", ip.ip)),
|
|
|
|
};
|
2022-05-20 23:39:47 +02:00
|
|
|
let user = match User::find_by_uuid(user_uuid, &mut conn).await {
|
2022-01-19 11:51:26 +01:00
|
|
|
Some(user) => user,
|
|
|
|
None => err!("Invalid client_id", format!("IP: {}.", ip.ip)),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if the user is disabled
|
|
|
|
if !user.enabled {
|
|
|
|
err!("This user has been disabled (API key login)", format!("IP: {}. Username: {}.", ip.ip, user.email))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check API key. Note that API key logins bypass 2FA.
|
|
|
|
let client_secret = data.client_secret.as_ref().unwrap();
|
|
|
|
if !user.check_valid_api_key(client_secret) {
|
|
|
|
err!("Incorrect client_secret", format!("IP: {}. Username: {}.", ip.ip, user.email))
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
let (mut device, new_device) = get_device(&data, &mut conn, &user).await;
|
2022-01-19 11:51:26 +01:00
|
|
|
|
|
|
|
if CONFIG.mail_enabled() && new_device {
|
|
|
|
let now = Utc::now().naive_utc();
|
2022-07-06 23:57:37 +02:00
|
|
|
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &now, &device.name).await {
|
2022-01-19 11:51:26 +01:00
|
|
|
error!("Error sending new device email: {:#?}", e);
|
|
|
|
|
|
|
|
if CONFIG.require_device_email() {
|
|
|
|
err!("Could not send login notification email. Please contact your administrator.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common
|
2022-05-20 23:39:47 +02:00
|
|
|
let orgs = UserOrganization::find_confirmed_by_user(&user.uuid, &mut conn).await;
|
2022-01-21 06:50:58 +01:00
|
|
|
let (access_token, expires_in) = device.refresh_tokens(&user, orgs, scope_vec);
|
2022-05-20 23:39:47 +02:00
|
|
|
device.save(&mut conn).await?;
|
2022-01-19 11:51:26 +01:00
|
|
|
|
|
|
|
info!("User {} logged in successfully via API key. IP: {}", user.email, ip.ip);
|
|
|
|
|
2022-01-21 06:50:58 +01:00
|
|
|
// Note: No refresh_token is returned. The CLI just repeats the
|
|
|
|
// client_credentials login flow when the existing token expires.
|
2022-01-19 11:51:26 +01:00
|
|
|
Ok(Json(json!({
|
|
|
|
"access_token": access_token,
|
|
|
|
"expires_in": expires_in,
|
|
|
|
"token_type": "Bearer",
|
|
|
|
"Key": user.akey,
|
|
|
|
"PrivateKey": user.private_key,
|
|
|
|
|
|
|
|
"Kdf": user.client_kdf_type,
|
|
|
|
"KdfIterations": user.client_kdf_iter,
|
|
|
|
"ResetMasterPassword": false, // TODO: Same as above
|
2022-01-21 06:50:58 +01:00
|
|
|
"scope": scope,
|
2022-01-19 11:51:26 +01:00
|
|
|
"unofficialServer": true,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2019-07-25 20:47:58 +02:00
|
|
|
/// Retrieves an existing device or creates a new device from ConnectData and the User
|
2022-05-20 23:39:47 +02:00
|
|
|
async fn get_device(data: &ConnectData, conn: &mut DbConn, user: &User) -> (Device, bool) {
|
2019-07-22 08:24:19 +02:00
|
|
|
// On iOS, device_type sends "iOS", on others it sends a number
|
|
|
|
let device_type = util::try_parse_string(data.device_type.as_ref()).unwrap_or(0);
|
|
|
|
let device_id = data.device_identifier.clone().expect("No device id provided");
|
|
|
|
let device_name = data.device_name.clone().expect("No device name provided");
|
|
|
|
|
|
|
|
let mut new_device = false;
|
|
|
|
// Find device or create new
|
2022-03-03 21:00:10 +01:00
|
|
|
let device = match Device::find_by_uuid_and_user(&device_id, &user.uuid, conn).await {
|
|
|
|
Some(device) => device,
|
2019-07-22 08:24:19 +02:00
|
|
|
None => {
|
|
|
|
new_device = true;
|
|
|
|
Device::new(device_id, user.uuid.clone(), device_name, device_type)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-25 20:47:58 +02:00
|
|
|
(device, new_device)
|
2019-07-22 08:24:19 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
async fn twofactor_auth(
|
2018-12-30 23:34:31 +01:00
|
|
|
user_uuid: &str,
|
|
|
|
data: &ConnectData,
|
|
|
|
device: &mut Device,
|
2020-05-14 00:19:50 +02:00
|
|
|
ip: &ClientIp,
|
2022-05-20 23:39:47 +02:00
|
|
|
conn: &mut DbConn,
|
2018-12-30 23:34:31 +01:00
|
|
|
) -> ApiResult<Option<String>> {
|
2021-11-16 17:07:55 +01:00
|
|
|
let twofactors = TwoFactor::find_by_user(user_uuid, conn).await;
|
2018-07-12 21:46:50 +02:00
|
|
|
|
|
|
|
// No twofactor token if twofactor is disabled
|
2018-09-13 21:55:23 +02:00
|
|
|
if twofactors.is_empty() {
|
2018-07-12 21:46:50 +02:00
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
TwoFactorIncomplete::mark_incomplete(user_uuid, &device.uuid, &device.name, ip, conn).await?;
|
2021-10-25 10:36:05 +02:00
|
|
|
|
2019-05-20 21:24:29 +02:00
|
|
|
let twofactor_ids: Vec<_> = twofactors.iter().map(|tf| tf.atype).collect();
|
2019-03-03 16:09:15 +01:00
|
|
|
let selected_id = data.two_factor_provider.unwrap_or(twofactor_ids[0]); // If we aren't given a two factor provider, asume the first one
|
2018-07-12 21:46:50 +02:00
|
|
|
|
2018-10-17 22:25:28 +02:00
|
|
|
let twofactor_code = match data.two_factor_token {
|
|
|
|
Some(ref code) => code,
|
2021-11-16 17:07:55 +01:00
|
|
|
None => err_json!(_json_err_twofactor(&twofactor_ids, user_uuid, conn).await?, "2FA token not provided"),
|
2018-07-12 21:46:50 +02:00
|
|
|
};
|
|
|
|
|
2021-03-31 22:18:35 +02:00
|
|
|
let selected_twofactor = twofactors.into_iter().find(|tf| tf.atype == selected_id && tf.enabled);
|
2018-07-12 21:46:50 +02:00
|
|
|
|
2019-03-03 16:09:15 +01:00
|
|
|
use crate::api::core::two_factor as _tf;
|
|
|
|
use crate::crypto::ct_eq;
|
2018-07-12 21:46:50 +02:00
|
|
|
|
2019-03-03 16:09:15 +01:00
|
|
|
let selected_data = _selected_data(selected_twofactor);
|
|
|
|
let mut remember = data.two_factor_remember.unwrap_or(0);
|
2018-07-12 21:46:50 +02:00
|
|
|
|
2019-03-03 16:09:15 +01:00
|
|
|
match TwoFactorType::from_i32(selected_id) {
|
2021-03-31 22:18:35 +02:00
|
|
|
Some(TwoFactorType::Authenticator) => {
|
2021-11-16 17:07:55 +01:00
|
|
|
_tf::authenticator::validate_totp_code_str(user_uuid, twofactor_code, &selected_data?, ip, conn).await?
|
|
|
|
}
|
|
|
|
Some(TwoFactorType::Webauthn) => {
|
|
|
|
_tf::webauthn::validate_webauthn_login(user_uuid, twofactor_code, conn).await?
|
2021-03-31 22:18:35 +02:00
|
|
|
}
|
2019-08-03 18:47:52 +02:00
|
|
|
Some(TwoFactorType::YubiKey) => _tf::yubikey::validate_yubikey_login(twofactor_code, &selected_data?)?,
|
2021-03-31 22:18:35 +02:00
|
|
|
Some(TwoFactorType::Duo) => {
|
2022-03-03 21:00:10 +01:00
|
|
|
_tf::duo::validate_duo_login(data.username.as_ref().unwrap().trim(), twofactor_code, conn).await?
|
2021-03-31 22:18:35 +02:00
|
|
|
}
|
|
|
|
Some(TwoFactorType::Email) => {
|
2021-11-16 17:07:55 +01:00
|
|
|
_tf::email::validate_email_code_str(user_uuid, twofactor_code, &selected_data?, conn).await?
|
2021-03-31 22:18:35 +02:00
|
|
|
}
|
2018-07-12 21:46:50 +02:00
|
|
|
|
2019-03-03 16:09:15 +01:00
|
|
|
Some(TwoFactorType::Remember) => {
|
|
|
|
match device.twofactor_remember {
|
|
|
|
Some(ref code) if !CONFIG.disable_2fa_remember() && ct_eq(code, twofactor_code) => {
|
|
|
|
remember = 1; // Make sure we also return the token here, otherwise it will only remember the first time
|
|
|
|
}
|
2021-04-06 22:54:42 +02:00
|
|
|
_ => {
|
2021-11-16 17:07:55 +01:00
|
|
|
err_json!(
|
|
|
|
_json_err_twofactor(&twofactor_ids, user_uuid, conn).await?,
|
|
|
|
"2FA Remember token not provided"
|
|
|
|
)
|
2021-04-06 22:54:42 +02:00
|
|
|
}
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => err!("Invalid two factor provider"),
|
|
|
|
}
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
TwoFactorIncomplete::mark_complete(user_uuid, &device.uuid, conn).await?;
|
2021-10-25 10:36:05 +02:00
|
|
|
|
2019-03-03 16:09:15 +01:00
|
|
|
if !CONFIG.disable_2fa_remember() && remember == 1 {
|
2018-07-12 21:46:50 +02:00
|
|
|
Ok(Some(device.refresh_twofactor_remember()))
|
|
|
|
} else {
|
|
|
|
device.delete_twofactor_remember();
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 16:09:15 +01:00
|
|
|
fn _selected_data(tf: Option<TwoFactor>) -> ApiResult<String> {
|
2020-07-14 18:00:09 +02:00
|
|
|
tf.map(|t| t.data).map_res("Two factor doesn't exist")
|
2019-03-03 16:09:15 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
async fn _json_err_twofactor(providers: &[i32], user_uuid: &str, conn: &mut DbConn) -> ApiResult<Value> {
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::api::core::two_factor;
|
2018-07-12 21:46:50 +02:00
|
|
|
|
|
|
|
let mut result = json!({
|
2018-06-01 15:08:03 +02:00
|
|
|
"error" : "invalid_grant",
|
|
|
|
"error_description" : "Two factor required.",
|
2018-07-12 21:46:50 +02:00
|
|
|
"TwoFactorProviders" : providers,
|
|
|
|
"TwoFactorProviders2" : {} // { "0" : null }
|
|
|
|
});
|
|
|
|
|
|
|
|
for provider in providers {
|
|
|
|
result["TwoFactorProviders2"][provider.to_string()] = Value::Null;
|
|
|
|
|
|
|
|
match TwoFactorType::from_i32(*provider) {
|
|
|
|
Some(TwoFactorType::Authenticator) => { /* Nothing to do for TOTP */ }
|
|
|
|
|
2021-06-07 23:34:00 +02:00
|
|
|
Some(TwoFactorType::Webauthn) if CONFIG.domain_set() => {
|
2021-11-16 17:07:55 +01:00
|
|
|
let request = two_factor::webauthn::generate_webauthn_login(user_uuid, conn).await?;
|
2021-06-07 23:34:00 +02:00
|
|
|
result["TwoFactorProviders2"][provider.to_string()] = request.0;
|
|
|
|
}
|
|
|
|
|
2019-04-07 18:58:15 +02:00
|
|
|
Some(TwoFactorType::Duo) => {
|
2021-11-16 17:07:55 +01:00
|
|
|
let email = match User::find_by_uuid(user_uuid, conn).await {
|
2019-04-05 22:09:53 +02:00
|
|
|
Some(u) => u.email,
|
2019-04-07 18:58:15 +02:00
|
|
|
None => err!("User does not exist"),
|
2019-04-05 22:09:53 +02:00
|
|
|
};
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
let (signature, host) = duo::generate_duo_signature(&email, conn).await?;
|
2019-04-05 22:09:53 +02:00
|
|
|
|
2019-04-07 18:58:15 +02:00
|
|
|
result["TwoFactorProviders2"][provider.to_string()] = json!({
|
2019-04-15 13:06:42 +02:00
|
|
|
"Host": host,
|
2019-04-07 18:58:15 +02:00
|
|
|
"Signature": signature,
|
|
|
|
});
|
2019-04-05 22:09:53 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
Some(tf_type @ TwoFactorType::YubiKey) => {
|
2021-11-16 17:07:55 +01:00
|
|
|
let twofactor = match TwoFactor::find_by_user_and_type(user_uuid, tf_type as i32, conn).await {
|
2018-11-17 09:25:07 +01:00
|
|
|
Some(tf) => tf,
|
|
|
|
None => err!("No YubiKey devices registered"),
|
|
|
|
};
|
|
|
|
|
2019-08-03 18:47:52 +02:00
|
|
|
let yubikey_metadata: yubikey::YubikeyMetadata = serde_json::from_str(&twofactor.data)?;
|
2018-11-17 09:25:07 +01:00
|
|
|
|
2019-04-07 18:58:15 +02:00
|
|
|
result["TwoFactorProviders2"][provider.to_string()] = json!({
|
|
|
|
"Nfc": yubikey_metadata.Nfc,
|
|
|
|
})
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 16:56:39 +02:00
|
|
|
Some(tf_type @ TwoFactorType::Email) => {
|
2019-10-15 21:19:49 +02:00
|
|
|
use crate::api::core::two_factor as _tf;
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
let twofactor = match TwoFactor::find_by_user_and_type(user_uuid, tf_type as i32, conn).await {
|
2019-08-04 16:56:39 +02:00
|
|
|
Some(tf) => tf,
|
|
|
|
None => err!("No twofactor email registered"),
|
|
|
|
};
|
2019-08-03 18:47:52 +02:00
|
|
|
|
2019-10-15 21:19:49 +02:00
|
|
|
// Send email immediately if email is the only 2FA option
|
|
|
|
if providers.len() == 1 {
|
2021-11-16 17:07:55 +01:00
|
|
|
_tf::email::send_token(user_uuid, conn).await?
|
2019-10-15 21:19:49 +02:00
|
|
|
}
|
2019-08-03 18:47:52 +02:00
|
|
|
|
2019-10-15 21:19:49 +02:00
|
|
|
let email_data = EmailTokenData::from_json(&twofactor.data)?;
|
2019-08-03 18:47:52 +02:00
|
|
|
result["TwoFactorProviders2"][provider.to_string()] = json!({
|
2019-08-03 20:09:20 +02:00
|
|
|
"Email": email::obscure_email(&email_data.email),
|
2019-08-03 18:47:52 +02:00
|
|
|
})
|
2019-08-04 16:56:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 21:46:50 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
2018-06-01 15:08:03 +02:00
|
|
|
}
|
|
|
|
|
2022-03-20 18:51:24 +01:00
|
|
|
#[post("/accounts/prelogin", data = "<data>")]
|
|
|
|
async fn prelogin(data: JsonUpcase<PreloginData>, conn: DbConn) -> Json<Value> {
|
|
|
|
_prelogin(data, conn).await
|
|
|
|
}
|
|
|
|
|
2022-01-19 11:51:26 +01:00
|
|
|
// https://github.com/bitwarden/jslib/blob/master/common/src/models/request/tokenRequest.ts
|
2020-03-22 23:04:25 +01:00
|
|
|
// https://github.com/bitwarden/mobile/blob/master/src/Core/Models/Request/TokenRequest.cs
|
2021-11-07 18:53:39 +01:00
|
|
|
#[derive(Debug, Clone, Default, FromForm)]
|
2018-10-17 22:25:28 +02:00
|
|
|
#[allow(non_snake_case)]
|
2018-02-10 01:00:55 +01:00
|
|
|
struct ConnectData {
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("grant_type"))]
|
|
|
|
#[field(name = uncased("granttype"))]
|
|
|
|
grant_type: String, // refresh_token, password, client_credentials (API key)
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-10-17 22:25:28 +02:00
|
|
|
// Needed for grant_type="refresh_token"
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("refresh_token"))]
|
|
|
|
#[field(name = uncased("refreshtoken"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
refresh_token: Option<String>,
|
|
|
|
|
2022-01-19 11:51:26 +01:00
|
|
|
// Needed for grant_type = "password" | "client_credentials"
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("client_id"))]
|
|
|
|
#[field(name = uncased("clientid"))]
|
|
|
|
client_id: Option<String>, // web, cli, desktop, browser, mobile
|
|
|
|
#[field(name = uncased("client_secret"))]
|
|
|
|
#[field(name = uncased("clientsecret"))]
|
|
|
|
client_secret: Option<String>,
|
|
|
|
#[field(name = uncased("password"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
password: Option<String>,
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("scope"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
scope: Option<String>,
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("username"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
username: Option<String>,
|
|
|
|
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("device_identifier"))]
|
|
|
|
#[field(name = uncased("deviceidentifier"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
device_identifier: Option<String>,
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("device_name"))]
|
|
|
|
#[field(name = uncased("devicename"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
device_name: Option<String>,
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("device_type"))]
|
|
|
|
#[field(name = uncased("devicetype"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
device_type: Option<String>,
|
2021-12-28 00:48:33 +01:00
|
|
|
#[allow(unused)]
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("device_push_token"))]
|
|
|
|
#[field(name = uncased("devicepushtoken"))]
|
2021-11-16 17:07:55 +01:00
|
|
|
_device_push_token: Option<String>, // Unused; mobile device push not yet supported.
|
2018-10-17 22:25:28 +02:00
|
|
|
|
|
|
|
// Needed for two-factor auth
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("two_factor_provider"))]
|
|
|
|
#[field(name = uncased("twofactorprovider"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
two_factor_provider: Option<i32>,
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("two_factor_token"))]
|
|
|
|
#[field(name = uncased("twofactortoken"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
two_factor_token: Option<String>,
|
2021-11-07 18:53:39 +01:00
|
|
|
#[field(name = uncased("two_factor_remember"))]
|
|
|
|
#[field(name = uncased("twofactorremember"))]
|
2018-10-17 22:25:28 +02:00
|
|
|
two_factor_remember: Option<i32>,
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
2018-02-15 19:05:57 +01:00
|
|
|
|
2018-10-17 22:25:28 +02:00
|
|
|
fn _check_is_some<T>(value: &Option<T>, msg: &str) -> EmptyResult {
|
|
|
|
if value.is_none() {
|
|
|
|
err!(msg)
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
2018-10-17 22:25:28 +02:00
|
|
|
Ok(())
|
2018-02-17 20:47:13 +01:00
|
|
|
}
|