Skip to content

Commit c7b4f25

Browse files
committed
🦂
1 parent 45b279f commit c7b4f25

File tree

5 files changed

+26
-46
lines changed

5 files changed

+26
-46
lines changed

auth/app/api/auth/callback/[provider]/route.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "@/lib/constant";
99
import { deleteCookie, setAuthSessionCookie } from "@/lib/cookie";
1010
import { prisma } from "@/lib/prisma";
11+
import { getWellKnown } from "@/lib/zitadel";
1112
import jwt from "jsonwebtoken";
1213
import { cookies } from "next/headers";
1314
import { NextRequest, NextResponse } from "next/server";
@@ -48,21 +49,7 @@ async function handler(
4849
tokenParams.append("redirect_uri", configuration[provider].redirectUrl);
4950
tokenParams.append("code_verifier", codeVerifierCookie.value);
5051

51-
const wellKnownResponse = await fetch(
52-
`${configuration[provider].issuer}/.well-known/openid-configuration`
53-
);
54-
55-
const wellKnown = (await wellKnownResponse.json()) as {
56-
issuer: string;
57-
authorization_endpoint: string;
58-
token_endpoint: string;
59-
userinfo_endpoint: string;
60-
end_session_endpoint: string;
61-
};
62-
63-
if (wellKnownResponse.status !== 200) {
64-
throw { code: wellKnownResponse.status, details: wellKnown };
65-
}
52+
const wellKnown = await getWellKnown(configuration[provider].issuer);
6653

6754
const response = await fetch(wellKnown.token_endpoint, {
6855
method: "post",

auth/app/api/auth/signin/[provider]/route.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
stateCookieName,
1313
} from "@/lib/constant";
1414
import { deleteCookie, setShortLiveCookie } from "@/lib/cookie";
15+
import { getWellKnown } from "@/lib/zitadel";
1516
import { cookies } from "next/headers";
1617
import { NextRequest, NextResponse } from "next/server";
1718
import { URLSearchParams } from "url";
@@ -38,21 +39,7 @@ export async function POST(
3839
if (!csrfTokenCookie) throw new Error("csrfToken cookie not found");
3940
if (csrfTokenCookie.value !== csrfToken) throw new Error("Invalid csrfToken");
4041

41-
const wellKnownResponse = await fetch(
42-
`${configuration[provider].issuer}/.well-known/openid-configuration`
43-
);
44-
45-
const wellKnown = (await wellKnownResponse.json()) as {
46-
issuer: string;
47-
authorization_endpoint: string;
48-
token_endpoint: string;
49-
userinfo_endpoint: string;
50-
end_session_endpoint: string;
51-
};
52-
53-
if (wellKnownResponse.status !== 200) {
54-
throw { code: wellKnownResponse.status, details: wellKnown };
55-
}
42+
const wellKnown = await getWellKnown(configuration[provider].issuer);
5643

5744
const codeVerifier = generateCodeVerifier();
5845
const codeChallenge = generateCodeChallenge(codeVerifier);

auth/app/api/auth/signout/[provider]/route.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import configuration from "@/configuration";
22
import { authSessionCookieName, returnUrlCookieName } from "@/lib/constant";
33
import { setShortLiveCookie } from "@/lib/cookie";
44
import { prisma } from "@/lib/prisma";
5+
import { getWellKnown } from "@/lib/zitadel";
56
import { Prisma } from "@prisma/client";
67
import { cookies } from "next/headers";
78
import { NextRequest, NextResponse } from "next/server";
@@ -23,21 +24,7 @@ export async function POST(
2324
const provider = params.provider;
2425
if (!provider) throw new Error("provider not found");
2526

26-
const wellKnownResponse = await fetch(
27-
`${configuration[provider].issuer}/.well-known/openid-configuration`
28-
);
29-
30-
const wellKnown = (await wellKnownResponse.json()) as {
31-
issuer: string;
32-
authorization_endpoint: string;
33-
token_endpoint: string;
34-
userinfo_endpoint: string;
35-
end_session_endpoint: string;
36-
};
37-
38-
if (wellKnownResponse.status !== 200) {
39-
throw { code: wellKnownResponse.status, details: wellKnown };
40-
}
27+
const wellKnown = await getWellKnown(configuration[provider].issuer);
4128

4229
const requestParams = new URLSearchParams({
4330
client_id: configuration[provider].clientId,

auth/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if (process.env.ENV_PATH) {
1616
const schema = z.object({
1717
appUrl: z.string(),
1818
domain: z.string(),
19-
originRegex: z.unknown(),
19+
originRegex: z.any(),
2020
cookie: z.object({
2121
httpOnly: z.boolean(),
2222
secure: z.boolean(),

auth/lib/zitadel.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export async function getWellKnown(issuer: string) {
2+
const wellKnownResponse = await fetch(
3+
new URL(`/.well-known/openid-configuration`, issuer).toString()
4+
);
5+
6+
const wellKnown = (await wellKnownResponse.json()) as {
7+
issuer: string;
8+
authorization_endpoint: string;
9+
token_endpoint: string;
10+
userinfo_endpoint: string;
11+
end_session_endpoint: string;
12+
};
13+
14+
if (wellKnownResponse.status !== 200) {
15+
throw { code: wellKnownResponse.status, details: wellKnown };
16+
}
17+
18+
return wellKnown;
19+
}

0 commit comments

Comments
 (0)