mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 03:23:40 +00:00
core
This commit is contained in:
15
apps/login/lib/zitadel.ts
Normal file
15
apps/login/lib/zitadel.ts
Normal 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();
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { SignInWithGoogle } from "#/../../packages/zitadel-react/dist";
|
import { SignInWithGoogle } from "@zitadel/react";
|
||||||
|
|
||||||
export default function IdentityProviders() {
|
export default function IdentityProviders() {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -28,7 +28,8 @@
|
|||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"jose": "^4.13.1",
|
||||||
"nice-grpc": "2.0.1",
|
"nice-grpc": "2.0.1",
|
||||||
"jose": "^4.13.1"
|
"protobufjs": "^7.2.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,17 +15,14 @@ const createClient = <Client>(
|
|||||||
definition: CompatServiceDefinition,
|
definition: CompatServiceDefinition,
|
||||||
accessToken: string
|
accessToken: string
|
||||||
) => {
|
) => {
|
||||||
const channel = createChannel(process.env.ZITADEL_API_URL);
|
const channel = createChannel(process.env.ZITADEL_API_URL ?? "");
|
||||||
return createClientFactory()
|
return createClientFactory()
|
||||||
.use(authMiddleware(accessToken))
|
.use(authMiddleware(accessToken))
|
||||||
.create(definition, channel) as Client;
|
.create(definition, channel) as Client;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAuth = async () =>
|
|
||||||
createClient<AuthServiceClient>(AuthServiceDefinition, "");
|
|
||||||
|
|
||||||
export const getAdmin = () =>
|
export const getAdmin = () =>
|
||||||
createClient<AdminServiceClient>(
|
createClient<AdminServiceClient>(
|
||||||
AdminServiceDefinition,
|
AdminServiceDefinition as CompatServiceDefinition,
|
||||||
process.env.ZITADEL_ADMIN_TOKEN ?? ""
|
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,
|
AuthServiceClient,
|
||||||
AuthServiceDefinition,
|
AuthServiceDefinition,
|
||||||
} from "./proto/server/zitadel/auth";
|
} from "./proto/server/zitadel/auth";
|
||||||
import { ZitadelApp } from "./core";
|
import { ZitadelApp } from "./app";
|
||||||
import { authMiddleware } from "./middleware";
|
import { authMiddleware } from "./middleware";
|
||||||
|
|
||||||
const createClient = <Client>(
|
const createClient = <Client>(
|
||||||
definition: CompatServiceDefinition,
|
definition: CompatServiceDefinition,
|
||||||
accessToken: string
|
accessToken: string
|
||||||
) => {
|
) => {
|
||||||
const channel = createChannel(process.env.ZITADEL_API_URL);
|
const channel = createChannel(process.env.ZITADEL_API_URL ?? "");
|
||||||
return createClientFactory()
|
return createClientFactory()
|
||||||
.use(authMiddleware(accessToken))
|
.use(authMiddleware(accessToken))
|
||||||
.create(definition, channel) as Client;
|
.create(definition, channel) as Client;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function getAuth(app?: ZitadelApp): Promise<AuthServiceClient> {
|
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";
|
} from "./proto/server/zitadel/management";
|
||||||
|
|
||||||
import { authMiddleware } from "./middleware";
|
import { authMiddleware } from "./middleware";
|
||||||
import { ZitadelApp } from "./core";
|
import { ZitadelApp, getApps } from "./app";
|
||||||
|
|
||||||
const createClient = <Client>(
|
const createClient = <Client>(
|
||||||
definition: CompatServiceDefinition,
|
definition: CompatServiceDefinition,
|
||||||
accessToken: string
|
apiUrl: string,
|
||||||
|
token: string
|
||||||
) => {
|
) => {
|
||||||
const apiUrl = process.env.ZITADEL_API_URL;
|
|
||||||
|
|
||||||
if (!apiUrl) {
|
if (!apiUrl) {
|
||||||
throw Error("ZITADEL_API_URL not set");
|
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()
|
return createClientFactory()
|
||||||
.use(authMiddleware(accessToken))
|
.use(authMiddleware(token))
|
||||||
.create(definition, channel) as Client;
|
.create(definition, channel) as Client;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getManagement = (app?: ZitadelApp) =>
|
export const getManagement = (app?: string | ZitadelApp) => {
|
||||||
createClient<ManagementServiceClient>(
|
let config;
|
||||||
ManagementServiceDefinition,
|
if (app && typeof app === "string") {
|
||||||
process.env.ZITADEL_ADMIN_TOKEN ?? ""
|
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,
|
definition: CompatServiceDefinition,
|
||||||
accessToken: string
|
accessToken: string
|
||||||
) => {
|
) => {
|
||||||
const channel = createChannel(process.env.ZITADEL_SYSTEM_API_URL);
|
const channel = createChannel(process.env.ZITADEL_SYSTEM_API_URL ?? "");
|
||||||
return createClientFactory()
|
return createClientFactory()
|
||||||
.use(authMiddleware(accessToken))
|
.use(authMiddleware(accessToken))
|
||||||
.create(definition, channel) as Client;
|
.create(definition, channel) as Client;
|
||||||
@@ -23,13 +23,13 @@ export const getSystem = async () => {
|
|||||||
.setProtectedHeader({ alg: "RS256" })
|
.setProtectedHeader({ alg: "RS256" })
|
||||||
.setIssuedAt()
|
.setIssuedAt()
|
||||||
.setExpirationTime("1h")
|
.setExpirationTime("1h")
|
||||||
.setIssuer(process.env.ZITADEL_SYSTEM_API_USERID)
|
.setIssuer(process.env.ZITADEL_SYSTEM_API_USERID ?? "")
|
||||||
.setSubject(process.env.ZITADEL_SYSTEM_API_USERID)
|
.setSubject(process.env.ZITADEL_SYSTEM_API_USERID ?? "")
|
||||||
.setAudience(process.env.ZITADEL_ISSUER)
|
.setAudience(process.env.ZITADEL_ISSUER ?? "")
|
||||||
.sign(await importPKCS8(process.env.ZITADEL_SYSTEM_API_KEY, "RS256"));
|
.sign(await importPKCS8(process.env.ZITADEL_SYSTEM_API_KEY ?? "", "RS256"));
|
||||||
|
|
||||||
return createSystemClient<SystemServiceClient>(
|
return createSystemClient<SystemServiceClient>(
|
||||||
SystemServiceDefinition,
|
SystemServiceDefinition as CompatServiceDefinition,
|
||||||
token
|
token
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"extends": "@zitadel/tsconfig/react-library.json",
|
"extends": "@zitadel/tsconfig/node14.json",
|
||||||
"include": ["."],
|
"include": ["."],
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"baseUrl": "."
|
"baseUrl": "."
|
||||||
|
|||||||
2
pnpm-lock.yaml
generated
2
pnpm-lock.yaml
generated
@@ -110,12 +110,14 @@ importers:
|
|||||||
eslint-config-zitadel: workspace:*
|
eslint-config-zitadel: workspace:*
|
||||||
jose: ^4.13.1
|
jose: ^4.13.1
|
||||||
nice-grpc: 2.0.1
|
nice-grpc: 2.0.1
|
||||||
|
protobufjs: ^7.2.3
|
||||||
ts-proto: ^1.139.0
|
ts-proto: ^1.139.0
|
||||||
tsup: ^5.10.1
|
tsup: ^5.10.1
|
||||||
typescript: ^4.5.3
|
typescript: ^4.5.3
|
||||||
dependencies:
|
dependencies:
|
||||||
jose: 4.13.1
|
jose: 4.13.1
|
||||||
nice-grpc: 2.0.1
|
nice-grpc: 2.0.1
|
||||||
|
protobufjs: 7.2.3
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@zitadel/tsconfig': link:../zitadel-tsconfig
|
'@zitadel/tsconfig': link:../zitadel-tsconfig
|
||||||
eslint: 7.32.0
|
eslint: 7.32.0
|
||||||
|
|||||||
Reference in New Issue
Block a user