Commit 4f5ff873 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

ERP5Security: code improvments to ERP5JSONWebTokenPlugin

parent c7fbea70
...@@ -33,16 +33,11 @@ from Products.PageTemplates.PageTemplateFile import PageTemplateFile ...@@ -33,16 +33,11 @@ from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Products.PluggableAuthService.interfaces import plugins from Products.PluggableAuthService.interfaces import plugins
from Products.PluggableAuthService.utils import classImplements from Products.PluggableAuthService.utils import classImplements
from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin from Products.PluggableAuthService.plugins.BasePlugin import BasePlugin
from Products.ERP5Security.ERP5UserManager import SUPER_USER, ERP5UserManager from Products.ERP5Security.ERP5UserManager import ERP5UserManager
from Products.PluggableAuthService.permissions import ManageUsers from Products.PluggableAuthService.permissions import ManageUsers
from Products.PluggableAuthService.PluggableAuthService import DumbHTTPExtractor from Products.PluggableAuthService.PluggableAuthService import DumbHTTPExtractor
from AccessControl.SecurityManagement import getSecurityManager, \ from os import urandom
setSecurityManager, newSecurityManager from zLOG import LOG, INFO
from Products.ERP5Type.Cache import DEFAULT_CACHE_SCOPE
from os import urandom as SystemRandom
import socket
from Products.ERP5Security.ERP5UserManager import getUserByLogin
from zLOG import LOG, ERROR, INFO
try: try:
from itsdangerous import JSONWebSignatureSerializer, BadSignature from itsdangerous import JSONWebSignatureSerializer, BadSignature
...@@ -74,8 +69,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -74,8 +69,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
security = ClassSecurityInfo() security = ClassSecurityInfo()
name_cookie = "n_jwt" name_cookie = "n_jwt"
data_cookie = "d_jwt" data_cookie = "d_jwt"
default_cookie_name = "__ac"
manage_options = ( ( { 'label': 'Update Secret', manage_options = ( ( { 'label': 'Update Secret',
'action': 'manage_updateERP5JSONWebTokenPluginForm', } 'action': 'manage_updateERP5JSONWebTokenPluginForm', }
, ,
...@@ -88,7 +82,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -88,7 +82,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
#Register value #Register value
self._setId(id) self._setId(id)
self.title = title self.title = title
self.secret = SystemRandom(256) self._secret = self.manage_updateERP5JSONWebTokenPlugin()
self.erp5usermanager = ERP5UserManager(self.getId() + "_user_manager") self.erp5usermanager = ERP5UserManager(self.getId() + "_user_manager")
#################################### ####################################
...@@ -97,7 +91,6 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -97,7 +91,6 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
security.declarePrivate('extractCredentials') security.declarePrivate('extractCredentials')
def extractCredentials(self, request): def extractCredentials(self, request):
""" Extract JWT from the request header. """ """ Extract JWT from the request header. """
if JSONWebSignatureSerializer is None: if JSONWebSignatureSerializer is None:
LOG('ERP5JSONWebTokenPlugin', INFO, LOG('ERP5JSONWebTokenPlugin', INFO,
'No itsdangerous module, install itsdangerous package. ' 'No itsdangerous module, install itsdangerous package. '
...@@ -106,7 +99,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -106,7 +99,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
login_pw = request._authUserPW() login_pw = request._authUserPW()
name_serializer = JSONWebSignatureSerializer(self.secret) name_serializer = JSONWebSignatureSerializer(self._secret)
creds = {} creds = {}
if login_pw is not None: if login_pw is not None:
...@@ -116,7 +109,6 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -116,7 +109,6 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
else: else:
name_token = None name_token = None
if self.name_cookie in request.cookies: if self.name_cookie in request.cookies:
# 1st - try to fetch from Authorization header
name_token = request.cookies.get(self.name_cookie) name_token = request.cookies.get(self.name_cookie)
if name_token is None: if name_token is None:
...@@ -137,7 +129,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -137,7 +129,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
# no name_token # no name_token
return None return None
data_serializer = JSONWebSignatureSerializer(self.secret + user.getPassword()) data_serializer = JSONWebSignatureSerializer(self._secret + user.getPassword())
data = data_serializer.loads(data_token) data = data_serializer.loads(data_token)
data_okay = self.getPortalObject().ERP5Site_processJWTData(data) data_okay = self.getPortalObject().ERP5Site_processJWTData(data)
if data_okay: if data_okay:
...@@ -171,8 +163,8 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -171,8 +163,8 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
return authentication_result return authentication_result
name = authentication_result[0] name = authentication_result[0]
user = self.erp5usermanager.getUserByLogin(name)[0] user = self.erp5usermanager.getUserByLogin(name)[0]
name_serializer = JSONWebSignatureSerializer(self.secret) name_serializer = JSONWebSignatureSerializer(self._secret)
data_serializer = JSONWebSignatureSerializer(self.secret + user.getPassword()) data_serializer = JSONWebSignatureSerializer(self._secret + user.getPassword())
request = self.REQUEST request = self.REQUEST
response = request.response response = request.response
...@@ -200,7 +192,6 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -200,7 +192,6 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
secure=True, secure=True,
http_only=True, http_only=True,
) )
response.expireCookie(self.default_cookie_name, path='/')
return authentication_result return authentication_result
...@@ -219,7 +210,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin): ...@@ -219,7 +210,7 @@ class ERP5JSONWebTokenPlugin(BasePlugin):
"""Edit the object""" """Edit the object"""
#Save user_id_key #Save user_id_key
self.secret = SystemRandom(256) self._secret = urandom(256)
#Redirect #Redirect
if RESPONSE is not None: if RESPONSE is not None:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment