Package: cklauth
ckl-rest-authAn opinionated Django app to provide user authentication.Installationpip install cklauthAdd to your project'sINSTALLED_APPS:rest_frameworkrest_framework.authtokencorsheaderscklauthIncludeckl-rest-authurls to your project:
Onurls.pyaddpath('', include('cklauth.urls'))Add settings according to the project requirementsFor Django's default user config:# Field used for authencation together with password (required)'LOGIN_FIELD':'email',# Fields used on user serializer'REGISTER_FIELDS':('username','email'),# From email used on password reset emails (optional)'FROM_EMAIL':'default@email.com',# Google authentication settings (optional)'GOOGLE':{'CLIENT_ID':'insert-your-key','CLIENT_SECRET':'insert-your-key','REDIRECT_URI':'insert-your-uri',},# Facebook authentication settings (optional)'FACEBOOK':{'CLIENT_ID':'insert-your-key','CLIENT_SECRET':'insert-your-key','REDIRECT_URI':'insert-your-uri',},Note that the defaultLOGIN_FIELDisemailand then you need to use the helper
authentication backend:AUTHENTICATION_BACKENDS=['cklauth.auth.EmailOrUsernameModelBackend']For a custom user model, you can define additional options:CKL_REST_AUTH={# Field used for authencation together with password (required)'LOGIN_FIELD':'email',# Override the default serializer used on registration and authentication responses (optional)'USER_SERIALIZER':'cklauth.api.v1.serializers.UserSerializer',# Fields used on user serializer (not used if USER_SERIALIZER is defined above)'REGISTER_FIELDS':('username','email'),# From email used on password reset emails (optional)'FROM_EMAIL':'default@email.com',# Google authentication settings (optional)'GOOGLE':{'CLIENT_ID':'insert-your-key','CLIENT_SECRET':'insert-your-key','REDIRECT_URI':'insert-your-uri',# Define a callable that receives the social user payload and returns the value on of the# User model USERNAME_FIELD (username, for instance). The default function already checks# if the value is in use. Set it to `None`, if you don't want to generate a USERNAME_FIELD.'AUTH_FIELD_GENERATOR':'cklauth.utils.auth_field_generator',# How to map the social user payload to the User model fields. It accepts a callable that# receives the whole social user payload to map more complex data.'USER_INFO_MAPPING':{'full_name':'full_name':lambdainfo:'{}{}'.format(info.get('given_name'),info.get('family_name')),'email':'email',},},# Facebook authentication settings (optional)'FACEBOOK':{'CLIENT_ID':'insert-your-key','CLIENT_SECRET':'insert-your-key','REDIRECT_URI':'insert-your-uri','AUTH_FIELD_GENERATOR':'cklauth.utils.auth_field_generator','USER_INFO_MAPPING':{'full_name':'full_name':lambdainfo:'{}{}'.format(info.get('first_name'),info.get('last_name')),'email':'email',},},}Basic EndpointsPOST /api/v1/loginBody (depends on LOGIN_FIELD){"email":"example@example.com","password":"secret"}Response (depends on REGISTER_FIELDS and USER_SERIALIZER) - 200 OK{"token":"supersecret","user":{"id":1,"email":"example@example.com","first_name":"Example","last_name":"Example"}}Note:the user payload may vary according to specified REGISTER_FIELDS and USER_SERIALIZER.POST /api/v1/registerBody (depends on REGISTER_FIELDS and USER_SERIALIZER -- always has a password){"email":"example@example.com","password":"secret","first_name":"Example","last_name":"Example"}Response (depends on REGISTER_FIELDS and USER_SERIALIZER) - 201 CREATED{"token":"supersecret","user":{"id":1,"email":"example@example.com","first_name":"Example","last_name":"Example"}}Note:the user payload may vary according to specified REGISTER_FIELDS and USER_SERIALIZER.POST /api/v1/password-reset/Body{"email":"example@example.com"}Response - 200 OK{"email":"example@example.com"}Note: it always returns success, even if the provided email is not registered.Social EndpointsGET /api/v1/social/googleGET /api/v1/social/facebookNote:this should not be XHR request, the user will be redirected to consent screen. After
consent, the user is redirected to platform REDIRECT_URI added on settings, where a code is
extracted from the URL hash.POST /api/v1/social/googlePOST /api/v1/social/facebookBody{"code":"<code from previous step>","user_extra_fields":{"role":"admin"}}Note:You can pass additional user fields in theuser_extra_fieldskey, as long as they are
part of the mainREGISTER_FIELDSlist.Response - 200 OK{"token":"supersecret","user":{"id":1,"email":"example@example.com","first_name":"Example","last_name":"Example"}}Note:the user payload may vary according to specified REGISTER_FIELDS and USER_SERIALIZER.ContributingThe library code is undercklauthfolder and tests are in a test project undertestappfolder.Running tests:Ensure that you have the app requirements installedpip install -r requirements.txt
pip install -e cklauthRun the testspython -m pytest test_default_user
python -m pytest test_custom_user