mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-12-03 22:01:50 +00:00
refactor(session): Use lookup_by_identifier in login route
This commit is contained in:
3 files changed
+15
-35
No files matched your search
@@ -75,7 +75,6 @@ pub(super) use report::*;
|
||||
pub(super) use room::*;
|
||||
pub(super) use search::*;
|
||||
pub(super) use send::*;
|
||||
pub use session::handle_login;
|
||||
pub(super) use session::*;
|
||||
pub(super) use space::*;
|
||||
pub(super) use state::*;
|
||||
|
||||
+13
-28
@@ -8,7 +8,6 @@ use conduwuit::{
|
||||
};
|
||||
use conduwuit_service::Services;
|
||||
use futures::StreamExt;
|
||||
use lettre::Address;
|
||||
use ruma::{
|
||||
OwnedUserId, UserId,
|
||||
api::client::{
|
||||
@@ -24,7 +23,7 @@ use ruma::{
|
||||
},
|
||||
logout, logout_all,
|
||||
},
|
||||
uiaa::{EmailUserIdentifier, MatrixUserIdentifier, UserIdentifier},
|
||||
uiaa::{MatrixUserIdentifier, UserIdentifier},
|
||||
},
|
||||
assign,
|
||||
};
|
||||
@@ -56,39 +55,25 @@ pub(crate) async fn get_login_types_route(
|
||||
]))
|
||||
}
|
||||
|
||||
pub async fn handle_login(
|
||||
async fn handle_login(
|
||||
services: &Services,
|
||||
identifier: Option<&UserIdentifier>,
|
||||
identifier: Option<UserIdentifier>,
|
||||
password: &str,
|
||||
user: Option<&String>,
|
||||
) -> Result<OwnedUserId> {
|
||||
debug!("Got password login type");
|
||||
|
||||
let user_id_or_localpart = match (identifier, user) {
|
||||
| (Some(UserIdentifier::Matrix(MatrixUserIdentifier { user, .. })), _)
|
||||
| (None, Some(user)) => user,
|
||||
| (Some(UserIdentifier::Email(EmailUserIdentifier { address, .. })), _) => {
|
||||
let email = Address::try_from(address.to_owned())
|
||||
.map_err(|_| err!(Request(InvalidParam("Email is malformed"))))?;
|
||||
|
||||
&services
|
||||
.threepid
|
||||
.get_localpart_for_email(&email)
|
||||
.await
|
||||
.ok_or_else(|| err!(Request(Forbidden("Invalid identifier or password"))))?
|
||||
},
|
||||
| _ => {
|
||||
return Err!(Request(InvalidParam("Identifier type not recognized")));
|
||||
},
|
||||
let user_id = if let Some(identifier) = identifier {
|
||||
services.users.lookup_by_identifier(identifier).await
|
||||
} else if let Some(user) = user {
|
||||
UserId::parse_with_server_name(user, &services.config.server_name).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let user_id =
|
||||
UserId::parse_with_server_name(user_id_or_localpart, &services.config.server_name)
|
||||
.map_err(|_| err!(Request(InvalidUsername("User ID is malformed"))))?;
|
||||
|
||||
if services.users.is_locked(&user_id).await? {
|
||||
return Err!(Request(UserLocked("This account has been locked.")));
|
||||
}
|
||||
let Some(user_id) = user_id else {
|
||||
return Err!(Request(InvalidUsername("User ID is malformed.")));
|
||||
};
|
||||
|
||||
if services.users.is_login_disabled(&user_id).await {
|
||||
warn!(%user_id, "user attempted to log in with a login-disabled account");
|
||||
@@ -139,7 +124,7 @@ pub(crate) async fn login_route(
|
||||
let user_id = match &body.login_info {
|
||||
#[allow(deprecated)]
|
||||
| LoginInfo::Password(login::v3::Password { identifier, password, user, .. }) =>
|
||||
handle_login(&services, identifier.as_ref(), password, user.as_ref()).await?,
|
||||
handle_login(&services, identifier.to_owned(), password, user.as_ref()).await?,
|
||||
| LoginInfo::Token(login::v3::Token { token, .. }) => {
|
||||
debug!("Got token login type");
|
||||
if !services.server.config.login_via_existing_session {
|
||||
|
||||
@@ -745,12 +745,8 @@ impl super::Service {
|
||||
.unwrap(),
|
||||
)
|
||||
},
|
||||
| UserIdentifier::Matrix(MatrixUserIdentifier { user, .. }) => UserId::parse(&user)
|
||||
.ok()
|
||||
.filter(|user_id| self.services.globals.user_is_local(user_id))
|
||||
.or_else(|| {
|
||||
UserId::parse(format!("@{user}:{}", self.services.globals.server_name())).ok()
|
||||
}),
|
||||
| UserIdentifier::Matrix(MatrixUserIdentifier { user, .. }) =>
|
||||
UserId::parse_with_server_name(user, self.services.globals.server_name()).ok(),
|
||||
| _ => None,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user