This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from bartfeenstra/silver
Silver
- Loading branch information
Showing
10 changed files
with
274 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
asn1crypto==0.23.0 | ||
certifi==2017.11.5 | ||
cffi==1.11.2 | ||
chardet==3.0.4 | ||
click==6.7 | ||
cryptography==2.1.3 | ||
Flask==0.12.2 | ||
Flask-HTTPAuth==3.2.3 | ||
idna==2.6 | ||
itsdangerous==0.24 | ||
Jinja2==2.10 | ||
jwcrypto==0.4.2 | ||
MarkupSafe==1.0 | ||
pycparser==2.18 | ||
python-jwt==3.0.0 | ||
requests==2.18.4 | ||
requests-futures==0.9.7 | ||
six==1.11.0 | ||
urllib3==1.22 | ||
Werkzeug==0.12.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
flask ~= 0.12.2 | ||
flask-httpauth ~= 3.2.3 | ||
python_jwt ~= 3.0.0 | ||
requests ~= 2.18.4 | ||
requests-futures ~= 0.9.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import datetime | ||
|
||
import jwcrypto.jwk as jwk | ||
import python_jwt as jwt | ||
|
||
|
||
class Auth: | ||
def __init__(self, secret_key, ttl): | ||
assert secret_key is not None | ||
jwt_key = jwk.JWK.generate(kty='oct', k=secret_key) | ||
# A little assurance that we will always enforce a secure signature. If | ||
# the key is None, the JWT signature algorithm will be None, leading to | ||
# unsigned and therefore unauthenticated tokens. For more details, see | ||
# https://www.chosenplaintext.ca/2015/03/31/jwt-algorithm-confusion.html. | ||
assert jwt_key is not None | ||
self._jwt_key = jwt_key | ||
self._algorithm = 'HS512' | ||
self._ttl = ttl | ||
|
||
def grant_access_token(self): | ||
return jwt.generate_jwt({}, self._jwt_key, self._algorithm, | ||
datetime.timedelta(seconds=self._ttl)) | ||
|
||
def verify_access_token(self, access_token): | ||
try: | ||
jwt.verify_jwt(access_token, self._jwt_key, | ||
[self._algorithm]) | ||
# This is an overly broad exception clause, because: | ||
# 1) the JWT library does not raise exceptions of a single type. | ||
# 2) calling code will convert this to an appropriate HTTP response. | ||
except Exception as e: | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from time import sleep | ||
from unittest import TestCase | ||
|
||
from tk.auth import Auth | ||
|
||
|
||
class AuthTest(TestCase): | ||
def testGrantShouldProduceAccessToken(self): | ||
auth = Auth('foo', 9) | ||
self.assertRegex(auth.grant_access_token(), | ||
'^[^.]+\.[^.]+\.[^.]+$') | ||
|
||
def testVerification(self): | ||
auth = Auth('foo', 9) | ||
self.assertTrue(auth.verify_access_token(auth.grant_access_token())) | ||
|
||
def testVerifyExpiredToken(self): | ||
auth = Auth('foo', 1) | ||
token = auth.grant_access_token() | ||
sleep(2) | ||
self.assertFalse(auth.verify_access_token(token)) | ||
|
||
def testVerifyInvalidSignature(self): | ||
auth_a = Auth('foo', 9) | ||
auth_b = Auth('bar', 9) | ||
self.assertFalse(auth_b.verify_access_token( | ||
auth_a.grant_access_token())) |
Oops, something went wrong.