Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User authentication should be done in middleware #369

Conversation

mario-nt
Copy link
Contributor

@mario-nt mario-nt commented Nov 3, 2023

Resolves #39.

@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (9c4b5f0) 41.35% compared to head (acb8610) 41.42%.

Files Patch % Lines
src/web/api/v1/extractors/user_id.rs 90.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #369      +/-   ##
===========================================
+ Coverage    41.35%   41.42%   +0.07%     
===========================================
  Files           80       81       +1     
  Lines         4996     5002       +6     
===========================================
+ Hits          2066     2072       +6     
  Misses        2930     2930              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 3d94826 to 2c74ebe Compare November 6, 2023 21:07
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 2c74ebe to 8d19a66 Compare November 6, 2023 21:24
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 8d19a66 to 84dd507 Compare November 6, 2023 22:30
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 84dd507 to 349b438 Compare November 6, 2023 22:39
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from ba0c25d to 4696653 Compare November 9, 2023 21:48
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 4696653 to 52be4b0 Compare November 28, 2023 18:45
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 52be4b0 to c384e58 Compare November 30, 2023 01:22
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from c384e58 to 684a922 Compare November 30, 2023 01:23
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 684a922 to 6d772dd Compare December 1, 2023 21:17
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 6d772dd to c5b956e Compare December 19, 2023 18:14
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from c5b956e to a6ad8cc Compare December 26, 2023 23:02
@cgbosse cgbosse added this to the v3.0.0-beta milestone Jan 12, 2024
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from a6ad8cc to 30b646a Compare January 24, 2024 12:30
@mario-nt mario-nt force-pushed the 39-user-authentication-should-be-done-in-middleware branch from 7710f19 to acb8610 Compare January 24, 2024 12:51
@mario-nt
Copy link
Contributor Author

@josecelano Could you please review the final refactor of the extractor and the upload torrent handler before I continue with the remaining handlers?

Copy link
Member

@josecelano josecelano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mario-nt it looks good to me. I've just added some comments regarding the other extractor when the logged-in user is optional.

See #39 (comment)

@@ -37,20 +38,19 @@ use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;
#[allow(clippy::unused_async)]
pub async fn upload_torrent_handler(
State(app_data): State<Arc<AppData>>,
Extract(maybe_bearer_token): Extract,
ExtractLoggedInUser(user_id): ExtractLoggedInUser,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mario-nt I think this is non-optional Acum extractor that returns an optional value. That's not the same as an optional extractor. See #39 (comment)

Comment on lines 49 to 53
match app_data
.torrent_service
.add_torrent(add_torrent_form, user_id.unwrap().value())
.await
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mario-nt you can't just unwrap because that would make the API panic. We need to return the unauthorized response. This endpoint requires a logged-in user. The whole point of using the extractor is to remove the logic from the handler. The extractor should return the unauthorized response if there is no logged-in user. So it user_id should be an UserId not an Option<UserId>.

This extractor would be used when you need a logged-in user. The Optional Extractor could be used in an endpoint that optionally can allow a logged-in user.

You can create two different extractors. See #39 (comment).

@mario-nt mario-nt closed this Jan 29, 2024
@mario-nt mario-nt reopened this Jan 29, 2024
@mario-nt mario-nt marked this pull request as ready for review January 29, 2024 15:30
@mario-nt mario-nt marked this pull request as draft January 29, 2024 15:31
@mario-nt
Copy link
Contributor Author

Parent issue #39 has been split into three new issues with their own PRs. Closing this PR.

@mario-nt mario-nt closed this Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

User authentication should be done in middleware
4 participants