-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.py
32 lines (27 loc) · 1.15 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
Contains the MiddleWare Class for Edly Discussion
"""
import jwt
from django.conf import settings as django_settings
class UserSessionSharingMiddleware(object):
"""
Middleware to set jwt login token on sign in
Used for session sharing with Edly Discussion
"""
def process_response(self, request, response):
try:
if request.user.is_authenticated():
user_data = {
'id': request.user.id,
'username': request.user.username,
'email': request.user.email
}
jwt_secret = django_settings.EDLY_DISCUSSION_SECRETS['DISCUSSION_JWT_SECRET']
jwt_algorithm = django_settings.EDLY_DISCUSSION_SECRETS['DISCUSSION_JWT_ALGORITHM']
encoded_jwt = jwt.encode(user_data, jwt_secret, jwt_algorithm)
response.set_cookie('token', encoded_jwt, domain=django_settings.EDLY_DISCUSSION_SETTINGS['DOMAIN'])
else:
response.delete_cookie('token', domain=django_settings.EDLY_DISCUSSION_SETTINGS['DOMAIN'])
except AttributeError:
pass
return response