Skip to content

Commit 4815905

Browse files
authored
Merge pull request #210 from p1c2u/fix/b64decode-issue29427-fix
b64decode issue29427 fix
2 parents c27cd89 + 05148b8 commit 4815905

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

openapi_core/security/providers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import base64
21
import binascii
32
import warnings
43

54
from openapi_core.security.exceptions import SecurityError
5+
from openapi_core.security.util import b64decode
66

77

88
class BaseProvider(object):
@@ -41,7 +41,6 @@ def __call__(self, request):
4141
raise SecurityError(
4242
'Unknown authorization method %s' % auth_type)
4343
try:
44-
return base64.b64decode(
45-
encoded_credentials.encode('ascii')).decode('latin1')
44+
return b64decode(encoded_credentials).decode('latin1')
4645
except binascii.Error:
4746
raise SecurityError('Invalid base64 encoding.')

openapi_core/security/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from base64 import urlsafe_b64decode
2+
3+
4+
def b64decode(s):
5+
# Code from
6+
# https://github.com/GehirnInc/python-jwt/blob/master/jwt/utils.py#L29
7+
s_bin = s.encode('ascii')
8+
s_bin += b'=' * (4 - len(s_bin) % 4)
9+
return urlsafe_b64decode(s_bin)

tests/unit/security/test_providers.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
3+
from openapi_core.schema.security_schemes.models import SecurityScheme
4+
from openapi_core.security.providers import HttpProvider
5+
from openapi_core.testing import MockRequest
6+
7+
8+
class TestHttpProvider(object):
9+
10+
@pytest.fixture
11+
def scheme(self):
12+
return SecurityScheme('http', scheme='bearer')
13+
14+
@pytest.fixture
15+
def provider(self, scheme):
16+
return HttpProvider(scheme)
17+
18+
def test_issue29427(self, provider):
19+
"""Tests HttpProvider against Issue29427
20+
https://bugs.python.org/issue29427
21+
"""
22+
jwt = 'MQ'
23+
headers = {
24+
'Authorization': 'Bearer {0}'.format(jwt),
25+
}
26+
request = MockRequest(
27+
'http://localhost', 'GET', '/pets',
28+
headers=headers,
29+
)
30+
31+
result = provider(request)
32+
33+
assert result == '1'

0 commit comments

Comments
 (0)