Django app for integration with Onfido.
Project description
Django app for integration with the Onfido API (v3)
The current version supports Django 3.1+/4.0+ and Python 3.7+.
For v2 API please check version prior to 1.0
Background
Onfido is an online identity verification service. It provides API access to a range of tests (identity, right to work, criminal history, credit report). It is assumed that you are only interested in this project because you are already aware of what Onfido does, and so I won’t repeat it here. If you want to find out more, head over to their website.
If you are using Onfido, and you are using Django, then this project can be used to manage Onfido checks against your existing Django users. It handles the API interactions, as well as providing the callback webhooks required to support live status updates.
Installation
The project is available through PyPI as django-onfido:
$ pip install django-onfido
And the main package itself is just onfido:
>>> from onfido import api, models, views, urls, admin, signals, helpers, decorators
Usage
The main use case is as follows:
Create an Onfido Applicant from your Django user:
>>> from django.contrib.auth import get_user_model
>>> from onfido.helpers import create_applicant
>>> user = get_user_model().objects.last() # any old one will do
>>> applicant = create_applicant(user)
DEBUG Making POST request to https://api.onfido.com/v3/applicants
DEBUG <Response [201]>
DEBUG {u'first_name': u'hugo', u'last_name': u'rb', u'middle_name': None, ...}
DEBUG Creating new Onfido applicant from JSON: {u'first_name': u'hugo', u'last_name': u'rb', ...}
<Applicant id=a2c98eae-XXX user='hugo'>
Create your check + reports for the applicant:
>>> from onfido.helpers import create_check
>>> create_check(applicant, 'standard', ['identity', 'right_to_work'])
>>> assert Check.objects.count() == 1
>>> assert Report.objects.count() == 2
This will create the Check and Report objects on Onfido, and store them locally as Django model objects.
Wait for callback events to update the status of reports and checks:
DEBUG Received Onfido callback: {"payload":{...}}
DEBUG Processing 'check.completed' action on check.bd8232c4-...
NB If you are using the callback functionality, you must set the ONFIDO_WEBHOOK_TOKEN property (see settings section below). The callback handler will force verification of the X-Signature request header as specified in the webhooks documentation.
The raw JSON returned from the API for a given entity (Applicant, Check, Report) is stored on the model as the raw attribute, and this can be parsed into the relevant model attributes. (Yes this does mean duplication of data.) The core pattern for interaction with the API on a per- object basis is a read-only fetch / pull pattern (analagous to git operations of the same name). If you call the fetch method on an object, it will use the href value in the raw JSON to fetch the latest data from the API and parse it, but without saving the changes. If you want to update the object, use the pull method instead.
The Report object is a special case, where the raw data from the API often contains sensitive information that you may not wish to store locally (passport numbers, Visa information, personal data). In order to get around this, there is a scrub_report_data function that will remove certain attributes of the raw data before it is parsed. By default this will remove the breakdown and properties elements.
>>> check = Check.objects.last()
>>> check.raw
{
"id": "c26f22d5-4903-401f-8a48-7b0211d03c1f",
"created_at": "2016-10-15T19:05:50Z",
"status": "awaiting_applicant",
"type": "standard",
"result": "clear",
"href": "applicants/123/checks/456"
}
>>> check.fetch() # fetch and parse the latest raw data
>>> check.pull() # calls fetch and then saves the object
There is a management command onfido_sync which can be used to pull all the objects in a queryset. It takes a single positional arg - ‘applicant’, check’ or ‘report’, and has two options - --filter and --exclude - both of which take multiple space-separated args which can be used to manage the queryset that is used.
Examples:
$ ./manage.py onfido_sync check
$ ./manage.py onfido_sync report
$ ./manage.py onfido_sync check --filter complete
$ ./manage.py onfido_sync check --exclude complete
Settings
The following settings can be specified as environment settings or within the Django settings.
ONFIDO_API_KEY: your API key, found under setting in your Onfido account.
ONFIDO_WEBHOOK_TOKEN: (optional) the Onfido webhook callback token - required if using webhooks.
The following settings can be specified in the Django settings:
ONFIDO_LOG_EVENTS: (optional) if True then callback events from the API will also be recorded as Event objects. Defaults to False.
ONFIDO_REPORT_SCRUBBER: (optional) a function that is used to scrub sensitive data from Report objects. The default implementation will remove breakdown and properties.
Tests
If you want to run the tests manually, install poetry.
$ poetry install
$ poetry run pytest
If you are hacking on the project, please keep coverage up.
Contributing
Standard GH rules apply: clone the repo to your own account, make sure you update the tests, and submit a pull request.
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 django-onfido-2.2.tar.gz
.
File metadata
- Download URL: django-onfido-2.2.tar.gz
- Upload date:
- Size: 28.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.1 Darwin/21.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 899e4614fd334d07a703c91634f2eab0c85762182d6364f30c87d881d390b562 |
|
MD5 | 34fd36350fd1d4857b6702818c783981 |
|
BLAKE2b-256 | 7b5895562110d01d5a7e8b062750a324a1899c100b2ad4fc067c2b472d4ec2a7 |
File details
Details for the file django_onfido-2.2-py3-none-any.whl
.
File metadata
- Download URL: django_onfido-2.2-py3-none-any.whl
- Upload date:
- Size: 39.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.1 Darwin/21.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b44dc13618b5eb56e6b642f15141e09c3f3257766d0dd976a41098e4f62358fa |
|
MD5 | 0ef81533790b9d751790c9826f27d02a |
|
BLAKE2b-256 | 2d9805eadf8ae39f96c53d3c8e347dc834a6f8cd275e78fc55b26eadd3b21aff |