Skip to content

Commit

Permalink
Merge pull request #20 from getyoti/Image_Handling
Browse files Browse the repository at this point in the history
[SDK-230]: Changed image handling so that the conversion to base64 is done via a helper method, bug fixes
  • Loading branch information
echarrod authored Jan 29, 2018
2 parents b60e18f + a266d2c commit 241108d
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 46 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,16 @@ Both example applications utilise the env variables described in [Configuration]

#### Flask

* Run `python examples/yoti_example_flask/app.py`
1. Change directories to the Flask project: `cd examples/yoti_example_flask`
1. Run `python app.py`
1. Navigate to http://localhost:5000

#### Django

1. Change directories to the Django project: `cd examples/yoti_example_django`
2. Apply migrations before the first start by running: `python manage.py migrate`
3. Run: `python manage.py runserver 0.0.0.0:5000`
1. Apply migrations before the first start by running: `python manage.py migrate`
1. Run: `python manage.py runserver 0.0.0.0:5000`
1. Navigate to http://localhost:5000

### Plugins ###

Expand Down
1 change: 1 addition & 0 deletions examples/yoti_example_django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ RUN pip install --no-cache-dir -r requirements.txt
ENV YOTI_APPLICATION_ID $YOTI_APPLICATION_ID
ENV YOTI_CLIENT_SDK_ID $YOTI_CLIENT_SDK_ID
ENV YOTI_KEY_FILE_PATH $YOTI_KEY_FILE_PATH
CMD ["python", "manage.py", "migrate"]
CMD ["python", "manage.py", "runserver", "0.0.0.0:5000"]
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ <h3><a href="/">Home</a></h3>
</tr>
{% endif %}

{% if selfie %}
{% if base64_selfie_uri %}
<tr>
<td>Selfie in base64 format:</td>
<td><img width="200" alt="base64photo" src="{{selfie}}" /></td>
<td><img width="200" alt="base64photo" src="{{base64_selfie_uri}}" /></td>
</tr>
<tr>
<td>Selfie from saved image:</td>
Expand Down
20 changes: 8 additions & 12 deletions examples/yoti_example_django/yoti_example/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
from binascii import a2b_base64
from os.path import join, dirname

from django.views.generic import TemplateView
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), '..\\', '.env')
load_dotenv(dotenv_path)
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())

from yoti_python_sdk import Client
from app_settings import (
Expand All @@ -29,13 +24,14 @@ def get(self, request, *args, **kwargs):
client = Client(YOTI_CLIENT_SDK_ID, YOTI_KEY_FILE_PATH)
activity_details = client.get_activity_details(request.GET['token'])
context = activity_details.user_profile
self.save_image(context.get('selfie'))
context['base64_selfie_uri'] = getattr(activity_details, 'base64_selfie_uri')
selfie = context.get('selfie')
if selfie is not None:
self.save_image(selfie)
return self.render_to_response(context)

@staticmethod
def save_image(base64_uri):
base64_data_stripped = base64_uri[base64_uri.find(",") + 1:]
binary_data = a2b_base64(base64_data_stripped)
def save_image(selfie_data):
fd = open('yoti_example/static/YotiSelfie.jpg', 'wb')
fd.write(binary_data)
fd.write(selfie_data)
fd.close()
12 changes: 6 additions & 6 deletions examples/yoti_example_flask/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# noinspection PyPackageRequirements
import os
from binascii import a2b_base64
from os.path import join, dirname

from dotenv import load_dotenv
Expand All @@ -20,12 +19,10 @@
app = Flask(__name__)


def save_image(base64_uri):
base64_data_stripped = base64_uri[base64_uri.find(",") + 1:]
binary_data = a2b_base64(base64_data_stripped)
def save_image(selfie_data):
upload_path = os.path.join(app.root_path, 'static', 'YotiSelfie.jpg')
fd = open(upload_path, 'wb')
fd.write(binary_data)
fd.write(selfie_data)
fd.close()


Expand All @@ -39,7 +36,10 @@ def auth():
client = Client(YOTI_CLIENT_SDK_ID, YOTI_KEY_FILE_PATH)
activity_details = client.get_activity_details(request.args['token'])
user_profile = activity_details.user_profile
save_image(user_profile.get('selfie'))
user_profile['base64_selfie_uri'] = getattr(activity_details, 'base64_selfie_uri')
selfie = user_profile.get('selfie')
if selfie is not None:
save_image(selfie)
return render_template('profile.html',
**user_profile)

Expand Down
4 changes: 2 additions & 2 deletions examples/yoti_example_flask/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ <h3><a href="/">Home</a></h3>
</tr>
{% endif %}

{% if selfie %}
{% if base64_selfie_uri %}
<tr>
<td>Selfie in base64 format:</td>
<td><img width="200" alt="base64photo" src="{{selfie}}" /></td>
<td><img width="200" alt="base64photo" src="{{base64_selfie_uri}}" /></td>
</tr>
<tr>
<td>Selfie from saved image:</td>
Expand Down
6 changes: 3 additions & 3 deletions plugins/django_yoti/django_yoti/templates/yoti_profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ <h3><a href="/">Home</a></h3>
</tr>
{% endif %}

{% if selfie %}
{% if base64_selfie_uri %}
<tr>
<td>Selfie:</td>
<td><img width="200" alt="photo" src="{{selfie}}" /></td>
<td>Selfie in base64 format:</td>
<td><img width="200" alt="base64photo" src="{{base64_selfie_uri}}" /></td>
</tr>
{% endif %}

Expand Down
6 changes: 3 additions & 3 deletions plugins/flask_yoti/flask_yoti/templates/yoti_profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ <h3><a href="/">Home</a></h3>
</tr>
{% endif %}

{% if selfie %}
{% if base64_selfie_uri %}
<tr>
<td>Selfie:</td>
<td><img width="200" alt="photo" src="{{selfie}}" /></td>
<td>Selfie in base64 format:</td>
<td><img width="200" alt="base64photo" src="{{base64_selfie_uri}}" /></td>
</tr>
{% endif %}

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ MarkupSafe==0.23
mock==2.0.0
pbr==1.10.0
protobuf==3.1.0.post1
pytest==3.0.3
pytest==3.3.2
requests==2.11.1
six==1.10.0
tox>=1.7.2
virtualenv==13.1.2
future==0.15.2
Werkzeug==0.11.11
Werkzeug==0.11.15
wheel==0.24.0
2 changes: 1 addition & 1 deletion requirements_tox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ six==1.10.0
tox>=1.7.2
virtualenv==13.1.2
future==0.15.2
Werkzeug==0.11.11
Werkzeug==0.11.15
wheel==0.24.0
7 changes: 7 additions & 0 deletions yoti_python_sdk/activity_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ActivityDetails:
def __init__(self, receipt, decrypted_profile=None):
self.decrypted_profile = decrypted_profile
self.user_profile = {}
self.base64_selfie_uri = ''

if decrypted_profile and hasattr(decrypted_profile, 'attributes'):
for field in decrypted_profile.attributes:
Expand All @@ -14,6 +15,11 @@ def __init__(self, receipt, decrypted_profile=None):
field.content_type
)
self.user_profile[field.name] = value
if field.name == 'selfie':
self.base64_selfie_uri = Protobuf().image_uri_based_on_content_type(
field.value,
field.content_type
)

self.user_id = receipt['remember_me_id']
self.outcome = receipt['sharing_outcome']
Expand All @@ -22,3 +28,4 @@ def __iter__(self):
yield 'user_id', self.user_id
yield 'outcome', self.outcome
yield 'user_profile', self.user_profile
yield 'base64_selfie_uri', self.base64_selfie_uri
15 changes: 10 additions & 5 deletions yoti_python_sdk/protobuf/v1/protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Protobuf(object):
CT_DATE = 3 # string in RFC3339 format (YYYY-MM-DD)
CT_PNG = 4 # standard .png image

def current_user(self, receipt):
@staticmethod
def current_user(receipt):
if receipt.get('other_party_profile_content') is None or receipt.get('other_party_profile_content') == '':
return None

Expand All @@ -25,7 +26,8 @@ def current_user(self, receipt):
merged_user.MergeFromString(decoded_profile_content)
return merged_user

def attribute_list(self, data):
@staticmethod
def attribute_list(data):
attribute_list = attrpubapi.AttributeList()
attribute_list.MergeFromString(data)
return attribute_list
Expand All @@ -35,11 +37,14 @@ def value_based_on_content_type(self, value, content_type=None):
raise TypeError('Wrong content type')
elif content_type == self.CT_STRING:
return value.decode('utf-8')
elif content_type == self.CT_JPEG:
data = base64.b64encode(value).decode('utf-8')
return 'data:image/jpeg;base64,{0}'.format(data)
elif content_type == self.CT_DATE:
return value.decode('utf-8')
return value

def image_uri_based_on_content_type(self, value, content_type=None):
if content_type == self.CT_JPEG:
data = base64.b64encode(value).decode('utf-8')
return 'data:image/jpeg;base64,{0}'.format(data)
elif content_type == self.CT_PNG:
data = base64.b64encode(value).decode('utf-8')
return 'data:image/png;base64,{0}'.format(data)
Expand Down
4 changes: 3 additions & 1 deletion yoti_python_sdk/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def test_requesting_activity_details_with_correct_data(
assert activity_details.user_id == "ijH4kkqMKTG0FSNUgQIvd2Z3Nx1j8f5RjVQMyoKOvO/hkv43Ik+t6d6mGfP2tdrN"
selfie = activity_details.user_profile.get('selfie')
assert isinstance(selfie, basestring)
assert selfie.startswith('data:image/jpeg;base64')
base64_selfie_uri = getattr(activity_details, 'base64_selfie_uri')
assert isinstance(base64_selfie_uri, basestring)
assert base64_selfie_uri.startswith('data:image/jpeg;base64')


@mock.patch('requests.get', side_effect=mocked_requests_get_null_profile)
Expand Down
16 changes: 10 additions & 6 deletions yoti_python_sdk/tests/test_protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ def test_protobuf_value_based_on_content_type(proto):
result = proto.value_based_on_content_type(value, proto.CT_STRING)
assert isinstance(result, basestring)

result = proto.value_based_on_content_type(value, proto.CT_JPEG)
assert result == 'data:image/jpeg;base64,dGVzdCBzdHJpbmc='

result = proto.value_based_on_content_type(value, proto.CT_DATE)
assert isinstance(result, basestring)

result = proto.value_based_on_content_type(value, proto.CT_PNG)
assert result == 'data:image/png;base64,dGVzdCBzdHJpbmc='

result = proto.value_based_on_content_type(value)
assert result == value


def test_protobuf_image_uri_based_on_content_type(proto):
value = b'test string'

result = proto.image_uri_based_on_content_type(value, proto.CT_JPEG)
assert result == 'data:image/jpeg;base64,dGVzdCBzdHJpbmc='

result = proto.image_uri_based_on_content_type(value, proto.CT_PNG)
assert result == 'data:image/png;base64,dGVzdCBzdHJpbmc='

0 comments on commit 241108d

Please sign in to comment.