Django-Rest框架的Firebase令牌认证
drf-firebase-token-auth的Python项目详细描述
灵感来自garyburgmann/drf-firebase-auth 基于Rest Framework’s TokenAuthentication, drf-firebase-token-auth应该正是启用客户端所需的 使用Firebase Authentication进行身份验证。在
它是如何工作的
- 对于每个REST请求,将从 授权标头。在
- 根据Firebase验证ID令牌。在
- 如果已经知道Firebase用户(具有相应UID的记录 存在于FirebaseUser表中),则对应的本地用户为 已成功验证。在
- 否则,将尝试匹配不熟悉的Firebase用户 本地用户通过电子邮件或用户名记录。如果不存在匹配项, 然后创建一个新的用户。其用户名分配给 Firebase电子邮件或UID(如果电子邮件不可用)。 最后,成功地验证了新创建的本地用户。在
安装
安装pip包:
$ pip install drf-firebase-token-auth
在将应用程序添加到项目的INSTALLED_APPS:
^{pr2}$ 在将FirebaseTokenAuthentication添加到Rest框架的默认列表中 身份验证类:
# settings.pyREST_FRAMEWORK={...'DEFAULT_AUTHENTICATION_CLASSES':[...'drf_firebase_token_auth.authentication.FirebaseTokenAuthentication',]}
Note:保留其他身份验证类也很好。 例如,您可能希望保留rest_framework.authentication.SessionAuthentication 允许本地用户使用密码访问可浏览的API。在
在配置应用程序:
# settings.pyDRF_FIREBASE_TOKEN_AUTH={# REQUIRED SETTINGS:# Path to JSON file with firebase secrets'FIREBASE_SERVICE_ACCOUNT_KEY_FILE_PATH':r'/mnt/c/Users/ronhe/Google Drive/ProgramsData/WizWot/paywiz-c4b4f-firebase-adminsdk-ekbjf-9b7776879a.json',# OPTIONAL SETTINGS:# Create new matching local user in db, if no match found.# Otherwise, Firebase user not matching a local user will not# be authenticated.'SHOULD_CREATE_LOCAL_USER':True,# Authentication header token keyword (usually 'Token', 'JWT' or 'Bearer')'AUTH_HEADER_TOKEN_KEYWORD':'Token',# Verify that Firebase token has not been revoked.'VERIFY_FIREBASE_TOKEN_NOT_REVOKED':True,# Require that Firebase user email_verified is True.# If set to True, non verified email addresses from Firebase are ignored.'IGNORE_FIREBASE_UNVERIFIED_EMAIL':True,}
在迁移:
$ python manage.py migrate drf-firebase-token-auth
在让您的客户在 REST请求的授权头。在
在
- 项目
标签: