Skip to content

Api token updates - #5664

Merged
SchrodingersGat merged 28 commits into
inventree:masterfrom
SchrodingersGat:api-token-updates
Oct 20, 2023
Merged

SchrodingersGat merged 28 commits into
inventree:masterfrom
SchrodingersGat:api-token-updates

Conversation

@SchrodingersGat

@SchrodingersGat SchrodingersGat commented Oct 4, 2023 •

Copy link
Copy Markdown
Member

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

netlify Bot commented Oct 4, 2023 •

Copy link
Copy Markdown

✅ 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

@matmair matmair left a comment

Copy link
Copy Markdown
Member

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.

@matmair matmair added enhancement This is an suggested enhancement or new feature security Relates to a security issue labels Oct 4, 2023
@wolflu05

wolflu05 commented Oct 4, 2023

Copy link
Copy Markdown
Member

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

matmair commented Oct 4, 2023

Copy link
Copy Markdown
Member

@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
Comment thread InvenTree/users/api.py
name = request.query_params.get('name', '')

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

Copy link
Copy Markdown
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
Copy Markdown
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 thread InvenTree/users/admin.py Outdated
Comment on lines +20 to +22
list_display = ('token', 'user', 'name', 'expiry', 'active')
fields = ('token', 'name', 'revoked', 'expiry')
readonly_fields = ('token', 'created')

Copy link
Copy Markdown
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
Copy Markdown
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
@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