Auth module for Django/DRF projects. Part of GarpixCMS.
Used packages:
- django rest framework
- social-auth-app-django
- django-rest-framework-social-oauth2
- etc; see setup.py
Install with pip:
pip install garpix_auth
Add the garpix_auth
to your INSTALLED_APPS
:
# settings.py
INSTALLED_APPS = [
# ...
'garpix_auth',
]
Add to urls.py
:
from garpix_auth.views import LogoutView, LoginView
urlpatterns = [
# ...
path('logout/', LogoutView.as_view(url='/'), name="logout"),
path('login/', LoginView.as_view(), name="authorize"),
# ...
]
For custom auth with phone and email use this in settings.py
:
AUTHENTICATION_BACKENDS = (
# Django
'garpix_auth.models.CustomAuthenticationBackend',
'django.contrib.auth.backends.ModelBackend',
)
Add this for SPA:
INSTALLED_APPS += [
# ...
'rest_framework',
'rest_framework.authtoken',
'oauth2_provider',
'social_django',
'rest_framework_social_oauth2',
# ...
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'garpix_auth.rest.authentication.MainAuthentication',
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
'rest_framework_social_oauth2.authentication.SocialAuthentication',
),
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}
AUTHENTICATION_BACKENDS = (
# Only your social networks
'social_core.backends.google.GoogleOAuth2',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.vk.VKOAuth2',
'social_core.backends.facebook.FacebookAppOAuth2',
'social_core.backends.facebook.FacebookOAuth2',
# Django
'rest_framework_social_oauth2.backends.DjangoOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.social_auth.associate_by_email',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details'
)
Add to urls.py
:
from django.urls import path, include
urlpatterns = [
# ...
path('api/auth/', include(('garpix_auth.urls', 'garpix_auth'), namespace='garpix_auth')),
# ...
]
You can override the Bearer authorization header by GARPIX_REST_AUTH_HEADER_KEY
setting.
And also allow this custom header for cors-headers:
# settings.py
# ...
from corsheaders.defaults import default_headers
GARPIX_REST_AUTH_HEADER_KEY = 'HTTP_BEARER_AUTHORIZATION'
CORS_ALLOW_HEADERS = list(default_headers) + [
"Bearer-Authorization",
]
Now you need to add Bearer-Authorization
header instead of Authorization
header with your Bearer token to all requests.
See garpix_auth/tests/test_api.py
for examples.
See CHANGELOG.md.
See CONTRIBUTING.md.
Developed by Garpix / https://garpix.com