Django REST with remote authentication jwt

I am going to create a Django REST project that should be remote users: an external server provides api authentication and uses JWT. It also provides me with a token carrier that allows me to use apis. My Django will have to open an authentication api for clients (mobile applications). I don't have a clear idea of ​​how to handle the authentication flow: do I need to create user authentication?

Thank!

+4
source share
1 answer

Perhaps I can help you implement JWT in your DRF project. I hope this gives you some clarity regarding the REST of your question.

In the project settings file, include jwt in INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_framework_swagger',
    'rest_framework_jwt',
    'django_filters',
    'corsheaders',
    'sslserver',
]

Then make this entry -

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'core.pagination.StandardPageNumberPagination',
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
}

And then

JWT_AUTH = {
    'JWT_SECRET_KEY': SECRET_KEY,
    'JWT_GET_USER_SECRET_KEY': None,
    'JWT_PUBLIC_KEY': None,
    'JWT_PRIVATE_KEY': None,
    'JWT_ALGORITHM': 'HS256',
    'JWT_VERIFY': True,
    'JWT_VERIFY_EXPIRATION': True,
    'JWT_LEEWAY': 0,
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300),
    'JWT_ALLOW_REFRESH': False,
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
}

Now, to understand what each of these settings does, see the documentation. And even then, if you need clarity on any of the points, I would be happy to explain.

0
source

Source: https://habr.com/ru/post/1671050/


All Articles