mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 05:12:20 +00:00
core
This commit is contained in:
@@ -28,7 +28,8 @@
|
||||
"access": "public"
|
||||
},
|
||||
"dependencies": {
|
||||
"jose": "^4.13.1",
|
||||
"nice-grpc": "2.0.1",
|
||||
"jose": "^4.13.1"
|
||||
"protobufjs": "^7.2.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ?? ""
|
||||
);
|
||||
|
||||
45
packages/zitadel-core/src/app.ts
Normal file
45
packages/zitadel-core/src/app.ts
Normal 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;
|
||||
}
|
||||
@@ -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,
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
@@ -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
|
||||
);
|
||||
};
|
||||
|
||||
@@ -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
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"extends": "@zitadel/tsconfig/react-library.json",
|
||||
"extends": "@zitadel/tsconfig/node14.json",
|
||||
"include": ["."],
|
||||
"compilerOptions": {
|
||||
"baseUrl": "."
|
||||
|
||||
Reference in New Issue
Block a user