You've already forked AstralRinth
forked from didirus/AstralRinth
Commonize and distinguish a lot of struct names in labrinth::database::models (#3691)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use crate::auth::email::send_email;
|
||||
use crate::auth::validate::get_user_record_from_bearer_token;
|
||||
use crate::auth::{AuthProvider, AuthenticationError, get_user_from_headers};
|
||||
use crate::database::models::flow_item::Flow;
|
||||
use crate::database::models::flow_item::DBFlow;
|
||||
use crate::database::redis::RedisPool;
|
||||
use crate::file_hosting::FileHost;
|
||||
use crate::models::pats::Scopes;
|
||||
@@ -76,7 +76,7 @@ impl TempUser {
|
||||
redis: &RedisPool,
|
||||
) -> Result<crate::database::models::DBUserId, AuthenticationError> {
|
||||
if let Some(email) = &self.email {
|
||||
if crate::database::models::User::get_email(email, client)
|
||||
if crate::database::models::DBUser::get_email(email, client)
|
||||
.await?
|
||||
.is_some()
|
||||
{
|
||||
@@ -101,7 +101,7 @@ impl TempUser {
|
||||
}
|
||||
);
|
||||
|
||||
let new_id = crate::database::models::User::get(
|
||||
let new_id = crate::database::models::DBUser::get(
|
||||
&test_username,
|
||||
client,
|
||||
redis,
|
||||
@@ -156,7 +156,7 @@ impl TempUser {
|
||||
};
|
||||
|
||||
if let Some(username) = username {
|
||||
crate::database::models::User {
|
||||
crate::database::models::DBUser {
|
||||
id: user_id,
|
||||
github_id: if provider == AuthProvider::GitHub {
|
||||
Some(
|
||||
@@ -1083,7 +1083,7 @@ pub async fn init(
|
||||
None
|
||||
};
|
||||
|
||||
let state = Flow::OAuth {
|
||||
let state = DBFlow::OAuth {
|
||||
user_id,
|
||||
url: info.url,
|
||||
provider: info.provider,
|
||||
@@ -1112,16 +1112,16 @@ pub async fn auth_callback(
|
||||
|
||||
let state = state_string.clone();
|
||||
let res: Result<HttpResponse, AuthenticationError> = async move {
|
||||
let flow = Flow::get(&state, &redis).await?;
|
||||
let flow = DBFlow::get(&state, &redis).await?;
|
||||
|
||||
// Extract cookie header from request
|
||||
if let Some(Flow::OAuth {
|
||||
if let Some(DBFlow::OAuth {
|
||||
user_id,
|
||||
provider,
|
||||
url,
|
||||
}) = flow
|
||||
{
|
||||
Flow::remove(&state, &redis).await?;
|
||||
DBFlow::remove(&state, &redis).await?;
|
||||
|
||||
let token = provider.get_token(query).await?;
|
||||
let oauth_user = provider.get_user(&token).await?;
|
||||
@@ -1138,7 +1138,7 @@ pub async fn auth_callback(
|
||||
.update_user_id(id, Some(&oauth_user.id), &mut transaction)
|
||||
.await?;
|
||||
|
||||
let user = crate::database::models::User::get_id(id, &**client, &redis).await?;
|
||||
let user = crate::database::models::DBUser::get_id(id, &**client, &redis).await?;
|
||||
|
||||
if provider == AuthProvider::PayPal {
|
||||
sqlx::query!(
|
||||
@@ -1165,19 +1165,19 @@ pub async fn auth_callback(
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
crate::database::models::User::clear_caches(&[(id, None)], &redis).await?;
|
||||
crate::database::models::DBUser::clear_caches(&[(id, None)], &redis).await?;
|
||||
|
||||
Ok(HttpResponse::TemporaryRedirect()
|
||||
.append_header(("Location", &*url))
|
||||
.json(serde_json::json!({ "url": url })))
|
||||
} else {
|
||||
let user_id = if let Some(user_id) = user_id_opt {
|
||||
let user = crate::database::models::User::get_id(user_id, &**client, &redis)
|
||||
let user = crate::database::models::DBUser::get_id(user_id, &**client, &redis)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
if user.totp_secret.is_some() {
|
||||
let flow = Flow::Login2FA { user_id: user.id }
|
||||
let flow = DBFlow::Login2FA { user_id: user.id }
|
||||
.insert(Duration::minutes(30), &redis)
|
||||
.await?;
|
||||
|
||||
@@ -1279,7 +1279,7 @@ pub async fn delete_auth_provider(
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
crate::database::models::User::clear_caches(
|
||||
crate::database::models::DBUser::clear_caches(
|
||||
&[(user.id.into(), None)],
|
||||
&redis,
|
||||
)
|
||||
@@ -1346,7 +1346,7 @@ pub async fn create_account_with_password(
|
||||
return Err(ApiError::Turnstile);
|
||||
}
|
||||
|
||||
if crate::database::models::User::get(
|
||||
if crate::database::models::DBUser::get(
|
||||
&new_account.username,
|
||||
&**pool,
|
||||
&redis,
|
||||
@@ -1385,7 +1385,7 @@ pub async fn create_account_with_password(
|
||||
.hash_password(new_account.password.as_bytes(), &salt)?
|
||||
.to_string();
|
||||
|
||||
if crate::database::models::User::get_email(&new_account.email, &**pool)
|
||||
if crate::database::models::DBUser::get_email(&new_account.email, &**pool)
|
||||
.await?
|
||||
.is_some()
|
||||
{
|
||||
@@ -1394,7 +1394,7 @@ pub async fn create_account_with_password(
|
||||
));
|
||||
}
|
||||
|
||||
crate::database::models::User {
|
||||
crate::database::models::DBUser {
|
||||
id: user_id,
|
||||
github_id: None,
|
||||
discord_id: None,
|
||||
@@ -1426,7 +1426,7 @@ pub async fn create_account_with_password(
|
||||
let session = issue_session(req, user_id, &mut transaction, &redis).await?;
|
||||
let res = crate::models::sessions::Session::from(session, true, None);
|
||||
|
||||
let flow = Flow::ConfirmEmail {
|
||||
let flow = DBFlow::ConfirmEmail {
|
||||
user_id,
|
||||
confirm_email: new_account.email.clone(),
|
||||
}
|
||||
@@ -1467,17 +1467,19 @@ pub async fn login_password(
|
||||
}
|
||||
|
||||
let user = if let Some(user) =
|
||||
crate::database::models::User::get(&login.username, &**pool, &redis)
|
||||
crate::database::models::DBUser::get(&login.username, &**pool, &redis)
|
||||
.await?
|
||||
{
|
||||
user
|
||||
} else {
|
||||
let user =
|
||||
crate::database::models::User::get_email(&login.username, &**pool)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
let user = crate::database::models::DBUser::get_email(
|
||||
&login.username,
|
||||
&**pool,
|
||||
)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
crate::database::models::User::get_id(user, &**pool, &redis)
|
||||
crate::database::models::DBUser::get_id(user, &**pool, &redis)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?
|
||||
};
|
||||
@@ -1495,7 +1497,7 @@ pub async fn login_password(
|
||||
.map_err(|_| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
if user.totp_secret.is_some() {
|
||||
let flow = Flow::Login2FA { user_id: user.id }
|
||||
let flow = DBFlow::Login2FA { user_id: user.id }
|
||||
.insert(Duration::minutes(30), &redis)
|
||||
.await?;
|
||||
|
||||
@@ -1568,7 +1570,7 @@ async fn validate_2fa_code(
|
||||
Ok(true)
|
||||
} else if allow_backup {
|
||||
let backup_codes =
|
||||
crate::database::models::User::get_backup_codes(user_id, pool)
|
||||
crate::database::models::DBUser::get_backup_codes(user_id, pool)
|
||||
.await?;
|
||||
|
||||
if !backup_codes.contains(&input) {
|
||||
@@ -1587,7 +1589,7 @@ async fn validate_2fa_code(
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
|
||||
crate::database::models::User::clear_caches(
|
||||
crate::database::models::DBUser::clear_caches(
|
||||
&[(user_id, None)],
|
||||
redis,
|
||||
)
|
||||
@@ -1607,13 +1609,13 @@ pub async fn login_2fa(
|
||||
redis: Data<RedisPool>,
|
||||
login: web::Json<Login2FA>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let flow = Flow::get(&login.flow, &redis)
|
||||
let flow = DBFlow::get(&login.flow, &redis)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
if let Flow::Login2FA { user_id } = flow {
|
||||
if let DBFlow::Login2FA { user_id } = flow {
|
||||
let user =
|
||||
crate::database::models::User::get_id(user_id, &**pool, &redis)
|
||||
crate::database::models::DBUser::get_id(user_id, &**pool, &redis)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
@@ -1634,7 +1636,7 @@ pub async fn login_2fa(
|
||||
AuthenticationError::InvalidCredentials,
|
||||
));
|
||||
}
|
||||
Flow::remove(&login.flow, &redis).await?;
|
||||
DBFlow::remove(&login.flow, &redis).await?;
|
||||
|
||||
let session =
|
||||
issue_session(req, user_id, &mut transaction, &redis).await?;
|
||||
@@ -1670,7 +1672,7 @@ pub async fn begin_2fa_flow(
|
||||
let string = totp_rs::Secret::generate_secret();
|
||||
let encoded = string.to_encoded();
|
||||
|
||||
let flow = Flow::Initialize2FA {
|
||||
let flow = DBFlow::Initialize2FA {
|
||||
user_id: user.id.into(),
|
||||
secret: encoded.to_string(),
|
||||
}
|
||||
@@ -1696,11 +1698,11 @@ pub async fn finish_2fa_flow(
|
||||
login: web::Json<Login2FA>,
|
||||
session_queue: Data<AuthQueue>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let flow = Flow::get(&login.flow, &redis)
|
||||
let flow = DBFlow::get(&login.flow, &redis)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
if let Flow::Initialize2FA { user_id, secret } = flow {
|
||||
if let DBFlow::Initialize2FA { user_id, secret } = flow {
|
||||
let user = get_user_from_headers(
|
||||
&req,
|
||||
&**pool,
|
||||
@@ -1735,7 +1737,7 @@ pub async fn finish_2fa_flow(
|
||||
));
|
||||
}
|
||||
|
||||
Flow::remove(&login.flow, &redis).await?;
|
||||
DBFlow::remove(&login.flow, &redis).await?;
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
@@ -1794,7 +1796,7 @@ pub async fn finish_2fa_flow(
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
crate::database::models::User::clear_caches(
|
||||
crate::database::models::DBUser::clear_caches(
|
||||
&[(user.id.into(), None)],
|
||||
&redis,
|
||||
)
|
||||
@@ -1893,7 +1895,7 @@ pub async fn remove_2fa(
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
crate::database::models::User::clear_caches(&[(user.id, None)], &redis)
|
||||
crate::database::models::DBUser::clear_caches(&[(user.id, None)], &redis)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::NoContent().finish())
|
||||
@@ -1916,15 +1918,17 @@ pub async fn reset_password_begin(
|
||||
return Err(ApiError::Turnstile);
|
||||
}
|
||||
|
||||
let user = if let Some(user_id) = crate::database::models::User::get_email(
|
||||
&reset_password.username,
|
||||
&**pool,
|
||||
)
|
||||
.await?
|
||||
let user = if let Some(user_id) =
|
||||
crate::database::models::DBUser::get_email(
|
||||
&reset_password.username,
|
||||
&**pool,
|
||||
)
|
||||
.await?
|
||||
{
|
||||
crate::database::models::User::get_id(user_id, &**pool, &redis).await?
|
||||
crate::database::models::DBUser::get_id(user_id, &**pool, &redis)
|
||||
.await?
|
||||
} else {
|
||||
crate::database::models::User::get(
|
||||
crate::database::models::DBUser::get(
|
||||
&reset_password.username,
|
||||
&**pool,
|
||||
&redis,
|
||||
@@ -1933,7 +1937,7 @@ pub async fn reset_password_begin(
|
||||
};
|
||||
|
||||
if let Some(user) = user {
|
||||
let flow = Flow::ForgotPassword { user_id: user.id }
|
||||
let flow = DBFlow::ForgotPassword { user_id: user.id }
|
||||
.insert(Duration::hours(24), &redis)
|
||||
.await?;
|
||||
|
||||
@@ -1975,13 +1979,14 @@ pub async fn change_password(
|
||||
session_queue: Data<AuthQueue>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let user = if let Some(flow) = &change_password.flow {
|
||||
let flow = Flow::get(flow, &redis).await?;
|
||||
let flow = DBFlow::get(flow, &redis).await?;
|
||||
|
||||
if let Some(Flow::ForgotPassword { user_id }) = flow {
|
||||
let user =
|
||||
crate::database::models::User::get_id(user_id, &**pool, &redis)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
if let Some(DBFlow::ForgotPassword { user_id }) = flow {
|
||||
let user = crate::database::models::DBUser::get_id(
|
||||
user_id, &**pool, &redis,
|
||||
)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
Some(user)
|
||||
} else {
|
||||
@@ -2085,7 +2090,7 @@ pub async fn change_password(
|
||||
.await?;
|
||||
|
||||
if let Some(flow) = &change_password.flow {
|
||||
Flow::remove(flow, &redis).await?;
|
||||
DBFlow::remove(flow, &redis).await?;
|
||||
}
|
||||
|
||||
if let Some(email) = user.email {
|
||||
@@ -2105,7 +2110,7 @@ pub async fn change_password(
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
crate::database::models::User::clear_caches(&[(user.id, None)], &redis)
|
||||
crate::database::models::DBUser::clear_caches(&[(user.id, None)], &redis)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
@@ -2183,7 +2188,7 @@ pub async fn set_email(
|
||||
.await?;
|
||||
}
|
||||
|
||||
let flow = Flow::ConfirmEmail {
|
||||
let flow = DBFlow::ConfirmEmail {
|
||||
user_id: user.id.into(),
|
||||
confirm_email: email.email.clone(),
|
||||
}
|
||||
@@ -2197,7 +2202,7 @@ pub async fn set_email(
|
||||
)?;
|
||||
|
||||
transaction.commit().await?;
|
||||
crate::database::models::User::clear_caches(
|
||||
crate::database::models::DBUser::clear_caches(
|
||||
&[(user.id.into(), None)],
|
||||
&redis,
|
||||
)
|
||||
@@ -2230,7 +2235,7 @@ pub async fn resend_verify_email(
|
||||
));
|
||||
}
|
||||
|
||||
let flow = Flow::ConfirmEmail {
|
||||
let flow = DBFlow::ConfirmEmail {
|
||||
user_id: user.id.into(),
|
||||
confirm_email: email.clone(),
|
||||
}
|
||||
@@ -2262,15 +2267,15 @@ pub async fn verify_email(
|
||||
redis: Data<RedisPool>,
|
||||
email: web::Json<VerifyEmail>,
|
||||
) -> Result<HttpResponse, ApiError> {
|
||||
let flow = Flow::get(&email.flow, &redis).await?;
|
||||
let flow = DBFlow::get(&email.flow, &redis).await?;
|
||||
|
||||
if let Some(Flow::ConfirmEmail {
|
||||
if let Some(DBFlow::ConfirmEmail {
|
||||
user_id,
|
||||
confirm_email,
|
||||
}) = flow
|
||||
{
|
||||
let user =
|
||||
crate::database::models::User::get_id(user_id, &**pool, &redis)
|
||||
crate::database::models::DBUser::get_id(user_id, &**pool, &redis)
|
||||
.await?
|
||||
.ok_or_else(|| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
@@ -2294,10 +2299,13 @@ pub async fn verify_email(
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
Flow::remove(&email.flow, &redis).await?;
|
||||
DBFlow::remove(&email.flow, &redis).await?;
|
||||
transaction.commit().await?;
|
||||
crate::database::models::User::clear_caches(&[(user.id, None)], &redis)
|
||||
.await?;
|
||||
crate::database::models::DBUser::clear_caches(
|
||||
&[(user.id, None)],
|
||||
&redis,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::NoContent().finish())
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user