mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
refactor: Ruma upstraming, bake a little more
This commit is contained in:
+17
-44
@@ -8,18 +8,18 @@ use conduwuit::{Err, Error, Result, error, utils, utils::hash};
|
||||
use lettre::Address;
|
||||
use ruma::{
|
||||
UserId,
|
||||
api::client::{
|
||||
error::{ErrorKind, StandardErrorBody},
|
||||
uiaa::{
|
||||
api::{
|
||||
client::uiaa::{
|
||||
AuthData, AuthFlow, AuthType, EmailIdentity, Password, ReCaptcha, RegistrationToken,
|
||||
ThirdpartyIdCredentials, UiaaInfo, UserIdentifier,
|
||||
ThirdpartyIdCredentials, UiaaInfo, UserIdentifier, MatrixUserIdentifier
|
||||
},
|
||||
error::{ErrorKind, StandardErrorBody},
|
||||
},
|
||||
};
|
||||
use serde_json::value::RawValue;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::{Dep, config, globals, registration_tokens, threepid, users};
|
||||
use crate::{Dep, config, globals, registration_tokens, rooms::user, threepid, users};
|
||||
|
||||
pub struct Service {
|
||||
services: Services,
|
||||
@@ -190,7 +190,7 @@ impl Service {
|
||||
let mut uiaa_sessions = self.uiaa_sessions.lock().await;
|
||||
|
||||
let session_id = utils::random_string(Self::SESSION_ID_LENGTH);
|
||||
let mut info = UiaaInfo::new(flows, params);
|
||||
let mut info = assign::assign!(UiaaInfo::new(flows), {params: Some(params)});
|
||||
info.session = Some(session_id.clone());
|
||||
|
||||
uiaa_sessions.insert(session_id, UiaaSession {
|
||||
@@ -339,13 +339,10 @@ impl Service {
|
||||
#[allow(clippy::useless_let_if_seq)]
|
||||
| AuthData::Password(Password { identifier, password, .. }) => {
|
||||
let user_id_or_localpart = match identifier {
|
||||
| Some(UserIdentifier::UserIdOrLocalpart(username)) => username.to_owned(),
|
||||
| Some(UserIdentifier::Email { address }) => {
|
||||
| Some(UserIdentifier::Matrix(MatrixUserIdentifier{user})) => user.to_owned(),
|
||||
| Some(UserIdentifier::Email(address)) => {
|
||||
let Ok(email) = Address::try_from(address.to_owned()) else {
|
||||
return Err(StandardErrorBody {
|
||||
kind: ErrorKind::InvalidParam,
|
||||
message: "Email is malformed".to_owned(),
|
||||
});
|
||||
return Err(StandardErrorBody::new(ErrorKind::InvalidParam, "Email is malformed".to_owned()));
|
||||
};
|
||||
|
||||
if let Some(localpart) =
|
||||
@@ -355,27 +352,18 @@ impl Service {
|
||||
|
||||
localpart
|
||||
} else {
|
||||
return Err(StandardErrorBody {
|
||||
kind: ErrorKind::forbidden(),
|
||||
message: "Invalid identifier or password".to_owned(),
|
||||
});
|
||||
return Err(StandardErrorBody::new(ErrorKind::Forbidden, "Invalid identifier or password".to_owned()));
|
||||
}
|
||||
},
|
||||
| _ =>
|
||||
return Err(StandardErrorBody {
|
||||
kind: ErrorKind::Unrecognized,
|
||||
message: "Identifier type not recognized".to_owned(),
|
||||
}),
|
||||
return Err(StandardErrorBody::new(ErrorKind::Unrecognized, "Identifier type not recognized".to_owned())),
|
||||
};
|
||||
|
||||
let Ok(user_id) = UserId::parse_with_server_name(
|
||||
user_id_or_localpart,
|
||||
self.services.globals.server_name(),
|
||||
) else {
|
||||
return Err(StandardErrorBody {
|
||||
kind: ErrorKind::InvalidParam,
|
||||
message: "User ID is malformed".to_owned(),
|
||||
});
|
||||
return Err(StandardErrorBody::new(ErrorKind::InvalidParam, "User ID is malformed".to_owned()));
|
||||
};
|
||||
|
||||
// Check if password is correct
|
||||
@@ -408,29 +396,20 @@ impl Service {
|
||||
|
||||
Ok(AuthType::Password)
|
||||
} else {
|
||||
Err(StandardErrorBody {
|
||||
kind: ErrorKind::forbidden(),
|
||||
message: "Invalid identifier or password".to_owned(),
|
||||
})
|
||||
Err(StandardErrorBody::new(ErrorKind::Forbidden, "Invalid identifier or password".to_owned()))
|
||||
}
|
||||
},
|
||||
| AuthData::ReCaptcha(ReCaptcha { response, .. }) => {
|
||||
let Some(ref private_site_key) = self.services.config.recaptcha_private_site_key
|
||||
else {
|
||||
return Err(StandardErrorBody {
|
||||
kind: ErrorKind::forbidden(),
|
||||
message: "ReCaptcha is not configured".to_owned(),
|
||||
});
|
||||
return Err(StandardErrorBody::new(ErrorKind::Forbidden, "ReCaptcha is not configured".to_owned()));
|
||||
};
|
||||
|
||||
match recaptcha_verify::verify_v3(private_site_key, response, None).await {
|
||||
| Ok(()) => Ok(AuthType::ReCaptcha),
|
||||
| Err(e) => {
|
||||
error!("ReCaptcha verification failed: {e:?}");
|
||||
Err(StandardErrorBody {
|
||||
kind: ErrorKind::forbidden(),
|
||||
message: "ReCaptcha verification failed".to_owned(),
|
||||
})
|
||||
Err(StandardErrorBody::new(ErrorKind::Forbidden, "ReCaptcha verification failed".to_owned()))
|
||||
},
|
||||
}
|
||||
},
|
||||
@@ -449,17 +428,11 @@ impl Service {
|
||||
|
||||
Ok(AuthType::RegistrationToken)
|
||||
} else {
|
||||
Err(StandardErrorBody {
|
||||
kind: ErrorKind::forbidden(),
|
||||
message: "Invalid registration token".to_owned(),
|
||||
})
|
||||
Err(StandardErrorBody::new(ErrorKind::Forbidden, "Invalid registration token".to_owned()))
|
||||
}
|
||||
},
|
||||
| AuthData::Terms(_) => Ok(AuthType::Terms),
|
||||
| _ => Err(StandardErrorBody {
|
||||
kind: ErrorKind::Unrecognized,
|
||||
message: "Unsupported stage type".into(),
|
||||
}),
|
||||
| _ => Err(StandardErrorBody::new(ErrorKind::Unrecognized, "Unsupported stage type".into())),
|
||||
}
|
||||
.map(|auth_type| (auth_type, identity))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user