@@ -390,25 +390,11 @@ def refresh():
390
390
set_access_cookies (resp , access_token )
391
391
return resp , 200
392
392
393
- @self .app .route ('/api/protected' )
393
+ @self .app .route ('/api/protected' , methods = [ 'POST' ] )
394
394
@jwt_required
395
395
def protected ():
396
396
return jsonify ({'msg' : "hello world" })
397
397
398
- def _jwt_post (self , url , jwt ):
399
- response = self .client .post (url , content_type = 'application/json' ,
400
- headers = {'Authorization' : 'Bearer {}' .format (jwt )})
401
- status_code = response .status_code
402
- data = json .loads (response .get_data (as_text = True ))
403
- return status_code , data
404
-
405
- def _jwt_get (self , url , jwt , header_name = 'Authorization' , header_type = 'Bearer' ):
406
- header_type = '{} {}' .format (header_type , jwt ).strip ()
407
- response = self .client .get (url , headers = {header_name : header_type })
408
- status_code = response .status_code
409
- data = json .loads (response .get_data (as_text = True ))
410
- return status_code , data
411
-
412
398
def _login (self ):
413
399
resp = self .client .post ('/auth/login' )
414
400
index = 1
@@ -491,7 +477,7 @@ def test_endpoints_with_cookies(self):
491
477
self .app .config ['JWT_COOKIE_CSRF_PROTECT' ] = False
492
478
493
479
# Try access without logging in
494
- response = self .client .get ('/api/protected' )
480
+ response = self .client .post ('/api/protected' )
495
481
status_code = response .status_code
496
482
data = json .loads (response .get_data (as_text = True ))
497
483
self .assertEqual (status_code , 401 )
@@ -506,7 +492,7 @@ def test_endpoints_with_cookies(self):
506
492
507
493
# Try with logging in
508
494
self ._login ()
509
- response = self .client .get ('/api/protected' )
495
+ response = self .client .post ('/api/protected' )
510
496
status_code = response .status_code
511
497
data = json .loads (response .get_data (as_text = True ))
512
498
self .assertEqual (status_code , 200 )
@@ -525,7 +511,7 @@ def test_endpoints_with_cookies(self):
525
511
access_cookie_key = access_cookie_str .split ('=' )[0 ]
526
512
access_cookie_value = "" .join (access_cookie_str .split ('=' )[1 :])
527
513
self .client .set_cookie ('localhost' , access_cookie_key , access_cookie_value )
528
- response = self .client .get ('/api/protected' )
514
+ response = self .client .post ('/api/protected' )
529
515
status_code = response .status_code
530
516
data = json .loads (response .get_data (as_text = True ))
531
517
self .assertEqual (status_code , 200 )
@@ -535,7 +521,7 @@ def test_access_endpoints_with_cookies_and_csrf(self):
535
521
self .app .config ['JWT_COOKIE_CSRF_PROTECT' ] = True
536
522
537
523
# Try without logging in
538
- response = self .client .get ('/api/protected' )
524
+ response = self .client .post ('/api/protected' )
539
525
status_code = response .status_code
540
526
data = json .loads (response .get_data (as_text = True ))
541
527
self .assertEqual (status_code , 401 )
@@ -545,30 +531,30 @@ def test_access_endpoints_with_cookies_and_csrf(self):
545
531
access_csrf , refresh_csrf = self ._login ()
546
532
547
533
# Try with logging in but without double submit csrf protection
548
- response = self .client .get ('/api/protected' )
534
+ response = self .client .post ('/api/protected' )
549
535
status_code = response .status_code
550
536
data = json .loads (response .get_data (as_text = True ))
551
537
self .assertEqual (status_code , 401 )
552
538
self .assertIn ('msg' , data )
553
539
554
540
# Try with logged in and bad header name for double submit token
555
- response = self .client .get ('/api/protected' ,
541
+ response = self .client .post ('/api/protected' ,
556
542
headers = {'bad-header-name' : 'banana' })
557
543
status_code = response .status_code
558
544
data = json .loads (response .get_data (as_text = True ))
559
545
self .assertEqual (status_code , 401 )
560
546
self .assertIn ('msg' , data )
561
547
562
548
# Try with logged in and bad header data for double submit token
563
- response = self .client .get ('/api/protected' ,
549
+ response = self .client .post ('/api/protected' ,
564
550
headers = {'X-CSRF-TOKEN' : 'banana' })
565
551
status_code = response .status_code
566
552
data = json .loads (response .get_data (as_text = True ))
567
553
self .assertEqual (status_code , 401 )
568
554
self .assertIn ('msg' , data )
569
555
570
556
# Try with logged in and good double submit token
571
- response = self .client .get ('/api/protected' ,
557
+ response = self .client .post ('/api/protected' ,
572
558
headers = {'X-CSRF-TOKEN' : access_csrf })
573
559
status_code = response .status_code
574
560
data = json .loads (response .get_data (as_text = True ))
@@ -582,7 +568,7 @@ def test_access_endpoints_with_cookie_missing_csrf_field(self):
582
568
self ._login ()
583
569
self .app .config ['JWT_COOKIE_CSRF_PROTECT' ] = True
584
570
585
- response = self .client .get ('/api/protected' )
571
+ response = self .client .post ('/api/protected' )
586
572
status_code = response .status_code
587
573
data = json .loads (response .get_data (as_text = True ))
588
574
self .assertEqual (status_code , 422 )
@@ -606,12 +592,58 @@ def test_access_endpoints_with_cookie_csrf_claim_not_string(self):
606
592
self .client .set_cookie ('localhost' , access_cookie_key , encoded_token )
607
593
608
594
self .app .config ['JWT_COOKIE_CSRF_PROTECT' ] = True
609
- response = self .client .get ('/api/protected' )
595
+ response = self .client .post ('/api/protected' )
610
596
status_code = response .status_code
611
597
data = json .loads (response .get_data (as_text = True ))
612
598
self .assertEqual (status_code , 422 )
613
599
self .assertIn ('msg' , data )
614
600
601
+ def test_custom_csrf_methods (self ):
602
+ @self .app .route ('/protected-post' , methods = ['POST' ])
603
+ @jwt_required
604
+ def protected_post ():
605
+ return jsonify ({'msg' : "hello world" })
606
+
607
+ @self .app .route ('/protected-get' , methods = ['GET' ])
608
+ @jwt_required
609
+ def protected_get ():
610
+ return jsonify ({'msg' : "hello world" })
611
+
612
+ # Login (saves jwts in the cookies for the test client
613
+ self .app .config ['JWT_COOKIE_CSRF_PROTECT' ] = True
614
+ self ._login ()
615
+
616
+ # Test being able to access GET without CSRF protection, and POST with
617
+ # CSRF protection
618
+ self .app .config ['JWT_CSRF_METHODS' ] = ['POST' ]
619
+
620
+ response = self .client .post ('/protected-post' )
621
+ status_code = response .status_code
622
+ data = json .loads (response .get_data (as_text = True ))
623
+ self .assertEqual (status_code , 401 )
624
+ self .assertIn ('msg' , data )
625
+
626
+ response = self .client .get ('/protected-get' )
627
+ status_code = response .status_code
628
+ data = json .loads (response .get_data (as_text = True ))
629
+ self .assertEqual (status_code , 200 )
630
+ self .assertEqual (data , {'msg' : 'hello world' })
631
+
632
+ # Now swap it around, and verify the JWT_CRSF_METHODS are being honored
633
+ self .app .config ['JWT_CSRF_METHODS' ] = ['GET' ]
634
+
635
+ response = self .client .get ('/protected-get' )
636
+ status_code = response .status_code
637
+ data = json .loads (response .get_data (as_text = True ))
638
+ self .assertEqual (status_code , 401 )
639
+ self .assertIn ('msg' , data )
640
+
641
+ response = self .client .post ('/protected-post' )
642
+ status_code = response .status_code
643
+ data = json .loads (response .get_data (as_text = True ))
644
+ self .assertEqual (status_code , 200 )
645
+ self .assertEqual (data , {'msg' : 'hello world' })
646
+
615
647
616
648
class TestEndpointsWithHeadersAndCookies (unittest .TestCase ):
617
649
0 commit comments