import { ZitadelServer, ZitadelServerOptions, user, oidc, settings, getServers, initializeServer, session, GetGeneralSettingsResponse, CreateSessionResponse, GetBrandingSettingsResponse, GetPasswordComplexitySettingsResponse, GetLegalAndSupportSettingsResponse, AddHumanUserResponse, BrandingSettings, ListSessionsResponse, LegalAndSupportSettings, PasswordComplexitySettings, GetSessionResponse, VerifyEmailResponse, SetSessionResponse, DeleteSessionResponse, VerifyPasskeyRegistrationResponse, LoginSettings, GetLoginSettingsResponse, ListAuthenticationMethodTypesResponse, StartIdentityProviderFlowRequest, StartIdentityProviderFlowResponse, RetrieveIdentityProviderInformationRequest, RetrieveIdentityProviderInformationResponse, GetAuthRequestResponse, GetAuthRequestRequest, CreateCallbackRequest, CreateCallbackResponse, RequestChallenges, } from "@zitadel/server"; export const zitadelConfig: ZitadelServerOptions = { name: "zitadel login", apiUrl: process.env.ZITADEL_API_URL ?? "", token: process.env.ZITADEL_SERVICE_USER_TOKEN ?? "", }; let server: ZitadelServer; if (!getServers().length) { console.log("initialize server"); server = initializeServer(zitadelConfig); } export async function getBrandingSettings( server: ZitadelServer ): Promise { const settingsService = settings.getSettings(server); return settingsService .getBrandingSettings({}, {}) .then((resp: GetBrandingSettingsResponse) => resp.settings); } export async function getLoginSettings( server: ZitadelServer ): Promise { const settingsService = settings.getSettings(server); return settingsService .getLoginSettings({}, {}) .then((resp: GetLoginSettingsResponse) => resp.settings); } export async function getGeneralSettings( server: ZitadelServer ): Promise { const settingsService = settings.getSettings(server); return settingsService .getGeneralSettings({}, {}) .then((resp: GetGeneralSettingsResponse) => resp.supportedLanguages); } export async function getLegalAndSupportSettings( server: ZitadelServer ): Promise { const settingsService = settings.getSettings(server); return settingsService .getLegalAndSupportSettings({}, {}) .then((resp: GetLegalAndSupportSettingsResponse) => { return resp.settings; }); } export async function getPasswordComplexitySettings( server: ZitadelServer ): Promise { const settingsService = settings.getSettings(server); return settingsService .getPasswordComplexitySettings({}, {}) .then((resp: GetPasswordComplexitySettingsResponse) => resp.settings); } export async function createSession( server: ZitadelServer, loginName: string, password: string | undefined, challenges: RequestChallenges | undefined ): Promise { const sessionService = session.getSession(server); return password ? sessionService.createSession( { checks: { user: { loginName }, password: { password } }, challenges, }, {} ) : sessionService.createSession( { checks: { user: { loginName } }, challenges }, {} ); } export async function setSession( server: ZitadelServer, sessionId: string, sessionToken: string, password: string | undefined, webAuthN: { credentialAssertionData: any } | undefined, challenges: RequestChallenges | undefined ): Promise { const sessionService = session.getSession(server); const payload = { sessionId, sessionToken, challenges }; return password ? sessionService.setSession( { ...payload, checks: { password: { password }, webAuthN }, }, {} ) : sessionService.setSession(payload, {}); } export async function getSession( server: ZitadelServer, sessionId: string, sessionToken: string ): Promise { const sessionService = session.getSession(server); return sessionService.getSession({ sessionId, sessionToken }, {}); } export async function deleteSession( server: ZitadelServer, sessionId: string, sessionToken: string ): Promise { const sessionService = session.getSession(server); return sessionService.deleteSession({ sessionId, sessionToken }, {}); } export async function listSessions( server: ZitadelServer, ids: string[] ): Promise { 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 = { firstName: string; lastName: string; email: string; password: string | undefined; }; export async function addHumanUser( server: ZitadelServer, { email, firstName, lastName, password }: AddHumanUserData ): Promise { const userService = user.getUser(server); const payload = { email: { email }, username: email, profile: { firstName, lastName }, }; return userService .addHumanUser( password ? { ...payload, password: { password }, } : payload, {} ) .then((resp: AddHumanUserResponse) => { return resp.userId; }); } export async function startIdentityProviderFlow( server: ZitadelServer, { idpId, urls }: StartIdentityProviderFlowRequest ): Promise { const userService = user.getUser(server); return userService.startIdentityProviderFlow({ idpId, urls, }); } export async function retrieveIdentityProviderInformation( server: ZitadelServer, { intentId, token }: RetrieveIdentityProviderInformationRequest ): Promise { const userService = user.getUser(server); return userService.retrieveIdentityProviderInformation({ intentId, token, }); } export async function getAuthRequest( server: ZitadelServer, { authRequestId }: GetAuthRequestRequest ): Promise { const oidcService = oidc.getOidc(server); return oidcService.getAuthRequest({ authRequestId, }); } export async function createCallback( server: ZitadelServer, { authRequestId }: CreateCallbackRequest ): Promise { const oidcService = oidc.getOidc(server); return oidcService.createCallback({ authRequestId, }); } export async function verifyEmail( server: ZitadelServer, userId: string, verificationCode: string ): Promise { const userservice = user.getUser(server); return userservice.verifyEmail( { userId, verificationCode, }, {} ); } /** * * @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 { 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( userId: string ): Promise { const userservice = user.getUser(server); 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, publicKeyCredential: | { [key: string]: any; } | undefined, userId: string ): Promise { const userservice = user.getUser(server); return userservice.verifyPasskeyRegistration( { passkeyId, passkeyName, publicKeyCredential, userId, }, {} ); } /** * * @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, code: { id: string; code: string }, domain: string ): Promise { const userservice = user.getUser(server); return userservice.registerPasskey({ userId, code, domain, // authenticator: }); } /** * * @param server * @param userId the id of the user where the email should be set * @returns the newly set email */ export async function listAuthenticationMethodTypes( userId: string ): Promise { const userservice = user.getUser(server); return userservice.listAuthenticationMethodTypes({ userId, }); } export { server };