Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Refactor auth code#11379

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Turbo87 wants to merge1 commit intorust-lang:main
base:main
Choose a base branch
Loading
fromTurbo87:auth-refactor
Draft

Conversation

Turbo87
Copy link
Member

Up until this point our "auth" code was scattered all over the place and mixed up authentication (who are you?) and authorization (what are you allowed to do?) in multiple places:

  • Whether a user account was locked was checked as part of authentication, although it is more of an authorization concern.
  • Whether a user has a verified email address was checked in the individual endpoints, if at all.
  • Whether a user is an admin was also checked in the individual endpoints that were adjusted to support admin users.
  • Whether a user is an owner (or team member) of a crate was also checked in the individual endpoints, in various different ways.
  • Whether an API token has the right scopes was also checked as part of authentication, but it is an authorization concern.

This commit centralizes these checks with a three step approach:

  1. In the first step the endpoint uses one of theCredentials structs as an axum request extractor to extract the raw credentials from the request metadata (user ID from session cookie, API token, Trusted Publishing token).
  2. The credentials are used to look up a corresponding user (or Trusted Publishing token) from the database.
  3. The authenticated entity is used to check for authorization of the requested operation.

The second and third step are folded into one fn call (.validate(permission)) to make it harder to use an authenticated entity without knowing if they are authorized for the operation.

While the auth code and the endpoints have changed quite a bit due to this refactoring, the test code is mostly the same except for a few minor details caused by using the request extractors now.

Up until this point our "auth" code was scattered all over the place and mixed up authentication (who are you?) and authorization (what are you allowed to do?) in multiple places:- Whether a user account was locked was checked as part of authentication, although it is more of an authorization concern.- Whether a user has a verified email address was checked in the individual endpoints, if at all.- Whether a user is an admin was also checked in the individual endpoints that were adjusted to support admin users.- Whether a user is an owner (or team member) of a crate was also checked in the individual endpoints, in various different ways.- Whether an API token has the right scopes was also checked as part of authentication, but it is an authorization concern.This commit centralizes these checks with a three step approach:1. In the first step the endpoint uses one of the `Credentials` structs as an axum request extractor to extract the raw credentials from the request metadata (user ID from session cookie, API token, Trusted Publishing token).2. The credentials are used to look up a corresponding user (or Trusted Publishing token) from the database.3. The authenticated entity is used to check for authorization of the requested operation.The second and third step are folded into one fn call (`.validate(permission)`) to make it harder to use an authenticated entity without knowing if they are authorized for the operation.While the auth code and the endpoints have changed quite a bit due to this refactoring, the test code is mostly the same except for a few minor details caused by using the request extractors now.
@Turbo87Turbo87 added C-internal 🔧Category: Nonessential work that would make the codebase more consistent or clear A-backend ⚙️ labelsJun 18, 2025
@Turbo87
Copy link
MemberAuthor

@rust-lang/crates-io I've opened this as a draft since I'm mostly looking for feedback on this idea. Please take a look and let me know if this is actually an improvement or not. I'm a little worried that I've over-complicated it, but I'm not quite sure if it can be simplified in a significant way.

@Turbo87Turbo87 requested a review froma teamJuly 13, 2025 17:46
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
A-backend ⚙️C-internal 🔧Category: Nonessential work that would make the codebase more consistent or clear
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@Turbo87

[8]ページ先頭

©2009-2025 Movatter.jp