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

Api token updates #5664

Merged
merged 28 commits into from
Oct 20, 2023
Merged

Conversation

SchrodingersGat
Copy link
Member

@SchrodingersGat SchrodingersGat commented Oct 4, 2023

Fixes #2326

This PR implements a custom extension of the current API token implementation:

  • Adds custom expiry to the token
  • Adds a "name" to each token

TODO

  • Add new model
  • Migrate existing tokens
  • Admin integration
  • Update auth to use new model (and check if the token is currently active)
  • Implement / extend method for creating tokens "on the fly" via the API (or do we remove this entirely)
  • Look into integration with the mobile app
  • Look into integration with the python API
  • Unit tests

A key point here is to preserve backwards compatibility with any existing external systems (such as the mobile app) which use tokens and expect them to appear in a certain way on the API.

References

@netlify
Copy link

netlify bot commented Oct 4, 2023

Deploy Preview for inventree-web-pui-preview canceled.

Name Link
🔨 Latest commit 73682c3
🔍 Latest deploy log https://app.netlify.com/sites/inventree-web-pui-preview/deploys/6531e86d58113e0008f6ed5a

@SchrodingersGat SchrodingersGat added the api Relates to the API label Oct 4, 2023
@SchrodingersGat SchrodingersGat added this to the 0.13.0 milestone Oct 4, 2023
Copy link
Member

@matmair matmair left a comment

Choose a reason for hiding this comment

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

I think this would be a good point to address some things that are not optimal about the current design of tokens and change things to be more like best-practice examples like GitHub and stripe.

  • No token without expiry
  • Configurable max expiry (industry standard seems to be 1 year nowadays)
  • Token events (creation, deletion, expert imminent)
  • Use an identifiable token format (see https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats/#identifiable-prefixes) - gives us the option to support multiple token formats in the future
  • Add metadata support to the model and issuing API (ie. the app could start sending the model and name of the phone when requesting a token to make identifying easier)

InvenTree/users/migrations/0008_apitoken.py Outdated Show resolved Hide resolved
@matmair matmair added enhancement This is an suggested enhancement or new feature security Relates to a security issue labels Oct 4, 2023
@wolflu05
Copy link
Contributor

wolflu05 commented Oct 4, 2023

It would be also good to add some scoping to the tokens. I imagine this would be easy if we use the existing permission sets as scopes.

@matmair
Copy link
Member

matmair commented Oct 4, 2023

@wolflu05 it is not really easy as the tokens currently are basically impersonating their user so we would probably need separate users or change all permission checks (which are user based currently)

- Correctly handles revoked tokens
- Correctly handles expired tokens
- Check for token active status
name = request.query_params.get('name', '')

# Delete any matching tokens
ApiToken.objects.filter(user=user, name=name).delete()
Copy link
Member

Choose a reason for hiding this comment

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

Comment: I think it would be prudent to log it a token was actually deleted for development (maybe log the identifier)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call I'll add that.

Comment on lines 20 to 22
list_display = ('token', 'user', 'name', 'expiry', 'active')
fields = ('token', 'name', 'revoked', 'expiry')
readonly_fields = ('token', 'created')
Copy link
Member

Choose a reason for hiding this comment

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

Question: Why is that changed? The key is normally used for identification in systems like this

Copy link
Member Author

Choose a reason for hiding this comment

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

The "token" is the redacted version, once it is created the raw value is not shown in the admin interface - I believe this is what you requested?

- Allows raw token to be viewed in the admin interface when created
- After created, no longer visible
- Also provides ability to generate token with static prefix
@SchrodingersGat SchrodingersGat merged commit 23ea746 into inventree:master Oct 20, 2023
24 checks passed
@SchrodingersGat SchrodingersGat deleted the api-token-updates branch October 20, 2023 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Relates to the API enhancement This is an suggested enhancement or new feature security Relates to a security issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FR] Multiple tokens and scopes for API access per user
3 participants