Federation SAML Source Property Mapping
Describe your question/ So I want to hook up my Authentik instance to a SAML IdP source for federated login.
I checked the following documentation pages
- https://docs.goauthentik.io/docs/users-sources/sources/protocols/saml/
- https://docs.goauthentik.io/docs/users-sources/sources/property-mappings/
and tried following the "see documentation for a list of all variables" link in the "SAML Source Property Mapping" which points to a non-existing page "https://version-2024-12.goauthentik.io/docs/user-sources/sources/property-mappings/expressions?utm_source=authentik"
but I'm not really seeing how one is supposed to do the attribute mappings.
Is it really required to write your own lookup code for each property like it is done for "name_id" (https://github.com/goauthentik/authentik/blob/main/authentik/sources/saml/processors/response.py#L209)?
Am I something missing?
Thanks
The latest link is https://docs.goauthentik.io/docs/users-sources/sources/property-mappings/expressions. @tanberry let's check we're linking probably from the UI
As for the source property mappings code themselves, yes, you need to write whatever you want in there, allowing you to get as much customization as needed
thank you so much for the answer!
it would be nice to have an explicit sentence about that there is no automatic extraction done in this case in the readme (less searching and ambiguity for users like me :D).
and i would also propose a starting example on how to do that ... i myself know etree a bit but from my point of view its more complicated than the other methods... would be really helpful to have a saml xml response with attributes and then example code to extract the fields
adding to that: there is a test button with the mappings. how would i go about suppyling a saml response there to test the code? can i just use the context there and say e.g. { root = ElementTree.fromstring(b'mysamlresponse') }?
i have an additional question regarding ids.
the idp(s) we would like to integrate are from a federation and they suggest to find the ids based on the following rules
extract attributes
- urn:oasis:names:tc:SAML:attribute:subject-id -> subject-id
- urn:oasis:names:tc:SAML:attribute:pairwise-id -> pairwise-id
- urn:oid:1.3.6.1.4.1.5923.1.1.1.6 AND urn:mace:dir:attribute-def:eduPersonPrincipalName -> eppn
- urn:oid:1.3.6.1.4.1.5923.1.1.1.10 -> targeted-id
- urn:oasis:names:tc:SAML:2.0:nameid-format:persistent -> persistent-id
and then determine the actual id by going through a priority list (left most has highest priority): eppn subject-id pairwise-id persistent-id targeted-id
from what i could see authentik just reads the subject attributes nameid value and uses that as identifier (unless the nameid policy is set to transient). how does it work in the other cases (email, windows & x509)? is there any way to customize it?
So we need two things:
- a unique identifier for the user to link their remote account to their authentik account. This is not overridable and done in https://github.com/goauthentik/authentik/blob/main/authentik/sources/saml/processors/response.py#L209
- user properties (username, email, attributes, etc.). A default set of properties is built in https://github.com/goauthentik/authentik/blob/main/authentik/sources/saml/models.py#L212. Those can be overridden by property mappings as necessary
The latest link is https://docs.goauthentik.io/docs/users-sources/sources/property-mappings/expressions. @tanberry let's check we're linking probably from the UI
Yep, I just found the bad link in the UI, will open a PR. Thanks so much @katrinm86 and sorry about that! If you want, feel free to open a PR against the docs and clarify some of the issues you ran into, that would be great. If not, I will do so. Ether way, we appreciate your questions and points!
* a unique identifier for the user to link their remote account to their authentik account. This is not overridable and done in https://github.com/goauthentik/authentik/blob/main/authentik/sources/saml/processors/response.py#L209
Hm, would there be another way? like coming up with a custom 'processor' (dont know if this is the right term)?
* user properties (username, email, attributes, etc.). A default set of properties is built in https://github.com/goauthentik/authentik/blob/main/authentik/sources/saml/models.py#L212. Those can be overridden by property mappings as necessary
Ok thats fine I think I got it covered, however the code got a bit longer than I would normally feel comfortable with putting in a dialog in a form :)
Yep, I just found the bad link in the UI, will open a PR. Thanks so much @katrinm86 and sorry about that! If you want, feel free to open a PR against the docs and clarify some of the issues you ran into, that would be great. If not, I will do so. Ether way, we appreciate your questions and points!
I thinks its mostly fine as it is, the only thing that would be really helpful would be to have an example SAML message with attributes and the parsing of that to get you started.
* a unique identifier for the user to link their remote account to their authentik account. This is not overridable and done in https://github.com/goauthentik/authentik/blob/main/authentik/sources/saml/processors/response.py#L209Hm, would there be another way? like coming up with a custom 'processor' (dont know if this is the right term)?
You can change how the matching is done (by email or username, which in turn are modifiable with property mappings), but you won't be able to change which ID we use for internal reference. We are thinking of adding a way to configure this further, but haven't settled on a design yet.
and i would also propose a starting example on how to do that ... i myself know etree a bit but from my point of view its more complicated than the other methods... would be really helpful to have a saml xml response with attributes and then example code to extract the fields
PLEASE provide an example! I've been arguing with this for actual WEEKS!
PLEASE provide an example! I've been arguing with this for actual WEEKS!
So for my prototype I did something (not ideal) like this:
NS_SAML_ASSERTION = "urn:oasis:names:tc:SAML:2.0:assertion"
assertion = root.find(f"{{{NS_SAML_ASSERTION}}}Assertion")
if assertion is None:
raise ValueError("Assertion element not found")
attribute_statement = assertion.find(f"{{{NS_SAML_ASSERTION}}}AttributeStatement")
if attribute_statement is None:
raise ValueError("AttributeStatement element not found")
def _name_id(element):
name_id = element.find(f"{{{NS_SAML_ASSERTION}}}NameID")
name_qualifier = name_id.attrib.get("NameQualifier", "")
spname_qualifier = name_id.attrib.get("SPNameQualifier", "")
return f"{name_qualifier}!{spname_qualifier}!{name_id.text}"
_extractors = {"urn:oid:1.3.6.1.4.1.5923.1.1.1.10": _name_id}
# extract attribute values from attribute statements into attributes dictionary
attributes = {}
for element in attribute_statement.findall(f"{{{NS_SAML_ASSERTION}}}Attribute"):
name = element.attrib["Name"]
element_value = element.find(f"{{{NS_SAML_ASSERTION}}}AttributeValue")
extractor_fun = _extractors.get(name, lambda el: el.text)
attributes[name] = extractor_fun(element_value)
def extract(
attributes: dict,
name: str,
lookup_list: tuple[str],
is_mandatory: bool = False,
def_value=None,
):
for attrib_name in lookup_list:
val = attributes.get(attrib_name, None)
if val:
return val
if is_mandatory:
raise ValueError(
f'No SAML source attribute found for mandatory value: "{name}"'
)
return def_value
###
# next extract the attributes we want from the dict
###
remote_user = extract(
attributes,
"remote_user",
(
"urn:oid:1.3.6.1.4.1.5923.1.1.1.6",
"urn:mace:dir:attribute-def:eduPersonPrincipalName",
"urn:oasis:names:tc:SAML:attribute:subject-id",
"urn:oasis:names:tc:SAML:attribute:pairwise-id",
"urn:oasis:names:tc:SAML:2.0:nameid-format:persistent",
"urn:oid:1.3.6.1.4.1.5923.1.1.1.10",
),
is_mandatory=True,
)
firstname = extract(
attributes,
"firstname",
("urn:oid:2.5.4.42", "urn:mace:dir:attribute-def:givenName"),
)
lastname = extract(
attributes,
"lastname",
("urn:oid:2.5.4.4", "urn:mace:dir:attribute-def:sn"),
)
name = f"{firstname} {lastname}".strip()
email = extract(
attributes,
"email",
("urn:oid:0.9.2342.19200300.100.1.3", "urn:mace:dir:attribute-def:mail"),
)
username = extract(
attributes,
"username",
("urn:oid:0.9.2342.19200300.100.1.1", "urn:mace:dir:attribute-def:uid"),
)
if not username:
username = f"{lastname.lower()}{firstname[:1].lower()}".strip()
organization = extract(attributes, "organization", ("urn:oid:1.3.6.1.4.1.25178.1.2.9",))
unit = extract(
attributes,
"unit",
(
"urn:oid:2.5.4.11",
"urn:mace:dir:attribute-def:ou",
),
)
return dict(
name=name,
email=email,
username=username,
attributes=dict(
firstname=firstname,
lastname=lastname,
organization=organization,
unit=unit,
remote_user=remote_user,
saml_source=attributes,
),
)
So for my prototype I did something (not ideal) like this:
HOW ON EARTH did you figure that out?! THANK YOU! Cool hair color, btw. :-)
thx
just a disclaimer: the above is just an example. i'm using some saml properties that are defined on our eduID service provider. your properties will most likely be different (depending on the source you have etc)...
I've managed to find all the OIDs and such for what I'm returning. However, I have no idea how you're supposed to pass a list of groups back to Authentik. :-(
Okay! I've extracted all my data and matched it up to property names listed in the docs. However, none of the properties get updated. I have my mapping defined in the source. What am I missing?
My (well, mostly katrinm86's) user property mapping code:
from lxml import etree
NS_SAML_ASSERTION = "urn:oasis:names:tc:SAML:2.0:assertion"
assertion = root.find(f"{{{NS_SAML_ASSERTION}}}Assertion")
if assertion is None:
raise ValueError("Assertion element not found")
attribute_statement = assertion.find(f"{{{NS_SAML_ASSERTION}}}AttributeStatement")
if attribute_statement is None:
raise ValueError("AttributeStatement element not found")
def _name_id():
name_id = element.find(f"{{{NS_SAML_ASSERTION}}}NameID")
name_qualifier = name_id.attrib.get("NameQualifier", "")
spname_qualifier = name_id.attrib.get("SPNameQualifier", "")
return f"{name_qualifier}!{spname_qualifier}!{name_id.text}"
_extractors = {"urn:oid:1.3.6.1.4.1.5923.1.1.1.10": _name_id}
# extract attribute values from attribute statements into attributes dictionary
attributes = {}
for element in attribute_statement.findall(f"{{{NS_SAML_ASSERTION}}}Attribute"):
name = element.attrib["Name"]
element_value = element.find(f"{{{NS_SAML_ASSERTION}}}AttributeValue")
extractor_fun = _extractors.get(name, lambda el: el.text)
attributes[name] = extractor_fun(element_value)
def extract(lookup_list: tuple, is_mandatory: bool = False, def_value=None):
for attrib_name in lookup_list:
val = attributes.get(attrib_name, None)
if val:
return val
if is_mandatory:
raise ValueError(
f'No SAML source attribute found for mandatory value: "{name}"'
)
return def_value
uid = extract(("urn:oid:0.9.2342.19200300.100.1.1", "urn:mace:dir:attribute-def:uid"), is_mandatory=True)
name = extract(("urn:oid:2.5.4.41", "urn:mace:dir:attribute-def:name"))
displayName = extract(("urn:oid:2.16.840.1.113730.3.1.241", "urn:mace:dir:attribute-def:displayName"))
email = extract(("urn:oid:1.2.840.113549.1.9.1", "urn:mace:dir:attribute-def:email"))
# User fields.
results = {
"username": uid,
"email": email,
"uid": uid,
"name": displayName,
"is_active": True,
"is_staff": False,
}
return results
@sduensin have you checked the elements it finds or the attributes dict it extracts it into aka which attributes do you see? the code was really just used to experiment a bit (e.g. the only extractor that tries to do a bit more is for the name, the other ones are just assumed to be strings)
@sduensin have you checked the elements it finds or the
attributesdict it extracts it into aka which attributes do you see? the code was really just used to experiment a bit (e.g. the only extractor that tries to do a bit more is for the name, the other ones are just assumed to be strings)
I have! I finally managed to get some debug output from my code and it's doing exactly as expected.
Authentik just doesn't care. :-(
oh i totally missed the structure of the variables you return. it think you have to pack custom stuff into the 'attributes' dict. see: https://github.com/goauthentik/authentik/blob/643a97f0a56f23a767b90d0fcf615689a86bc404/authentik/core/models.py#L265 and attributes being here: https://github.com/goauthentik/authentik/blob/643a97f0a56f23a767b90d0fcf615689a86bc404/authentik/core/models.py#L107
edit: nevermind, all fields are existing afaik
except for 'uid' maybe? that seems to be a property on the user class
except for 'uid' maybe? that seems to be a property on the user class
According to https://version-2025-2.goauthentik.io/docs/users-sources/user/user_ref#object-properties , everything I have is a valid property.
This is beyond frustrating. :-( Thank you for all your help.