mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
refactor: Update logic for checking if a username is available
This commit is contained in:
@@ -49,39 +49,16 @@ pub(crate) async fn get_register_available_route(
|
|||||||
ClientIp(client): ClientIp,
|
ClientIp(client): ClientIp,
|
||||||
body: Ruma<get_username_availability::v3::Request>,
|
body: Ruma<get_username_availability::v3::Request>,
|
||||||
) -> Result<get_username_availability::v3::Response> {
|
) -> Result<get_username_availability::v3::Response> {
|
||||||
// Validate user id
|
let _ = services
|
||||||
let user_id =
|
.users
|
||||||
match UserId::parse_with_server_name(&body.username, services.globals.server_name()) {
|
.determine_registration_user_id(
|
||||||
| Ok(user_id) => {
|
Some(body.username.clone()),
|
||||||
if let Err(e) = user_id.validate_strict() {
|
None,
|
||||||
return Err!(Request(InvalidUsername(debug_warn!(
|
body.identity
|
||||||
"Username {} contains disallowed characters or spaces: {e}",
|
.as_ref()
|
||||||
body.username
|
.and_then(ClientIdentity::appservice_info),
|
||||||
))));
|
)
|
||||||
}
|
.await?;
|
||||||
|
|
||||||
user_id
|
|
||||||
},
|
|
||||||
| Err(e) => {
|
|
||||||
return Err!(Request(InvalidUsername(debug_warn!(
|
|
||||||
"Username {} is not valid: {e}",
|
|
||||||
body.username
|
|
||||||
))));
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Check if username is creative enough
|
|
||||||
if services.users.exists(&user_id).await {
|
|
||||||
return Err!(Request(UserInUse("User ID is not available.")));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(ClientIdentity::Appservice { appservice_info, .. }) = &body.identity
|
|
||||||
&& !appservice_info.is_user_match(&user_id)
|
|
||||||
{
|
|
||||||
return Err!(Request(Exclusive("Username is not in an appservice namespace.")));
|
|
||||||
} else if services.appservice.is_exclusive_user_id(&user_id).await {
|
|
||||||
return Err!(Request(Exclusive("Username is reserved by an appservice.")));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(get_username_availability::v3::Response::new(true))
|
Ok(get_username_availability::v3::Response::new(true))
|
||||||
}
|
}
|
||||||
@@ -109,8 +86,7 @@ pub(crate) async fn change_password_route(
|
|||||||
ClientIp(client): ClientIp,
|
ClientIp(client): ClientIp,
|
||||||
body: Ruma<change_password::v3::Request>,
|
body: Ruma<change_password::v3::Request>,
|
||||||
) -> Result<change_password::v3::Response> {
|
) -> Result<change_password::v3::Response> {
|
||||||
let identity = if let Some(identity) = body.identity.as_ref()
|
let identity = if let Some(identity) = body.identity.as_ref() {
|
||||||
{
|
|
||||||
// A signed-in user is trying to change their password, prompt them for their
|
// A signed-in user is trying to change their password, prompt them for their
|
||||||
// existing one
|
// existing one
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,12 @@ pub(crate) async fn add_3pid_route(
|
|||||||
// Require password auth to add an email
|
// Require password auth to add an email
|
||||||
let _ = services
|
let _ = services
|
||||||
.uiaa
|
.uiaa
|
||||||
.authenticate_password(&body.auth, body.identity.sender_user(), body.identity.sender_device(), None)
|
.authenticate_password(
|
||||||
|
&body.auth,
|
||||||
|
body.identity.sender_user(),
|
||||||
|
body.identity.sender_device(),
|
||||||
|
None,
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let email = services
|
let email = services
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ use conduwuit::{Err, Result};
|
|||||||
use ruma::{
|
use ruma::{
|
||||||
api::client::discovery::{
|
api::client::discovery::{
|
||||||
discover_homeserver::{self, HomeserverInfo},
|
discover_homeserver::{self, HomeserverInfo},
|
||||||
discover_policy_server,
|
discover_policy_server, discover_support,
|
||||||
discover_support,
|
|
||||||
},
|
},
|
||||||
assign,
|
assign,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -157,7 +157,9 @@ impl CheckAuth for AccessToken {
|
|||||||
query: AuthQueryParams,
|
query: AuthQueryParams,
|
||||||
route: TypeId,
|
route: TypeId,
|
||||||
) -> Result<Self::Identity> {
|
) -> Result<Self::Identity> {
|
||||||
if let Some((sender_user, sender_device, status)) = services.users.find_from_token(&output).await {
|
if let Some((sender_user, sender_device, status)) =
|
||||||
|
services.users.find_from_token(&output).await
|
||||||
|
{
|
||||||
// If the token is expired we return a soft logout
|
// If the token is expired we return a soft logout
|
||||||
if matches!(status, AccessTokenStatus::Expired) {
|
if matches!(status, AccessTokenStatus::Expired) {
|
||||||
return Err(Error::Request(
|
return Err(Error::Request(
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use conduwuit::{Err, Error, Result, error, utils};
|
use conduwuit::{Err, Error, Result, error, utils};
|
||||||
use futures::future::OptionFuture;
|
|
||||||
use lettre::Address;
|
use lettre::Address;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
DeviceId, UserId,
|
DeviceId, UserId,
|
||||||
@@ -254,8 +253,7 @@ impl Service {
|
|||||||
|
|
||||||
let session_metadata = if let Some(initiator) = initiator {
|
let session_metadata = if let Some(initiator) = initiator {
|
||||||
let is_oauth = if let Some(device_id) = initiator.device_id {
|
let is_oauth = if let Some(device_id) = initiator.device_id {
|
||||||
self
|
self.services
|
||||||
.services
|
|
||||||
.oauth
|
.oauth
|
||||||
.get_session_info_for_device(initiator.user_id, device_id)
|
.get_session_info_for_device(initiator.user_id, device_id)
|
||||||
.await
|
.await
|
||||||
|
|||||||
Reference in New Issue
Block a user