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

Combine User and UserTokenContext for user-backed identities in requests #15757

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

facutuesca
Copy link
Contributor

@facutuesca facutuesca commented Apr 11, 2024

Fixes #15748.

We have three types that a request.identity can be:

  • User: when the identity is a user backed by a login session
  • UserTokenContext: when the identity is a user backed by an API token (i.e. macaroon)
  • PublisherTokenContext: when the identity is an OIDCPublisher backed by an API token

Of these, User and UserTokenContext are confusable and prone to error.

This PR collapses them into a single UserContext type.

cc @woodruffw @di

@facutuesca facutuesca requested a review from a team as a code owner April 11, 2024 17:23
@@ -104,7 +104,7 @@ class RequestUser(Caveat):
user_id: StrictStr

def verify(self, request: Request, context: Any, permission: str) -> Result:
if not isinstance(request.identity, UserTokenContext):
if not isinstance(request.identity, UserContext):
return Failure("token with user restriction without a user")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should there be a new extra check here?:

        if request.identity.macaroon is None:
            return Failure("token with user restriction without a macaroon")

or will that never happen?

Copy link
Member

Choose a reason for hiding this comment

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

I think this can't happen, since the only place we end up checking the caveats is in MacaroonSecurityPolicy.permits, where request.identity will invariably either be an PublisherTokenContext or a UserContext that always has its macaroon set.

That being said, I see no harm in it as a sanity check either 🙂.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added!

@facutuesca facutuesca changed the title Combine User and UserTokenContext for user-backed identities Combine User and UserTokenContext for user-backed identities Apr 11, 2024
@facutuesca facutuesca changed the title Combine User and UserTokenContext for user-backed identities Combine User and UserTokenContext for user-backed identities in requests Apr 11, 2024
@woodruffw woodruffw self-requested a review April 11, 2024 20:46
@facutuesca facutuesca force-pushed the combine-request-identity-user branch 2 times, most recently from 45cf7cb to 4cc055f Compare April 14, 2024 17:08
We have three types that a request.identity can be:

- `User`: when the identity is a user backed by a login session
- `UserTokenContext`: when the identity is a user backed by an API token (i.e. macaroon)
- `PublisherTokenContext`: when the identity is an OIDCPublisher backed by an API token

Of these, User and UserTokenContext are confusable and prone to error. This change
collapses them into a single UserContext type.
@facutuesca facutuesca force-pushed the combine-request-identity-user branch from 4cc055f to d3bf7f2 Compare April 18, 2024 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace Userrequest.identity usages with UserContext
2 participants