35
35
logger = logging .getLogger (__name__ )
36
36
37
37
38
+ def get_memorized_idp (context , config , force_authn ):
39
+ memorized_idp = (
40
+ config .get (SAMLBackend .KEY_MEMORIZE_DISCO_IDP )
41
+ and context .state .get (Context .KEY_MEMORIZED_DISCO_IDP )
42
+ )
43
+ use_when_force_authn = config .get (
44
+ SAMLBackend .KEY_USE_MEMORIZED_DISCO_IDP_WHEN_FORCE_AUTHN
45
+ )
46
+ value = (not force_authn or use_when_force_authn ) and memorized_idp
47
+ return value
48
+
49
+
50
+ # XXX check KEY_FORCE_AUTHN value type (boolean vs str)
51
+ def get_force_authn (context , config , sp_config ):
52
+ value = (
53
+ config .get (SAMLBackend .KEY_MIRROR_SAML_FORCE_AUTHN )
54
+ and (
55
+ context .state .get (Context .KEY_FORCE_AUTHN )
56
+ or context .get_decoration (Context .KEY_FORCE_AUTHN )
57
+ )
58
+ or sp_config .getattr ("force_authn" , "sp" )
59
+ )
60
+ return value
61
+
62
+
38
63
class SAMLBackend (BackendModule , SAMLBaseModule ):
39
64
"""
40
65
A saml2 backend module (acting as a SP).
@@ -102,45 +127,24 @@ def get_idp_entity_id(self, context):
102
127
103
128
idps = self .sp .metadata .identity_providers ()
104
129
only_one_idp_in_metadata = (
105
- len (idps ) == 1 and "mdq" not in self .config ["sp_config" ]["metadata" ]
130
+ "mdq" not in self .config ["sp_config" ]["metadata" ]
131
+ and len (idps ) == 1
106
132
)
107
133
134
+ only_idp = only_one_idp_in_metadata and idps [0 ]
108
135
target_entity_id = context .get_decoration (Context .KEY_TARGET_ENTITYID )
109
-
110
- force_authn = context .get_decoration (Context .KEY_FORCE_AUTHN )
111
- memorized_disco_idp = (
112
- self .config .get (SAMLBackend .KEY_MEMORIZE_DISCO_IDP )
113
- and context .state .get (Context .KEY_MEMORIZED_DISCO_IDP )
114
- )
115
- use_memorized_disco_idp_when_force_authn = self .config .get (
116
- SAMLBackend .KEY_USE_MEMORIZED_DISCO_IDP_WHEN_FORCE_AUTHN
117
- )
118
- use_memorized_disco_idp = memorized_disco_idp and (
119
- not force_authn or use_memorized_disco_idp_when_force_authn
120
- )
121
-
122
- if only_one_idp_in_metadata :
123
- entity_id = idps [0 ]
124
- elif use_memorized_disco_idp :
125
- entity_id = memorized_disco_idp
126
- elif target_entity_id :
127
- entity_id = target_entity_id
128
- else :
129
- entity_id = None
136
+ force_authn = get_force_authn (context , self .config , self .sp .config )
137
+ memorized_idp = get_memorized_idp (context , self .config , force_authn )
138
+ entity_id = only_idp or target_entity_id or memorized_idp or None
130
139
131
140
satosa_logging (
132
141
logger , logging .INFO ,
133
142
{
134
- "message" : "Selected IdP entity ID" ,
135
- "idps" : idps ,
136
- "only_one_idp_in_metadata" : only_one_idp_in_metadata ,
137
- "force_authn" : force_authn ,
138
- "memorized_disco_idp" : memorized_disco_idp ,
139
- "use_memorized_disco_idp_when_force_authn" : (
140
- use_memorized_disco_idp_when_force_authn
141
- ),
142
- "use_memorized_disco_idp" : use_memorized_disco_idp ,
143
+ "message" : "Selected IdP" ,
144
+ "only_one" : only_idp ,
143
145
"target_entity_id" : target_entity_id ,
146
+ "force_authn" : force_authn ,
147
+ "memorized_idp" : memorized_idp ,
144
148
"entity_id" : entity_id ,
145
149
},
146
150
context .state ,
@@ -160,8 +164,8 @@ def start_auth(self, context, internal_req):
160
164
if entity_id is None :
161
165
# since context is not passed to disco_query
162
166
# keep the information in the state cookie
163
- context .state [Context .KEY_FORCE_AUTHN ] = context . get_decoration (
164
- Context . KEY_FORCE_AUTHN
167
+ context .state [Context .KEY_FORCE_AUTHN ] = get_force_authn (
168
+ context , self . config , self . sp . config
165
169
)
166
170
return self .disco_query (context )
167
171
@@ -247,9 +251,8 @@ def authn_request(self, context, entity_id):
247
251
if authn_context :
248
252
kwargs ["requested_authn_context" ] = authn_context
249
253
if self .config .get (SAMLBackend .KEY_MIRROR_SAML_FORCE_AUTHN ):
250
- kwargs ["force_authn" ] = (
251
- context .state .get (Context .KEY_FORCE_AUTHN )
252
- or context .get_decoration (Context .KEY_FORCE_AUTHN )
254
+ kwargs ["force_authn" ] = get_force_authn (
255
+ context , self .config , self .sp .config
253
256
)
254
257
255
258
try :
0 commit comments