mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-11 20:02:34 +00:00
feat: add zitadel-proto pkg
This commit is contained in:
@@ -26,7 +26,7 @@ export interface ZitadelApp {
|
||||
|
||||
export async function initializeApp(
|
||||
config: ZitadelCoreProps,
|
||||
name?: string
|
||||
name?: string,
|
||||
): Promise<ZitadelApp> {
|
||||
const app = { config, name };
|
||||
return app;
|
||||
@@ -40,6 +40,6 @@ export function getApp(name?: string): ZitadelApp | undefined {
|
||||
return name
|
||||
? apps.find((a) => a.name === name)
|
||||
: apps.length === 1
|
||||
? apps[0]
|
||||
: undefined;
|
||||
? apps[0]
|
||||
: undefined;
|
||||
}
|
||||
|
||||
@@ -1,59 +1,76 @@
|
||||
import { describe, expect, test, vitest } from "vitest";
|
||||
|
||||
import { CallOptions, ClientMiddlewareCall, Metadata, MethodDescriptor } from "nice-grpc-web";
|
||||
import {
|
||||
CallOptions,
|
||||
ClientMiddlewareCall,
|
||||
Metadata,
|
||||
MethodDescriptor,
|
||||
} from "nice-grpc-web";
|
||||
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"),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@ import { CallOptions, ClientMiddlewareCall, Metadata } from "nice-grpc-web";
|
||||
export const authMiddleware = (token: string) =>
|
||||
async function* <Request, Response>(
|
||||
call: ClientMiddlewareCall<Request, Response>,
|
||||
options: CallOptions
|
||||
options: CallOptions,
|
||||
) {
|
||||
if (!options.metadata?.has("authorization")) {
|
||||
options.metadata ??= new Metadata();
|
||||
|
||||
Reference in New Issue
Block a user