Skip to content

Commit

Permalink
Removing python2 compatibility. (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgliss committed Nov 21, 2016
1 parent 6eca2eb commit dd6d332
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 48 deletions.
14 changes: 3 additions & 11 deletions lemur/auth/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
"""
import sys
import jwt
import json
import binascii
Expand Down Expand Up @@ -40,12 +39,8 @@ def get_rsa_public_key(n, e):
:param e:
:return: a RSA Public Key in PEM format
"""
if sys.version_info >= (3, 0):
n = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(n, 'utf-8'))), 16)
e = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(e, 'utf-8'))), 16)
else:
n = int(binascii.hexlify(jwt.utils.base64url_decode(str(n))), 16)
e = int(binascii.hexlify(jwt.utils.base64url_decode(str(e))), 16)
n = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(n, 'utf-8'))), 16)
e = int(binascii.hexlify(jwt.utils.base64url_decode(bytes(e, 'utf-8'))), 16)

pub = RSAPublicNumbers(e, n).public_key(default_backend())
return pub.public_bytes(
Expand Down Expand Up @@ -128,10 +123,7 @@ def fetch_token_header(token):
raise jwt.DecodeError('Not enough segments')

try:
if sys.version_info >= (3, 0):
return json.loads(jwt.utils.base64url_decode(header_segment).decode('utf-8'))
else:
return json.loads(jwt.utils.base64url_decode(header_segment))
return json.loads(jwt.utils.base64url_decode(header_segment).decode('utf-8'))
except TypeError as e:
current_app.logger.exception(e)
raise jwt.DecodeError('Invalid header padding')
Expand Down
14 changes: 3 additions & 11 deletions lemur/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
:license: Apache, see LICENSE for more details.
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
"""
import sys
import jwt
import base64
import requests
Expand Down Expand Up @@ -143,12 +142,8 @@ def post(self):
# the secret and cliendId will be given to you when you signup for the provider
token = '{0}:{1}'.format(args['clientId'], current_app.config.get("PING_SECRET"))

if sys.version_info >= (3, 0):
basic = base64.b64encode(bytes(token, 'utf-8'))
headers = {'authorization': 'basic {0}'.format(basic.decode('utf-8'))}
else:
basic = base64.b64encode(token, 'utf-8')
headers = {'authorization': 'basic {0}'.format(basic)}
basic = base64.b64encode(bytes(token, 'utf-8'))
headers = {'authorization': 'basic {0}'.format(basic.decode('utf-8'))}

# exchange authorization code for access token.

Expand All @@ -172,10 +167,7 @@ def post(self):

# validate your token based on the key it was signed with
try:
if sys.version_info >= (3, 0):
jwt.decode(id_token, secret.decode('utf-8'), algorithms=[algo], audience=args['clientId'])
else:
jwt.decode(id_token, secret, algorithms=[algo], audience=args['clientId'])
jwt.decode(id_token, secret.decode('utf-8'), algorithms=[algo], audience=args['clientId'])
except jwt.DecodeError:
return dict(message='Token is invalid'), 403
except jwt.ExpiredSignatureError:
Expand Down
7 changes: 1 addition & 6 deletions lemur/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
"""
import six
import sys
import string
import random

Expand Down Expand Up @@ -37,10 +35,7 @@ def get_psuedo_random_string():


def parse_certificate(body):
if sys.version_info[0] <= 2:
return x509.load_pem_x509_certificate(bytes(body), default_backend())

if isinstance(body, six.string_types):
if isinstance(body, str):
body = body.encode('utf-8')

return x509.load_pem_x509_certificate(body, default_backend())
Expand Down
8 changes: 0 additions & 8 deletions lemur/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,6 @@ def unlock(path=None):
sys.stdout.write("[+] Keys have been unencrypted!\n")


def unicode_(data):
import sys

if sys.version_info.major < 3:
return data.decode('UTF-8')
return data


def print_certificate_details(details):
"""
Print the certificate details with formatting.
Expand Down
5 changes: 2 additions & 3 deletions lemur/plugins/lemur_java/tests/test_java.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import six

from lemur.tests.vectors import INTERNAL_CERTIFICATE_A_STR, INTERNAL_PRIVATE_KEY_A_STR

Expand All @@ -24,7 +23,7 @@ def test_export_truststore_default_password(app):
actual = p.export(INTERNAL_CERTIFICATE_A_STR, "", "", options)

assert actual[0] == 'jks'
assert isinstance(actual[1], six.string_types)
assert isinstance(actual[1], str)
assert isinstance(actual[2], bytes)


Expand Down Expand Up @@ -56,5 +55,5 @@ def test_export_keystore_default_password(app):
actual = p.export(INTERNAL_CERTIFICATE_A_STR, "", INTERNAL_PRIVATE_KEY_A_STR, options)

assert actual[0] == 'jks'
assert isinstance(actual[1], six.string_types)
assert isinstance(actual[1], str)
assert isinstance(actual[2], bytes)
9 changes: 1 addition & 8 deletions lemur/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
"""
import os
import sys
import six
from flask import current_app
from cryptography.fernet import Fernet, MultiFernet
import sqlalchemy.types as types
Expand Down Expand Up @@ -97,11 +95,8 @@ def process_bind_param(self, value, dialect):
if not value:
return

if sys.version_info[0] <= 2:
return MultiFernet(self.keys).encrypt(bytes(value))

# ensure bytes for fernet
if isinstance(value, six.string_types):
if isinstance(value, str):
value = value.encode('utf-8')

return MultiFernet(self.keys).encrypt(value)
Expand All @@ -122,6 +117,4 @@ def process_result_value(self, value, dialect):
if not value:
return

if sys.version_info[0] <= 2:
return MultiFernet(self.keys).decrypt(value)
return MultiFernet(self.keys).decrypt(value).decode('utf8')
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
'requests==2.11.1',
'psycopg2==2.6.1',
'arrow==0.7.0',
'six==1.10.0',
'gunicorn==19.4.1',
'marshmallow-sqlalchemy==0.8.0',
'marshmallow==2.4.0',
Expand Down

0 comments on commit dd6d332

Please sign in to comment.