rave/app/src/utils.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

use entities::{prelude::User, user};
use poem_ext::db::DbTxn;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use serde::Serializer;
use tracing::error;
use crate::{
authentication::Authentication,
subsonic::{Error, SubsonicResponse},
};
pub async fn verify_user(
conn: DbTxn,
auth: Authentication,
) -> Result<user::Model, SubsonicResponse> {
let user = User::find()
.filter(user::Column::Name.eq(&auth.username))
.one(&*conn)
.await;
match user {
Ok(Some(u)) => {
let ours = md5::compute(format!("{}{}", u.password, auth.salt));
let ours = format!("{ours:x}");
if ours == auth.token {
Ok(u)
} else {
Err(SubsonicResponse::new_error(Error::WrongUsernameOrPassword(
None,
)))
}
}
Ok(None) => Err(SubsonicResponse::new_error(Error::WrongUsernameOrPassword(
None,
))),
Err(e) => {
error!("Error getting user: {e}");
Err(SubsonicResponse::new_error(Error::WrongUsernameOrPassword(
None,
)))
}
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn album_id<S: Serializer>(id: &i64, s: S) -> Result<S::Ok, S::Error> {
let str = format!("al-{id}");
s.serialize_str(&str)
}