Skip to content

Commit d542229

Browse files
author
Rajat
committed
feat: hardcoding sso providerId
1 parent a33b29b commit d542229

7 files changed

Lines changed: 59 additions & 162 deletions

File tree

apps/web/app/(with-contexts)/dashboard/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import { getProfile } from "../action";
33
import { Profile } from "@courselit/common-models";
44
import { checkPermission } from "@courselit/utils";
55
import { ADMIN_PERMISSIONS } from "@ui-config/constants";
6+
import { auth } from "@/auth";
67

78
export default async function Page() {
89
const profile = (await getProfile()) as Profile;
910

11+
if (!profile) {
12+
await auth.signOut();
13+
}
14+
1015
if (checkPermission(profile?.permissions, ADMIN_PERMISSIONS)) {
1116
redirect("/dashboard/overview");
1217
} else {

apps/web/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const config: any = {
6464
(await UserModel.findOne({ _id: user.id })
6565
.select("userId")
6666
.lean()) as unknown as any
67-
).userId,
67+
)?.userId,
6868
},
6969
session: {
7070
...session,

apps/web/components/admin/settings/sso.tsx

Lines changed: 45 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ import { useToast } from "@courselit/components-library";
55
import { FetchBuilder } from "@courselit/utils";
66
import {
77
SITE_MISCELLANEOUS_SETTING_HEADER,
8-
// SSO_PROVIDER_CALLBACK_URL_LABEL,
98
SSO_PROVIDER_CERT_LABEL,
10-
// SSO_PROVIDER_DOMAIN_LABEL,
119
SSO_PROVIDER_ENTRY_POINT_LABEL,
1210
SSO_PROVIDER_IDP_METADATA_LABEL,
1311
SSO_PROVIDER_HEADER,
14-
SSO_PROVIDER_PROVIDER_ID_LABEL,
1512
SSO_PROVIDER_SUCCESS_MESSAGE,
1613
TOAST_TITLE_ERROR,
1714
TOAST_TITLE_SUCCESS,
@@ -20,7 +17,6 @@ import {
2017
BUTTON_SAVE,
2118
SSO_PROVIDER_CARD_HEADER,
2219
SSO_PROVIDER_CARD_DESCRIPTION,
23-
SSO_PROVIDER_SP_EMTPY,
2420
SSO_PROVIDER_SP_ACS_LABEL,
2521
SSO_PROVIDER_SP_ENTITY_ID_LABEL,
2622
} from "@ui-config/strings";
@@ -51,7 +47,7 @@ import {
5147
AlertDialogTitle,
5248
AlertDialogTrigger,
5349
} from "@components/ui/alert-dialog";
54-
import { Trash2, Loader2, Save, Copy, Key } from "lucide-react";
50+
import { Trash2, Loader2, Save, Copy } from "lucide-react";
5551
import {
5652
Card,
5753
CardContent,
@@ -63,18 +59,9 @@ import {
6359
import { Label } from "@components/ui/label";
6460

6561
const formSchema = z.object({
66-
providerId: z
67-
.string()
68-
.min(1, "Provider ID is required")
69-
.regex(
70-
/^[a-z-0-9]+$/,
71-
"Provider ID can only contain lowercase letters and hyphens",
72-
),
7362
idpMetadata: z.string().min(1, "IDP Metadata is required"),
74-
// domain: z.string().min(1, "Domain is required"),
7563
entryPoint: z.string().min(1, "Entry Point is required"),
7664
cert: z.string().min(1, "Certificate is required"),
77-
// backend: z.string().min(1, "Callback URL is required"),
7865
});
7966

8067
type FormData = z.infer<typeof formSchema>;
@@ -97,9 +84,7 @@ export default function SSOProvider({ address }: NewSSOProviderProps) {
9784
const form = useForm<FormData>({
9885
resolver: zodResolver(formSchema),
9986
defaultValues: {
100-
providerId: "",
10187
idpMetadata: "",
102-
// domain: "",
10388
entryPoint: "",
10489
cert: "",
10590
},
@@ -110,7 +95,6 @@ export default function SSOProvider({ address }: NewSSOProviderProps) {
11095
const query = `
11196
query {
11297
ssoProvider: getSSOProviderSettings {
113-
providerId
11498
idpMetadata
11599
entryPoint
116100
cert
@@ -126,7 +110,6 @@ export default function SSOProvider({ address }: NewSSOProviderProps) {
126110
const response = await fetcher.exec();
127111
const { ssoProvider } = response;
128112
if (ssoProvider) {
129-
form.setValue("providerId", ssoProvider.providerId);
130113
form.setValue("idpMetadata", ssoProvider.idpMetadata);
131114
form.setValue("entryPoint", ssoProvider.entryPoint);
132115
form.setValue("cert", ssoProvider.cert);
@@ -147,13 +130,11 @@ export default function SSOProvider({ address }: NewSSOProviderProps) {
147130
const query = `
148131
mutation (
149132
$idpMetadata: String!,
150-
$providerId: String!,
151133
$entryPoint: String!,
152134
$cert: String!,
153135
$backend: String!
154136
) {
155137
ssoProvider: updateSSOProvider(
156-
providerId: $providerId
157138
idpMetadata: $idpMetadata,
158139
entryPoint: $entryPoint,
159140
cert: $cert,
@@ -264,11 +245,11 @@ export default function SSOProvider({ address }: NewSSOProviderProps) {
264245
>
265246
<FormField
266247
control={form.control}
267-
name="providerId"
248+
name="entryPoint"
268249
render={({ field }) => (
269250
<FormItem>
270251
<FormLabel>
271-
{SSO_PROVIDER_PROVIDER_ID_LABEL}
252+
{SSO_PROVIDER_ENTRY_POINT_LABEL}
272253
</FormLabel>
273254
<FormControl>
274255
<Input {...field} />
@@ -297,36 +278,6 @@ export default function SSOProvider({ address }: NewSSOProviderProps) {
297278
</FormItem>
298279
)}
299280
/>
300-
{/* <FormField
301-
control={form.control}
302-
name="domain"
303-
render={({ field }) => (
304-
<FormItem>
305-
<FormLabel>
306-
{SSO_PROVIDER_DOMAIN_LABEL}
307-
</FormLabel>
308-
<FormControl>
309-
<Input {...field} />
310-
</FormControl>
311-
<FormMessage />
312-
</FormItem>
313-
)}
314-
/> */}
315-
<FormField
316-
control={form.control}
317-
name="entryPoint"
318-
render={({ field }) => (
319-
<FormItem>
320-
<FormLabel>
321-
{SSO_PROVIDER_ENTRY_POINT_LABEL}
322-
</FormLabel>
323-
<FormControl>
324-
<Input {...field} />
325-
</FormControl>
326-
<FormMessage />
327-
</FormItem>
328-
)}
329-
/>
330281
<FormField
331282
control={form.control}
332283
name="cert"
@@ -345,21 +296,6 @@ export default function SSOProvider({ address }: NewSSOProviderProps) {
345296
</FormItem>
346297
)}
347298
/>
348-
{/* <FormField
349-
control={form.control}
350-
name="backend"
351-
render={({ field }) => (
352-
<FormItem>
353-
<FormLabel>
354-
{SSO_PROVIDER_CALLBACK_URL_LABEL}
355-
</FormLabel>
356-
<FormControl>
357-
<Input {...field} />
358-
</FormControl>
359-
<FormMessage />
360-
</FormItem>
361-
)}
362-
/> */}
363299
<div>
364300
<Button type="submit" disabled={loading}>
365301
<Save className="mr-2 h-4 w-4" />
@@ -437,59 +373,50 @@ export default function SSOProvider({ address }: NewSSOProviderProps) {
437373
</CardDescription>
438374
</CardHeader>
439375
<CardContent>
440-
{!providerId ? (
441-
<div className="flex flex-col items-center justify-center py-8 text-muted-foreground">
442-
<Key className="h-8 w-8 mb-2 opacity-50" />
443-
<p>{SSO_PROVIDER_SP_EMTPY}</p>
444-
</div>
445-
) : (
446-
<>
447-
<div>
448-
<Label>{SSO_PROVIDER_SP_ACS_LABEL}</Label>
449-
<div className="flex gap-2">
450-
<Input
451-
type="text"
452-
disabled={true}
453-
value={`${address.backend}/api/auth/sso/saml2/sp/acs/${providerId}`}
454-
/>
455-
<Button
456-
variant="outline"
457-
size="icon"
458-
onClick={() =>
459-
copyToClipboard(
460-
`${address.backend}/api/auth/sso/saml2/sp/acs/${providerId}`,
461-
)
462-
}
463-
>
464-
<Copy className="h-4 w-4" />
465-
</Button>
466-
</div>
376+
<>
377+
<div>
378+
<Label>{SSO_PROVIDER_SP_ACS_LABEL}</Label>
379+
<div className="flex gap-2">
380+
<Input
381+
type="text"
382+
disabled={true}
383+
value={`${address.backend}/api/auth/sso/saml2/sp/acs/sso`}
384+
/>
385+
<Button
386+
variant="outline"
387+
size="icon"
388+
onClick={() =>
389+
copyToClipboard(
390+
`${address.backend}/api/auth/sso/saml2/sp/acs/sso`,
391+
)
392+
}
393+
>
394+
<Copy className="h-4 w-4" />
395+
</Button>
467396
</div>
468-
<div>
469-
<Label>
470-
{SSO_PROVIDER_SP_ENTITY_ID_LABEL}
471-
</Label>
472-
<div className="flex gap-2">
473-
<Input
474-
type="text"
475-
disabled={true}
476-
value={`${address.backend}/api/auth/sso/saml2/sp/metadata?providerId=${providerId}`}
477-
/>
478-
<Button
479-
variant="outline"
480-
size="icon"
481-
onClick={() =>
482-
copyToClipboard(
483-
`${address.backend}/api/auth/sso/saml2/sp/metadata?providerId=${providerId}`,
484-
)
485-
}
486-
>
487-
<Copy className="h-4 w-4" />
488-
</Button>
489-
</div>
397+
</div>
398+
<div>
399+
<Label>{SSO_PROVIDER_SP_ENTITY_ID_LABEL}</Label>
400+
<div className="flex gap-2">
401+
<Input
402+
type="text"
403+
disabled={true}
404+
value={`${address.backend}/api/auth/sso/saml2/sp/metadata?providerId=sso`}
405+
/>
406+
<Button
407+
variant="outline"
408+
size="icon"
409+
onClick={() =>
410+
copyToClipboard(
411+
`${address.backend}/api/auth/sso/saml2/sp/metadata?providerId=sso`,
412+
)
413+
}
414+
>
415+
<Copy className="h-4 w-4" />
416+
</Button>
490417
</div>
491-
</>
492-
)}
418+
</div>
419+
</>
493420
</CardContent>
494421
</Card>
495422
</div>

apps/web/graphql/settings/logic.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { checkPermission } from "@courselit/utils";
1313
import { Constants, LoginProvider, Typeface } from "@courselit/common-models";
1414
import ApikeyModel, { ApiKey } from "@models/ApiKey";
1515
import SSOProviderModel from "@models/SSOProvider";
16-
import AccountModel from "@models/Account";
1716

1817
const { permissions } = constants;
1918

@@ -227,14 +226,12 @@ export const removeApikey = async (keyId: string, ctx: GQLContext) => {
227226
};
228227

229228
export const updateSSOProvider = async ({
230-
providerId,
231229
idpMetadata,
232230
entryPoint,
233231
cert,
234232
backend,
235233
context: ctx,
236234
}: {
237-
providerId: string;
238235
idpMetadata: string;
239236
entryPoint: string;
240237
cert: string;
@@ -251,14 +248,10 @@ export const updateSSOProvider = async ({
251248
throw new Error(responses.action_not_allowed);
252249
}
253250

254-
if (!providerId || !idpMetadata || !entryPoint || !cert || !backend) {
251+
if (!idpMetadata || !entryPoint || !cert || !backend) {
255252
throw new Error(responses.provider_invalid_configuration);
256253
}
257254

258-
const existingSSOProvider = await SSOProviderModel.findOne({
259-
domain: ctx.subdomain._id,
260-
});
261-
262255
const backendUrl = new URL(backend);
263256

264257
try {
@@ -267,11 +260,11 @@ export const updateSSOProvider = async ({
267260
domain: ctx.subdomain._id,
268261
},
269262
{
270-
providerId,
263+
providerId: "sso",
271264
samlConfig: JSON.stringify({
272265
entryPoint,
273266
cert,
274-
callbackUrl: `${backendUrl.origin}/api/auth/sso/saml2/callback/${providerId}`,
267+
callbackUrl: `${backendUrl.origin}/api/auth/sso/saml2/callback/sso`,
275268
idpMetadata: {
276269
metadata: idpMetadata,
277270
},
@@ -286,18 +279,6 @@ export const updateSSOProvider = async ({
286279
},
287280
);
288281

289-
await AccountModel.updateMany(
290-
{
291-
domain: ctx.subdomain._id,
292-
providerId: existingSSOProvider?.providerId,
293-
},
294-
{
295-
$set: {
296-
providerId,
297-
},
298-
},
299-
);
300-
301282
return ssoProvider;
302283
} catch (error: any) {
303284
throw error;
@@ -326,12 +307,9 @@ export const getSSOProviderSettings = async (ctx: GQLContext) => {
326307
const samlConfig = JSON.parse(ssoProvider?.samlConfig || "{}");
327308

328309
return {
329-
providerId: ssoProvider?.providerId,
330310
idpMetadata: samlConfig?.idpMetadata?.metadata,
331-
// domain: ssoProvider?.domain_string,
332311
entryPoint: samlConfig?.entryPoint,
333312
cert: samlConfig?.cert,
334-
// callbackUrl: samlConfig?.callbackUrl,
335313
};
336314
};
337315

@@ -350,6 +328,10 @@ export const getSSOProvider = async (ctx: GQLContext) => {
350328
},
351329
);
352330

331+
if (!ssoProvider) {
332+
return null;
333+
}
334+
353335
return {
354336
providerId: ssoProvider.providerId,
355337
domain: ssoProvider.domain_string,
@@ -386,12 +368,6 @@ export const getFeatures = async (ctx: GQLContext) => {
386368
return ctx.subdomain.features || [];
387369
};
388370

389-
// export const getLoginProviders = async (ctx: GQLContext) => {
390-
// return ctx.subdomain.settings.logins?.length
391-
// ? ctx.subdomain.settings.logins
392-
// : [Constants.LoginProvider.EMAIL];
393-
// };
394-
395371
export const toggleLoginProvider = async ({
396372
provider,
397373
value,

0 commit comments

Comments
 (0)