2023-04-20 14:39:51 +02:00
|
|
|
import {
|
2023-04-21 13:49:15 +02:00
|
|
|
ZitadelServer,
|
2023-04-20 14:39:51 +02:00
|
|
|
ZitadelServerOptions,
|
2023-05-17 17:04:56 +02:00
|
|
|
user,
|
2023-05-16 17:34:52 +02:00
|
|
|
settings,
|
2023-04-20 14:39:51 +02:00
|
|
|
getServers,
|
2023-04-21 15:13:14 +02:00
|
|
|
initializeServer,
|
2023-05-16 17:34:52 +02:00
|
|
|
session,
|
2023-05-22 11:48:18 +02:00
|
|
|
GetGeneralSettingsResponse,
|
2023-05-24 16:27:35 +02:00
|
|
|
CreateSessionResponse,
|
2023-05-22 11:48:18 +02:00
|
|
|
GetBrandingSettingsResponse,
|
|
|
|
|
GetPasswordComplexitySettingsResponse,
|
|
|
|
|
GetLegalAndSupportSettingsResponse,
|
|
|
|
|
AddHumanUserResponse,
|
2023-05-24 16:27:35 +02:00
|
|
|
BrandingSettings,
|
|
|
|
|
ListSessionsResponse,
|
|
|
|
|
LegalAndSupportSettings,
|
|
|
|
|
PasswordComplexitySettings,
|
|
|
|
|
GetSessionResponse,
|
|
|
|
|
VerifyEmailResponse,
|
|
|
|
|
SetSessionResponse,
|
2023-06-06 17:11:49 +02:00
|
|
|
DeleteSessionResponse,
|
2023-06-08 16:21:02 +02:00
|
|
|
VerifyPasskeyRegistrationResponse,
|
2023-04-20 14:39:51 +02:00
|
|
|
} from "@zitadel/server";
|
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-06-08 16:21:02 +02:00
|
|
|
export async function getBrandingSettings(
|
2023-04-21 13:49:15 +02:00
|
|
|
server: ZitadelServer
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<BrandingSettings | undefined> {
|
2023-05-15 09:23:59 +02:00
|
|
|
const settingsService = settings.getSettings(server);
|
|
|
|
|
return settingsService
|
2023-05-24 16:27:35 +02:00
|
|
|
.getBrandingSettings({}, {})
|
2023-05-22 11:48:18 +02:00
|
|
|
.then((resp: GetBrandingSettingsResponse) => resp.settings);
|
2023-04-21 13:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function getGeneralSettings(
|
2023-05-16 17:34:52 +02:00
|
|
|
server: ZitadelServer
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<string[] | undefined> {
|
2023-05-16 17:34:52 +02:00
|
|
|
const settingsService = settings.getSettings(server);
|
|
|
|
|
return settingsService
|
2023-05-24 16:27:35 +02:00
|
|
|
.getGeneralSettings({}, {})
|
2023-05-22 11:48:18 +02:00
|
|
|
.then((resp: GetGeneralSettingsResponse) => resp.supportedLanguages);
|
2023-05-16 17:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function getLegalAndSupportSettings(
|
2023-04-26 15:14:28 +02:00
|
|
|
server: ZitadelServer
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<LegalAndSupportSettings | undefined> {
|
2023-05-15 09:23:59 +02:00
|
|
|
const settingsService = settings.getSettings(server);
|
|
|
|
|
return settingsService
|
2023-05-24 16:27:35 +02:00
|
|
|
.getLegalAndSupportSettings({}, {})
|
|
|
|
|
.then((resp: GetLegalAndSupportSettingsResponse) => {
|
|
|
|
|
return resp.settings;
|
|
|
|
|
});
|
2023-04-26 15:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function getPasswordComplexitySettings(
|
2023-04-26 15:14:28 +02:00
|
|
|
server: ZitadelServer
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<PasswordComplexitySettings | undefined> {
|
2023-05-15 09:23:59 +02:00
|
|
|
const settingsService = settings.getSettings(server);
|
2023-05-16 17:34:52 +02:00
|
|
|
|
2023-05-15 09:23:59 +02:00
|
|
|
return settingsService
|
2023-05-24 16:27:35 +02:00
|
|
|
.getPasswordComplexitySettings({}, {})
|
2023-05-22 11:48:18 +02:00
|
|
|
.then((resp: GetPasswordComplexitySettingsResponse) => resp.settings);
|
2023-04-26 15:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function createSession(
|
2023-05-16 17:34:52 +02:00
|
|
|
server: ZitadelServer,
|
|
|
|
|
loginName: string
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<CreateSessionResponse | undefined> {
|
2023-05-16 17:34:52 +02:00
|
|
|
const sessionService = session.getSession(server);
|
|
|
|
|
return sessionService.createSession({ checks: { user: { loginName } } }, {});
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function setSession(
|
2023-05-16 17:34:52 +02:00
|
|
|
server: ZitadelServer,
|
2023-05-17 13:46:44 +02:00
|
|
|
sessionId: string,
|
|
|
|
|
sessionToken: string,
|
|
|
|
|
password: string
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<SetSessionResponse | undefined> {
|
2023-05-17 13:46:44 +02:00
|
|
|
const sessionService = session.getSession(server);
|
|
|
|
|
return sessionService.setSession(
|
|
|
|
|
{ sessionId, sessionToken, checks: { password: { password } } },
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function getSession(
|
2023-05-17 13:46:44 +02:00
|
|
|
server: ZitadelServer,
|
|
|
|
|
sessionId: string,
|
|
|
|
|
sessionToken: string
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<GetSessionResponse | undefined> {
|
2023-05-16 17:34:52 +02:00
|
|
|
const sessionService = session.getSession(server);
|
2023-05-17 13:46:44 +02:00
|
|
|
return sessionService.getSession({ sessionId, sessionToken }, {});
|
2023-05-16 17:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function deleteSession(
|
2023-06-06 17:11:49 +02:00
|
|
|
server: ZitadelServer,
|
|
|
|
|
sessionId: string,
|
|
|
|
|
sessionToken: string
|
|
|
|
|
): Promise<DeleteSessionResponse | undefined> {
|
|
|
|
|
const sessionService = session.getSession(server);
|
|
|
|
|
return sessionService.deleteSession({ sessionId, sessionToken }, {});
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function listSessions(
|
2023-05-17 15:25:25 +02:00
|
|
|
server: ZitadelServer,
|
|
|
|
|
ids: string[]
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<ListSessionsResponse | undefined> {
|
2023-05-17 15:25:25 +02:00
|
|
|
const sessionService = session.getSession(server);
|
|
|
|
|
const query = { offset: 0, limit: 100, asc: true };
|
|
|
|
|
const queries = [{ idsQuery: { ids } }];
|
|
|
|
|
return sessionService.listSessions({ queries: queries }, {});
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
2023-05-17 17:04:56 +02:00
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function addHumanUser(
|
2023-04-26 16:04:56 +02:00
|
|
|
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> {
|
2023-05-17 17:04:56 +02:00
|
|
|
const mgmt = user.getUser(server);
|
2023-04-26 16:04:56 +02:00
|
|
|
return mgmt
|
|
|
|
|
.addHumanUser(
|
|
|
|
|
{
|
2023-05-19 10:13:05 +02:00
|
|
|
email: { email },
|
2023-05-17 17:04:56 +02:00
|
|
|
username: email,
|
2023-04-26 18:36:09 +02:00
|
|
|
profile: { firstName, lastName },
|
2023-05-17 17:04:56 +02:00
|
|
|
password: { password },
|
2023-04-26 16:04:56 +02:00
|
|
|
},
|
2023-05-24 16:27:35 +02:00
|
|
|
{}
|
2023-04-26 16:04:56 +02:00
|
|
|
)
|
2023-05-22 11:48:18 +02:00
|
|
|
.then((resp: AddHumanUserResponse) => {
|
2023-04-26 16:04:56 +02:00
|
|
|
return resp.userId;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function verifyEmail(
|
2023-05-17 17:04:56 +02:00
|
|
|
server: ZitadelServer,
|
|
|
|
|
userId: string,
|
|
|
|
|
verificationCode: string
|
2023-05-24 16:27:35 +02:00
|
|
|
): Promise<VerifyEmailResponse> {
|
2023-05-22 11:48:18 +02:00
|
|
|
const userservice = user.getUser(server);
|
|
|
|
|
return userservice.verifyEmail(
|
2023-05-17 17:04:56 +02:00
|
|
|
{
|
|
|
|
|
userId,
|
|
|
|
|
verificationCode,
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 11:48:18 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param server
|
|
|
|
|
* @param userId the id of the user where the email should be set
|
|
|
|
|
* @returns the newly set email
|
|
|
|
|
*/
|
2023-06-08 16:21:02 +02:00
|
|
|
export async function setEmail(
|
|
|
|
|
server: ZitadelServer,
|
|
|
|
|
userId: string
|
|
|
|
|
): Promise<any> {
|
2023-05-22 11:48:18 +02:00
|
|
|
const userservice = user.getUser(server);
|
|
|
|
|
return userservice.setEmail(
|
|
|
|
|
{
|
|
|
|
|
userId,
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param server
|
|
|
|
|
* @param userId the id of the user where the email should be set
|
|
|
|
|
* @returns the newly set email
|
|
|
|
|
*/
|
|
|
|
|
export async function registerPasskey(
|
|
|
|
|
server: ZitadelServer,
|
|
|
|
|
userId: string
|
|
|
|
|
): Promise<any> {
|
|
|
|
|
const userservice = user.getUser(server);
|
|
|
|
|
return userservice.registerPasskey(
|
|
|
|
|
{
|
|
|
|
|
userId,
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param server
|
|
|
|
|
* @param userId the id of the user where the email should be set
|
|
|
|
|
* @returns the newly set email
|
|
|
|
|
*/
|
|
|
|
|
export async function createPasskeyRegistrationLink(
|
2023-06-08 17:27:35 +02:00
|
|
|
userId: string,
|
|
|
|
|
sessionToken: string
|
2023-06-08 16:21:02 +02:00
|
|
|
): Promise<any> {
|
2023-06-08 17:27:35 +02:00
|
|
|
// this actions will be made from the currently seleected user
|
|
|
|
|
const zitadelConfig: ZitadelServerOptions = {
|
|
|
|
|
name: "zitadel login",
|
|
|
|
|
apiUrl: process.env.ZITADEL_API_URL ?? "",
|
|
|
|
|
token: `${sessionToken}`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const server: ZitadelServer = initializeServer(zitadelConfig);
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
const userservice = user.getUser(server);
|
|
|
|
|
return userservice.createPasskeyRegistrationLink(
|
|
|
|
|
{
|
|
|
|
|
userId,
|
|
|
|
|
// returnCode: new ReturnPasskeyRegistrationCode(),
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param server
|
|
|
|
|
* @param userId the id of the user where the email should be set
|
|
|
|
|
* @returns the newly set email
|
|
|
|
|
*/
|
|
|
|
|
export async function verifyPasskeyRegistration(
|
|
|
|
|
server: ZitadelServer,
|
|
|
|
|
passkeyId: string,
|
|
|
|
|
passkeyName: string,
|
|
|
|
|
publicKeyCredential: any,
|
|
|
|
|
userId: string
|
|
|
|
|
): Promise<VerifyPasskeyRegistrationResponse> {
|
|
|
|
|
const userservice = user.getUser(server);
|
|
|
|
|
return userservice.verifyPasskeyRegistration(
|
|
|
|
|
{
|
|
|
|
|
passkeyId,
|
|
|
|
|
passkeyName,
|
|
|
|
|
publicKeyCredential,
|
|
|
|
|
userId,
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-04-21 13:49:15 +02:00
|
|
|
export { server };
|