Skip to content

Commit c2a9587

Browse files
authored
feat: add claim mapping functionality to OIDC registration dialog (Dokploy#4712)
- Introduced a new mapping schema to handle user claims for OIDC providers, allowing for customizable mapping of claims to user fields. - Implemented default mapping logic for Azure and generic providers, ensuring appropriate claim handling based on the issuer. - Enhanced the registration dialog UI to include fields for claim mapping, improving user experience and configurability. - Updated parsing logic to accommodate claim mapping in OIDC configuration. This update enhances the flexibility of OIDC integration by allowing users to define how claims from identity providers map to application user fields.
1 parent 3ca5afd commit c2a9587

1 file changed

Lines changed: 123 additions & 15 deletions

File tree

‎apps/dokploy/components/proprietary/sso/register-oidc-dialog.tsx‎

Lines changed: 123 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,71 @@ const domainsArraySchema = z
4747

4848
const scopesArraySchema = z.array(z.string().trim());
4949

50+
const mappingSchema = z.object({
51+
id: z.string().min(1, "Required").trim(),
52+
email: z.string().min(1, "Required").trim(),
53+
emailVerified: z.string().trim(),
54+
name: z.string().min(1, "Required").trim(),
55+
image: z.string().trim(),
56+
});
57+
5058
const oidcProviderSchema = z.object({
5159
providerId: z.string().min(1, "Provider ID is required").trim(),
5260
issuer: z.string().min(1, "Issuer URL is required").url("Invalid URL").trim(),
5361
domains: domainsArraySchema,
5462
clientId: z.string().min(1, "Client ID is required").trim(),
5563
clientSecret: z.string().min(1, "Client secret is required"),
5664
scopes: scopesArraySchema,
65+
mapping: mappingSchema,
5766
});
5867

5968
type OidcProviderForm = z.infer<typeof oidcProviderSchema>;
6069

70+
type ClaimMapping = z.infer<typeof mappingSchema>;
71+
72+
const isAzureIssuer = (issuer: string) =>
73+
issuer.includes("login.microsoftonline.com");
74+
75+
// Microsoft Graph UserInfo endpoint (used by discovery) returns `email`, not
76+
// `preferred_username` — the latter is only present in the ID token. Default
77+
// Azure/Entra to the `email` claim so discovery-based login resolves the email.
78+
const azureMapping: ClaimMapping = {
79+
id: "sub",
80+
email: "email",
81+
emailVerified: "email_verified",
82+
name: "name",
83+
image: "",
84+
};
85+
86+
const genericMapping: ClaimMapping = {
87+
id: "sub",
88+
email: "email",
89+
emailVerified: "email_verified",
90+
name: "preferred_username",
91+
image: "picture",
92+
};
93+
94+
const defaultMappingFor = (issuer: string): ClaimMapping =>
95+
isAzureIssuer(issuer) ? azureMapping : genericMapping;
96+
97+
const MAPPING_FIELDS: Array<{
98+
key: keyof ClaimMapping;
99+
label: string;
100+
placeholder: string;
101+
optional?: boolean;
102+
}> = [
103+
{ key: "id", label: "User ID", placeholder: "sub" },
104+
{ key: "email", label: "Email", placeholder: "email" },
105+
{
106+
key: "emailVerified",
107+
label: "Email verified",
108+
placeholder: "email_verified",
109+
optional: true,
110+
},
111+
{ key: "name", label: "Name", placeholder: "name" },
112+
{ key: "image", label: "Image", placeholder: "picture", optional: true },
113+
];
114+
61115
interface RegisterOidcDialogProps {
62116
providerId?: string;
63117
children: React.ReactNode;
@@ -70,24 +124,31 @@ const formDefaultValues = {
70124
clientId: "",
71125
clientSecret: "",
72126
scopes: [...DEFAULT_SCOPES],
127+
mapping: { ...genericMapping },
73128
};
74129

75130
function parseOidcConfig(oidcConfig: string | null): {
76131
clientId?: string;
77132
clientSecret?: string;
78133
scopes?: string[];
134+
mapping?: Partial<ClaimMapping>;
79135
} | null {
80136
if (!oidcConfig) return null;
81137
try {
82138
const parsed = JSON.parse(oidcConfig) as {
83139
clientId?: string;
84140
clientSecret?: string;
85141
scopes?: string[];
142+
mapping?: Partial<ClaimMapping>;
86143
};
87144
return {
88145
clientId: parsed.clientId,
89146
clientSecret: parsed.clientSecret,
90147
scopes: Array.isArray(parsed.scopes) ? parsed.scopes : undefined,
148+
mapping:
149+
parsed.mapping && typeof parsed.mapping === "object"
150+
? parsed.mapping
151+
: undefined,
91152
};
92153
} catch {
93154
return null;
@@ -127,8 +188,22 @@ export function RegisterOidcDialog({
127188
defaultValue: "",
128189
});
129190

191+
const watchedIssuer = useWatch({
192+
control: form.control,
193+
name: "issuer",
194+
defaultValue: "",
195+
});
196+
130197
const baseURL = useUrl();
131198

199+
// Register mode: keep the mapping defaults in sync with the issuer so
200+
// Azure/Entra gets the `email`-claim default. Editing the issuer after
201+
// customizing the mapping resets it to the issuer's default.
202+
useEffect(() => {
203+
if (isEdit) return;
204+
form.setValue("mapping", { ...defaultMappingFor(watchedIssuer) });
205+
}, [watchedIssuer, isEdit, form]);
206+
132207
useEffect(() => {
133208
if (!data || !open) return;
134209
const domains = data.domain
@@ -139,6 +214,7 @@ export function RegisterOidcDialog({
139214
: [""];
140215
if (domains.length === 0) domains.push("");
141216
const oidc = parseOidcConfig(data.oidcConfig);
217+
const baseMapping = defaultMappingFor(data.issuer);
142218
form.reset({
143219
providerId: data.providerId,
144220
issuer: data.issuer,
@@ -149,6 +225,14 @@ export function RegisterOidcDialog({
149225
oidc?.scopes && oidc.scopes.length > 0
150226
? oidc.scopes
151227
: [...DEFAULT_SCOPES],
228+
mapping: {
229+
id: oidc?.mapping?.id ?? baseMapping.id,
230+
email: oidc?.mapping?.email ?? baseMapping.email,
231+
emailVerified:
232+
oidc?.mapping?.emailVerified ?? baseMapping.emailVerified,
233+
name: oidc?.mapping?.name ?? baseMapping.name,
234+
image: oidc?.mapping?.image ?? baseMapping.image,
235+
},
152236
});
153237
}, [data, open, form]);
154238

@@ -174,21 +258,17 @@ export function RegisterOidcDialog({
174258
? data.scopes.filter(Boolean)
175259
: DEFAULT_SCOPES;
176260

177-
const isAzure = data.issuer.includes("login.microsoftonline.com");
178-
const mapping = isAzure
179-
? {
180-
id: "sub",
181-
email: "preferred_username",
182-
emailVerified: "email_verified",
183-
name: "name",
184-
}
185-
: {
186-
id: "sub",
187-
email: "email",
188-
emailVerified: "email_verified",
189-
name: "preferred_username",
190-
image: "picture",
191-
};
261+
const mapping = {
262+
id: data.mapping.id.trim(),
263+
email: data.mapping.email.trim(),
264+
name: data.mapping.name.trim(),
265+
...(data.mapping.emailVerified.trim()
266+
? { emailVerified: data.mapping.emailVerified.trim() }
267+
: {}),
268+
...(data.mapping.image.trim()
269+
? { image: data.mapping.image.trim() }
270+
: {}),
271+
};
192272
await mutateAsync({
193273
providerId: data.providerId,
194274
issuer: data.issuer,
@@ -426,6 +506,34 @@ export function RegisterOidcDialog({
426506
/>
427507
))}
428508
</div>
509+
<div className="space-y-2">
510+
<FormLabel>Claim mapping</FormLabel>
511+
<FormDescription>
512+
Which provider claims map to each user field. Defaults adapt to
513+
the issuer.
514+
</FormDescription>
515+
<div className="grid grid-cols-2 gap-3">
516+
{MAPPING_FIELDS.map(({ key, label, placeholder, optional }) => (
517+
<FormField
518+
key={key}
519+
control={form.control}
520+
name={`mapping.${key}`}
521+
render={({ field }) => (
522+
<FormItem>
523+
<FormLabel className="text-xs text-muted-foreground">
524+
{label}
525+
{optional ? " (optional)" : ""}
526+
</FormLabel>
527+
<FormControl>
528+
<Input placeholder={placeholder} {...field} />
529+
</FormControl>
530+
<FormMessage />
531+
</FormItem>
532+
)}
533+
/>
534+
))}
535+
</div>
536+
</div>
429537
<DialogFooter>
430538
<Button
431539
type="button"

0 commit comments

Comments
 (0)