authentik icon indicating copy to clipboard operation
authentik copied to clipboard

sources/saml: Allow user matching via attributes when NameID is transient

Open ikob opened this issue 1 year ago β€’ 12 comments

Details

This PR extends the behavior when handling SAML Assertions with a transient NameID, as discussed in https://github.com/goauthentik/authentik/issues/12766

In the current implementation, if the NameID is transient, the system immediately creates a temporary user without evaluating any SAML attributes. However, some Identity Providers (IdPs) expect that attribute-based user identification will still occur in such cases.

This patch introduces a proof-of-concept behavior in which, when NameID is transient, certain attributes are used to construct a provisional username, which is then passed to the user matching process according to the configured SourceUserMatchingModes. This approach is more aligned with how platforms like Authentik handle such scenarios.

Specifically:

  • If SourceUserMatchingModes is EMAIL_LINK or DENY, and an email attribute is present, it is used as the provisional username.
  • If USERNAME_LINK or DENY, and an ePPN attribute is present, it is used as the provisional username.
  • In all other cases (e.g., modes including IDENTITY), the system falls back to the existing behavior, using the transient NameID as the username to create a temporary user, which will be deleted upon logout or after a timeout.

In addition, this patch changes the behavior so that if the user's username is explicitly set via property mapping, the user is no longer treated as temporary β€” even if the original NameID was transient.


Checklist

  • [x] Local tests pass (ak test authentik/)
  • [ ] The code has been formatted (make lint-fix)

If an API change has been made

  • [ ] The API schema has been updated (make gen-build)

If changes to the frontend have been made

  • [ ] The code has been formatted (make web)

If applicable

  • [ ] The documentation has been updated
  • [ ] The documentation has been formatted (make docs)

ikob avatar Nov 23 '25 05:11 ikob

Deploy Preview for authentik-integrations ready!

Name Link
Latest commit 227e48aae9415af9f9ceae58c484cde7ca261385
Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/697f44dfa4e83e0008c96940
Deploy Preview https://deploy-preview-18331--authentik-integrations.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify avatar Nov 23 '25 05:11 netlify

Deploy Preview for authentik-storybook ready!

Name Link
Latest commit 227e48aae9415af9f9ceae58c484cde7ca261385
Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/697f44df31f07600082e1781
Deploy Preview https://deploy-preview-18331--authentik-storybook.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify avatar Nov 23 '25 05:11 netlify

Deploy Preview for authentik-docs ready!

Name Link
Latest commit 2d3f57da57b2f612266a5cfa0783886452631781
Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6961c99a29a4b600080715b6
Deploy Preview https://deploy-preview-18331--authentik-docs.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify avatar Nov 23 '25 06:11 netlify

Codecov Report

:x: Patch coverage is 88.57143% with 8 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 93.40%. Comparing base (5a33833) to head (1ddea9d).

Files with missing lines Patch % Lines
authentik/sources/saml/processors/response.py 66.66% 7 Missing :warning:
authentik/sources/saml/models.py 90.00% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #18331      +/-   ##
==========================================
- Coverage   93.45%   93.40%   -0.05%     
==========================================
  Files        1058     1058              
  Lines       63107    63156      +49     
  Branches      424      424              
==========================================
+ Hits        58974    58990      +16     
- Misses       4133     4166      +33     
Flag Coverage Ξ”
conformance 35.34% <4.28%> (-0.07%) :arrow_down:
e2e 40.40% <18.57%> (-0.02%) :arrow_down:
integration 31.37% <4.28%> (-0.50%) :arrow_down:
rust 0.00% <ΓΈ> (ΓΈ)
unit 92.63% <88.57%> (-0.01%) :arrow_down:
unit-migrate 92.64% <88.57%> (+0.01%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Nov 23 '25 06:11 codecov[bot]

instead of adding the transient ID to the user attributes, it should be saved in a UserSAMLSourceConnection object (will also need to add a field to that, probably a nullable text field transient_id)

BeryJu avatar Dec 30 '25 18:12 BeryJu

instead of adding the transient ID to the user attributes, it should be saved in a UserSAMLSourceConnection object (will also need to add a field to that, probably a nullable text field transient_id)

A temporary token derived from the transient NameID may be stored to support lifecycle-related behavior such as logout and expiry. Although it is expected to expire, the original transient NameID is intentionally not stored in clear form.

Note: The primary goal of this PR is to preserve and make SAML assertion attributes available even when the NameID format is transient. The temporary token exists only as a supporting mechanism for this behavior.

ikob avatar Jan 09 '26 06:01 ikob

@ikob Apologies, are you saying that there is an implementation reason as to why we cannot store a representation of the transient NameID in the UserSAMLSourceConnection instead of the User Attributes? If so, could you point it out to me so we can discuss alternatives?

PeshekDotDev avatar Jan 15 '26 17:01 PeshekDotDev

@PeshekDotDev, I’d like to better understand the reasoning behind the suggestion to store the transient NameID (or a derived representation) in UserSAMLSourceConnection rather than in user attributes.

At this stage, I’m not trying to push a particular design decision in this PR; I mainly want to understand the motivation and assumptions behind that suggestion.

For example, is the concern primarily about visibility β€” that storing any representation of a transient NameID in user attributes is undesirable because attributes may be visible to users or administrators?

Or is there a more general architectural reason why user attributes are not considered suitable for holding transient, lifecycle-scoped data (used only for logout or expiry handling)?

ikob avatar Jan 16 '26 01:01 ikob

It's mainly just a organization/cleanlyness concern. We want to prevent the user attributes as light as possible, and the session is already designed to be temporary, so it makes sense to keep it in that space organizationally to us

PeshekDotDev avatar Jan 22 '26 17:01 PeshekDotDev

@PeshekDotDev, I understand the preference to keep user attributes as lightweight as possible, and that transient data fits better in a session- or connection-scoped structure.

I’ll take this into account and consider alternatives (e.g. using UserSourceConnection) when looking at next steps.

ikob avatar Jan 24 '26 01:01 ikob

@PeshekDotDev,

Following the discussion, I decided to keep this PR minimal: it does not add transient data to user attributes and does not modify UserSAMLSourceConnection.

The change is limited to adjusting how attributes are used during user matching when a transient NameID is received.

ikob avatar Feb 01 '26 04:02 ikob

Updated this branch to the latest upstream/main (5a3383338e) and rebuilt the transient SAML changes on top of it.

ikob avatar Jul 18 '26 08:07 ikob