Skip to content

Commit e1f3348

Browse files
committed
Redo _construct_filter_value
Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent e9a947f commit e1f3348

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/satosa/micro_services/ldap_attribute_store.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ def __init__(self, config, *args, **kwargs):
131131
msg = "LDAP Attribute Store microservice initialized"
132132
satosa_logging(logger, logging.INFO, msg, None)
133133

134-
def _construct_filter_value(self, candidate, data):
134+
def _construct_filter_value(
135+
self, candidate, name_id_value, name_id_format, issuer, attributes
136+
):
135137
"""
136138
Construct and return a LDAP directory search filter value from the
137139
candidate identifier.
@@ -162,40 +164,30 @@ def _construct_filter_value(self, candidate, data):
162164
entityID for the IdP will be concatenated to "scope" the value. If the
163165
string is any other value it will be directly concatenated.
164166
"""
165-
context = self.context
166-
state = context.state
167-
168-
attributes = data.attributes
169-
msg = "Input attributes {}".format(attributes)
170-
satosa_logging(logger, logging.DEBUG, msg, state)
171-
172167
# Get the values configured list of identifier names for this candidate
173168
# and substitute None if there are no values for a configured
174169
# identifier.
175-
values = []
176-
for identifier_name in candidate["attribute_names"]:
177-
v = attributes.get(identifier_name, None)
178-
if isinstance(v, list):
179-
v = v[0]
180-
values.append(v)
170+
values = [
171+
attr_value[0] if isinstance(attr_value, list) else attr_value
172+
for identifier_name in candidate["attribute_names"]
173+
for attr_value in [attributes.get(identifier_name)]
174+
]
181175
msg = "Found candidate values {}".format(values)
182-
satosa_logging(logger, logging.DEBUG, msg, state)
176+
satosa_logging(logger, logging.DEBUG, msg, None)
183177

184178
# If one of the configured identifier names is name_id then if there is
185179
# also a configured name_id_format add the value for the NameID of that
186180
# format if it was asserted by the IdP or else add the value None.
187181
if "name_id" in candidate["attribute_names"]:
188182
candidate_nameid_value = None
189183
candidate_name_id_format = candidate.get("name_id_format")
190-
name_id_value = data.subject_id
191-
name_id_format = data.subject_type
192184
if (
193185
name_id_value
194186
and candidate_name_id_format
195187
and candidate_name_id_format == name_id_format
196188
):
197189
msg = "IdP asserted NameID {}".format(name_id_value)
198-
satosa_logging(logger, logging.DEBUG, msg, state)
190+
satosa_logging(logger, logging.DEBUG, msg, None)
199191
candidate_nameid_value = name_id_value
200192

201193
# Only add the NameID value asserted by the IdP if it is not
@@ -206,37 +198,38 @@ def _construct_filter_value(self, candidate, data):
206198
if candidate_nameid_value not in values:
207199
msg = "Added NameID {} to candidate values"
208200
msg = msg.format(candidate_nameid_value)
209-
satosa_logging(logger, logging.DEBUG, msg, state)
201+
satosa_logging(logger, logging.DEBUG, msg, None)
210202
values.append(candidate_nameid_value)
211203
else:
212204
msg = "NameID {} value also asserted as attribute value"
213205
msg = msg.format(candidate_nameid_value)
214-
satosa_logging(logger, logging.WARN, msg, state)
206+
satosa_logging(logger, logging.WARN, msg, None)
215207

216208
# If no value was asserted by the IdP for one of the configured list of
217209
# identifier names for this candidate then go onto the next candidate.
218210
if None in values:
219211
msg = "Candidate is missing value so skipping"
220-
satosa_logging(logger, logging.DEBUG, msg, state)
212+
satosa_logging(logger, logging.DEBUG, msg, None)
221213
return None
222214

223215
# All values for the configured list of attribute names are present
224216
# so we can create a value. Add a scope if configured
225217
# to do so.
226218
if "add_scope" in candidate:
227-
if candidate["add_scope"] == "issuer_entityid":
228-
scope = data.auth_info.issuer
229-
else:
230-
scope = candidate["add_scope"]
219+
scope = (
220+
issuer
221+
if candidate["add_scope"] == "issuer_entityid"
222+
else candidate["add_scope"]
223+
)
231224
msg = "Added scope {} to values".format(scope)
232-
satosa_logging(logger, logging.DEBUG, msg, state)
225+
satosa_logging(logger, logging.DEBUG, msg, None)
233226
values.append(scope)
234227

235228
# Concatenate all values to create the filter value.
236229
value = "".join(values)
237230

238231
msg = "Constructed filter value {}".format(value)
239-
satosa_logging(logger, logging.DEBUG, msg, state)
232+
satosa_logging(logger, logging.DEBUG, msg, None)
240233

241234
return value
242235

@@ -443,7 +436,13 @@ def process(self, context, data):
443436
# and find asserted values to construct the ordered list of values for
444437
# the LDAP search filters.
445438
for candidate in config["ordered_identifier_candidates"]:
446-
value = self._construct_filter_value(candidate, data)
439+
value = self._construct_filter_value(
440+
candidate,
441+
data.subject_id,
442+
data.subject_type,
443+
data.auth_info.issuer,
444+
data.attriutes,
445+
)
447446

448447
# If we have constructed a non empty value then add it as the next
449448
# filter value to use when searching for the user record.

0 commit comments

Comments
 (0)