Эх сурвалжийг харах

[jwt] Decode JWT in custom auth backend for py2 also (#2877)

Harsh Gupta 3 жил өмнө
parent
commit
90713cea6d

+ 18 - 11
desktop/core/src/desktop/auth/api_authentications.py

@@ -19,6 +19,7 @@ import logging
 import requests
 import requests
 import jwt
 import jwt
 import json
 import json
+import sys
 
 
 from cryptography.hazmat.primitives import serialization
 from cryptography.hazmat.primitives import serialization
 from rest_framework import authentication, exceptions
 from rest_framework import authentication, exceptions
@@ -52,22 +53,28 @@ class JwtAuthentication(authentication.BaseAuthentication):
 
 
     LOG.debug('JwtAuthentication: got access token from %s: %s' % (request.path, access_token))
     LOG.debug('JwtAuthentication: got access token from %s: %s' % (request.path, access_token))
 
 
-    decode_options = {
-      'verify_signature': AUTH.JWT.VERIFY.get(),
-    }
     public_key_pem = ''
     public_key_pem = ''
     if AUTH.JWT.VERIFY.get():
     if AUTH.JWT.VERIFY.get():
       public_key_pem = self._handle_public_key(access_token)
       public_key_pem = self._handle_public_key(access_token)
 
 
+    params = {
+      'jwt': access_token,
+      'key': public_key_pem,
+      'issuer': AUTH.JWT.ISSUER.get(),
+      'audience': AUTH.JWT.AUDIENCE.get(),
+      'algorithms': ["RS256"]
+    }
+
+    if sys.version_info[0] > 2:
+      params['options'] = {
+        'verify_signature': AUTH.JWT.VERIFY.get()
+      }
+    else:
+      params['verify'] = AUTH.JWT.VERIFY.get()
+
     try:
     try:
-      payload = jwt.decode(
-        access_token,
-        public_key_pem,
-        issuer=AUTH.JWT.ISSUER.get(),
-        audience=AUTH.JWT.AUDIENCE.get(),
-        algorithms=["RS256"],
-        options=decode_options
-      )
+      payload = jwt.decode(**params)
+
     except jwt.DecodeError:
     except jwt.DecodeError:
       LOG.error('JwtAuthentication: Invalid token')
       LOG.error('JwtAuthentication: Invalid token')
       raise exceptions.AuthenticationFailed('JwtAuthentication: Invalid token')
       raise exceptions.AuthenticationFailed('JwtAuthentication: Invalid token')

+ 5 - 5
desktop/core/src/desktop/auth/api_authentications_tests.py

@@ -193,8 +193,11 @@ class TestJwtAuthentication():
           user, token = JwtAuthentication().authenticate(request=self.request)
           user, token = JwtAuthentication().authenticate(request=self.request)
 
 
           jwt_decode.assert_called_with(
           jwt_decode.assert_called_with(
-            self.sample_token,
-            b'-----BEGIN PUBLIC KEY-----\n'
+            algorithms=['RS256'],
+            audience=AUTH.JWT.AUDIENCE.get(),
+            issuer=AUTH.JWT.ISSUER.get(),
+            jwt=self.sample_token,
+            key=b'-----BEGIN PUBLIC KEY-----\n'
             b'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArtT3gR0NDIx6gv8xYLiP\n'
             b'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArtT3gR0NDIx6gv8xYLiP\n'
             b'ue/ItaIbognCGGgQbipp3IOuobu2RnJjedsIRBTEOdkVx+xjV6m92VYtrpW6gM9v\n'
             b'ue/ItaIbognCGGgQbipp3IOuobu2RnJjedsIRBTEOdkVx+xjV6m92VYtrpW6gM9v\n'
             b'ldwTfI0UmoSLGKT5uYd0JGHvYWoN9inCZYZcnala58T8HDgLiXa9KlEuQxGGQDem\n'
             b'ldwTfI0UmoSLGKT5uYd0JGHvYWoN9inCZYZcnala58T8HDgLiXa9KlEuQxGGQDem\n'
@@ -203,9 +206,6 @@ class TestJwtAuthentication():
             b'Vno2e527clXzQisfJKwb4hjfKRMhHfnYfyJxaoHqWfx8DjXmH3CMqlWr/+hL3y1+\n'
             b'Vno2e527clXzQisfJKwb4hjfKRMhHfnYfyJxaoHqWfx8DjXmH3CMqlWr/+hL3y1+\n'
             b'4QIDAQAB\n'
             b'4QIDAQAB\n'
             b'-----END PUBLIC KEY-----\n',
             b'-----END PUBLIC KEY-----\n',
-            issuer=AUTH.JWT.ISSUER.get(),
-            audience=AUTH.JWT.AUDIENCE.get(),
-            algorithms=['RS256'],
             options={'verify_signature': True}
             options={'verify_signature': True}
           )
           )
           assert_equal(user, self.user)
           assert_equal(user, self.user)