一种灵活的用于firebase认证的django-rest框架认证后端
drf-firebase-authentication的Python项目详细描述
django rest framework firebase身份验证
这个包为django rest框架提供了一个基本的firebase身份验证后端类。两个关键方法没有实现更大的灵活性。让我们keep it simple, stupid。
要求
- python 2.7或3.4+
- Django(版本1.11+)
- Django Rest Framework
- Firebase Admin Python
安装
pip install drf-firebase-authentication
用法
1.子类BaseFirebaseAuthentication
并实现其模板方法:
通过相应地实现.get_django_user()
方法,自己决定如何将firebase用户与本地django用户相关联。
把你的代码放入一个authentication.py
文件中,这个文件位于一个专门用于你的项目rest api的应用程序中。
fromdrf_firebase.authenticationimportBaseFirebaseAuthenticationfromfirebase_adminimportcredentials,initialize_appfromdjango.contrib.authimportget_user_modelfirebase_creds=credentials.Certificate('path/to/firebase/credentials.json')firebase_app=initialize_app(firebase_creds)classFirebaseAuthentication(BaseFirebaseAuthentication):""" Example implementation of a DRF Firebase Authentication backend class """defget_firebase_app(self):returnfirebase_appdefget_django_user(self,firebase_user_record):returnget_user_model().objects.get_or_create(username=firebase_user_record.uid,)[0]
2.将刚刚创建的firebase身份验证后端添加到您的settings.py
:
将YOUR_RESTAPI_APP
替换为放入authentication.py
文件的应用程序。
REST_FRAMEWORK={'DEFAULT_AUTHENTICATION_CLASSES':('rest_framework.authentication.SessionAuthentication',# default'rest_framework.authentication.BasicAuthentication',# default'YOUR_RESTAPI_APP.authentication.FirebaseAuthentication',),}