This repository has been archived by the owner on Apr 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #26 from plone/auth-rework2
Implement basic jwt validator
- Loading branch information
Showing
11 changed files
with
128 additions
and
64 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
This file was deleted.
Oops, something went wrong.
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,70 @@ | ||
from plone.server.utils import strings_differ | ||
from plone.server import app_settings | ||
from plone.server.auth import find_user | ||
import jwt | ||
|
||
import hashlib | ||
import uuid | ||
|
||
|
||
class BaseValidator(object): | ||
def __init__(self, request): | ||
self.request = request | ||
|
||
|
||
def hash_password(password, salt=None): | ||
if salt is None: | ||
salt = uuid.uuid4().hex | ||
|
||
if isinstance(salt, str): | ||
salt = salt.encode('utf-8') | ||
|
||
if isinstance(password, str): | ||
password = password.encode('utf-8') | ||
|
||
hashed_password = hashlib.sha512(password + salt).hexdigest() | ||
return '{}:{}'.format(salt.decode('utf-8'), hashed_password) | ||
|
||
|
||
class SaltedHashPasswordValidator(object): | ||
|
||
def __init__(self, request): | ||
self.request = request | ||
|
||
async def validate(self, token): | ||
user = await find_user(self.request, token) | ||
user_pw = getattr(user, 'password', None) | ||
if (not user_pw or | ||
':' not in user_pw or | ||
'token' not in token): | ||
return False | ||
salt = user.password.split(':')[0] | ||
if not strings_differ(hash_password(token['token'], salt), user_pw): | ||
return user | ||
|
||
|
||
class JWTValidator(object): | ||
def __init__(self, request): | ||
self.request = request | ||
|
||
async def validate(self, token): | ||
if token.get('type') != 'bearer': | ||
return False | ||
|
||
if '.' not in token.get('token', ''): | ||
# quick way to check if actually might be jwt | ||
return False | ||
|
||
try: | ||
validated_jwt = jwt.decode( | ||
token['token'], | ||
app_settings['jwt']['secret'], | ||
algorithms=[app_settings['jwt']['algorithm']]) | ||
token['id'] = validated_jwt['id'] | ||
user = await find_user(self.request, token) | ||
if user and user.id == token['id']: | ||
return user | ||
except jwt.exceptions.DecodeError: | ||
pass | ||
|
||
return False |
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,23 @@ | ||
# -*- coding: utf-8 -*- | ||
from datetime import datetime | ||
from datetime import timedelta | ||
from plone.server import app_settings | ||
from plone.server.testing import PloneFunctionalTestCase | ||
|
||
import jwt | ||
|
||
|
||
class TestAuth(PloneFunctionalTestCase): | ||
|
||
def test_jwt_auth(self): | ||
from plone.server.auth.participation import ROOT_USER_ID | ||
jwt_token = jwt.encode({ | ||
'exp': datetime.utcnow() + timedelta(seconds=60), | ||
'id': ROOT_USER_ID | ||
}, app_settings['jwt']['secret']).decode('utf-8') | ||
|
||
resp = self.layer.requester( | ||
'GET', '/plone/plone/@addons', | ||
token=jwt_token | ||
) | ||
assert resp.status_code == 200 |
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 |
---|---|---|
|
@@ -62,6 +62,7 @@ | |
'zope.schema', | ||
'zope.security', | ||
'zope.securitypolicy', | ||
'pyjwt' | ||
], | ||
extras_require={ | ||
'test': [ | ||
|