Skip to content

Commit 2f57b5b

Browse files
committed
Centralize value derivation of force_authn and memorized_idp
force_authn now derives its value also from the pysaml2 configuration settings. Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent bcd2388 commit 2f57b5b

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

src/satosa/backends/saml2.py

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,31 @@
3535
logger = logging.getLogger(__name__)
3636

3737

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+
3863
class SAMLBackend(BackendModule, SAMLBaseModule):
3964
"""
4065
A saml2 backend module (acting as a SP).
@@ -102,45 +127,24 @@ def get_idp_entity_id(self, context):
102127

103128
idps = self.sp.metadata.identity_providers()
104129
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
106132
)
107133

134+
only_idp = only_one_idp_in_metadata and idps[0]
108135
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
130139

131140
satosa_logging(
132141
logger, logging.INFO,
133142
{
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,
143145
"target_entity_id": target_entity_id,
146+
"force_authn": force_authn,
147+
"memorized_idp": memorized_idp,
144148
"entity_id": entity_id,
145149
},
146150
context.state,
@@ -160,8 +164,8 @@ def start_auth(self, context, internal_req):
160164
if entity_id is None:
161165
# since context is not passed to disco_query
162166
# 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
165169
)
166170
return self.disco_query(context)
167171

@@ -247,9 +251,8 @@ def authn_request(self, context, entity_id):
247251
if authn_context:
248252
kwargs["requested_authn_context"] = authn_context
249253
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
253256
)
254257

255258
try:

tests/satosa/backends/test_saml2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def test_use_of_disco_or_redirect_to_idp_when_using_mdq_and_forceauthn_is_set(
222222
SAMLBackend.KEY_SP_CONFIG: sp_conf,
223223
SAMLBackend.KEY_DISCO_SRV: DISCOSRV_URL,
224224
SAMLBackend.KEY_MEMORIZE_DISCO_IDP: True,
225+
SAMLBackend.KEY_MIRROR_FORCE_AUTHN: True,
225226
}
226227
samlbackend = SAMLBackend(
227228
None, INTERNAL_ATTRIBUTES, backend_conf, "base_url", "saml_backend"

0 commit comments

Comments
 (0)