mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 05:12:20 +00:00
feat: add zitadel-proto pkg
This commit is contained in:
@@ -9,7 +9,7 @@ import { authMiddleware } from "../middleware";
|
||||
|
||||
const createClient = <Client>(
|
||||
definition: CompatServiceDefinition,
|
||||
accessToken: string
|
||||
accessToken: string,
|
||||
) => {
|
||||
const channel = createChannel(process.env.ZITADEL_API_URL ?? "");
|
||||
return createClientFactory()
|
||||
@@ -20,5 +20,5 @@ const createClient = <Client>(
|
||||
export const getAdmin = () =>
|
||||
createClient<AdminServiceClient>(
|
||||
AdminServiceDefinition as CompatServiceDefinition,
|
||||
process.env.ZITADEL_ADMIN_TOKEN ?? ""
|
||||
process.env.ZITADEL_ADMIN_TOKEN ?? "",
|
||||
);
|
||||
|
||||
@@ -11,7 +11,7 @@ import { authMiddleware } from "../middleware";
|
||||
const createClient = <Client>(
|
||||
definition: CompatServiceDefinition,
|
||||
apiUrl: string,
|
||||
token: string
|
||||
token: string,
|
||||
) => {
|
||||
if (!apiUrl) {
|
||||
throw Error("ZITADEL_API_URL not set");
|
||||
@@ -39,7 +39,7 @@ export const getAuth = (app?: string | ZitadelServer) => {
|
||||
return createClient<AuthServiceClient>(
|
||||
AuthServiceDefinition as CompatServiceDefinition,
|
||||
config.apiUrl,
|
||||
config.token
|
||||
config.token,
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import { ZitadelServer, getServers } from "../server";
|
||||
const createClient = <Client>(
|
||||
definition: CompatServiceDefinition,
|
||||
apiUrl: string,
|
||||
token: string
|
||||
token: string,
|
||||
) => {
|
||||
if (!apiUrl) {
|
||||
throw Error("ZITADEL_API_URL not set");
|
||||
@@ -40,6 +40,6 @@ export const getManagement = (app?: string | ZitadelServer) => {
|
||||
return createClient<ManagementServiceClient>(
|
||||
ManagementServiceDefinition as CompatServiceDefinition,
|
||||
config.apiUrl,
|
||||
config.token
|
||||
config.token,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,59 +1,76 @@
|
||||
import { describe, expect, test, vitest } from "vitest";
|
||||
|
||||
import { CallOptions, ClientMiddlewareCall, Metadata, MethodDescriptor } from "nice-grpc";
|
||||
import {
|
||||
CallOptions,
|
||||
ClientMiddlewareCall,
|
||||
Metadata,
|
||||
MethodDescriptor,
|
||||
} from "nice-grpc";
|
||||
import { authMiddleware } from "./middleware";
|
||||
|
||||
describe('authMiddleware', () => {
|
||||
const scenarios = [
|
||||
{
|
||||
name: 'should add authorization if metadata is undefined',
|
||||
initialMetadata: undefined,
|
||||
expectedMetadata: new Metadata().set("authorization", "Bearer mock-token"),
|
||||
token: "mock-token"
|
||||
},
|
||||
{
|
||||
name: 'should add authorization if metadata exists but no authorization',
|
||||
initialMetadata: new Metadata().set("other-key", "other-value"),
|
||||
expectedMetadata: new Metadata().set("other-key", "other-value").set("authorization", "Bearer mock-token"),
|
||||
token: "mock-token"
|
||||
},
|
||||
{
|
||||
name: 'should not modify authorization if it already exists',
|
||||
initialMetadata: new Metadata().set("authorization", "Bearer initial-token"),
|
||||
expectedMetadata: new Metadata().set("authorization", "Bearer initial-token"),
|
||||
token: "mock-token"
|
||||
},
|
||||
];
|
||||
describe("authMiddleware", () => {
|
||||
const scenarios = [
|
||||
{
|
||||
name: "should add authorization if metadata is undefined",
|
||||
initialMetadata: undefined,
|
||||
expectedMetadata: new Metadata().set(
|
||||
"authorization",
|
||||
"Bearer mock-token",
|
||||
),
|
||||
token: "mock-token",
|
||||
},
|
||||
{
|
||||
name: "should add authorization if metadata exists but no authorization",
|
||||
initialMetadata: new Metadata().set("other-key", "other-value"),
|
||||
expectedMetadata: new Metadata()
|
||||
.set("other-key", "other-value")
|
||||
.set("authorization", "Bearer mock-token"),
|
||||
token: "mock-token",
|
||||
},
|
||||
{
|
||||
name: "should not modify authorization if it already exists",
|
||||
initialMetadata: new Metadata().set(
|
||||
"authorization",
|
||||
"Bearer initial-token",
|
||||
),
|
||||
expectedMetadata: new Metadata().set(
|
||||
"authorization",
|
||||
"Bearer initial-token",
|
||||
),
|
||||
token: "mock-token",
|
||||
},
|
||||
];
|
||||
|
||||
scenarios.forEach(({ name, initialMetadata, expectedMetadata, token }) => {
|
||||
test(name, async () => {
|
||||
scenarios.forEach(({ name, initialMetadata, expectedMetadata, token }) => {
|
||||
test(name, async () => {
|
||||
const mockNext = vitest.fn().mockImplementation(async function* () {});
|
||||
const mockRequest = {};
|
||||
|
||||
const mockNext = vitest.fn().mockImplementation(async function*() { });
|
||||
const mockRequest = {};
|
||||
const mockMethodDescriptor: MethodDescriptor = {
|
||||
options: { idempotencyLevel: undefined },
|
||||
path: "",
|
||||
requestStream: false,
|
||||
responseStream: false,
|
||||
};
|
||||
|
||||
const mockMethodDescriptor: MethodDescriptor = {
|
||||
options: {idempotencyLevel: undefined},
|
||||
path: '',
|
||||
requestStream: false,
|
||||
responseStream: false,
|
||||
};
|
||||
const mockCall: ClientMiddlewareCall<unknown, unknown> = {
|
||||
method: mockMethodDescriptor,
|
||||
requestStream: false,
|
||||
responseStream: false,
|
||||
request: mockRequest,
|
||||
next: mockNext,
|
||||
};
|
||||
const options: CallOptions = {
|
||||
metadata: initialMetadata,
|
||||
};
|
||||
|
||||
const mockCall: ClientMiddlewareCall<unknown, unknown> = {
|
||||
method: mockMethodDescriptor,
|
||||
requestStream: false,
|
||||
responseStream: false,
|
||||
request: mockRequest,
|
||||
next: mockNext,
|
||||
};
|
||||
const options: CallOptions = {
|
||||
metadata: initialMetadata
|
||||
};
|
||||
await authMiddleware(token)(mockCall, options).next();
|
||||
|
||||
await authMiddleware(token)(mockCall, options).next();
|
||||
|
||||
expect(mockNext).toHaveBeenCalledTimes(1);
|
||||
const actualMetadata = mockNext.mock.calls[0][1].metadata;
|
||||
expect(actualMetadata?.get('authorization')).toEqual(expectedMetadata.get('authorization'));
|
||||
});
|
||||
expect(mockNext).toHaveBeenCalledTimes(1);
|
||||
const actualMetadata = mockNext.mock.calls[0][1].metadata;
|
||||
expect(actualMetadata?.get("authorization")).toEqual(
|
||||
expectedMetadata.get("authorization"),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import { CallOptions, ClientMiddleware, ClientMiddlewareCall, Metadata } from "nice-grpc";
|
||||
import {
|
||||
CallOptions,
|
||||
ClientMiddleware,
|
||||
ClientMiddlewareCall,
|
||||
Metadata,
|
||||
} from "nice-grpc";
|
||||
|
||||
export function authMiddleware (token: string): ClientMiddleware {
|
||||
export function authMiddleware(token: string): ClientMiddleware {
|
||||
return async function* <Request, Response>(
|
||||
call: ClientMiddlewareCall<Request, Response>,
|
||||
options: CallOptions
|
||||
options: CallOptions,
|
||||
) {
|
||||
if (!options.metadata?.has("authorization")) {
|
||||
options.metadata ??= new Metadata();
|
||||
|
||||
@@ -15,7 +15,7 @@ export interface ZitadelServerOptions extends ZitadelServerProps {
|
||||
|
||||
export function initializeServer(
|
||||
config: ZitadelServerProps,
|
||||
name?: string
|
||||
name?: string,
|
||||
): ZitadelServer {
|
||||
const server = new ZitadelServer(config, name);
|
||||
return server;
|
||||
@@ -57,7 +57,7 @@ export function getServer(name?: string): ZitadelServer {
|
||||
export const createClient = <Client>(
|
||||
definition: CompatServiceDefinition,
|
||||
apiUrl: string,
|
||||
token: string
|
||||
token: string,
|
||||
) => {
|
||||
if (!apiUrl) {
|
||||
throw Error("ZITADEL_API_URL not set");
|
||||
|
||||
@@ -10,7 +10,7 @@ import { authMiddleware } from "../middleware";
|
||||
|
||||
const createSystemClient = <Client>(
|
||||
definition: CompatServiceDefinition,
|
||||
accessToken: string
|
||||
accessToken: string,
|
||||
) => {
|
||||
const channel = createChannel(process.env.ZITADEL_SYSTEM_API_URL ?? "");
|
||||
return createClientFactory()
|
||||
@@ -30,6 +30,6 @@ export const getSystem = async () => {
|
||||
|
||||
return createSystemClient<SystemServiceClient>(
|
||||
SystemServiceDefinition as CompatServiceDefinition,
|
||||
token
|
||||
token,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -19,6 +19,6 @@ export const getOidc = (server?: string | ZitadelServer) => {
|
||||
return createClient<OIDCServiceClient>(
|
||||
OIDCServiceDefinition as CompatServiceDefinition,
|
||||
config.apiUrl,
|
||||
config.token
|
||||
config.token,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -23,6 +23,6 @@ export const getSession = (server?: string | ZitadelServer) => {
|
||||
return createClient<SessionServiceClient>(
|
||||
SessionServiceDefinition as CompatServiceDefinition,
|
||||
config.apiUrl,
|
||||
config.token
|
||||
config.token,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -23,6 +23,6 @@ export const getSettings = (server?: string | ZitadelServer) => {
|
||||
return createClient<SettingsServiceClient>(
|
||||
SettingsServiceDefinition as CompatServiceDefinition,
|
||||
config.apiUrl,
|
||||
config.token
|
||||
config.token,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -23,6 +23,6 @@ export const getUser = (server?: string | ZitadelServer) => {
|
||||
return createClient<UserServiceClient>(
|
||||
UserServiceDefinition as CompatServiceDefinition,
|
||||
config.apiUrl,
|
||||
config.token
|
||||
config.token,
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user