Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Merge djmitche/build-relengapi:issue236 (PR #288)
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Jul 9, 2015
2 parents 43761e9 + 5772942 commit b31720b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
12 changes: 10 additions & 2 deletions relengapi/blueprints/tokenauth/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from relengapi.blueprints.tokenauth import tables
from relengapi.blueprints.tokenauth import tokenstr
from relengapi.lib import auth
from werkzeug.exceptions import BadRequest

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -71,10 +72,17 @@ def __call__(self, request):
# see https://github.com/mozilla/build-relengapi/pull/192/files
logger.warning("client is using 'Authentication' header instead of "
"'Authorization'")

# If we've gotten this far, then we'll either authenticate the request
# or fail with 400 Bad Request. This is less confusing for users than
# succeeding with no permissions.
header = header.split()
if len(header) != 2 or header[0].lower() != 'bearer':
return
return self.from_str(header[1])
raise BadRequest("Invalid Authorization header")
user = self.from_str(header[1])
if not user:
raise BadRequest("Invalid Authorization header")
return user

def from_str(self, token_str):
claims = tokenstr.str_to_claims(token_str)
Expand Down
40 changes: 19 additions & 21 deletions relengapi/blueprints/tokenauth/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ def test_loader_no_header(app, client):

@test_context
def test_loader_not_bearer(app, client):
"""With an Authorization header that does not start with 'Bearer', no
permissions are allowed"""
auth = json.loads(
client.get('/test_tokenauth',
headers=[('Authorization', 'Penguiner TOK/1/v1')]).data)
eq_(auth['permissions'], [])
"""With an Authorization header that does not start with 'Bearer', the
request is denied as a bad request"""
resp = client.get('/test_tokenauth',
headers=[('Authorization', 'Penguiner TOK/1/v1')])
eq_(resp.status_code, 400)


@test_context.specialize(db_setup=insert_prm)
Expand Down Expand Up @@ -96,29 +95,28 @@ def test_from_str_bad_type(app):

@test_context
def test_loader_bad_header(app, client):
"""With a bad Authorization header, no permissions are allowed"""
auth = json.loads(
client.get('/test_tokenauth',
headers=[('Authorization', 'Bearer xxxxx')]).data)
eq_(auth['permissions'], [])
"""With a bad Authorization header, the request is denied as a bad
request"""
resp = client.get('/test_tokenauth',
headers=[('Authorization', 'Bearer xxxxx')])
eq_(resp.status_code, 400)


@test_context
def test_loader_malformed_header(app, client):
"""With a malformed Authorization header, no permissions are allowed"""
auth = json.loads(
client.get('/test_tokenauth',
headers=[('Authorization', 'no-space-ma')]).data)
eq_(auth['permissions'], [])
"""With a malformed Authorization header, the request is denied as a bad request"""
resp = client.get('/test_tokenauth',
headers=[('Authorization', 'no-space-ma')])
eq_(resp.status_code, 400)


@test_context.specialize(db_setup=insert_prm)
def test_loader_good_header_not_in_db(app, client):
"""With a good Authorization header but no row in the DB, no permissions are allowed"""
auth = json.loads(
client.get('/test_tokenauth',
headers=[('Authorization', 'Bearer TOK/2/v1')]).data)
eq_(auth['permissions'], [])
"""With a good Authorization header but no row in the DB, the request is
denied as a bad request"""
resp = client.get('/test_tokenauth',
headers=[('Authorization', 'Bearer TOK/2/v1')])
eq_(resp.status_code, 400)


@test_context.specialize(db_setup=insert_prm)
Expand Down

0 comments on commit b31720b

Please sign in to comment.