client setup

This commit is contained in:
Max Peintner
2023-04-06 18:57:58 +02:00
parent 316c9cf22b
commit c86e959a6c
8 changed files with 153 additions and 10 deletions

View File

@@ -26,5 +26,9 @@
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"nice-grpc": "2.0.1",
"jose": "^4.13.1"
}
}

View File

@@ -0,0 +1,31 @@
import { CompatServiceDefinition } from "nice-grpc/lib/service-definitions";
import { createChannel, createClientFactory } from "nice-grpc";
import {
AuthServiceClient,
AuthServiceDefinition,
} from "./proto/server/zitadel/auth";
import {
AdminServiceClient,
AdminServiceDefinition,
} from "./proto/server/zitadel/admin";
import { authMiddleware } from "./middleware";
const createClient = <Client>(
definition: CompatServiceDefinition,
accessToken: string
) => {
const channel = createChannel(process.env.ZITADEL_API_URL);
return createClientFactory()
.use(authMiddleware(accessToken))
.create(definition, channel) as Client;
};
export const getAuth = async () =>
createClient<AuthServiceClient>(AuthServiceDefinition, "");
export const getAdmin = () =>
createClient<AdminServiceClient>(
AdminServiceDefinition,
process.env.ZITADEL_ADMIN_TOKEN ?? ""
);

View File

@@ -0,0 +1,22 @@
import { CompatServiceDefinition } from "nice-grpc/lib/service-definitions";
import { createChannel, createClientFactory } from "nice-grpc";
import {
AuthServiceClient,
AuthServiceDefinition,
} from "./proto/server/zitadel/auth";
import { ZitadelApp } from "./core";
import { authMiddleware } from "./middleware";
const createClient = <Client>(
definition: CompatServiceDefinition,
accessToken: string
) => {
const channel = createChannel(process.env.ZITADEL_API_URL);
return createClientFactory()
.use(authMiddleware(accessToken))
.create(definition, channel) as Client;
};
export async function getAuth(app?: ZitadelApp): Promise<AuthServiceClient> {
return createClient<AuthServiceClient>(AuthServiceDefinition, "");
}

View File

@@ -13,7 +13,9 @@ export interface ZitadelApp {
config: ZitadelCoreProps;
}
export function initializeApp(config: ZitadelCoreProps): ZitadelApp {
export async function initializeApp(
config: ZitadelCoreProps
): Promise<ZitadelApp> {
const app = { config };
return app;
}

View File

@@ -0,0 +1,32 @@
import { CompatServiceDefinition } from "nice-grpc/lib/service-definitions";
import { createChannel, createClientFactory } from "nice-grpc";
import {
ManagementServiceClient,
ManagementServiceDefinition,
} from "./proto/server/zitadel/management";
import { authMiddleware } from "./middleware";
import { ZitadelApp } from "./core";
const createClient = <Client>(
definition: CompatServiceDefinition,
accessToken: string
) => {
const apiUrl = process.env.ZITADEL_API_URL;
if (!apiUrl) {
throw Error("ZITADEL_API_URL not set");
}
const channel = createChannel(process.env.ZITADEL_API_URL);
return createClientFactory()
.use(authMiddleware(accessToken))
.create(definition, channel) as Client;
};
export const getManagement = (app?: ZitadelApp) =>
createClient<ManagementServiceClient>(
ManagementServiceDefinition,
process.env.ZITADEL_ADMIN_TOKEN ?? ""
);

View File

@@ -0,0 +1,14 @@
import { CallOptions, ClientMiddlewareCall, Metadata } from "nice-grpc";
export const authMiddleware = (token: string) =>
async function* <Request, Response>(
call: ClientMiddlewareCall<Request, Response>,
options: CallOptions
) {
if (!options.metadata?.has("authorization")) {
options.metadata ??= new Metadata();
options.metadata?.set("authorization", `Bearer ${token}`);
}
return yield* call.next(call.request, options);
};

View File

@@ -0,0 +1,35 @@
import { CompatServiceDefinition } from "nice-grpc/lib/service-definitions";
import { importPKCS8, SignJWT } from "jose";
import { createChannel, createClientFactory } from "nice-grpc";
import {
SystemServiceClient,
SystemServiceDefinition,
} from "./proto/server/zitadel/system";
import { authMiddleware } from "./middleware";
const createSystemClient = <Client>(
definition: CompatServiceDefinition,
accessToken: string
) => {
const channel = createChannel(process.env.ZITADEL_SYSTEM_API_URL);
return createClientFactory()
.use(authMiddleware(accessToken))
.create(definition, channel) as Client;
};
export const getSystem = async () => {
const token = await new SignJWT({})
.setProtectedHeader({ alg: "RS256" })
.setIssuedAt()
.setExpirationTime("1h")
.setIssuer(process.env.ZITADEL_SYSTEM_API_USERID)
.setSubject(process.env.ZITADEL_SYSTEM_API_USERID)
.setAudience(process.env.ZITADEL_ISSUER)
.sign(await importPKCS8(process.env.ZITADEL_SYSTEM_API_KEY, "RS256"));
return createSystemClient<SystemServiceClient>(
SystemServiceDefinition,
token
);
};

21
pnpm-lock.yaml generated
View File

@@ -86,7 +86,7 @@ importers:
lint-staged: 13.0.3
make-dir-cli: 3.0.0
postcss: 8.4.21
prettier-plugin-tailwindcss: 0.1.13_prettier@2.7.1
prettier-plugin-tailwindcss: 0.1.13_prettier@2.8.0
tailwindcss: 3.2.4_postcss@8.4.21
ts-proto: 1.146.0
typescript: 4.8.4
@@ -108,9 +108,14 @@ importers:
'@zitadel/tsconfig': workspace:*
eslint: ^7.32.0
eslint-config-zitadel: workspace:*
jose: ^4.13.1
nice-grpc: 2.0.1
ts-proto: ^1.139.0
tsup: ^5.10.1
typescript: ^4.5.3
dependencies:
jose: 4.13.1
nice-grpc: 2.0.1
devDependencies:
'@zitadel/tsconfig': link:../zitadel-tsconfig
eslint: 7.32.0
@@ -4423,6 +4428,10 @@ packages:
/isexe/2.0.0:
resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}
/jose/4.13.1:
resolution: {integrity: sha512-MSJQC5vXco5Br38mzaQKiq9mwt7lwj2eXpgpRyQYNHYt2lq1PjkWa7DLXX0WVcQLE9HhMh3jPiufS7fhJf+CLQ==}
dev: false
/joycon/3.1.1:
resolution: {integrity: sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==}
engines: {node: '>=10'}
@@ -5381,19 +5390,13 @@ packages:
resolution: {integrity: sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==}
engines: {node: '>= 0.8.0'}
/prettier-plugin-tailwindcss/0.1.13_prettier@2.7.1:
/prettier-plugin-tailwindcss/0.1.13_prettier@2.8.0:
resolution: {integrity: sha512-/EKQURUrxLu66CMUg4+1LwGdxnz8of7IDvrSLqEtDqhLH61SAlNNUSr90UTvZaemujgl3OH/VHg+fyGltrNixw==}
engines: {node: '>=12.17.0'}
peerDependencies:
prettier: '>=2.2.0'
dependencies:
prettier: 2.7.1
dev: true
/prettier/2.7.1:
resolution: {integrity: sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==}
engines: {node: '>=10.13.0'}
hasBin: true
prettier: 2.8.0
dev: true
/prettier/2.8.0: