Skip to content

Commit

Permalink
Merge branch 'master' into coc
Browse files Browse the repository at this point in the history
  • Loading branch information
m4tx authored Jan 31, 2025
2 parents a6dcf74 + cdd5da9 commit 2faf3bd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
coverage:
status:
project:
default:
threshold: 0.5%
github_checks:
annotations: false
12 changes: 12 additions & 0 deletions cot/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ pub enum AuthError {
/// An error occurred while accessing the user object.
#[error("Error while accessing the user object")]
UserBackend(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
/// The credentials type provided to [`AuthBackend::authenticate`] is not
/// supported.
#[error("Tried to authenticate with an unsupported credentials type")]
CredentialsTypeNotSupported,
/// The [`UserId`] type provided to [`AuthBackend::get_user_by_id`] is not
/// supported.
#[error("Tried to get a user by an unsupported user ID type")]
UserIdTypeNotSupported,
}

impl AuthError {
Expand Down Expand Up @@ -958,6 +966,8 @@ pub trait AuthBackend: Send + Sync {
/// # Errors
///
/// Returns an error if the user object cannot be fetched.
///
/// Returns an error if the credentials type is not supported.
async fn authenticate(
&self,
request: &Request,
Expand All @@ -973,6 +983,8 @@ pub trait AuthBackend: Send + Sync {
///
/// Returns an error if the user object cannot be fetched.
///
/// Returns an error if the user ID type is not supported.
///
/// # Examples
///
/// ```
Expand Down
16 changes: 8 additions & 8 deletions cot/src/auth/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl DatabaseUser {
/// )
/// .await?;
///
/// let user_from_db = DatabaseUser::get_by_id(request.db(), UserId::Int(user.id())).await?;
/// let user_from_db = DatabaseUser::get_by_id(request.db(), user.id()).await?;
///
/// Ok(Response::new_html(
/// StatusCode::OK,
Expand All @@ -167,9 +167,7 @@ impl DatabaseUser {
/// # Ok(())
/// # }
/// ```
pub async fn get_by_id<DB: DatabaseBackend>(db: &DB, id: UserId) -> Result<Option<Self>> {
let id = id.as_int().expect("User ID should be an integer");

pub async fn get_by_id<DB: DatabaseBackend>(db: &DB, id: i64) -> Result<Option<Self>> {
let db_user = query!(DatabaseUser, $id == id)
.get(db)
.await
Expand Down Expand Up @@ -503,7 +501,7 @@ impl AuthBackend for DatabaseUserBackend {
.await
.map(|user| user.map(|user| Box::new(user) as Box<dyn User + Send + Sync>))?)
} else {
Ok(None)
Err(AuthError::CredentialsTypeNotSupported)
}
}

Expand All @@ -512,6 +510,10 @@ impl AuthBackend for DatabaseUserBackend {
request: &Request,
id: UserId,
) -> Result<Option<Box<dyn User + Send + Sync>>> {
let UserId::Int(id) = id else {
return Err(AuthError::UserIdTypeNotSupported);
};

#[allow(trivial_casts)] // Upcast to the correct Box type
Ok(DatabaseUser::get_by_id(request.db(), id)
.await?
Expand Down Expand Up @@ -648,9 +650,7 @@ mod tests {
.expect_get::<DatabaseUser>()
.returning(move |_| Ok(Some(user.clone())));

let result = DatabaseUser::get_by_id(&mock_db, UserId::Int(1))
.await
.unwrap();
let result = DatabaseUser::get_by_id(&mock_db, 1).await.unwrap();
assert!(result.is_some());
assert_eq!(result.unwrap().username(), "testuser");
}
Expand Down

0 comments on commit 2faf3bd

Please sign in to comment.