Package: gdmty-drf-firebase-auth
gdmty-drf-firebase-authFirebase backend to receive a user idToken and authenticate via Django REST Framework
'authentication.BaseAuthentication'. Optionally, a new local user can be created in the process.RequirementsPython 3Django 4Django Rest Framework 3Firebase Admin SDKInstallation$pipinstallgdmty-drf-firebase-authConfigurationAdd the application to your project'sINSTALLED_APPSinsettings.py.INSTALLED_APPS=[# ...'gdmty_drf_firebase_auth',...]General settingsIn your project'ssettings.py, add this to theREST_FRAMEWORKconfiguration. Note that if you want to retain access to the browsable API for locally created users, then you will probably want to keeprest_framework.authentication.SessionAuthenticationtoo.REST_FRAMEWORK={# ...'DEFAULT_AUTHENTICATION_CLASSES':[# ...'rest_framework.authentication.SessionAuthentication',# Optional, better to remove for production'rest_framework.authentication.BasicAuthentication',# Optional, better to remove for production'gdmty_drf_firebase_auth.authentication.FirebaseAuthentication',]}Thegdmty_drf_firebase_authapplication comes with the following settings as default, which can be overridden in your project'ssettings.pyfile. For convenience, most of these can be conveniently set form environment variables also. Make sure to nest them withinDEFAULT_FIREBASE_AUTH_CONFIGas below:DEFAULT_FIREBASE_AUTH_CONFIG={# 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':False,# Authorization header prefix, commonly JWT or Bearer (e.g. Bearer <token>)'FIREBASE_AUTH_HEADER_PREFIX':'Bearer',# 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,# function should accept firebase_admin.auth.UserRecord as argument and return str'FIREBASE_USERNAME_MAPPING_FUNC':map_firebase_uid_to_username}You can get away with leaving all the settings as defaultFirebase per-project settingsIt is required to have GCP service accounts, the original project only supports one; the original code has been modified to allow more than one service account, To configure the service accounts, the following configuration must be added in thesettings.pyfile of your project, where each element corresponds to a project and in turn, its respective service account.BASE_DIRis the root of your project, where manage.py lives. We assume that you have a folder calledsain the root of your project, and that you have placed your service account keyfiles there. You can change this to wherever you like.importosBASE_DIR=os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))...FIREBASE_AUTH_SA_KEYFILES={'project-1':os.path.join(BASE_DIR,'sa','project-1-keyfile.json'),'project-2':os.path.join(BASE_DIR,'sa','project-2-keyfile.json')}FIREBASE_AUTH_PROJECTS={{'PROJECT_ID':'project-1','SERVICE_ACCOUNT_KEY':FIREBASE_AUTH_SA_KEYFILES['project-1']},{'PROJECT_ID':'project-2','SERVICE_ACCOUNT_KEY':FIREBASE_AUTH_SA_KEYFILES['project-2']},}Now that you have configured the application, run the migrations so that the Firebase data can be stored.(venv)$./manage.pymigrategdmty_drf_firebase_authAll 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 anAuthorizationheader in requests to your API.Bearer<token>Now, all is ready!ContributingPlease raise an issue/feature and name your branch 'feature-n' or 'issue-n', where 'n' is the issue number.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 :)Additional NotesThis project is a fork of drf-firebase-auth, which seems no longer maintained. The original project only supports one service account, the original code has been modified to allow more than one service account.The initial proposal of this project had the option of use Firebase Auth with email and password, but this option was removed to keep the project clean and simple, and focused on the use of Firebase Auth with idToken for Django REST Framework.If you are looking for a project that supports Firebase Auth with email and password, you can use our side project:gdmty-django-firebase-auth-email-passwordathttps://github.com/SIGAMty/gdmty-django-firebase-auth-email-passwordI almost always setup Django with a custom user class inheriting from AbstractUser, where I switch the USERNAME_FIELD to be 'email'. This backend is set up 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!