Skip to content

Commit

Permalink
Check issuer
Browse files Browse the repository at this point in the history
  • Loading branch information
tymofij committed Dec 2, 2014
1 parent dddb4d7 commit dea3904
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion notesapi/v1/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

logger = logging.getLogger(__name__)

class TokenWrongIssuer(Exception):
pass

class HasAccessToken(BasePermission):
"""
Allow requests having valid ID Token.
Expand All @@ -19,6 +22,8 @@ def has_permission(self, request, view):
try:
data = jwt.decode(token, settings.CLIENT_SECRET)
auth_user = data['sub']
if data['aud'] != settings.CLIENT_ID:
raise TokenWrongIssuer
for request_field in ('GET', 'POST', 'DATA'):
if 'user' in getattr(request, request_field):
req_user = getattr(request, request_field)['user']
Expand All @@ -30,9 +35,12 @@ def has_permission(self, request, view):
))
return False
logger.info("No user was present to compare in GET, POST or DATA")
# but user still has valid token, so let them pass
# but user still has valid token, and request is not user-specific so let them pass?
# have to make sure they will not be able to grab ALL data with that...
return True
except jwt.ExpiredSignature:
logger.exception("Token was expired: {}".format(token))
except jwt.DecodeError:
logger.exception("Could not decode token {}".format(token))
except TokenWrongIssuer:
logger.exception("Token has wrong issuer {}".format(token))
1 change: 1 addition & 0 deletions notesapi/v1/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def mock_authorizer(*args, **kwargs):
def get_id_token(user):
now = datetime.utcnow()
return jwt.encode({
'aud': settings.CLIENT_ID,
'sub': user,
'iat': timegm(now.utctimetuple()),
'exp': timegm((now + timedelta(seconds=30)).utctimetuple()),
Expand Down

0 comments on commit dea3904

Please sign in to comment.