11"""OpenAPI core validation request validators module"""
2+ import base64
3+ import binascii
24from itertools import chain
35from six import iteritems
46import warnings
1214)
1315from openapi_core .schema .paths .exceptions import InvalidPath
1416from openapi_core .schema .request_bodies .exceptions import MissingRequestBody
15- from openapi_core .schema .security_schemes .enums import SecuritySchemeType
17+ from openapi_core .schema .security_schemes .enums import (
18+ SecuritySchemeType , HttpAuthScheme ,
19+ )
1620from openapi_core .schema .servers .exceptions import InvalidServer
1721from openapi_core .unmarshalling .schemas .exceptions import (
1822 UnmarshalError , ValidateError ,
@@ -103,7 +107,7 @@ def _get_operation(self, request):
103107 def _get_security (self , request , operation ):
104108 security = operation .security or self .spec .security
105109 if not security :
106- return
110+ return {}
107111
108112 for security_requirement in security :
109113 data = {
@@ -113,6 +117,8 @@ def _get_security(self, request, operation):
113117 if all (value for value in data .values ()):
114118 return data
115119
120+ return {}
121+
116122 def _get_parameters (self , request , params ):
117123 errors = []
118124 seen = set ()
@@ -195,6 +201,22 @@ def _get_security_value(self, scheme_name, request):
195201 if scheme .type == SecuritySchemeType .API_KEY :
196202 source = getattr (request .parameters , scheme .apikey_in .value )
197203 return source .get (scheme .name )
204+ elif scheme .type == SecuritySchemeType .HTTP :
205+ auth_header = request .parameters .header .get ('Authorization' )
206+ try :
207+ auth_type , encoded_credentials = auth_header .split (' ' , 1 )
208+ except ValueError :
209+ raise ValueError ('Could not parse authorization header.' )
210+
211+ if auth_type .lower () != scheme .scheme .value :
212+ raise ValueError (
213+ 'Unknown authorization method %s' % auth_type )
214+ try :
215+ return base64 .b64decode (
216+ encoded_credentials .encode ('ascii' ), validate = True
217+ ).decode ('latin1' )
218+ except binascii .Error :
219+ raise ValueError ('Invalid base64 encoding.' )
198220
199221 warnings .warn ("Only api key security scheme type supported" )
200222
0 commit comments