Skip to content

Commit b388265

Browse files
committed
providers/SCIM: fix object exists error for users, attempt to look up user ID in remote system (#13437)
* providers/scim: handle ObjectExistsSyncException when filtering is supported by remote system Signed-off-by: Jens Langhammer <[email protected]> * unrelated: correctly check for backchannel application in SCIM view page Signed-off-by: Jens Langhammer <[email protected]> * unrelated: fix missing ignore paths in codespell Signed-off-by: Jens Langhammer <[email protected]> * format Signed-off-by: Jens Langhammer <[email protected]> --------- Signed-off-by: Jens Langhammer <[email protected]> # Conflicts: # pyproject.toml
1 parent faefd97 commit b388265

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

authentik/providers/scim/clients/users.py

+32-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""User client"""
22

3+
from django.db import transaction
4+
from django.utils.http import urlencode
35
from pydantic import ValidationError
46

57
from authentik.core.models import User
68
from authentik.lib.sync.mapper import PropertyMappingManager
7-
from authentik.lib.sync.outgoing.exceptions import StopSync
9+
from authentik.lib.sync.outgoing.exceptions import ObjectExistsSyncException, StopSync
810
from authentik.policies.utils import delete_none_values
911
from authentik.providers.scim.clients.base import SCIMClient
1012
from authentik.providers.scim.clients.schema import SCIM_USER_SCHEMA
@@ -55,18 +57,35 @@ def delete(self, obj: User):
5557
def create(self, user: User):
5658
"""Create user from scratch and create a connection object"""
5759
scim_user = self.to_schema(user, None)
58-
response = self._request(
59-
"POST",
60-
"/Users",
61-
json=scim_user.model_dump(
62-
mode="json",
63-
exclude_unset=True,
64-
),
65-
)
66-
scim_id = response.get("id")
67-
if not scim_id or scim_id == "":
68-
raise StopSync("SCIM Response with missing or invalid `id`")
69-
return SCIMProviderUser.objects.create(provider=self.provider, user=user, scim_id=scim_id)
60+
with transaction.atomic():
61+
try:
62+
response = self._request(
63+
"POST",
64+
"/Users",
65+
json=scim_user.model_dump(
66+
mode="json",
67+
exclude_unset=True,
68+
),
69+
)
70+
except ObjectExistsSyncException as exc:
71+
if not self._config.filter.supported:
72+
raise exc
73+
users = self._request(
74+
"GET", f"/Users?{urlencode({'filter': f'userName eq {scim_user.userName}'})}"
75+
)
76+
users_res = users.get("Resources", [])
77+
if len(users_res) < 1:
78+
raise exc
79+
return SCIMProviderUser.objects.create(
80+
provider=self.provider, user=user, scim_id=users_res[0]["id"]
81+
)
82+
else:
83+
scim_id = response.get("id")
84+
if not scim_id or scim_id == "":
85+
raise StopSync("SCIM Response with missing or invalid `id`")
86+
return SCIMProviderUser.objects.create(
87+
provider=self.provider, user=user, scim_id=scim_id
88+
)
7089

7190
def update(self, user: User, connection: SCIMProviderUser):
7291
"""Update existing user"""

web/src/admin/providers/RelatedApplicationButton.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ export class RelatedApplicationButton extends AKElement {
2121
@property({ attribute: false })
2222
provider?: Provider;
2323

24+
@property()
25+
mode: "primary" | "backchannel" = "primary";
26+
2427
render(): TemplateResult {
25-
if (this.provider?.assignedApplicationSlug) {
28+
if (this.mode === "primary" && this.provider?.assignedApplicationSlug) {
2629
return html`<a href="#/core/applications/${this.provider.assignedApplicationSlug}">
2730
${this.provider.assignedApplicationName}
2831
</a>`;
2932
}
33+
if (this.mode === "backchannel" && this.provider?.assignedBackchannelApplicationSlug) {
34+
return html`<a
35+
href="#/core/applications/${this.provider.assignedBackchannelApplicationSlug}"
36+
>
37+
${this.provider.assignedBackchannelApplicationName}
38+
</a>`;
39+
}
3040
return html`<ak-forms-modal>
3141
<span slot="submit"> ${msg("Create")} </span>
3242
<span slot="header"> ${msg("Create Application")} </span>

web/src/admin/providers/scim/SCIMProviderViewPage.ts

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export class SCIMProviderViewPage extends AKElement {
173173
<dd class="pf-c-description-list__description">
174174
<div class="pf-c-description-list__text">
175175
<ak-provider-related-application
176+
mode="backchannel"
176177
.provider=${this.provider}
177178
></ak-provider-related-application>
178179
</div>

0 commit comments

Comments
 (0)