Skip to content

Commit

Permalink
Some code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Jul 19, 2019
1 parent a7e1bec commit 91e2928
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions restfulpy/application/cli/jwttoken.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ujson
from easycli import SubCommand, Argument

from restfulpy.principal import JwtPrincipal
from restfulpy.principal import JWTPrincipal


class CreateJWTTokenSubSubCommand(SubCommand):
Expand All @@ -26,7 +26,7 @@ class CreateJWTTokenSubSubCommand(SubCommand):

def __call__(self, args):
payload = ujson.loads(args.payload)
print(JwtPrincipal(payload).dump(args.expire_in).decode())
print(JWTPrincipal(payload).dump(args.expire_in).decode())


class JWTSubCommand(SubCommand):
Expand Down
6 changes: 3 additions & 3 deletions restfulpy/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import user_agents
from nanohttp import context, HTTPBadRequest, settings, HTTPUnauthorized

from restfulpy.principal import JwtPrincipal, JwtRefreshToken
from restfulpy.principal import JWTPrincipal, JWTRefreshToken


class Authenticator:
Expand Down Expand Up @@ -108,7 +108,7 @@ def try_refresh_token(self, session_id):
refresh_token_encoded = morsel.value
# Decoding the refresh token
try:
refresh_principal = JwtRefreshToken.load(refresh_token_encoded)
refresh_principal = JWTRefreshToken.load(refresh_token_encoded)
self.ok(
self.create_principal(
member_id=refresh_principal.id,
Expand Down Expand Up @@ -146,7 +146,7 @@ def ok(self, principal, setup_header=False):
self.setup_response_headers(principal)

def verify_token(self, encoded_token):
return JwtPrincipal.load(encoded_token)
return JWTPrincipal.load(encoded_token)

def authenticate_request(self):
if self.token_key not in context.environ:
Expand Down
8 changes: 4 additions & 4 deletions restfulpy/principal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from nanohttp import settings, context, HTTPForbidden


class BaseJwtPrincipal:
class BaseJWTPrincipal:
def __init__(self, payload):
self.payload = payload

Expand Down Expand Up @@ -38,7 +38,7 @@ def get_config(cls):
raise NotImplementedError()


class JwtPrincipal(BaseJwtPrincipal):
class JWTPrincipal(BaseJWTPrincipal):
def is_in_roles(self, *roles):
if 'roles' in self.payload:
if set(self.payload['roles']).intersection(roles):
Expand Down Expand Up @@ -81,7 +81,7 @@ def get_config(cls):
return settings.jwt


class JwtRefreshToken:
class JWTRefreshToken:
def __init__(self, payload):
self.payload = payload

Expand All @@ -106,7 +106,7 @@ def id(self):
return self.payload.get('id')


class DummyIdentity(JwtPrincipal):
class DummyIdentity(JWTPrincipal):
def __init__(self, *roles):
super().__init__({'roles': list(roles)})

Expand Down
2 changes: 1 addition & 1 deletion restfulpy/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def create_session(cls, *a, expire_on_commit=False, **kw):
@property
def _session(self):
if self.__session is None:
self.__session == self.create_session()
self.__session = self.create_session()

return self.__session

Expand Down
2 changes: 1 addition & 1 deletion tests/test_appcli_mule.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class WorkerTask(MuleTask):

__mapper_args__ = {
'polymorphic_identity': 'mule_task'
'polymorphic_identity': 'worker_task'
}

def do_(self, context):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from restfulpy.mockup import MockupApplication
from restfulpy.authentication import Authenticator
from restfulpy.authorization import authorize
from restfulpy.principal import JwtPrincipal, JwtRefreshToken
from restfulpy.principal import JWTPrincipal, JWTRefreshToken
from restfulpy.testing import ApplicableTestCase


Expand All @@ -25,12 +25,12 @@ def validate_credentials(self, credentials):
return MockupMember(id=1, email=email, roles=['admin', 'test'])

def create_refresh_principal(self, member_id=None):
return JwtRefreshToken(dict(
return JWTRefreshToken(dict(
id=member_id
))

def create_principal(self, member_id=None, session_id=None):
return JwtPrincipal(dict(
return JWTPrincipal(dict(
id=1,
email='test@example.com',
roles=['admin', 'test'],
Expand Down
10 changes: 5 additions & 5 deletions tests/test_principal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from nanohttp import configure, settings

from restfulpy.principal import JwtPrincipal
from restfulpy.principal import JWTPrincipal


def test_principal():
Expand All @@ -17,7 +17,7 @@ def test_principal():
configure(force=True)
settings.merge(__configuration__)

principal = JwtPrincipal(dict(
principal = JWTPrincipal(dict(
email='test@example.com',
id=1,
sessionId=1,
Expand All @@ -33,15 +33,15 @@ def test_principal():

encoded = principal.dump()

principal = JwtPrincipal.load(encoded.decode())
principal = JWTPrincipal.load(encoded.decode())
assert principal.email == 'test@example.com'
assert principal.id == 1
assert principal.session_id == 1
assert principal.roles == ['admin']
assert principal.is_in_roles('admin') is True
assert principal.is_in_roles('admin', 'god') is True

principal = JwtPrincipal.load(encoded.decode(), force=True)
principal = JWTPrincipal.load(encoded.decode(), force=True)
assert principal.email == 'test@example.com'
assert principal.id == 1
assert principal.session_id == 1
Expand All @@ -50,7 +50,7 @@ def test_principal():
assert principal.is_in_roles('admin', 'god') is True

principal =\
JwtPrincipal.load((b'Bearer %s' % encoded).decode(), force=True)
JWTPrincipal.load((b'Bearer %s' % encoded).decode(), force=True)
assert principal.email == 'test@example.com'
assert principal.id == 1
assert principal.session_id == 1
Expand Down
6 changes: 3 additions & 3 deletions tests/test_refreshtoken_without_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from restfulpy.mockup import MockupApplication
from restfulpy.authentication import StatefulAuthenticator
from restfulpy.principal import JwtPrincipal, JwtRefreshToken
from restfulpy.principal import JWTPrincipal, JWTRefreshToken
from restfulpy.testing import ApplicableTestCase


Expand All @@ -16,12 +16,12 @@ def validate_credentials(self, credentials):
raise NotImplementedError()

def create_refresh_principal(self, member_id=None):
return JwtRefreshToken(dict(
return JWTRefreshToken(dict(
id=member_id
))

def create_principal(self, member_id=None, session_id=None, **kwargs):
return JwtPrincipal(
return JWTPrincipal(
dict(id=1, email='test@example.com', roles=roles, sessionId='1')
)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_stateful_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from restfulpy.mockup import MockupApplication
from restfulpy.authentication import StatefulAuthenticator
from restfulpy.authorization import authorize
from restfulpy.principal import JwtPrincipal, JwtRefreshToken
from restfulpy.principal import JWTPrincipal, JWTRefreshToken
from restfulpy.testing import ApplicableTestCase


Expand All @@ -27,12 +27,12 @@ def validate_credentials(self, credentials):
return MockupMember(id=1, email=email, roles=['admin', 'test'])

def create_refresh_principal(self, member_id=None):
return JwtRefreshToken(dict(
return JWTRefreshToken(dict(
id=member_id
))

def create_principal(self, member_id=None, session_id=None):
return JwtPrincipal(dict(
return JWTPrincipal(dict(
id=1,
email='test@example.com',
roles=roles,
Expand Down

0 comments on commit 91e2928

Please sign in to comment.