Package: django-labs-accounts
Django Labs AccountsRequirementsPython 3.6+Django 2.1+InstallationInstall with pippip install django-labs-accountsAddaccountstoINSTALLED_APPSINSTALLED_APPS=(...'accounts.apps.AccountsConfig','identity.apps.IdentityConfig',# If you want to enable B2B IPC...)Add the new accounts backend toAUTHENTICATION_BACKENDSAUTHENTICATION_BACKENDS=(...'accounts.backends.LabsUserBackend','django.contrib.auth.backends.ModelBackend',...)(Optional) Add the new Platform DRF authentication class to rest framework'sDEFAULT_AUTHENTICATION_CLASSES. This authentication class should go at the end of the list of authentication classes in most cases.REST_FRAMEWORK={...'DEFAULT_AUTHENTICATION_CLASSES':['rest_framework.authentication.SessionAuthentication','rest_framework.authentication.BasicAuthentication','accounts.authentication.PlatformAuthentication',]...}Add the following tourls.pyurlpatterns=[...path('accounts/',include('accounts.urls',namespace='accounts')),...]DocumentationAll settings are handled within aPLATFORM_ACCOUNTSdictionary.Example:PLATFORM_ACCOUNTS={'CLIENT_ID':'id','CLIENT_SECRET':'secret','REDIRECT_URI':'example','ADMIN_PERMISSION':'example_admin''CUSTOM_ADMIN':True}The available settings are:CLIENT_IDthe client ID to connect to platform with. Defaults toLABS_CLIENT_IDenvironment variable.CLIENT_SECRETthe client secret to connect to platform with. Defaults toLABS_CLIENT_SECRETenvironment variable.REDIRECT_URIthe redirect uri to send to platform. Defaults to first theLABS_REDIRECT_URIenvironment variable and then generating the value from the request object.SCOPEthe scope for this applications tokens. Must includeintrospection. Defaults to['read', 'introspection'].PLATFORM_URLURL of platform server to connect to. Should behttps://platform(-dev).pennlabs.org(no trailing slash)ADMIN_PERMISSIONThe name of the permission on platform to grant admin access. Defaults toexample_adminCUSTOM_ADMINenable the custom admin login page to log in users through platform. Defaults toTrueWhen developing locally with an http (not https) callback URL, it may be helpful to set theOAUTHLIB_INSECURE_TRANSPORTenvironment variable.os.environ['OAUTHLIB_INSECURE_TRANSPORT']="1"Custom post authenticationIf you want to customize how DLA saves user information from platform into User objects, you can subclassaccounts.backends.LabsUserBackendand redefine the post_authenticate method. This method will be run after the user is logged in. The parameters are:userthe user objectcreateda boolean delineating if the user was just createddictionarya dictionary of user information from platform.Then just set theAUTHENTICATION_BACKENDSsetting to be the subclassed backend.Here is an example of a custom backend that sets every user's first name to"Modified".fromaccounts.backendsimportLabsUserBackendclassCustomBackend(LabsUserBackend):defpost_authenticate(self,user,created,dictionary):user.first_name='Modified'user.save()B2B IPCDLA also provides an interface for backend to backend IPC requests. With B2B IPC implemented, the backend of a product will—at startup time—request platform for a JWT to verify its identity. Each product will have an allow-list, and this will enable products to make requests to each other.In order to limit a view to only be available to a B2B IPC request, you can use the included DRF permission:fromidentity.permissionsimportB2BPermissionclassTestView(APIView):permission_classes=[B2BPermission("urn:pennlabs:example")]Make sure to define an URN to limit access. Valid URNs are either a specific product (ex.urn:pennlabs:platform) or a wildcard (ex.urn:pennlabs:*)In order to make an IPC request, use the included helper function:fromidentity.identityimportauthenticated_b2b_requestresult=authenticated_b2b_request('GET','http://url/path')Use in ProductionDLA and Penn Labs' templates are set up so that no configuration is needed to run in development. However, in production a client ID and client secret need to be set. These values should be set in vault. Contact platform for both credentials and any questions you have.B2B IPCDLA also provides an interface for backend to backend IPC requests. In order to limit a view to only be available to a B2B IPC request, you can use the included DRF permission:fromidentity.permissionsimportB2BPermissionclassTestView(APIView):permission_classes=[B2BPermission("urn:pennlabs:example")]Make sure to define an URN to limit access. Valid URNs are either a specific product (ex.urn:pennlabs:platform) or a wildcard (ex.urn:pennlabs:*)In order to make an IPC request, use the included helper function:fromidentity.identityimportauthenticated_b2b_requestresult=authenticated_b2b_request('GET','http://url/path')Development SetupInstall poetry:pipx install poetryInstall Dependencies:poetry installTesting:export DJANGO_SETTINGS_MODULE=tests.settings && poetry run pytestLinting:poetry run black . && poetry run isort . && poetry run flake8ChangelogSeeCHANGELOG.mdLicenseSeeLICENSE