Files
zitadel/apps/login/lib/zitadel.ts

264 lines
6.3 KiB
TypeScript
Raw Normal View History

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,
settings,
2023-04-20 14:39:51 +02:00
getServers,
initializeServer,
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,
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 = {
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
};
let server: ZitadelServer;
2023-04-20 14:26:55 +02:00
if (!getServers().length) {
console.log("initialize server");
server = initializeServer(zitadelConfig);
2023-04-13 13:26: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
}
export async function getGeneralSettings(
server: ZitadelServer
2023-05-24 16:27:35 +02:00
): Promise<string[] | undefined> {
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);
}
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
}
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-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
}
export async function createSession(
server: ZitadelServer,
loginName: string,
2023-06-19 16:44:08 +02:00
password: string | undefined
2023-05-24 16:27:35 +02:00
): Promise<CreateSessionResponse | undefined> {
const sessionService = session.getSession(server);
return password
? sessionService.createSession(
2023-06-19 16:44:08 +02:00
{ checks: { user: { loginName }, password: { password } } },
{}
)
: sessionService.createSession({ checks: { user: { loginName } } }, {});
}
export async function setSession(
server: ZitadelServer,
sessionId: string,
sessionToken: string,
password: string
2023-05-24 16:27:35 +02:00
): Promise<SetSessionResponse | undefined> {
const sessionService = session.getSession(server);
return sessionService.setSession(
{ sessionId, sessionToken, checks: { password: { password } } },
{}
);
}
export async function getSession(
server: ZitadelServer,
sessionId: string,
sessionToken: string
2023-05-24 16:27:35 +02:00
): Promise<GetSessionResponse | undefined> {
const sessionService = session.getSession(server);
return sessionService.getSession({ sessionId, sessionToken }, {});
}
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 }, {});
}
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 }, {});
}
export type AddHumanUserData = {
2023-04-26 18:36:09 +02:00
firstName: string;
lastName: string;
email: string;
password: string | undefined;
};
2023-05-17 17:04:56 +02:00
export async function addHumanUser(
server: ZitadelServer,
2023-04-26 18:36:09 +02:00
{ email, firstName, lastName, password }: AddHumanUserData
): Promise<string> {
2023-05-17 17:04:56 +02:00
const mgmt = user.getUser(server);
return mgmt
.addHumanUser(
password
? {
email: { email },
username: email,
profile: { firstName, lastName },
password: { password },
}
: {
email: { email },
username: email,
profile: { firstName, lastName },
},
2023-05-24 16:27:35 +02:00
{}
)
2023-05-22 11:48:18 +02:00
.then((resp: AddHumanUserResponse) => {
return resp.userId;
});
}
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
*/
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,
},
{}
);
}
/**
*
* @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-15 13:58:32 +02:00
userId: string
): Promise<any> {
const userservice = user.getUser(server);
2023-06-15 13:58:32 +02:00
return userservice.createPasskeyRegistrationLink({
userId,
returnCode: {},
});
}
/**
*
* @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,
2023-06-15 13:58:32 +02:00
publicKeyCredential:
| {
[key: string]: any;
}
| undefined,
userId: string
): Promise<VerifyPasskeyRegistrationResponse> {
const userservice = user.getUser(server);
return userservice.verifyPasskeyRegistration(
{
passkeyId,
passkeyName,
publicKeyCredential,
userId,
},
{}
);
}
2023-06-12 10:38:28 +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(
userId: string,
2023-06-13 17:43:37 +02:00
code: { id: string; code: string }
2023-06-12 10:38:28 +02:00
): Promise<any> {
const userservice = user.getUser(server);
2023-06-13 17:43:37 +02:00
return userservice.registerPasskey({
userId,
code,
});
2023-06-12 10:38:28 +02:00
}
2023-04-21 13:49:15 +02:00
export { server };