Skip to content

[bug] Users cannot create their own API tokens #11486

Closed

Description

@SchrodingersGat

Please verify that this bug has NOT been raised before.

  • I checked and didn't find a similar issue

Describe the bug*

Any non-admin user does not have the required API role permissions to generate their own API tokens. This prevents users from generating API tokens unless they have "admin" (role permissions).

Image

Steps to Reproduce

  1. Login using non-admin account (e.g. user: reader, pass: readonly)
  2. Navigate to "User Settings" / "Security"
  3. Select "Access Tokens" -> "Add Token"
  4. Get "Permission Denied" error

Expected behaviour

Users should be able to control their own API tokens

Relevant Code

from ./src/backend/InvenTree/InvenTree/metadata.py:InvenTreeMetadata:determine_metadata (lines 133-140):

            # Remove any HTTP methods that the user does not have permission for
            for method, permission in rolemap.items():
                result = check_user_permission(user, self.model, permission) or (
                    role_required and check_user_role(user, role_required, permission)
                )

                if method in actions and not result:
                    del actions[method]

Note here that the users_apitoken model requires the RuleSetEnum.ADMIN role permission:

def get_ruleset_models() -> dict:
    """Return a dictionary of models associated with each ruleset.

    This function maps particular database models to each ruleset.
    """
    ruleset_models = {
        RuleSetEnum.ADMIN: [
            'auth_group',
            'auth_user',
            'auth_permission',
            'users_apitoken',
            ...

Which only certain groups of users should have.

Activity

  1. added
    bug
    question
    triage:not-checked
    and removed
    question
    triage:not-checked
    on Mar 11, 2026
  2. added this to the 1.3.0 milestone on Mar 11, 2026
  3. added
    api
    on Mar 11, 2026
  4. SchrodingersGat commented on Mar 11, 2026

    @SchrodingersGat
    MemberAuthor

    @matmair any thoughts on how we best should handle this?

  5. matmair commented on Mar 11, 2026

    @matmair
    Member

    I have to look into it but I think we have/had a permission overwrite that allowed the user to create a token for themself

  6. removed
    api
    on Mar 11, 2026
  7. self-assigned this
    on Mar 11, 2026
  8. added
    User Interface
    on Mar 11, 2026
  9. matmair commented on Mar 11, 2026

    @matmair
    Member

    this looks like the wrong endpoint is called

  10. SchrodingersGat commented on Mar 11, 2026

    @SchrodingersGat
    MemberAuthor

    Which endpoint should we be calling instead?

  11. added a commit that references this issue on Mar 11, 2026
    872743d
  12. matmair commented on Mar 11, 2026

    @matmair
    Member

    /user/token/ but there is also an issue in that; PR will follow

  13. modified the milestones: 1.3.0, 1.2.4 on Mar 11, 2026
  14. added a commit that references this issue on Mar 14, 2026
    4d59329
  15. added a commit that references this issue on Mar 14, 2026
    fda3204
  16. added a commit that references this issue on Mar 14, 2026
    e749ece
  17. sur5r commented on Nov 4, 2026

    @sur5r
    Contributor

    It seems this reappeared with 1.7.0. At least the behavior is the same. Admin+Superuser can create tokens (The OPTIONS request returns GET in actions) while a normal user can't (actions is just an empty dictionary).

    Admin Superuser:

    {
      "name": "Get Auth Token",
      "description": "Return authentication token for an authenticated user.",
      "renders": [
        "application/json"
      ],
      "parses": [
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data"
      ],
      "actions": {
        "GET": {
          "token": {
            "type": "string",
            "required": false,
            "read_only": true,
            "label": "Token",
            "allow_blank": false,
            "allow_null": false
          },
          "name": {
            "type": "string",
            "required": true,
            "read_only": false,
            "label": "Token Name",
            "allow_blank": false,
            "allow_null": false,
            "help_text": "Custom token name",
            "max_length": 100
          },
          "expiry": {
            "type": "date",
            "required": false,
            "read_only": true,
            "label": "Expiry Date",
            "allow_null": false,
            "default": "2027-11-04",
            "help_text": "Token expiry date"
          },
          "warning": {
            "type": "string",
            "required": false,
            "read_only": true,
            "label": "Warning",
            "allow_blank": false,
            "allow_null": false
          }
        }
      },
      "model": "apitoken"
    }

    User:

    {
      "name": "Get Auth Token",
      "description": "Return authentication token for an authenticated user.",
      "renders": [
        "application/json"
      ],
      "parses": [
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data"
      ],
      "actions": {},
      "model": "apitoken"
    }
  18. matmair commented on Nov 4, 2026

    @matmair
    Member

    Against which endpoint? We had to do a lot of mfa security hardening; only the me/token/ subpath will issue tokens to non-staff users anymore

  19. sur5r commented on Nov 4, 2026

    @sur5r
    Contributor

    That was PUI against /api/user/me/token.

  20. SchrodingersGat commented on Nov 5, 2026

    @SchrodingersGat
    MemberAuthor

    I have just been able to generate a new token for a non-staff, non-superuser account. We also have a regression test for this workflow in playwright.

    @sur5r please run some more tests, and if you are certain there is an ongoing problem here, open a new issue.

  21. sur5r commented on Nov 5, 2026

    @sur5r
    Contributor

    That's good to know, thank you! I'll do further tests in a fresh instance to rule out deployment issues. Can you point me at the piece of code that handles the OPTIONS request? I'm not familiar enough with DRF yet to understand this.

  22. HannesMorre commented on Nov 16, 2026

    @HannesMorre

    We ran into the same issue on 1.7.0. I am admin and could create an api token from the UI. my coworker who is no admin gets the same permission denied error.

  23. sur5r commented on Nov 16, 2026

    @sur5r
    Contributor

    Workaround using manage.py shell:

    from django.contrib.auth.models import Permission,User
    u=User.objects.get(username='sur5r')
    p=Permission.objects.get(content_type__model="apitoken",codename="view_apitoken")
    u.user_permissions.add(p)
    u.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

User Interfacebug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions