2023-04-20 14:39:51 +02:00
|
|
|
import {
|
2023-04-21 13:49:15 +02:00
|
|
|
management,
|
|
|
|
|
ZitadelServer,
|
2023-04-20 14:39:51 +02:00
|
|
|
ZitadelServerOptions,
|
2023-04-21 13:49:15 +02:00
|
|
|
getManagement,
|
2023-04-21 15:13:14 +02:00
|
|
|
orgMetadata,
|
2023-04-20 14:39:51 +02:00
|
|
|
getServer,
|
|
|
|
|
getServers,
|
2023-04-21 13:49:15 +02:00
|
|
|
LabelPolicy,
|
2023-04-21 15:13:14 +02:00
|
|
|
initializeServer,
|
2023-04-26 15:14:28 +02:00
|
|
|
PrivacyPolicy,
|
|
|
|
|
PasswordComplexityPolicy,
|
2023-04-20 14:39:51 +02:00
|
|
|
} from "@zitadel/server";
|
2023-04-20 14:44:12 +02:00
|
|
|
// import { getAuth } from "@zitadel/server/auth";
|
2023-04-20 12:55:39 +02:00
|
|
|
|
2023-04-20 14:26:55 +02:00
|
|
|
export const zitadelConfig: ZitadelServerOptions = {
|
2023-04-21 15:13:14 +02:00
|
|
|
name: "zitadel login",
|
2023-04-13 13:26:02 +02:00
|
|
|
apiUrl: process.env.ZITADEL_API_URL ?? "",
|
2023-04-20 14:26:55 +02:00
|
|
|
token: process.env.ZITADEL_SERVICE_USER_TOKEN ?? "",
|
2023-04-13 13:26:02 +02:00
|
|
|
};
|
|
|
|
|
|
2023-04-21 15:13:14 +02:00
|
|
|
let server: ZitadelServer;
|
|
|
|
|
|
2023-04-20 14:26:55 +02:00
|
|
|
if (!getServers().length) {
|
2023-04-21 15:13:14 +02:00
|
|
|
console.log("initialize server");
|
|
|
|
|
server = initializeServer(zitadelConfig);
|
2023-04-13 13:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-21 13:49:15 +02:00
|
|
|
export function getBranding(
|
|
|
|
|
server: ZitadelServer
|
|
|
|
|
): Promise<LabelPolicy | undefined> {
|
|
|
|
|
const mgmt = getManagement(server);
|
2023-04-21 15:13:14 +02:00
|
|
|
return mgmt
|
|
|
|
|
.getLabelPolicy(
|
|
|
|
|
{},
|
|
|
|
|
{ metadata: orgMetadata(process.env.ZITADEL_ORG_ID ?? "") }
|
|
|
|
|
)
|
|
|
|
|
.then((resp) => resp.policy);
|
2023-04-21 13:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-26 15:14:28 +02:00
|
|
|
export function getPrivacyPolicy(
|
|
|
|
|
server: ZitadelServer
|
|
|
|
|
): Promise<PrivacyPolicy | undefined> {
|
|
|
|
|
const mgmt = getManagement(server);
|
|
|
|
|
return mgmt
|
|
|
|
|
.getPrivacyPolicy(
|
|
|
|
|
{},
|
|
|
|
|
{ metadata: orgMetadata(process.env.ZITADEL_ORG_ID ?? "") }
|
|
|
|
|
)
|
|
|
|
|
.then((resp) => resp.policy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getPasswordComplexityPolicy(
|
|
|
|
|
server: ZitadelServer
|
|
|
|
|
): Promise<PasswordComplexityPolicy | undefined> {
|
|
|
|
|
const mgmt = getManagement(server);
|
|
|
|
|
return mgmt
|
|
|
|
|
.getPasswordComplexityPolicy(
|
|
|
|
|
{},
|
|
|
|
|
{ metadata: orgMetadata(process.env.ZITADEL_ORG_ID ?? "") }
|
|
|
|
|
)
|
|
|
|
|
.then((resp) => resp.policy);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-26 16:04:56 +02:00
|
|
|
export type AddHumanUserData = {
|
2023-04-26 18:36:09 +02:00
|
|
|
firstName: string;
|
|
|
|
|
lastName: string;
|
2023-04-26 16:04:56 +02:00
|
|
|
email: string;
|
|
|
|
|
password: string;
|
|
|
|
|
};
|
|
|
|
|
export function addHumanUser(
|
|
|
|
|
server: ZitadelServer,
|
2023-04-26 18:36:09 +02:00
|
|
|
{ email, firstName, lastName, password }: AddHumanUserData
|
2023-04-26 16:04:56 +02:00
|
|
|
): Promise<string> {
|
|
|
|
|
const mgmt = getManagement(server);
|
|
|
|
|
return mgmt
|
|
|
|
|
.addHumanUser(
|
|
|
|
|
{
|
|
|
|
|
email: { email, isEmailVerified: false },
|
2023-04-26 18:36:09 +02:00
|
|
|
userName: email,
|
|
|
|
|
profile: { firstName, lastName },
|
2023-04-26 16:04:56 +02:00
|
|
|
initialPassword: password,
|
|
|
|
|
},
|
|
|
|
|
{ metadata: orgMetadata(process.env.ZITADEL_ORG_ID ?? "") }
|
|
|
|
|
)
|
|
|
|
|
.then((resp) => {
|
|
|
|
|
console.log("added user", resp.userId);
|
|
|
|
|
return resp.userId;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 13:49:15 +02:00
|
|
|
export { server };
|