Skip to content

Commit

Permalink
Extract token validation in tests into function assert_access_token()
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Dec 8, 2015
1 parent c2d0e36 commit 19e49d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
19 changes: 19 additions & 0 deletions integration/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ def __init__(self, status, error_code, error_desc):
})


def assert_access_token(request, access_token):
header = get_authorization_header(request)
method, token = header.split(' ')

if method != 'Bearer':
raise OAuthError(400, 'invalid_request', "Invalid authorization method: %s" % method)

if token != access_token:
raise OAuthError(401, 'invalid_token', "Invalid access token: %s" % token)


def free_tcp_port():
sock = socket()
try:
Expand All @@ -22,6 +33,14 @@ def free_tcp_port():
sock.close()


def get_authorization_header(request):
auth = request.headers.get('Authorization')
if auth:
return auth
raise OAuthError(401, 'unauthorized',
'Full authentication is required to access this resource')


def merge_dicts(*dicts):
result = {}
for d in dicts:
Expand Down
24 changes: 3 additions & 21 deletions integration/support/oaas_mock.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from base64 import b64decode
from urllib.parse import urlencode

from . import OAuthError
from bottle import Bottle, ConfigDict, HTTPError, LocalRequest, abort, redirect
from . import OAuthError, assert_access_token, get_authorization_header
from bottle import Bottle, ConfigDict, LocalRequest, abort, redirect

__all__ = ['OAuthServerMock']

Expand Down Expand Up @@ -85,11 +85,7 @@ def handle_refresh_token():

@app.get('/userinfo')
def get_userinfo():
auth = get_authorization_header(request)
token = parse_token_auth(auth)

if token != conf.access_token:
raise OAuthError(401, 'invalid_token', "Invalid access token: %s" % token)
assert_access_token(request, conf.access_token)

return {
'username': conf.username
Expand All @@ -107,17 +103,3 @@ def parse_client_auth(header):
method, credentials = header.split(' ')
assert method == 'Basic', 'Expected method Basic'
return tuple(b64decode(credentials).decode().split(':'))


def parse_token_auth(header):
method, token = header.split(' ')
assert method == 'Bearer'
return token


def get_authorization_header(request):
auth = request.headers.get('Authorization')
if auth:
return auth
raise OAuthError(401, 'unauthorized',
'Full authentication is required to access this resource')

0 comments on commit 19e49d7

Please sign in to comment.