This commit is contained in:
Max Peintner
2023-04-13 13:26:02 +02:00
parent 563a80a1f4
commit ed1ab86e69
12 changed files with 111 additions and 49 deletions

15
apps/login/lib/zitadel.ts Normal file
View File

@@ -0,0 +1,15 @@
import { ZitadelOptions, getApps, initializeApp } from "@zitadel/core";
export const zitadelConfig: ZitadelOptions = {
apiUrl: process.env.ZITADEL_API_URL ?? "",
projectId: process.env.ZITADEL_PROJECT_ID ?? "",
appId: process.env.ZITADEL_APP_ID ?? "",
token: "this should be a pat",
};
if (!getApps().length) {
initializeApp(zitadelConfig);
}
const app = getApp();
// const auth = getAuth();

View File

@@ -1,4 +1,4 @@
import { SignInWithGoogle } from "#/../../packages/zitadel-react/dist";
import { SignInWithGoogle } from "@zitadel/react";
export default function IdentityProviders() {
return (

View File

@@ -28,7 +28,8 @@
"access": "public"
},
"dependencies": {
"jose": "^4.13.1",
"nice-grpc": "2.0.1",
"jose": "^4.13.1"
"protobufjs": "^7.2.3"
}
}

View File

@@ -15,17 +15,14 @@ const createClient = <Client>(
definition: CompatServiceDefinition,
accessToken: string
) => {
const channel = createChannel(process.env.ZITADEL_API_URL);
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,
AdminServiceDefinition as CompatServiceDefinition,
process.env.ZITADEL_ADMIN_TOKEN ?? ""
);

View File

@@ -0,0 +1,45 @@
/**
* Return a slugified copy of a string.
*
* @param {CoreProps} str The ZITADEL client configuration
* @return {Core} The client implementation.
*/
let apps: ZitadelApp[] = [];
export interface ZitadelCoreProps {
clientId: string;
apiUrl: string; // process.env.ZITADEL_API_URL
token: string;
adminToken?: string;
managementToken?: string;
}
export interface ZitadelOptions extends ZitadelCoreProps {
name?: string;
}
export interface ZitadelApp {
name: string | undefined;
config: ZitadelCoreProps;
}
export async function initializeApp(
config: ZitadelCoreProps,
name?: string
): Promise<ZitadelApp> {
const app = { config, name };
return app;
}
export function getApps(): ZitadelApp[] {
return apps;
}
export function getApp(name?: string): ZitadelApp | undefined {
return name
? apps.find((a) => a.name === name)
: apps.length === 1
? apps[0]
: undefined;
}

View File

@@ -4,19 +4,22 @@ import {
AuthServiceClient,
AuthServiceDefinition,
} from "./proto/server/zitadel/auth";
import { ZitadelApp } from "./core";
import { ZitadelApp } from "./app";
import { authMiddleware } from "./middleware";
const createClient = <Client>(
definition: CompatServiceDefinition,
accessToken: string
) => {
const channel = createChannel(process.env.ZITADEL_API_URL);
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, "");
return createClient<AuthServiceClient>(
AuthServiceDefinition as CompatServiceDefinition,
""
);
}

View File

@@ -1,21 +0,0 @@
/**
* Return a slugified copy of a string.
*
* @param {CoreProps} str The ZITADEL client configuration
* @return {Core} The client implementation.
*/
export interface ZitadelCoreProps {
clientId: string;
}
export interface ZitadelApp {
config: ZitadelCoreProps;
}
export async function initializeApp(
config: ZitadelCoreProps
): Promise<ZitadelApp> {
const app = { config };
return app;
}

View File

@@ -1 +1,8 @@
export { initializeApp } from "./core";
export { initializeApp, getApps } from "./app";
export { getAuth } from "./auth";
export { getManagement } from "./management";
export { getAdmin } from "./admin";
export { getSystem } from "./system";
export type { ZitadelOptions } from "./app";

View File

@@ -7,26 +7,39 @@ import {
} from "./proto/server/zitadel/management";
import { authMiddleware } from "./middleware";
import { ZitadelApp } from "./core";
import { ZitadelApp, getApps } from "./app";
const createClient = <Client>(
definition: CompatServiceDefinition,
accessToken: string
apiUrl: string,
token: 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);
const channel = createChannel(process.env.ZITADEL_API_URL ?? "");
return createClientFactory()
.use(authMiddleware(accessToken))
.use(authMiddleware(token))
.create(definition, channel) as Client;
};
export const getManagement = (app?: ZitadelApp) =>
createClient<ManagementServiceClient>(
ManagementServiceDefinition,
process.env.ZITADEL_ADMIN_TOKEN ?? ""
export const getManagement = (app?: string | ZitadelApp) => {
let config;
if (app && typeof app === "string") {
const apps = getApps();
config = apps.find((a) => a.name === app)?.config;
} else if (app && typeof app === "object") {
config = app.config;
}
if (!config) {
throw Error("No ZITADEL app found");
}
return createClient<ManagementServiceClient>(
ManagementServiceDefinition as CompatServiceDefinition,
config.apiUrl,
config.token
);
};

View File

@@ -12,7 +12,7 @@ const createSystemClient = <Client>(
definition: CompatServiceDefinition,
accessToken: string
) => {
const channel = createChannel(process.env.ZITADEL_SYSTEM_API_URL);
const channel = createChannel(process.env.ZITADEL_SYSTEM_API_URL ?? "");
return createClientFactory()
.use(authMiddleware(accessToken))
.create(definition, channel) as Client;
@@ -23,13 +23,13 @@ export const getSystem = async () => {
.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"));
.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,
SystemServiceDefinition as CompatServiceDefinition,
token
);
};

View File

@@ -1,5 +1,5 @@
{
"extends": "@zitadel/tsconfig/react-library.json",
"extends": "@zitadel/tsconfig/node14.json",
"include": ["."],
"compilerOptions": {
"baseUrl": "."

2
pnpm-lock.yaml generated
View File

@@ -110,12 +110,14 @@ importers:
eslint-config-zitadel: workspace:*
jose: ^4.13.1
nice-grpc: 2.0.1
protobufjs: ^7.2.3
ts-proto: ^1.139.0
tsup: ^5.10.1
typescript: ^4.5.3
dependencies:
jose: 4.13.1
nice-grpc: 2.0.1
protobufjs: 7.2.3
devDependencies:
'@zitadel/tsconfig': link:../zitadel-tsconfig
eslint: 7.32.0