|
|
@ -17,7 +17,7 @@ use crate::{ |
|
|
|
push::register_push_device, |
|
|
|
ApiResult, EmptyResult, JsonResult, |
|
|
|
}, |
|
|
|
auth::{generate_organization_api_key_login_claims, ClientHeaders, ClientIp, ClientType}, |
|
|
|
auth::{generate_organization_api_key_login_claims, ClientHeaders, ClientIp}, |
|
|
|
db::{models::*, DbConn}, |
|
|
|
error::MapResult, |
|
|
|
mail, util, CONFIG, |
|
|
@ -48,7 +48,7 @@ async fn login(data: Form<ConnectData>, client_header: ClientHeaders, mut conn: |
|
|
|
_check_is_some(&data.device_name, "device_name cannot be blank")?; |
|
|
|
_check_is_some(&data.device_type, "device_type cannot be blank")?; |
|
|
|
|
|
|
|
_password_login(data, &mut user_uuid, &mut conn, &client_header.ip, client_header.client_type).await |
|
|
|
_password_login(data, &mut user_uuid, &mut conn, &client_header.ip).await |
|
|
|
} |
|
|
|
"client_credentials" => { |
|
|
|
_check_is_some(&data.client_id, "client_id cannot be blank")?; |
|
|
@ -140,7 +140,6 @@ async fn _password_login( |
|
|
|
user_uuid: &mut Option<String>, |
|
|
|
conn: &mut DbConn, |
|
|
|
ip: &ClientIp, |
|
|
|
client_type: ClientType, |
|
|
|
) -> JsonResult { |
|
|
|
// Validate scope
|
|
|
|
let scope = data.scope.as_ref().unwrap(); |
|
|
@ -251,7 +250,7 @@ async fn _password_login( |
|
|
|
|
|
|
|
let (mut device, new_device) = get_device(&data, conn, &user).await; |
|
|
|
|
|
|
|
let twofactor_token = twofactor_auth(&user, &data, &mut device, ip, &client_type, conn).await?; |
|
|
|
let twofactor_token = twofactor_auth(&user, &data, &mut device, ip, conn).await?; |
|
|
|
|
|
|
|
if CONFIG.mail_enabled() && new_device { |
|
|
|
if let Err(e) = mail::send_new_device_logged_in(&user.email, &ip.ip.to_string(), &now, &device.name).await { |
|
|
@ -486,7 +485,6 @@ async fn twofactor_auth( |
|
|
|
data: &ConnectData, |
|
|
|
device: &mut Device, |
|
|
|
ip: &ClientIp, |
|
|
|
client_type: &ClientType, |
|
|
|
conn: &mut DbConn, |
|
|
|
) -> ApiResult<Option<String>> { |
|
|
|
let twofactors = TwoFactor::find_by_user(&user.uuid, conn).await; |
|
|
@ -504,7 +502,7 @@ async fn twofactor_auth( |
|
|
|
|
|
|
|
let twofactor_code = match data.two_factor_token { |
|
|
|
Some(ref code) => code, |
|
|
|
None => err_json!(_json_err_twofactor(&twofactor_ids, &user.uuid, client_type, conn).await?, "2FA token not provided"), |
|
|
|
None => err_json!(_json_err_twofactor(&twofactor_ids, &user.uuid, &data, conn).await?, "2FA token not provided"), |
|
|
|
}; |
|
|
|
|
|
|
|
let selected_twofactor = twofactors.into_iter().find(|tf| tf.atype == selected_id && tf.enabled); |
|
|
@ -528,7 +526,11 @@ async fn twofactor_auth( |
|
|
|
} |
|
|
|
false => { |
|
|
|
// OIDC based flow
|
|
|
|
duo_oidc::validate_duo_login(data.username.as_ref().unwrap().trim(), twofactor_code, client_type, conn).await? |
|
|
|
duo_oidc::validate_duo_login(data.username.as_ref().unwrap().trim(), |
|
|
|
twofactor_code, |
|
|
|
data.client_id.as_ref().unwrap(), |
|
|
|
data.device_identifier.as_ref().unwrap(), |
|
|
|
conn).await? |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -543,7 +545,7 @@ async fn twofactor_auth( |
|
|
|
} |
|
|
|
_ => { |
|
|
|
err_json!( |
|
|
|
_json_err_twofactor(&twofactor_ids, &user.uuid, client_type, conn).await?, |
|
|
|
_json_err_twofactor(&twofactor_ids, &user.uuid, &data, conn).await?, |
|
|
|
"2FA Remember token not provided" |
|
|
|
) |
|
|
|
} |
|
|
@ -571,7 +573,7 @@ fn _selected_data(tf: Option<TwoFactor>) -> ApiResult<String> { |
|
|
|
tf.map(|t| t.data).map_res("Two factor doesn't exist") |
|
|
|
} |
|
|
|
|
|
|
|
async fn _json_err_twofactor(providers: &[i32], user_uuid: &str, client_type: &ClientType, conn: &mut DbConn) -> ApiResult<Value> { |
|
|
|
async fn _json_err_twofactor(providers: &[i32], user_uuid: &str, data: &ConnectData, conn: &mut DbConn) -> ApiResult<Value> { |
|
|
|
let mut result = json!({ |
|
|
|
"error" : "invalid_grant", |
|
|
|
"error_description" : "Two factor required.", |
|
|
@ -610,7 +612,10 @@ async fn _json_err_twofactor(providers: &[i32], user_uuid: &str, client_type: &C |
|
|
|
} |
|
|
|
false => { |
|
|
|
// OIDC based flow
|
|
|
|
let auth_url = duo_oidc::get_duo_auth_url(&email, client_type, conn).await?; |
|
|
|
let auth_url = duo_oidc::get_duo_auth_url(&email, |
|
|
|
data.client_id.as_ref().unwrap(), |
|
|
|
data.device_identifier.as_ref().unwrap(), |
|
|
|
conn).await?; |
|
|
|
|
|
|
|
result["TwoFactorProviders2"][provider.to_string()] = json!({ |
|
|
|
"AuthUrl": auth_url, |
|
|
|