drf_openapi icon indicating copy to clipboard operation
drf_openapi copied to clipboard

Oauth2 token authentication not recognized

Open lggwettmann opened this issue 7 years ago • 2 comments

  • DRF OpenAPI version: newest
  • Python version: 3.6

I try to get a schema documentation for my django-oauth2-toolkit oauth2 authenticated app. DRF Open Api doesn't recognize the oauth2 token authentication but recognizes it as the Basic Django authentication. I need a proper schema.json file

These are my DRF settings:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
        # 'rest_framework.authentication.SessionAuthentication',
    ],
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
    'DEFAULT_VERSION': '0.1',
    'ALLOWED_VERSIONS': ['0.1'],
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 100,
}

What did I do wrong or does DRF Open API just not recognize oauth2? How could I solve this?

lggwettmann avatar Jan 08 '18 20:01 lggwettmann

I have the same issue with token based authentication.

lino avatar Jan 17 '18 09:01 lino

As far as I can tell, this is just not well supported in the django-rest-swagger library on which this library builds. They simply have a setting for it, that you can also use when using drf-openapi.

SWAGGER_SETTINGS = {
    'SECURITY_DEFINITIONS': {
        'oauth2': {
            'flow': 'application',
            'tokenUrl': '/oauth2/token/',
            'scopes': {
                'write': 'Write description.',
                'read': 'Read description'
            }
        }
    },
}

This will be picked up fine by the schema in JSON format but the Redoc UI (renderer) doesn't do much with it (and leaves authentication blank).

See: https://django-rest-swagger.readthedocs.io/en/latest/settings/

ReDoc in turn also allows some fiddling with it to get it in.

See: https://github.com/Rebilly/ReDoc/blob/master/docs/security-definitions-injection.md

joeribekker avatar Jan 25 '18 11:01 joeribekker