@@ -41,9 +41,13 @@ def configure_security_client(client: requests.Session, security: dataclass):
41
41
continue
42
42
if metadata .get ('option' ):
43
43
_parse_security_option (client , value )
44
- return
44
+ return client
45
45
elif metadata .get ('scheme' ):
46
- _parse_security_scheme (client , metadata , value )
46
+ # Special case for basic auth which could be a flattened struct
47
+ if metadata .get ("sub_type" ) == "basic" and not is_dataclass (value ):
48
+ _parse_security_scheme (client , metadata , security )
49
+ else :
50
+ _parse_security_scheme (client , metadata , value )
47
51
48
52
return client
49
53
@@ -54,47 +58,60 @@ def _parse_security_option(client: SecurityClient, option: dataclass):
54
58
metadata = opt_field .metadata .get ('security' )
55
59
if metadata is None or metadata .get ('scheme' ) is None :
56
60
continue
57
- _parse_security_scheme (client , metadata . get (
58
- 'scheme' ) , getattr (option , opt_field .name ))
61
+ _parse_security_scheme (
62
+ client , metadata , getattr (option , opt_field .name ))
59
63
60
64
61
- def _parse_security_scheme (client : SecurityClient , scheme_metadata : dict , scheme : dataclass ):
65
+ def _parse_security_scheme (client : SecurityClient , scheme_metadata : dict , scheme : any ):
62
66
scheme_type = scheme_metadata .get ('type' )
63
67
sub_type = scheme_metadata .get ('sub_type' )
64
68
65
- if scheme_type == 'http' and sub_type == 'basic' :
66
- _parse_basic_auth_scheme (client , scheme )
67
- return
69
+ if is_dataclass (scheme ):
70
+ if scheme_type == 'http' and sub_type == 'basic' :
71
+ _parse_basic_auth_scheme (client , scheme )
72
+ return
68
73
69
- scheme_fields : Tuple [Field , ...] = fields (scheme )
70
- for scheme_field in scheme_fields :
71
- metadata = scheme_field .metadata .get ('security' )
72
- if metadata is None or metadata .get ('field_name' ) is None :
73
- continue
74
+ scheme_fields : Tuple [Field , ...] = fields (scheme )
75
+ for scheme_field in scheme_fields :
76
+ metadata = scheme_field .metadata .get ('security' )
77
+ if metadata is None or metadata .get ('field_name' ) is None :
78
+ continue
74
79
75
- header_name = metadata .get ('field_name' )
76
- value = getattr (scheme , scheme_field .name )
80
+ value = getattr (scheme , scheme_field .name )
77
81
78
- if scheme_type == "apiKey" :
79
- if sub_type == 'header' :
80
- client .client .headers [header_name ] = value
81
- elif sub_type == 'query' :
82
- client .query_params [header_name ] = value
83
- elif sub_type == 'cookie' :
84
- client .client .cookies [header_name ] = value
85
- else :
86
- raise Exception ('not supported' )
87
- elif scheme_type == "openIdConnect" :
82
+ _parse_security_scheme_value (
83
+ client , scheme_metadata , metadata , value )
84
+ else :
85
+ _parse_security_scheme_value (
86
+ client , scheme_metadata , scheme_metadata , scheme )
87
+
88
+
89
+ def _parse_security_scheme_value (client : SecurityClient , scheme_metadata : dict , security_metadata : dict , value : any ):
90
+ scheme_type = scheme_metadata .get ('type' )
91
+ sub_type = scheme_metadata .get ('sub_type' )
92
+
93
+ header_name = security_metadata .get ('field_name' )
94
+
95
+ if scheme_type == "apiKey" :
96
+ if sub_type == 'header' :
88
97
client .client .headers [header_name ] = value
89
- elif scheme_type == 'oauth2' :
98
+ elif sub_type == 'query' :
99
+ client .query_params [header_name ] = value
100
+ elif sub_type == 'cookie' :
101
+ client .client .cookies [header_name ] = value
102
+ else :
103
+ raise Exception ('not supported' )
104
+ elif scheme_type == "openIdConnect" :
105
+ client .client .headers [header_name ] = value
106
+ elif scheme_type == 'oauth2' :
107
+ client .client .headers [header_name ] = value
108
+ elif scheme_type == 'http' :
109
+ if sub_type == 'bearer' :
90
110
client .client .headers [header_name ] = value
91
- elif scheme_type == 'http' :
92
- if sub_type == 'bearer' :
93
- client .client .headers [header_name ] = value
94
- else :
95
- raise Exception ('not supported' )
96
111
else :
97
112
raise Exception ('not supported' )
113
+ else :
114
+ raise Exception ('not supported' )
98
115
99
116
100
117
def _parse_basic_auth_scheme (client : SecurityClient , scheme : dataclass ):
0 commit comments