Skip to content

Commit af4820c

Browse files
committed
Add option to process all ldap results
This patch adds an option to not only process the first ldap result, but all of them. This can be useful while trying to enrich the data e.g. with multiple group information.
1 parent 43fd132 commit af4820c

File tree

2 files changed

+73
-66
lines changed

2 files changed

+73
-66
lines changed

example/plugins/microservices/ldap_attribute_store.yaml.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ config:
9393
user_id_from_attrs:
9494
- employeeNumber
9595

96+
# If true, do not only process the first ldap result, but iterate over
97+
# the result and process all of them.
98+
use_all_results: false
99+
96100
# Where to redirect the browser if no record is returned
97101
# from LDAP. The default is not to redirect.
98102
on_ldap_search_result_empty: https://my.vo.org/please/go/enroll

src/satosa/micro_services/ldap_attribute_store.py

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,13 @@ def process(self, context, data):
526526

527527
# For now consider only the first record found (if any).
528528
if len(responses) > 0:
529-
if len(responses) > 1:
529+
if len(responses) > 1 and not config.get("use_all_results", False):
530530
msg = "LDAP server returned {} records using search filter"
531531
msg = msg + " value {}"
532532
msg = msg.format(len(responses), filter_val)
533533
logline = lu.LOG_FMT.format(id=session_id, message=msg)
534534
logger.warning(logline)
535-
record = responses[0]
535+
responses = responses[0:1]
536536
break
537537

538538
# Before using a found record, if any, to populate attributes
@@ -544,73 +544,76 @@ def process(self, context, data):
544544
logger.debug(logline)
545545
data.attributes = {}
546546

547-
# This adapts records with different search and connection strategy
548-
# (sync without pool), it should be tested with anonimous bind with
549-
# message_id.
550-
if isinstance(results, bool) and record:
551-
record = {
552-
"dn": record.entry_dn if hasattr(record, "entry_dn") else "",
553-
"attributes": (
554-
record.entry_attributes_as_dict
555-
if hasattr(record, "entry_attributes_as_dict")
556-
else {}
557-
),
558-
}
559-
560-
# Use a found record, if any, to populate attributes and input for
561-
# NameID
562-
if record:
563-
msg = {
564-
"message": "Using record with DN and attributes",
565-
"DN": record["dn"],
566-
"attributes": record["attributes"],
567-
}
568-
logline = lu.LOG_FMT.format(id=session_id, message=msg)
569-
logger.debug(logline)
547+
for record in responses:
548+
# This adapts records with different search and connection strategy
549+
# (sync without pool), it should be tested with anonimous bind with
550+
# message_id.
551+
if isinstance(results, bool) and record:
552+
record = {
553+
"dn": record.entry_dn if hasattr(record, "entry_dn") else "",
554+
"attributes": (
555+
record.entry_attributes_as_dict
556+
if hasattr(record, "entry_attributes_as_dict")
557+
else {}
558+
),
559+
}
560+
561+
# Use a found record, if any, to populate attributes and input for
562+
# NameID
563+
if record:
564+
msg = {
565+
"message": "Using record with DN and attributes",
566+
"DN": record["dn"],
567+
"attributes": record["attributes"],
568+
}
569+
logline = lu.LOG_FMT.format(id=session_id, message=msg)
570+
logger.debug(logline)
570571

571-
# Populate attributes as configured.
572-
new_attrs = self._populate_attributes(config, record)
573-
574-
overwrite = config["overwrite_existing_attributes"]
575-
for attr, values in new_attrs.items():
576-
if not overwrite:
577-
values = list(set(data.attributes.get(attr, []) + values))
578-
data.attributes[attr] = values
579-
580-
# Populate input for NameID if configured. SATOSA core does the
581-
# hashing of input to create a persistent NameID.
582-
user_ids = self._populate_input_for_name_id(config, record, data)
583-
if user_ids:
584-
data.subject_id = "".join(user_ids)
585-
msg = "NameID value is {}".format(data.subject_id)
586-
logger.debug(msg)
572+
# Populate attributes as configured.
573+
new_attrs = self._populate_attributes(config, record)
574+
575+
overwrite = config["overwrite_existing_attributes"]
576+
for attr, values in new_attrs.items():
577+
if not overwrite:
578+
values = list(map(str, set(data.attributes.get(attr, []) + values)))
579+
else:
580+
values = list(map(str, set(values)))
581+
data.attributes[attr] = values
582+
583+
# Populate input for NameID if configured. SATOSA core does the
584+
# hashing of input to create a persistent NameID.
585+
user_ids = self._populate_input_for_name_id(config, record, data)
586+
if user_ids:
587+
data.subject_id = "".join(user_ids)
588+
msg = "NameID value is {}".format(data.subject_id)
589+
logger.debug(msg)
587590

588-
# Add the record to the context so that later microservices
589-
# may use it if required.
590-
context.decorate(KEY_FOUND_LDAP_RECORD, record)
591-
msg = "Added record {} to context".format(record)
592-
logline = lu.LOG_FMT.format(id=session_id, message=msg)
593-
logger.debug(logline)
594-
else:
595-
msg = "No record found in LDAP so no attributes will be added"
596-
logline = lu.LOG_FMT.format(id=session_id, message=msg)
597-
logger.warning(logline)
598-
on_ldap_search_result_empty = config["on_ldap_search_result_empty"]
599-
if on_ldap_search_result_empty:
600-
# Redirect to the configured URL with
601-
# the entityIDs for the target SP and IdP used by the user
602-
# as query string parameters (URL encoded).
603-
encoded_sp_entity_id = urllib.parse.quote_plus(requester)
604-
encoded_idp_entity_id = urllib.parse.quote_plus(issuer)
605-
url = "{}?sp={}&idp={}".format(
606-
on_ldap_search_result_empty,
607-
encoded_sp_entity_id,
608-
encoded_idp_entity_id,
609-
)
610-
msg = "Redirecting to {}".format(url)
591+
# Add the record to the context so that later microservices
592+
# may use it if required.
593+
context.decorate(KEY_FOUND_LDAP_RECORD, record)
594+
msg = "Added record {} to context".format(record)
611595
logline = lu.LOG_FMT.format(id=session_id, message=msg)
612-
logger.info(logline)
613-
return Redirect(url)
596+
logger.debug(logline)
597+
else:
598+
msg = "No record found in LDAP so no attributes will be added"
599+
logline = lu.LOG_FMT.format(id=session_id, message=msg)
600+
logger.warning(logline)
601+
on_ldap_search_result_empty = config["on_ldap_search_result_empty"]
602+
if on_ldap_search_result_empty:
603+
# Redirect to the configured URL with
604+
# the entityIDs for the target SP and IdP used by the user
605+
# as query string parameters (URL encoded).
606+
encoded_sp_entity_id = urllib.parse.quote_plus(requester)
607+
encoded_idp_entity_id = urllib.parse.quote_plus(issuer)
608+
url = "{}?sp={}&idp={}".format(
609+
on_ldap_search_result_empty,
610+
encoded_sp_entity_id,
611+
encoded_idp_entity_id,
612+
)
613+
msg = "Redirecting to {}".format(url)
614+
logline = lu.LOG_FMT.format(id=session_id, message=msg)
615+
logger.info(logline)
616+
return Redirect(url)
614617

615618
msg = "Returning data.attributes {}".format(data.attributes)
616619
logline = lu.LOG_FMT.format(id=session_id, message=msg)

0 commit comments

Comments
 (0)