diff --git a/keep-ui/auth.ts b/keep-ui/auth.ts index 159de7439..3c7576ae8 100644 --- a/keep-ui/auth.ts +++ b/keep-ui/auth.ts @@ -1,5 +1,6 @@ import NextAuth from "next-auth"; import type { NextAuthConfig } from "next-auth"; +import { customFetch } from "next-auth"; import Credentials from "next-auth/providers/credentials"; import Keycloak from "next-auth/providers/keycloak"; import Auth0 from "next-auth/providers/auth0"; @@ -8,6 +9,9 @@ import { AuthError } from "next-auth"; import { AuthenticationError, AuthErrorCodes } from "@/errors"; import type { JWT } from "@auth/core/jwt"; +// see https://authjs.dev/guides/corporate-proxy +import { ProxyAgent, fetch as undici } from "undici"; + export class BackendRefusedError extends AuthError { static type = "BackendRefusedError"; } @@ -34,6 +38,22 @@ const authType = ? AuthType.NOAUTH : (authTypeEnv as AuthType); +// Proxy configuration +const proxyUrl = + process.env.HTTP_PROXY || + process.env.HTTPS_PROXY || + process.env.http_proxy || + process.env.https_proxy; +const dispatcher = proxyUrl ? new ProxyAgent(proxyUrl) : undefined; + +function proxyFetch( + ...args: Parameters +): ReturnType { + if (!dispatcher) return fetch(...args); + // @ts-expect-error `undici` has a `duplex` option + return undici(args[0], { ...args[1], dispatcher }); +} + async function refreshAccessToken(token: any) { const issuerUrl = process.env.KEYCLOAK_ISSUER; const refreshTokenUrl = `${issuerUrl}/protocol/openid-connect/token`; @@ -170,6 +190,8 @@ const providerConfigs = { .KEEP_AZUREAD_CLIENT_ID!}/default openid profile email`, }, }, + // see https://authjs.dev/guides/corporate-proxy + [customFetch]: proxyFetch, }), ], }; diff --git a/keep-ui/package-lock.json b/keep-ui/package-lock.json index e58b5e758..07b0b7601 100644 --- a/keep-ui/package-lock.json +++ b/keep-ui/package-lock.json @@ -96,6 +96,7 @@ "swr": "^2.2.5", "tailwind-merge": "^1.12.0", "tailwindcss": "^3.4.1", + "undici": "^6.21.0", "uuid": "^8.3.2", "yaml": "^2.2.2", "zustand": "^5.0.1" @@ -19977,6 +19978,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/undici": { + "version": "6.21.0", + "resolved": "https://registry.npmjs.org/undici/-/undici-6.21.0.tgz", + "integrity": "sha512-BUgJXc752Kou3oOIuU1i+yZZypyZRqNPW0vqoMPl8VaoalSfeR0D8/t4iAS3yirs79SSMTxTag+ZC86uswv+Cw==", + "license": "MIT", + "engines": { + "node": ">=18.17" + } + }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", diff --git a/keep-ui/package.json b/keep-ui/package.json index d06a62b64..de94bd570 100644 --- a/keep-ui/package.json +++ b/keep-ui/package.json @@ -97,6 +97,7 @@ "swr": "^2.2.5", "tailwind-merge": "^1.12.0", "tailwindcss": "^3.4.1", + "undici": "^6.21.0", "uuid": "^8.3.2", "yaml": "^2.2.2", "zustand": "^5.0.1"