Custom DRF authentication backend.
Project description
DRF Firebase Auth (Forked from drf-firebase-auth by garyburgman)
Requirements
- Python (tested with 2.7, 3.6)
- Django
- Django Rest Framework
Installation
$ pip install drf-firebase-auth-cavoke
Add the application to your project's INSTALLED_APPS
in settings.py
.
INSTALLED_APPS = [
...
'drf_firebase_auth_cavoke',
]
In your project's settings.py
, add this to the REST_FRAMEWORK
configuration. Note that if you want to retain access to the browsable API for locally created users, then you will probably want to keep rest_framework.authentication.SessionAuthentication
too.
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': [
...
'rest_framework.authentication.SessionAuthentication',
'drf_firebase_auth_cavoke.authentication.FirebaseAuthentication',
]
}
The drf_firebase_auth_cavoke
application comes with the following settings as default, which can be overridden in your project's settings.py
file. Make sure to nest them within drf_firebase_auth_cavoke
as below:
drf_firebase_auth_cavoke = {
# path to JSON file with firebase secrets
'FIREBASE_SERVICE_ACCOUNT_KEY': '',
# allow creation of new local user in db
'FIREBASE_CREATE_LOCAL_USER': True,
# attempt to split firebase user.display_name and set local user
# first_name and last_name
'FIREBASE_ATTEMPT_CREATE_WITH_DISPLAY_NAME': True,
# commonly JWT or Bearer (e.g. JWT <token>)
'FIREBASE_AUTH_HEADER_PREFIX': 'JWT',
# verify that JWT has not been revoked
'FIREBASE_CHECK_JWT_REVOKED': True,
# require that firebase user.email_verified is True
'FIREBASE_AUTH_EMAIL_VERIFICATION': False
}
You can get away with leaving all the settings as default except for FIREBASE_SERVICE_ACCOUNT_KEY
, which is obviously required. As a minimum, you will need to set this in your project's settings.py
. This must be the JSON service account key that you receive from the Firebase console for your application.
...
drf_firebase_auth_cavoke = {
'FIREBASE_SERVICE_ACCOUNT_KEY': 'project/config/firebase.json'
}
Now that you have configured the application, run the migrations so that the Firebase data can be stored.
$ ./manage.py migrate drf_firebase_auth_cavoke
All you need to do now is have your client code handle the Firebase popup/redirect authentication flow, retrieve the idToken from the currentUser (Firebase explains this flow well in their docs: https://firebase.google.com/docs/auth/admin/verify-id-tokens
), and then use the idToken for the user in an Authorization
header in requests to your API.
JWT <token>
Voila!
Contributing
- If you test this code with a Python version not listed above and all is well, please fork and update the README to include the Python version you used :)
- I almost always setup Django with a custom user class inheriting from AbstractUser, where I switch the USERNAME_FIELD to be 'email'. This backend is setup to assign a username still anyway, but if there are any issues, please raise them and/or make a pull request to help the community!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file drf-firebase-auth-cavoke-0.0.7.tar.gz
.
File metadata
- Download URL: drf-firebase-auth-cavoke-0.0.7.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ef7c48b2aad7c75ace05ee2977337aff5dea432a7328134f510bb8c47d8323c |
|
MD5 | 14b1218bc83086cf3ee3dc7dd0c0b4e8 |
|
BLAKE2b-256 | dd38b8cbaf6178d3d0eeeadb092726fa8a5d2feead9c36536a68c9698197d8af |
File details
Details for the file drf_firebase_auth_cavoke-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: drf_firebase_auth_cavoke-0.0.7-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 288c5decf03fbe3294ecc69c63e2f5f07b38cd04384279fdf98b93bc5e29f70d |
|
MD5 | 02db837f5a608b9a6cd73d31498338bc |
|
BLAKE2b-256 | 0190e83d6f34bca02106881a0d83ad56ec10a2b2afe12a82c130b35b90390b4e |