Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

providers/saml: configurable AuthnContextClassRef #13566

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions authentik/providers/saml/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class Meta:
"session_valid_not_on_or_after",
"property_mappings",
"name_id_mapping",
"authn_context_class_ref_mapping",
"digest_algorithm",
"signature_algorithm",
"signing_kp",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.0.13 on 2025-03-18 17:41

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("authentik_providers_saml", "0016_samlprovider_encryption_kp_and_more"),
]

operations = [
migrations.AddField(
model_name="samlprovider",
name="authn_context_class_ref_mapping",
field=models.ForeignKey(
blank=True,
default=None,
help_text="Configure how the AuthnContextClassRef value will be created. When left empty, the AuthnContextClassRef will be set based on which authentication methods the user used to authenticate.",
null=True,
on_delete=django.db.models.deletion.SET_DEFAULT,
related_name="+",
to="authentik_providers_saml.samlpropertymapping",
verbose_name="AuthnContextClassRef Property Mapping",
),
),
]
15 changes: 14 additions & 1 deletion authentik/providers/saml/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ class SAMLProvider(Provider):
"the NameIDPolicy of the incoming request will be considered"
),
)
authn_context_class_ref_mapping = models.ForeignKey(
"SAMLPropertyMapping",
default=None,
blank=True,
null=True,
on_delete=models.SET_DEFAULT,
verbose_name=_("AuthnContextClassRef Property Mapping"),
related_name="+",
help_text=_(
"Configure how the AuthnContextClassRef value will be created. When left empty, "
"the AuthnContextClassRef will be set based on which authentication methods the user "
"used to authenticate."
),
)

assertion_valid_not_before = models.TextField(
default="minutes=-5",
Expand Down Expand Up @@ -170,7 +184,6 @@ class SAMLProvider(Provider):
def launch_url(self) -> str | None:
"""Use IDP-Initiated SAML flow as launch URL"""
try:

return reverse(
"authentik_providers_saml:sso-init",
kwargs={"application_slug": self.application.slug},
Expand Down
31 changes: 30 additions & 1 deletion authentik/providers/saml/processors/assertion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""SAML Assertion generator"""

from datetime import datetime
from hashlib import sha256
from types import GeneratorType

Expand Down Expand Up @@ -52,6 +53,7 @@
_assertion_id: str
_response_id: str

_auth_instant: str
_valid_not_before: str
_session_not_on_or_after: str
_valid_not_on_or_after: str
Expand All @@ -65,6 +67,11 @@
self._assertion_id = get_random_id()
self._response_id = get_random_id()

_login_event = get_login_event(self.http_request)
_login_time = datetime.now()
if _login_event:
_login_time = _login_event.created
self._auth_instant = get_time_string(_login_time)
self._valid_not_before = get_time_string(
timedelta_from_string(self.provider.assertion_valid_not_before)
)
Expand Down Expand Up @@ -131,7 +138,7 @@
def get_assertion_auth_n_statement(self) -> Element:
"""Generate AuthnStatement with AuthnContext and ContextClassRef Elements."""
auth_n_statement = Element(f"{{{NS_SAML_ASSERTION}}}AuthnStatement")
auth_n_statement.attrib["AuthnInstant"] = self._valid_not_before
auth_n_statement.attrib["AuthnInstant"] = self._auth_instant
auth_n_statement.attrib["SessionIndex"] = sha256(
self.http_request.session.session_key.encode("ascii")
).hexdigest()
Expand All @@ -158,6 +165,28 @@
auth_n_context_class_ref.text = (
"urn:oasis:names:tc:SAML:2.0:ac:classes:MobileOneFactorContract"
)
if self.provider.authn_context_class_ref_mapping:
try:
value = self.provider.authn_context_class_ref_mapping.evaluate(

Check warning on line 170 in authentik/providers/saml/processors/assertion.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/processors/assertion.py#L169-L170

Added lines #L169 - L170 were not covered by tests
user=self.http_request.user,
request=self.http_request,
provider=self.provider,
)
if value is not None:
auth_n_context_class_ref.text = str(value)
return auth_n_statement
except PropertyMappingExpressionException as exc:
Event.new(

Check warning on line 179 in authentik/providers/saml/processors/assertion.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/processors/assertion.py#L175-L179

Added lines #L175 - L179 were not covered by tests
EventAction.CONFIGURATION_ERROR,
message=(
"Failed to evaluate property-mapping: "
f"'{self.provider.authn_context_class_ref_mapping.name}'"
),
provider=self.provider,
mapping=self.provider.authn_context_class_ref_mapping,
).from_http(self.http_request)
LOGGER.warning("Failed to evaluate property mapping", exc=exc)
return auth_n_statement

Check warning on line 189 in authentik/providers/saml/processors/assertion.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/processors/assertion.py#L188-L189

Added lines #L188 - L189 were not covered by tests
return auth_n_statement

def get_assertion_conditions(self) -> Element:
Expand Down
63 changes: 60 additions & 3 deletions authentik/providers/saml/tests/test_auth_n_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,61 @@
self.assertEqual(parsed_request.id, "aws_LDxLGeubpc5lx12gxCgS6uPbix1yd5re")
self.assertEqual(parsed_request.name_id_policy, SAML_NAME_ID_FORMAT_EMAIL)

def test_authn_context_class_ref_mapping(self):
"""Test custom authn_context_class_ref"""
authn_context_class_ref = generate_id()
mapping = SAMLPropertyMapping.objects.create(

Check warning on line 300 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L299-L300

Added lines #L299 - L300 were not covered by tests
name=generate_id(), expression=f"""return '{authn_context_class_ref}'"""
)
self.provider.authn_context_class_ref_mapping = mapping
self.provider.save()
user = create_test_admin_user()
http_request = get_request("/", user=user)

Check warning on line 306 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L303-L306

Added lines #L303 - L306 were not covered by tests

# First create an AuthNRequest
request_proc = RequestProcessor(self.source, http_request, "test_state")
request = request_proc.build_auth_n()

Check warning on line 310 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L309-L310

Added lines #L309 - L310 were not covered by tests

# To get an assertion we need a parsed request (parsed by provider)
parsed_request = AuthNRequestParser(self.provider).parse(

Check warning on line 313 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L313

Added line #L313 was not covered by tests
b64encode(request.encode()).decode(), "test_state"
)
# Now create a response and convert it to string (provider)
response_proc = AssertionProcessor(self.provider, http_request, parsed_request)
response = response_proc.build_response()
self.assertIn(user.username, response)
self.assertIn(authn_context_class_ref, response)

Check warning on line 320 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L317-L320

Added lines #L317 - L320 were not covered by tests

def test_authn_context_class_ref_mapping_invalid(self):
"""Test custom authn_context_class_ref (invalid)"""
mapping = SAMLPropertyMapping.objects.create(name=generate_id(), expression="q")
self.provider.authn_context_class_ref_mapping = mapping
self.provider.save()
user = create_test_admin_user()
http_request = get_request("/", user=user)

Check warning on line 328 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L324-L328

Added lines #L324 - L328 were not covered by tests

# First create an AuthNRequest
request_proc = RequestProcessor(self.source, http_request, "test_state")
request = request_proc.build_auth_n()

Check warning on line 332 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L331-L332

Added lines #L331 - L332 were not covered by tests

# To get an assertion we need a parsed request (parsed by provider)
parsed_request = AuthNRequestParser(self.provider).parse(

Check warning on line 335 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L335

Added line #L335 was not covered by tests
b64encode(request.encode()).decode(), "test_state"
)
# Now create a response and convert it to string (provider)
response_proc = AssertionProcessor(self.provider, http_request, parsed_request)
response = response_proc.build_response()
self.assertIn(user.username, response)

Check warning on line 341 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L339-L341

Added lines #L339 - L341 were not covered by tests

events = Event.objects.filter(

Check warning on line 343 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L343

Added line #L343 was not covered by tests
action=EventAction.CONFIGURATION_ERROR,
)
self.assertTrue(events.exists())
self.assertEqual(

Check warning on line 347 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L346-L347

Added lines #L346 - L347 were not covered by tests
events.first().context["message"],
f"Failed to evaluate property-mapping: '{mapping.name}'",
)

def test_request_attributes(self):
"""Test full SAML Request/Response flow, fully signed"""
user = create_test_admin_user()
Expand Down Expand Up @@ -321,8 +376,10 @@
request = request_proc.build_auth_n()

# Create invalid PropertyMapping
scope = SAMLPropertyMapping.objects.create(name="test", saml_name="test", expression="q")
self.provider.property_mappings.add(scope)
mapping = SAMLPropertyMapping.objects.create(

Check warning on line 379 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L379

Added line #L379 was not covered by tests
name=generate_id(), saml_name="test", expression="q"
)
self.provider.property_mappings.add(mapping)

Check warning on line 382 in authentik/providers/saml/tests/test_auth_n_request.py

View check run for this annotation

Codecov / codecov/patch

authentik/providers/saml/tests/test_auth_n_request.py#L382

Added line #L382 was not covered by tests

# To get an assertion we need a parsed request (parsed by provider)
parsed_request = AuthNRequestParser(self.provider).parse(
Expand All @@ -338,7 +395,7 @@
self.assertTrue(events.exists())
self.assertEqual(
events.first().context["message"],
"Failed to evaluate property-mapping: 'test'",
f"Failed to evaluate property-mapping: '{mapping.name}'",
)

def test_idp_initiated(self):
Expand Down
14 changes: 9 additions & 5 deletions authentik/providers/saml/utils/time.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""Time utilities"""

import datetime
from datetime import datetime, timedelta

from django.utils.timezone import now

def get_time_string(delta: datetime.timedelta | None = None) -> str:

def get_time_string(delta: timedelta | datetime | None = None) -> str:
"""Get Data formatted in SAML format"""
if delta is None:
delta = datetime.timedelta()
now = datetime.datetime.now()
final = now + delta
delta = timedelta()
if isinstance(delta, timedelta):
final = now() + delta
else:
final = delta
return final.strftime("%Y-%m-%dT%H:%M:%SZ")
5 changes: 5 additions & 0 deletions blueprints/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6462,6 +6462,11 @@
"title": "NameID Property Mapping",
"description": "Configure how the NameID value will be created. When left empty, the NameIDPolicy of the incoming request will be considered"
},
"authn_context_class_ref_mapping": {
"type": "integer",
"title": "AuthnContextClassRef Property Mapping",
"description": "Configure how the AuthnContextClassRef value will be created. When left empty, the AuthnContextClassRef will be set based on which authentication methods the user used to authenticate."
},
"digest_algorithm": {
"type": "string",
"enum": [
Expand Down
31 changes: 30 additions & 1 deletion schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22191,6 +22191,11 @@ paths:
schema:
type: string
format: uuid
- in: query
name: authn_context_class_ref_mapping
schema:
type: string
format: uuid
- in: query
name: authorization_flow
schema:
Expand Down Expand Up @@ -25745,7 +25750,7 @@ paths:
description: ''
delete:
operationId: sources_all_destroy
description: Source Viewset
description: Prevent deletion of built-in sources
parameters:
- in: path
name: slug
Expand Down Expand Up @@ -52228,6 +52233,14 @@ components:
title: NameID Property Mapping
description: Configure how the NameID value will be created. When left empty,
the NameIDPolicy of the incoming request will be considered
authn_context_class_ref_mapping:
type: string
format: uuid
nullable: true
title: AuthnContextClassRef Property Mapping
description: Configure how the AuthnContextClassRef value will be created.
When left empty, the AuthnContextClassRef will be set based on which authentication
methods the user used to authenticate.
digest_algorithm:
$ref: '#/components/schemas/DigestAlgorithmEnum'
signature_algorithm:
Expand Down Expand Up @@ -55183,6 +55196,14 @@ components:
title: NameID Property Mapping
description: Configure how the NameID value will be created. When left empty,
the NameIDPolicy of the incoming request will be considered
authn_context_class_ref_mapping:
type: string
format: uuid
nullable: true
title: AuthnContextClassRef Property Mapping
description: Configure how the AuthnContextClassRef value will be created.
When left empty, the AuthnContextClassRef will be set based on which authentication
methods the user used to authenticate.
digest_algorithm:
$ref: '#/components/schemas/DigestAlgorithmEnum'
signature_algorithm:
Expand Down Expand Up @@ -55348,6 +55369,14 @@ components:
title: NameID Property Mapping
description: Configure how the NameID value will be created. When left empty,
the NameIDPolicy of the incoming request will be considered
authn_context_class_ref_mapping:
type: string
format: uuid
nullable: true
title: AuthnContextClassRef Property Mapping
description: Configure how the AuthnContextClassRef value will be created.
When left empty, the AuthnContextClassRef will be set based on which authentication
methods the user used to authenticate.
digest_algorithm:
$ref: '#/components/schemas/DigestAlgorithmEnum'
signature_algorithm:
Expand Down
35 changes: 35 additions & 0 deletions web/src/admin/providers/saml/SAMLProviderFormForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,41 @@ export function renderForm(
)}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${msg("AuthnContextClassRef Property Mapping")}
name="authnContextClassRefMapping"
>
<ak-search-select
.fetchObjects=${async (query?: string): Promise<SAMLPropertyMapping[]> => {
const args: PropertymappingsProviderSamlListRequest = {
ordering: "saml_name",
};
if (query !== undefined) {
args.search = query;
}
const items = await new PropertymappingsApi(
DEFAULT_CONFIG,
).propertymappingsProviderSamlList(args);
return items.results;
}}
.renderElement=${(item: SAMLPropertyMapping): string => {
return item.name;
}}
.value=${(item: SAMLPropertyMapping | undefined): string | undefined => {
return item?.pk;
}}
.selected=${(item: SAMLPropertyMapping): boolean => {
return provider?.authnContextClassRefMapping === item.pk;
}}
?blankable=${true}
>
</ak-search-select>
<p class="pf-c-form__helper-text">
${msg(
"Configure how the AuthnContextClassRef value will be created. When left empty, the AuthnContextClassRef will be set based on which authentication methods the user used to authenticate.",
)}
</p>
</ak-form-element-horizontal>

<ak-text-input
name="assertionValidNotBefore"
Expand Down
Loading