mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: implement package and cli tool for repairing oidc links (#26418)
This commit is contained in:
@@ -113,3 +113,36 @@ WHERE
|
||||
ELSE true
|
||||
END
|
||||
;
|
||||
|
||||
-- name: CountOIDCLinkedIDsByIssuer :many
|
||||
-- Groups OIDC user links by their issuer prefix (the part before "||" in
|
||||
-- linked_id) and returns a count for each. Empty linked_ids are reported
|
||||
-- with an empty issuer_prefix. Used for analysis before resetting
|
||||
-- mismatched links.
|
||||
SELECT
|
||||
(CASE
|
||||
WHEN user_links.linked_id = '' THEN ''
|
||||
ELSE split_part(user_links.linked_id, '||', 1)
|
||||
END)::text AS issuer_prefix,
|
||||
COUNT(*)::int AS count
|
||||
FROM
|
||||
user_links
|
||||
INNER JOIN
|
||||
users ON user_links.user_id = users.id
|
||||
WHERE
|
||||
user_links.login_type = 'oidc'
|
||||
AND users.deleted = false
|
||||
GROUP BY issuer_prefix;
|
||||
|
||||
-- name: UnlinkOIDCUsersByIssuerMismatch :execrows
|
||||
-- Resets linked_id to '' for OIDC links where the linked_id is non-empty
|
||||
-- and does not begin with the expected issuer prefix. This allows users to
|
||||
-- re-authenticate under a new OIDC provider.
|
||||
UPDATE user_links
|
||||
SET linked_id = ''
|
||||
FROM users
|
||||
WHERE user_links.user_id = users.id
|
||||
AND user_links.login_type = 'oidc'
|
||||
AND user_links.linked_id != ''
|
||||
AND NOT starts_with(user_links.linked_id, @expected_prefix)
|
||||
AND users.deleted = false;
|
||||
|
||||
Reference in New Issue
Block a user