@@ -571,14 +571,22 @@ def do_POST(self):
571
571
self .server .SHARED ["schema" ] = self .headers .get ("Default-Schema" )
572
572
573
573
if self .headers .get ("Authorization" ) is not None :
574
- auth_header = self .headers ["Authorization" ].replace ("Basic " , "" )
575
- credentials = b64decode (auth_header ).decode ("utf-8" ).split (":" , 1 )
576
- self .server .SHARED ["username" ] = credentials [0 ]
577
- if len (credentials ) > 1 and credentials [1 ]:
578
- self .server .SHARED ["password" ] = credentials [1 ]
579
- else :
580
- self .server .SHARED ["password" ] = None
574
+ auth_header = self .headers ["Authorization" ]
575
+ if "Basic" in auth_header :
576
+ auth_header = auth_header .replace ("Basic " , "" )
577
+ credentials = (
578
+ b64decode (auth_header ).decode ("utf-8" ).split (":" , 1 )
579
+ )
580
+ self .server .SHARED ["username" ] = credentials [0 ]
581
+ if len (credentials ) > 1 and credentials [1 ]:
582
+ self .server .SHARED ["password" ] = credentials [1 ]
583
+ else :
584
+ self .server .SHARED ["password" ] = None
585
+ elif "Bearer" in auth_header :
586
+ jwt_token = auth_header .replace ("Bearer " , "" )
587
+ self .server .SHARED ["jwt_token" ] = jwt_token
581
588
else :
589
+ self .server .SHARED ["jwt_token" ] = None
582
590
self .server .SHARED ["username" ] = None
583
591
584
592
if self .headers .get ("X-User" ) is not None :
@@ -604,6 +612,7 @@ class TestingHTTPServer(HTTPServer):
604
612
SHARED = manager .dict ()
605
613
SHARED ["count" ] = 0
606
614
SHARED ["usernameFromXUser" ] = None
615
+ SHARED ["jwt_token" ] = None
607
616
SHARED ["username" ] = None
608
617
SHARED ["password" ] = None
609
618
SHARED ["schema" ] = None
@@ -689,13 +698,17 @@ class TestUsernameSentAsHeader(TestingHttpServerTestCase):
689
698
def setUp (self ):
690
699
super ().setUp ()
691
700
self .clientWithoutUsername = self .clientWithKwargs ()
701
+ self .clientWithJwtToken = self .clientWithKwargs (
702
+ jwt_token = "testJwtToken"
703
+ )
692
704
self .clientWithUsername = self .clientWithKwargs (username = "testDBUser" )
693
705
self .clientWithUsernameAndPassword = self .clientWithKwargs (
694
706
username = "testDBUser" , password = "test:password"
695
707
)
696
708
697
709
def tearDown (self ):
698
710
self .clientWithoutUsername .close ()
711
+ self .clientWithJwtToken .close ()
699
712
self .clientWithUsername .close ()
700
713
self .clientWithUsernameAndPassword .close ()
701
714
super ().tearDown ()
@@ -720,6 +733,13 @@ def test_username(self):
720
733
self .assertEqual (TestingHTTPServer .SHARED ["username" ], "testDBUser" )
721
734
self .assertEqual (TestingHTTPServer .SHARED ["password" ], "test:password" )
722
735
736
+ def test_jwt_token (self ):
737
+ self .clientWithoutUsername .sql ("select * from fake" )
738
+ self .assertEqual (TestingHTTPServer .SHARED ["jwt_token" ], None )
739
+
740
+ self .clientWithJwtToken .sql ("select * from fake" )
741
+ self .assertEqual (TestingHTTPServer .SHARED ["jwt_token" ], "testJwtToken" )
742
+
723
743
724
744
class TestCrateJsonEncoder (TestCase ):
725
745
def test_naive_datetime (self ):
0 commit comments