@@ -814,3 +814,113 @@ def test_accessing_endpoint_without_jwt(self):
814814 data = json .loads (response .get_data (as_text = True ))
815815 self .assertEqual (status_code , 401 )
816816 self .assertIn ('msg' , data )
817+
818+
819+ # random 1024bit RSA keypair
820+ RSA_PRIVATE = """
821+ -----BEGIN RSA PRIVATE KEY-----
822+ MIICXgIBAAKBgQDN+p9a9oMyqRzkae8yLdJcEK0O0WesH6JiMz+KDrpUwAoAM/KP
823+ DnxFnROJDSBHyHEmPVn5x8GqV5lQ9+6l97jdEEcPo6wkshycM82fgcxOmvtAy4Uo
824+ xq/AeplYqplhcUTGVuo4ZldOLmN8ksGmzhWpsOdT0bkYipHCn5sWZxd21QIDAQAB
825+ AoGBAMJ0++KVXXEDZMpjFDWsOq898xNNMHG3/8ZzmWXN161RC1/7qt/RjhLuYtX9
826+ NV9vZRrzyrDcHAKj5pMhLgUzpColKzvdG2vKCldUs2b0c8HEGmjsmpmgoI1Tdf9D
827+ G1QK+q9pKHlbj/MLr4vZPX6xEwAFeqRKlzL30JPD+O6mOXs1AkEA8UDzfadH1Y+H
828+ bcNN2COvCqzqJMwLNRMXHDmUsjHfR2gtzk6D5dDyEaL+O4FLiQCaNXGWWoDTy/HJ
829+ Clh1Z0+KYwJBANqRtJ+RvdgHMq0Yd45MMyy0ODGr1B3PoRbUK8EdXpyUNMi1g3iJ
830+ tXMbLywNkTfcEXZTlbbkVYwrEl6P2N1r42cCQQDb9UQLBEFSTRJE2RRYQ/CL4yt3
831+ cTGmqkkfyr/v19ii2jEpMBzBo8eQnPL+fdvIhWwT3gQfb+WqxD9v10bzcmnRAkEA
832+ mzTgeHd7wg3KdJRtQYTmyhXn2Y3VAJ5SG+3qbCW466NqoCQVCeFwEh75rmSr/Giv
833+ lcDhDZCzFuf3EWNAcmuMfQJARsWfM6q7v2p6vkYLLJ7+VvIwookkr6wymF5Zgb9d
834+ E6oTM2EeUPSyyrj5IdsU2JCNBH1m3JnUflz8p8/NYCoOZg==
835+ -----END RSA PRIVATE KEY-----
836+ """
837+ RSA_PUBLIC = """
838+ -----BEGIN RSA PUBLIC KEY-----
839+ MIGJAoGBAM36n1r2gzKpHORp7zIt0lwQrQ7RZ6wfomIzP4oOulTACgAz8o8OfEWd
840+ E4kNIEfIcSY9WfnHwapXmVD37qX3uN0QRw+jrCSyHJwzzZ+BzE6a+0DLhSjGr8B6
841+ mViqmWFxRMZW6jhmV04uY3ySwabOFamw51PRuRiKkcKfmxZnF3bVAgMBAAE=
842+ -----END RSA PUBLIC KEY-----
843+ """
844+
845+ class TestEndpointsWithAssymmetricCrypto (unittest .TestCase ):
846+
847+ def setUp (self ):
848+ self .app = Flask (__name__ )
849+ self .app .secret_key = RSA_PRIVATE
850+ self .app .config ['JWT_PUBLIC_KEY' ] = RSA_PUBLIC
851+ self .app .config ['JWT_ALGORITHM' ] = 'RS256'
852+ self .app .config ['JWT_ACCESS_TOKEN_EXPIRES' ] = timedelta (seconds = 1 )
853+ self .app .config ['JWT_REFRESH_TOKEN_EXPIRES' ] = timedelta (seconds = 1 )
854+ self .jwt_manager = JWTManager (self .app )
855+ self .client = self .app .test_client ()
856+
857+ @self .app .route ('/auth/login' , methods = ['POST' ])
858+ def login ():
859+ ret = {
860+ 'access_token' : create_access_token ('test' , fresh = True ),
861+ 'refresh_token' : create_refresh_token ('test' )
862+ }
863+ return jsonify (ret ), 200
864+
865+ @self .app .route ('/auth/refresh' , methods = ['POST' ])
866+ @jwt_refresh_token_required
867+ def refresh ():
868+ username = get_jwt_identity ()
869+ ret = {'access_token' : create_access_token (username , fresh = False )}
870+ return jsonify (ret ), 200
871+
872+ @self .app .route ('/auth/fresh-login' , methods = ['POST' ])
873+ def fresh_login ():
874+ ret = {'access_token' : create_access_token ('test' , fresh = True )}
875+ return jsonify (ret ), 200
876+
877+ @self .app .route ('/protected' )
878+ @jwt_required
879+ def protected ():
880+ return jsonify ({'msg' : "hello world" })
881+
882+ @self .app .route ('/fresh-protected' )
883+ @fresh_jwt_required
884+ def fresh_protected ():
885+ return jsonify ({'msg' : "fresh hello world" })
886+
887+ def _jwt_post (self , url , jwt ):
888+ response = self .client .post (url , content_type = 'application/json' ,
889+ headers = {'Authorization' : 'Bearer {}' .format (jwt )})
890+ status_code = response .status_code
891+ data = json .loads (response .get_data (as_text = True ))
892+ return status_code , data
893+
894+ def _jwt_get (self , url , jwt , header_name = 'Authorization' , header_type = 'Bearer' ):
895+ header_type = '{} {}' .format (header_type , jwt ).strip ()
896+ response = self .client .get (url , headers = {header_name : header_type })
897+ status_code = response .status_code
898+ data = json .loads (response .get_data (as_text = True ))
899+ return status_code , data
900+
901+ def test_login (self ):
902+ response = self .client .post ('/auth/login' )
903+ status_code = response .status_code
904+ data = json .loads (response .get_data (as_text = True ))
905+ self .assertEqual (status_code , 200 )
906+ self .assertIn ('access_token' , data )
907+ self .assertIn ('refresh_token' , data )
908+
909+ def test_fresh_login (self ):
910+ response = self .client .post ('/auth/fresh-login' )
911+ status_code = response .status_code
912+ data = json .loads (response .get_data (as_text = True ))
913+ self .assertEqual (status_code , 200 )
914+ self .assertIn ('access_token' , data )
915+ self .assertNotIn ('refresh_token' , data )
916+
917+ def test_refresh (self ):
918+ response = self .client .post ('/auth/login' )
919+ data = json .loads (response .get_data (as_text = True ))
920+ access_token = data ['access_token' ]
921+ refresh_token = data ['refresh_token' ]
922+
923+ status_code , data = self ._jwt_post ('/auth/refresh' , refresh_token )
924+ self .assertEqual (status_code , 200 )
925+ self .assertIn ('access_token' , data )
926+ self .assertNotIn ('refresh_token' , data )
0 commit comments