Skip to content
Merged
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: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Then modify settings.py:
...
],

# You can also populate the JWT fields by configuring your own callable. The
# callable should return a dictionary. The function should optionally accept user.
'CALLABLE': 'some.module.with.a.function',

# KEY can also be a tuple in order to specify public and private keys.
'KEY': 'string value or path to PEM key file',

Expand Down
2 changes: 2 additions & 0 deletions django_session_jwt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def get_fields(user=None):
return { 'foo': 'bar' }
14 changes: 14 additions & 0 deletions django_session_jwt/middleware/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,21 @@ def _parse_fields(fields):
return fields


def _parse_callable(f):
if not f:
return

module_name, _, f_name = f.rpartition('.')
m = import_module(module_name)

return getattr(m, f_name)


DJANGO_SESSION_JWT = getattr(settings, 'DJANGO_SESSION_JWT', {})
SESSION_FIELD = DJANGO_SESSION_JWT.get('SESSION_FIELD', 'sk')
KEY, PUBKEY, ALGO = _parse_key(DJANGO_SESSION_JWT.get('KEY', settings.SECRET_KEY))
FIELDS = _parse_fields(DJANGO_SESSION_JWT.get('FIELDS', []))
CALLABLE = _parse_callable(DJANGO_SESSION_JWT.get('CALLABLE'))
EXPIRES = DJANGO_SESSION_JWT.get('EXPIRES', None)
LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(logging.NullHandler())
Expand Down Expand Up @@ -134,6 +145,9 @@ def create_jwt(user, session_key, expires=None):
LOGGER.warning('Could not get missing field %s from user', attrname)
continue

if CALLABLE:
fields.update(CALLABLE(user))

return jwt.encode(fields, KEY, algorithm=ALGO).decode('utf8')


Expand Down
1 change: 1 addition & 0 deletions django_session_jwt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
('username', 'u', 'username'),
('email', 'e', 'email'),
],
'CALLABLE': 'django_session_jwt.get_fields',
'KEY': SECRET_KEY,
# The default, uncomment to override:
# 'SESSION_FIELD': 'sk',
Expand Down
2 changes: 2 additions & 0 deletions django_session_jwt/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def test_login(self):
self.assertTrue('username' in fields) # long form
self.assertTrue('e' in fields) # short form
self.assertTrue('email' in fields) # long form
self.assertTrue('foo' in fields) # from callable

def test_session(self):
"Test persisting session data"
Expand Down Expand Up @@ -114,3 +115,4 @@ def test_login(self):
self.assertTrue('username' in fields) # long form
self.assertTrue('e' in fields) # short form
self.assertTrue('email' in fields) # long form
self.assertTrue('foo' in fields) # from callable