From a0cba1aee181ac9d23cb8967a006b9daccec1ea4 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Fri, 21 Apr 2017 22:36:02 -0700 Subject: [PATCH] accept token directly in auth header --- sanic/request.py | 7 ++++--- tests/test_requests.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index 4a15c22f..31b6a08f 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -78,9 +78,10 @@ class Request(dict): :return: token related to request """ auth_header = self.headers.get('Authorization') - if auth_header is not None: - return auth_header.split()[1] - return auth_header + if 'Token ' in auth_header: + return auth_header.partition('Token ')[-1] + else: + return auth_header @property def form(self): diff --git a/tests/test_requests.py b/tests/test_requests.py index 7b453fc1..c0824850 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -141,6 +141,16 @@ def test_token(): return text('OK') # uuid4 generated token. + token = 'a1d895e0-553a-421a-8e22-5ff8ecb48cbf' + headers = { + 'content-type': 'application/json', + 'Authorization': '{}'.format(token) + } + + request, response = app.test_client.get('/', headers=headers) + + assert request.token == token + token = 'a1d895e0-553a-421a-8e22-5ff8ecb48cbf' headers = { 'content-type': 'application/json', @@ -151,6 +161,18 @@ def test_token(): assert request.token == token + token = 'a1d895e0-553a-421a-8e22-5ff8ecb48cbf' + headers = { + 'content-type': 'application/json', + 'Authorization': 'Bearer Token {}'.format(token) + } + + request, response = app.test_client.get('/', headers=headers) + + assert request.token == token + + + # ------------------------------------------------------------ # # POST # ------------------------------------------------------------ #