Skip to content

Commit 8a10d67

Browse files
authored
Merge pull request #11 from getyoti/Base64Selfie
[SDK-160]: Added saving of image to Django example project
2 parents 5ca3afc + 34997eb commit 8a10d67

File tree

41 files changed

+236
-507
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+236
-507
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,8 @@ ENV/
9494
*.un~
9595
.history/
9696
.vscode
97+
98+
# examples files
99+
examples/yoti_example_django/yoti_example/static/YotiSelfie.jpg
100+
examples/yoti_example_django/db.sqlite3
101+
examples/yoti_example_flask/static/YotiSelfie.jpg

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ How to install the SDK to your project
2222
6) [Configuration](#configuration)-
2323
Entry point explanation
2424

25-
7) [Profile Retrieval](#profile-retrieval)-
26-
How to retrieve a Yoti profile using the token
25+
7) [Handling Users](#handling-users)-
26+
How to manage users
2727

2828
8) [Running the examples](#running-the-examples)-
2929
How to retrieve a Yoti profile using the token
@@ -37,12 +37,12 @@ Attributes defined
3737
11) [Running the tests](running-the-tests)-
3838
Running tests for SDK example
3939

40-
12) [Support](#support)-
41-
Please feel free to reach out
42-
43-
13) [Version Support](version-support) -
40+
12) [Version Support](version-support) -
4441
Extra information on ensuring correct version of Python is being used
4542

43+
13) [Support](#support)-
44+
Please feel free to reach out
45+
4646
## An Architectural View
4747

4848
Before you start your integration, here is a bit of background on how the integration works. To integrate your application with Yoti, your back-end must expose a GET endpoint that Yoti will use to forward tokens.
@@ -139,7 +139,7 @@ nationality = user_profile.get('nationality')
139139

140140
Both example applications utilise the env variables described in [Configuration](#configuration), make sure they are accessible.
141141

142-
* Installing dependencies: `pip install -e .[examples]`
142+
* Installing dependencies: `pip install -e .[examples]` (If you're using `zsh` you need to escape the square brackets: `pip install -e .\[examples\]`)
143143

144144
### Flask:
145145

@@ -152,7 +152,7 @@ Both example applications utilise the env variables described in [Configuration]
152152

153153
### Plugins ###
154154

155-
Plugins for both Django and Flask are in the `plugins/` dir. Their purpose is to make it as easy as possible to use the Yoti SDK with those frameworks. See their respective `README` files for details.
155+
Plugins for both Django and Flask are in the `plugins/` dir. Their purpose is to make it as easy as possible to use the Yoti SDK with those frameworks. See the [Django](/plugins/django_yoti/README.md) and [Flask](/plugins/flask_yoti/README.md) README files for further details.
156156

157157
## Running the Tests
158158

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
YOTI_APPLICATION_ID=
2+
YOTI_CLIENT_SDK_ID=
3+
YOTI_KEY_FILE_PATH=

examples/yoti_example_django/yoti_example/settings.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# Build paths inside the project like this: join(BASE_DIR, ...)
1818
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1919

20-
2120
# Quick-start development settings - unsuitable for production
2221
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
2322

@@ -29,7 +28,6 @@
2928

3029
ALLOWED_HOSTS = []
3130

32-
3331
# Application definition
3432

3533
INSTALLED_APPS = [
@@ -71,7 +69,6 @@
7169

7270
WSGI_APPLICATION = 'yoti_example.wsgi.application'
7371

74-
7572
# Database
7673
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
7774

@@ -82,7 +79,6 @@
8279
}
8380
}
8481

85-
8682
# Password validation
8783
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
8884

@@ -101,7 +97,6 @@
10197
},
10298
]
10399

104-
105100
# Internationalization
106101
# https://docs.djangoproject.com/en/1.10/topics/i18n/
107102

@@ -115,8 +110,11 @@
115110

116111
USE_TZ = True
117112

118-
119113
# Static files (CSS, JavaScript, Images)
120114
# https://docs.djangoproject.com/en/1.10/howto/static-files/
121115

122116
STATIC_URL = '/static/'
117+
STATICFILES_FOLDER_NAME = 'static'
118+
PROJECT_DIR = 'yoti_example/'
119+
STATIC_ROOT = os.path.join(BASE_DIR, STATICFILES_FOLDER_NAME)
120+
STATICFILES_DIRS = os.path.join(PROJECT_DIR, STATICFILES_FOLDER_NAME),
File renamed without changes.

examples/yoti_example_django/yoti_example/templates/profile.html

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,81 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>PROFILE</title>
5+
<title>Yoti Profile</title>
66
</head>
77
<body>
88
<h3><a href="/">Home</a></h3>
99
<table>
1010
{% if user_id %}
1111
<tr>
12-
<td>USER ID:</td>
12+
<td>User ID:</td>
1313
<td>{{user_id}}</td>
1414
</tr>
1515
{% endif %}
1616

1717
{% if selfie %}
1818
<tr>
19-
<td>SELFIE:</td>
20-
<td><img width="200" alt="photo" src="{{selfie}}" /></td>
19+
<td>Selfie in base64 format:</td>
20+
<td><img width="200" alt="base64photo" src="{{selfie}}" /></td>
21+
</tr>
22+
<tr>
23+
<td>Selfie from saved image:</td>
24+
<td><img width="200" alt="saved image" src="/static/YotiSelfie.jpg" /></td>
25+
</tr>
26+
{% endif %}
27+
28+
{% if given_names %}
29+
<tr>
30+
<td>Given Names:</td>
31+
<td>{{given_names}}</td>
32+
</tr>
33+
{% endif %}
34+
35+
{% if family_name %}
36+
<tr>
37+
<td>Family Name:</td>
38+
<td>{{family_name}}</td>
39+
</tr>
40+
{% endif %}
41+
42+
{% if phone_number %}
43+
<tr>
44+
<td>Phone Number:</td>
45+
<td>{{phone_number}}</td>
2146
</tr>
2247
{% endif %}
2348

24-
{% if phone %}
49+
{% if date_of_birth %}
2550
<tr>
26-
<td>PHONE:</td>
27-
<td>{{phone}}</td>
51+
<td>Date of Birth:</td>
52+
<td>{{date_of_birth}}</td>
2853
</tr>
2954
{% endif %}
3055

31-
{% if names %}
56+
{% if postal_address %}
3257
<tr>
33-
<td>NAMES:</td>
34-
<td>{{names}}</td>
58+
<td>Postal Address:</td>
59+
<td>{{postal_address}}</td>
3560
</tr>
3661
{% endif %}
3762

38-
{% if birthdate %}
63+
{% if email_address %}
3964
<tr>
40-
<td>DATE OF BIRTH</td>
41-
<td>{{birthdate}}</td>
65+
<td>Email Address:</td>
66+
<td>{{email_address}}</td>
4267
</tr>
4368
{% endif %}
4469

4570
{% if nationality %}
4671
<tr>
47-
<td>NATIONALITY</td>
72+
<td>Nationality:</td>
4873
<td>{{nationality}}</td>
4974
</tr>
5075
{% endif %}
5176

5277
{% if gender %}
5378
<tr>
54-
<td>GENDER</td>
79+
<td>Gender:</td>
5580
<td>{{gender}}</td>
5681
</tr>
5782
{% endif %}

examples/yoti_example_django/yoti_example/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020

2121
urlpatterns = [
2222
url(r'^$', IndexView.as_view(), name='index'),
23-
url(r'^auth/$', AuthView.as_view(), name='auth'),
23+
url(r'^yoti/auth/$', AuthView.as_view(), name='auth'),
2424
url(r'^admin/', admin.site.urls),
2525
]

examples/yoti_example_django/yoti_example/views.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
from binascii import a2b_base64
2+
13
from django.views.generic import TemplateView
24

3-
from yoti_python_sdk import Client
5+
from os.path import join, dirname
6+
from dotenv import load_dotenv
7+
8+
dotenv_path = join(dirname(__file__), '.env')
9+
load_dotenv(dotenv_path)
410

11+
from yoti_python_sdk import Client
512
from .app_settings import (
613
YOTI_APPLICATION_ID,
714
YOTI_CLIENT_SDK_ID,
@@ -23,4 +30,13 @@ def get(self, request, *args, **kwargs):
2330
client = Client(YOTI_CLIENT_SDK_ID, YOTI_FULL_KEY_FILE_PATH)
2431
activity_details = client.get_activity_details(request.GET['token'])
2532
context = activity_details.user_profile
33+
self.save_image(context.get('selfie'))
2634
return self.render_to_response(context)
35+
36+
@staticmethod
37+
def save_image(base64_uri):
38+
base64_data_stripped = base64_uri[base64_uri.find(",") + 1:]
39+
binary_data = a2b_base64(base64_data_stripped)
40+
fd = open('yoti_example/static/YotiSelfie.jpg', 'wb')
41+
fd.write(binary_data)
42+
fd.close()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
YOTI_APPLICATION_ID=
2+
YOTI_CLIENT_SDK_ID=
3+
YOTI_KEY_FILE_PATH=

examples/yoti_example_flask/app.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import os
2+
from binascii import a2b_base64
3+
from os.path import join, dirname
4+
5+
from dotenv import load_dotenv
16
from flask import Flask, render_template, request
27

38
from yoti_python_sdk import Client
49

10+
dotenv_path = join(dirname(__file__), '.env')
11+
load_dotenv(dotenv_path)
12+
513
from settings import (
614
YOTI_APPLICATION_ID,
715
YOTI_CLIENT_SDK_ID,
@@ -11,16 +19,26 @@
1119
app = Flask(__name__)
1220

1321

22+
def save_image(base64_uri):
23+
base64_data_stripped = base64_uri[base64_uri.find(",") + 1:]
24+
binary_data = a2b_base64(base64_data_stripped)
25+
upload_path = os.path.join(app.root_path, 'static', 'YotiSelfie.jpg')
26+
fd = open(upload_path, 'wb')
27+
fd.write(binary_data)
28+
fd.close()
29+
30+
1431
@app.route('/')
1532
def index():
1633
return render_template('index.html', app_id=YOTI_APPLICATION_ID)
1734

1835

19-
@app.route('/auth')
36+
@app.route('/yoti/auth')
2037
def auth():
2138
client = Client(YOTI_CLIENT_SDK_ID, YOTI_FULL_KEY_FILE_PATH)
2239
activity_details = client.get_activity_details(request.args['token'])
2340
user_profile = activity_details.user_profile
41+
save_image(user_profile.get('selfie'))
2442
return render_template('profile.html',
2543
**user_profile)
2644

0 commit comments

Comments
 (0)