@@ -571,14 +571,20 @@ 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 = b64decode (auth_header ).decode ("utf-8" ).split (":" , 1 )
578
+ self .server .SHARED ["username" ] = credentials [0 ]
579
+ if len (credentials ) > 1 and credentials [1 ]:
580
+ self .server .SHARED ["password" ] = credentials [1 ]
581
+ else :
582
+ self .server .SHARED ["password" ] = None
583
+ elif "Bearer" in auth_header :
584
+ jwt_token = auth_header .replace ("Bearer " , "" )
585
+ self .server .SHARED ["jwt_token" ] = jwt_token
581
586
else :
587
+ self .server .SHARED ["jwt_token" ] = None
582
588
self .server .SHARED ["username" ] = None
583
589
584
590
if self .headers .get ("X-User" ) is not None :
@@ -604,6 +610,7 @@ class TestingHTTPServer(HTTPServer):
604
610
SHARED = manager .dict ()
605
611
SHARED ["count" ] = 0
606
612
SHARED ["usernameFromXUser" ] = None
613
+ SHARED ["jwt_token" ] = None
607
614
SHARED ["username" ] = None
608
615
SHARED ["password" ] = None
609
616
SHARED ["schema" ] = None
@@ -689,13 +696,15 @@ class TestUsernameSentAsHeader(TestingHttpServerTestCase):
689
696
def setUp (self ):
690
697
super ().setUp ()
691
698
self .clientWithoutUsername = self .clientWithKwargs ()
699
+ self .clientWithJwtToken = self .clientWithKwargs (jwt_token = "testJwtToken" )
692
700
self .clientWithUsername = self .clientWithKwargs (username = "testDBUser" )
693
701
self .clientWithUsernameAndPassword = self .clientWithKwargs (
694
702
username = "testDBUser" , password = "test:password"
695
703
)
696
704
697
705
def tearDown (self ):
698
706
self .clientWithoutUsername .close ()
707
+ self .clientWithJwtToken .close ()
699
708
self .clientWithUsername .close ()
700
709
self .clientWithUsernameAndPassword .close ()
701
710
super ().tearDown ()
@@ -720,6 +729,13 @@ def test_username(self):
720
729
self .assertEqual (TestingHTTPServer .SHARED ["username" ], "testDBUser" )
721
730
self .assertEqual (TestingHTTPServer .SHARED ["password" ], "test:password" )
722
731
732
+ def test_jwt_token (self ):
733
+ self .clientWithoutUsername .sql ("select * from fake" )
734
+ self .assertEqual (TestingHTTPServer .SHARED ["jwt_token" ], None )
735
+
736
+ self .clientWithJwtToken .sql ("select * from fake" )
737
+ self .assertEqual (TestingHTTPServer .SHARED ["jwt_token" ], "testJwtToken" )
738
+
723
739
724
740
class TestCrateJsonEncoder (TestCase ):
725
741
def test_naive_datetime (self ):
0 commit comments