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

Making invalid API Key raise 403 forbidden #262

Open
Enorio opened this issue Mar 6, 2024 · 0 comments
Open

Making invalid API Key raise 403 forbidden #262

Enorio opened this issue Mar 6, 2024 · 0 comments

Comments

@Enorio
Copy link

Enorio commented Mar 6, 2024

I have a project that uses jwt tokens as authentication. I'm now trying to add the api-key feature.
Basically I've customized the API based on an Organization permission (in a given organization, the api-key might have admin permissions, or staff, etc). In this step, I can filter the permissions with success

I have the following viewset:

class FooViewSet(viewsets.ModelViewSet):
    permission_classes = (FooPermissionsBasedOnOrganization | HasOrganizationBasedAPIKey, )

Assuming that both jwt token and api-key have staff permissions, I don't want them to be able to create resources, raising HTTP 403 forbidden.
But with api-keys, the error is 401 unauthorized...
With some debug, I've noticed the following:

  • Breakpoint in the APIView, method initial, line self.check_permissions(request)
    • In here, if the token or api-key don't have permission, will call the method self.permission_denied

The permission denied code is the following:

def permission_denied(self, request, message=None, code=None):
    if request.authenticators and not request.successful_authenticator:
        raise exceptions.NotAuthenticated()
    raise exceptions.PermissionDenied(detail=message, code=code)

API Keys will hit on the first raise, because it's not a User to be authenticated.

Should I need a Authentication class to use with API Keys, do I need something to bypass this, or is it something that I'm not seeing bacause this shouldn't be a problem?

Thanks 👍

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

No branches or pull requests

1 participant