Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion aiohttp_jwtplus/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def check_if_request_in_whitelist(path , whitelist):
async def _router(request, handler):
try:

if request.method.lower() == 'options': # to enable CORS
return await handler(request)
if self._whitelist:
if check_if_request_in_whitelist(request.path , self._whitelist):
return await handler(request)
Expand Down Expand Up @@ -136,4 +138,4 @@ def test_behav(self):
'''
more information in test_behav.py
'''
pass
pass
22 changes: 12 additions & 10 deletions aiohttp_jwtplus/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
class SecretManager:

def __init__(
self ,
secret:str = "" ,
refresh_interval = 0 ,
scheme = 'Bearer',
algorithm = 'HS256' ,
exptime = '30d'
self,
secret:str="",
refresh_interval=0,
scheme='Bearer',
algorithm='HS256',
exptime='30d',
audience:str=None
):
if not isinstance(secret , str):
if not isinstance(secret, str):
raise TypeError("Secret should be a string.")

self.algorithm = algorithm
Expand All @@ -30,6 +31,7 @@ def __init__(
self._exptime = self._parse_time(exptime)
self.scheme = scheme
self._scheme_bytes = self.scheme.encode('utf-8')
self._audience = audience

def _parse_time(self, interval):

Expand Down Expand Up @@ -65,8 +67,8 @@ def _random_token(self , n):
rc = lambda :chr(random.randint(0,74) + 48)
return ''.join((rc() for i in range(n)))

def decode(self , *args , **kwargs):
return pyjwt.decode( algorithms = [self.algorithm,] ,*args , **kwargs)
def decode(self, *args, **kwargs):
return pyjwt.decode(algorithms=[self.algorithm], audience=self._audience, *args, **kwargs)

def encode(self , payload , *args , **kwargs):
if not isinstance(payload , dict):
Expand Down Expand Up @@ -101,4 +103,4 @@ async def _remove_in_time(time , tk):
await asyncio.sleep(self._refresh_interval)
new_tk = self._random_token(self._secret_length)
self._secrets.append(new_tk)
loop.create_task(_remove_in_time(self._exptime * 2 , new_tk))
loop.create_task(_remove_in_time(self._exptime * 2 , new_tk))
8 changes: 4 additions & 4 deletions aiohttp_jwtplus/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ async def basic_token_getter(request):
return web.HTTPForbidden()

async def basic_identifier(payload):
if 'username' in payload:
ret = payload['username']
if 'sub' in payload:
ret = payload['sub'] # https://en.wikipedia.org/wiki/JSON_Web_Token
if ret:
return {'username' : ret , 'full_jwt_payload':payload}
return {'sub' : ret , 'full_jwt_payload':payload}
else:
return False

Expand All @@ -26,4 +26,4 @@ def show_request_info(request):
print(f"\trequest path :\t{request.path}")
print(f"\tauthen status :\t{request['auth_status']}")
print(f"\tcarry words :\t{request['auth_carry']}")
print("\t\t\t*/")
print("\t\t\t*/")
13 changes: 8 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# requires for 'bs4' and 'lxml' packages

from setuptools import setup, find_packages
from requests import get as rget
from bs4 import BeautifulSoup
import logging , sys
import logging, sys

VERSION = '0.2.1' # Setup script should work for any branch, not for releases. Usually, the version in repo is the lattest and not released

# init logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand All @@ -18,7 +23,6 @@ def get_install_requires(filename):

#
url = 'https://github.com/GoodManWEN/aiohttp-jwtplus'
release = f'{url}/releases/latest'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Connection": "keep-alive",
Expand All @@ -31,12 +35,11 @@ def get_install_requires(filename):
for kw in (' - GitHub', ' - GoodManWEN'):
if ' - GitHub' in description:
description = description[:description.index(' - GitHub')]
html = BeautifulSoup(rget(release , headers).text ,'lxml')
version = html.find('div',{'class':'release-header'}).find('a').text
version = VERSION
logger.info(f"description: {description}")
logger.info(f"version: {version}")

#
#version
with open('README.md','r',encoding='utf-8') as f:
long_description_lines = f.readlines()

Expand Down