mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
chore: implement OIDCClaimFieldValues for idp sync mappings auto complete (#15576)
When creating IDP sync mappings, these are the values that can be selected from. These are the values that can be mapped from in org/group/role sync.
This commit is contained in:
@ -9869,6 +9869,67 @@ func (q *sqlQuerier) InsertUserLink(ctx context.Context, arg InsertUserLinkParam
|
||||
return i, err
|
||||
}
|
||||
|
||||
const oIDCClaimFieldValues = `-- name: OIDCClaimFieldValues :many
|
||||
SELECT
|
||||
-- DISTINCT to remove duplicates
|
||||
DISTINCT jsonb_array_elements_text(CASE
|
||||
-- When the type is an array, filter out any non-string elements.
|
||||
-- This is to keep the return type consistent.
|
||||
WHEN jsonb_typeof(claims->'merged_claims'->$1::text) = 'array' THEN
|
||||
(
|
||||
SELECT
|
||||
jsonb_agg(element)
|
||||
FROM
|
||||
jsonb_array_elements(claims->'merged_claims'->$1::text) AS element
|
||||
WHERE
|
||||
-- Filtering out non-string elements
|
||||
jsonb_typeof(element) = 'string'
|
||||
)
|
||||
-- Some IDPs return a single string instead of an array of strings.
|
||||
WHEN jsonb_typeof(claims->'merged_claims'->$1::text) = 'string' THEN
|
||||
jsonb_build_array(claims->'merged_claims'->$1::text)
|
||||
END)
|
||||
FROM
|
||||
user_links
|
||||
WHERE
|
||||
-- IDP sync only supports string and array (of string) types
|
||||
jsonb_typeof(claims->'merged_claims'->$1::text) = ANY(ARRAY['string', 'array'])
|
||||
AND login_type = 'oidc'
|
||||
AND CASE
|
||||
WHEN $2 :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
|
||||
user_links.user_id = ANY(SELECT organization_members.user_id FROM organization_members WHERE organization_id = $2)
|
||||
ELSE true
|
||||
END
|
||||
`
|
||||
|
||||
type OIDCClaimFieldValuesParams struct {
|
||||
ClaimField string `db:"claim_field" json:"claim_field"`
|
||||
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) OIDCClaimFieldValues(ctx context.Context, arg OIDCClaimFieldValuesParams) ([]string, error) {
|
||||
rows, err := q.db.QueryContext(ctx, oIDCClaimFieldValues, arg.ClaimField, arg.OrganizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []string
|
||||
for rows.Next() {
|
||||
var jsonb_array_elements_text string
|
||||
if err := rows.Scan(&jsonb_array_elements_text); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, jsonb_array_elements_text)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const oIDCClaimFields = `-- name: OIDCClaimFields :many
|
||||
SELECT
|
||||
DISTINCT jsonb_object_keys(claims->'merged_claims')
|
||||
|
Reference in New Issue
Block a user