mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 15:27:33 +00:00
Merge pull request #502 from zitadel/fix-custom-request-headers-in-login
fix: use custom req headers in all server requests
This commit is contained in:
2
apps/login/next-env-vars.d.ts
vendored
2
apps/login/next-env-vars.d.ts
vendored
@@ -28,6 +28,6 @@ declare namespace NodeJS {
|
|||||||
* Optional: custom request headers to be added to every request
|
* Optional: custom request headers to be added to every request
|
||||||
* Split by comma, key value pairs separated by colon
|
* Split by comma, key value pairs separated by colon
|
||||||
*/
|
*/
|
||||||
CUSTOM_REQUEST_HEADERS: string;
|
CUSTOM_REQUEST_HEADERS?: string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,20 +1,16 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { createServerTransport } from "@zitadel/client/node";
|
|
||||||
import { createUserServiceClient } from "@zitadel/client/v2";
|
import { createUserServiceClient } from "@zitadel/client/v2";
|
||||||
import { headers } from "next/headers";
|
import { headers } from "next/headers";
|
||||||
import { getSessionCookieById } from "./cookies";
|
import { getSessionCookieById } from "./cookies";
|
||||||
import { getServiceUrlFromHeaders } from "./service-url";
|
import { getServiceUrlFromHeaders } from "./service-url";
|
||||||
import { getSession } from "./zitadel";
|
import { createServerTransport, getSession } from "./zitadel";
|
||||||
|
|
||||||
const transport = async (serviceUrl: string, token: string) => {
|
|
||||||
return createServerTransport(token, {
|
|
||||||
baseUrl: serviceUrl,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const myUserService = async (serviceUrl: string, sessionToken: string) => {
|
const myUserService = async (serviceUrl: string, sessionToken: string) => {
|
||||||
const transportPromise = await transport(serviceUrl, sessionToken);
|
const transportPromise = await createServerTransport(
|
||||||
|
sessionToken,
|
||||||
|
serviceUrl,
|
||||||
|
);
|
||||||
return createUserServiceClient(transportPromise);
|
return createUserServiceClient(transportPromise);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -17,7 +17,6 @@ import {
|
|||||||
setUserPassword,
|
setUserPassword,
|
||||||
} from "@/lib/zitadel";
|
} from "@/lib/zitadel";
|
||||||
import { ConnectError, create } from "@zitadel/client";
|
import { ConnectError, create } from "@zitadel/client";
|
||||||
import { createServerTransport } from "@zitadel/client/node";
|
|
||||||
import { createUserServiceClient } from "@zitadel/client/v2";
|
import { createUserServiceClient } from "@zitadel/client/v2";
|
||||||
import {
|
import {
|
||||||
Checks,
|
Checks,
|
||||||
@@ -39,6 +38,7 @@ import {
|
|||||||
checkPasswordChangeRequired,
|
checkPasswordChangeRequired,
|
||||||
checkUserVerification,
|
checkUserVerification,
|
||||||
} from "../verify-helper";
|
} from "../verify-helper";
|
||||||
|
import { createServerTransport } from "../zitadel";
|
||||||
|
|
||||||
type ResetPasswordCommand = {
|
type ResetPasswordCommand = {
|
||||||
loginName: string;
|
loginName: string;
|
||||||
@@ -428,9 +428,7 @@ export async function checkSessionAndSetPassword({
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const transport = async (serviceUrl: string, token: string) => {
|
const transport = async (serviceUrl: string, token: string) => {
|
||||||
return createServerTransport(token, {
|
return createServerTransport(token, serviceUrl);
|
||||||
baseUrl: serviceUrl,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const myUserService = async (serviceUrl: string, sessionToken: string) => {
|
const myUserService = async (serviceUrl: string, sessionToken: string) => {
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
import { createClientFor } from "@zitadel/client";
|
import { createClientFor } from "@zitadel/client";
|
||||||
import { createServerTransport } from "@zitadel/client/node";
|
|
||||||
import { IdentityProviderService } from "@zitadel/proto/zitadel/idp/v2/idp_service_pb";
|
import { IdentityProviderService } from "@zitadel/proto/zitadel/idp/v2/idp_service_pb";
|
||||||
import { OIDCService } from "@zitadel/proto/zitadel/oidc/v2/oidc_service_pb";
|
import { OIDCService } from "@zitadel/proto/zitadel/oidc/v2/oidc_service_pb";
|
||||||
import { OrganizationService } from "@zitadel/proto/zitadel/org/v2/org_service_pb";
|
import { OrganizationService } from "@zitadel/proto/zitadel/org/v2/org_service_pb";
|
||||||
@@ -8,6 +7,7 @@ import { SessionService } from "@zitadel/proto/zitadel/session/v2/session_servic
|
|||||||
import { SettingsService } from "@zitadel/proto/zitadel/settings/v2/settings_service_pb";
|
import { SettingsService } from "@zitadel/proto/zitadel/settings/v2/settings_service_pb";
|
||||||
import { UserService } from "@zitadel/proto/zitadel/user/v2/user_service_pb";
|
import { UserService } from "@zitadel/proto/zitadel/user/v2/user_service_pb";
|
||||||
import { systemAPIToken } from "./api";
|
import { systemAPIToken } from "./api";
|
||||||
|
import { createServerTransport } from "./zitadel";
|
||||||
|
|
||||||
type ServiceClass =
|
type ServiceClass =
|
||||||
| typeof IdentityProviderService
|
| typeof IdentityProviderService
|
||||||
@@ -43,24 +43,7 @@ export async function createServiceForHost<T extends ServiceClass>(
|
|||||||
throw new Error("No token found");
|
throw new Error("No token found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const transport = createServerTransport(token, {
|
const transport = createServerTransport(token, serviceUrl);
|
||||||
baseUrl: serviceUrl,
|
|
||||||
interceptors: !process.env.CUSTOM_REQUEST_HEADERS
|
|
||||||
? undefined
|
|
||||||
: [
|
|
||||||
(next) => {
|
|
||||||
return (req) => {
|
|
||||||
process.env.CUSTOM_REQUEST_HEADERS.split(",").forEach(
|
|
||||||
(header) => {
|
|
||||||
const kv = header.split(":");
|
|
||||||
req.header.set(kv[0], kv[1]);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
return next(req);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
return createClientFor<T>(service)(transport);
|
return createClientFor<T>(service)(transport);
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import { Client, create, Duration } from "@zitadel/client";
|
import { Client, create, Duration } from "@zitadel/client";
|
||||||
|
import { createServerTransport as libCreateServerTransport } from "@zitadel/client/node";
|
||||||
import { makeReqCtx } from "@zitadel/client/v2";
|
import { makeReqCtx } from "@zitadel/client/v2";
|
||||||
import { IdentityProviderService } from "@zitadel/proto/zitadel/idp/v2/idp_service_pb";
|
import { IdentityProviderService } from "@zitadel/proto/zitadel/idp/v2/idp_service_pb";
|
||||||
import {
|
import {
|
||||||
@@ -1497,3 +1498,28 @@ export async function listAuthenticationMethodTypes({
|
|||||||
userId,
|
userId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createServerTransport(token: string, baseUrl: string) {
|
||||||
|
return libCreateServerTransport(token, {
|
||||||
|
baseUrl,
|
||||||
|
interceptors: !process.env.CUSTOM_REQUEST_HEADERS
|
||||||
|
? undefined
|
||||||
|
: [
|
||||||
|
(next) => {
|
||||||
|
return (req) => {
|
||||||
|
process.env
|
||||||
|
.CUSTOM_REQUEST_HEADERS!.split(",")
|
||||||
|
.forEach((header) => {
|
||||||
|
const kv = header.split(":");
|
||||||
|
if (kv.length === 2) {
|
||||||
|
req.header.set(kv[0].trim(), kv[1].trim());
|
||||||
|
} else {
|
||||||
|
console.warn(`Skipping malformed header: ${header}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return next(req);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user