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

601 lines
14 KiB
TypeScript
Raw Normal View History

import { VerifyU2FRegistrationRequest } from "@zitadel/server";
import {
GetUserByIDResponse,
RegisterTOTPResponse,
VerifyTOTPRegistrationResponse,
} from "@zitadel/server";
2023-04-20 14:39:51 +02:00
import {
2024-04-12 16:38:38 +02:00
LegalAndSupportSettings,
PasswordComplexitySettings,
2023-04-21 13:49:15 +02:00
ZitadelServer,
2024-04-15 17:23:28 +02:00
VerifyMyAuthFactorOTPResponse,
2023-04-20 14:39:51 +02:00
ZitadelServerOptions,
2023-05-17 17:04:56 +02:00
user,
oidc,
settings,
2023-04-20 14:39:51 +02:00
getServers,
2024-04-15 17:23:28 +02:00
auth,
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,
2024-04-15 17:23:28 +02:00
AddMyAuthFactorOTPResponse,
2023-05-22 11:48:18 +02:00
GetLegalAndSupportSettingsResponse,
AddHumanUserResponse,
2023-05-24 16:27:35 +02:00
BrandingSettings,
ListSessionsResponse,
GetSessionResponse,
VerifyEmailResponse,
2024-04-04 13:50:54 +02:00
Checks,
2023-05-24 16:27:35 +02:00
SetSessionResponse,
2023-08-22 13:46:58 +02:00
SetSessionRequest,
2024-03-12 11:35:19 +01:00
ListUsersResponse,
2024-04-03 15:16:06 +02:00
management,
2023-06-06 17:11:49 +02:00
DeleteSessionResponse,
VerifyPasskeyRegistrationResponse,
2023-06-28 17:33:11 +02:00
LoginSettings,
2024-04-03 15:16:06 +02:00
GetOrgByDomainGlobalResponse,
2023-06-28 17:33:11 +02:00
GetLoginSettingsResponse,
2023-07-04 09:34:07 +02:00
ListAuthenticationMethodTypesResponse,
2023-08-23 08:24:12 +02:00
StartIdentityProviderIntentRequest,
StartIdentityProviderIntentResponse,
RetrieveIdentityProviderIntentRequest,
RetrieveIdentityProviderIntentResponse,
GetAuthRequestResponse,
GetAuthRequestRequest,
CreateCallbackRequest,
CreateCallbackResponse,
2023-08-21 17:00:29 +02:00
RequestChallenges,
2024-03-12 11:35:19 +01:00
TextQueryMethod,
2024-04-12 16:38:38 +02:00
ListHumanAuthFactorsResponse,
2023-08-23 08:24:12 +02:00
AddHumanUserRequest,
2024-04-16 15:33:14 +02:00
AddOTPEmailResponse,
AddOTPSMSResponse,
2023-04-20 14:39:51 +02:00
} from "@zitadel/server";
2023-04-20 12:55:39 +02:00
const SESSION_LIFETIME_S = 3000;
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(
2024-04-01 10:00:31 +02:00
server: ZitadelServer,
organization?: string
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
2024-04-01 10:00:31 +02:00
.getBrandingSettings(
{ ctx: organization ? { orgId: organization } : { instance: true } },
{}
)
2023-05-22 11:48:18 +02:00
.then((resp: GetBrandingSettingsResponse) => resp.settings);
2023-04-21 13:49:15 +02:00
}
2023-06-28 11:29:56 +02:00
export async function getLoginSettings(
2024-03-12 11:35:19 +01:00
server: ZitadelServer,
orgId?: string
2023-06-28 11:29:56 +02:00
): Promise<LoginSettings | undefined> {
const settingsService = settings.getSettings(server);
return settingsService
2024-03-12 11:35:19 +01:00
.getLoginSettings({ ctx: orgId ? { orgId } : { instance: true } }, {})
2023-06-28 11:29:56 +02:00
.then((resp: GetLoginSettingsResponse) => resp.settings);
}
2024-04-15 17:23:28 +02:00
export async function verifyMyAuthFactorOTP(
code: string
): Promise<VerifyMyAuthFactorOTPResponse> {
const authService = auth.getAuth(server);
return authService.verifyMyAuthFactorOTP({ code }, {});
}
2024-04-16 15:33:14 +02:00
export async function addOTPEmail(
userId: string
): Promise<AddOTPEmailResponse | undefined> {
const userService = user.getUser(server);
return userService.addOTPEmail(
{
userId,
},
{}
);
}
2024-04-15 17:23:28 +02:00
2024-04-16 15:33:14 +02:00
export async function addOTPSMS(
2024-04-25 15:39:13 +02:00
userId: string,
token?: string
2024-04-16 15:33:14 +02:00
): Promise<AddOTPSMSResponse | undefined> {
2024-04-25 15:39:13 +02:00
let userService;
if (token) {
const authConfig: ZitadelServerOptions = {
name: "zitadel login",
apiUrl: process.env.ZITADEL_API_URL ?? "",
token: token,
};
const sessionUser = initializeServer(authConfig);
userService = user.getUser(sessionUser);
} else {
userService = user.getUser(server);
}
2024-04-16 15:33:14 +02:00
return userService.addOTPSMS({ userId }, {});
}
2024-04-15 17:23:28 +02:00
2024-04-16 15:33:14 +02:00
export async function registerTOTP(
userId: string,
token?: string
): Promise<RegisterTOTPResponse | undefined> {
let userService;
if (token) {
const authConfig: ZitadelServerOptions = {
name: "zitadel login",
apiUrl: process.env.ZITADEL_API_URL ?? "",
token: token,
};
const sessionUser = initializeServer(authConfig);
userService = user.getUser(sessionUser);
} else {
userService = user.getUser(server);
}
return userService.registerTOTP({ userId }, {});
2024-04-15 17:23:28 +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(
server: ZitadelServer,
organization?: string
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
.getLegalAndSupportSettings(
{ ctx: organization ? { orgId: organization } : { instance: true } },
{}
)
2023-05-24 16:27:35 +02:00
.then((resp: GetLegalAndSupportSettingsResponse) => {
return resp.settings;
});
2023-04-26 15:14:28 +02:00
}
export async function getPasswordComplexitySettings(
server: ZitadelServer,
organization?: string
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
.getPasswordComplexitySettings(
organization
? { ctx: { orgId: organization } }
: { ctx: { instance: true } },
{}
)
2023-05-22 11:48:18 +02:00
.then((resp: GetPasswordComplexitySettingsResponse) => resp.settings);
2023-04-26 15:14:28 +02:00
}
2024-04-04 13:50:54 +02:00
export async function createSessionFromChecks(
server: ZitadelServer,
2024-04-04 13:50:54 +02:00
checks: Checks,
2024-03-25 13:39:23 +01:00
challenges: RequestChallenges | undefined
): Promise<CreateSessionResponse | undefined> {
const sessionService = session.getSession(server);
2024-04-04 13:50:54 +02:00
return sessionService.createSession(
{
checks: checks,
challenges,
lifetime: {
seconds: SESSION_LIFETIME_S,
2024-04-04 13:50:54 +02:00
nanos: 0,
},
},
{}
);
2024-03-25 13:39:23 +01:00
}
2024-03-15 17:21:21 +01:00
export async function createSessionForUserIdAndIdpIntent(
server: ZitadelServer,
userId: string,
idpIntent: {
idpIntentId?: string | undefined;
idpIntentToken?: string | undefined;
}
): Promise<CreateSessionResponse | undefined> {
const sessionService = session.getSession(server);
2024-03-19 14:15:54 +01:00
2024-03-15 17:21:21 +01:00
return sessionService.createSession(
{
checks: { user: { userId }, idpIntent },
2024-03-21 14:21:23 +01:00
// lifetime: {
// seconds: 300,
// nanos: 0,
// },
2024-03-15 17:21:21 +01:00
},
{}
);
}
export async function setSession(
server: ZitadelServer,
sessionId: string,
sessionToken: string,
2024-04-16 09:27:58 +02:00
challenges: RequestChallenges | undefined,
checks: Checks
2023-05-24 16:27:35 +02:00
): Promise<SetSessionResponse | undefined> {
const sessionService = session.getSession(server);
2023-06-29 17:04:34 +02:00
2023-08-22 13:46:58 +02:00
const payload: SetSessionRequest = {
sessionId,
sessionToken,
challenges,
checks: {},
metadata: {},
};
2024-04-16 09:27:58 +02:00
if (checks && payload.checks) {
payload.checks = checks;
2023-08-22 13:46:58 +02:00
}
return sessionService.setSession(payload, {});
}
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;
organization: string | undefined;
};
2023-05-17 17:04:56 +02:00
export async function addHumanUser(
server: ZitadelServer,
{ email, firstName, lastName, password, organization }: AddHumanUserData
): Promise<AddHumanUserResponse> {
2023-07-28 15:23:17 +02:00
const userService = user.getUser(server);
2023-06-29 12:53:48 +02:00
2023-08-23 08:24:12 +02:00
const payload: Partial<AddHumanUserRequest> = {
2023-06-29 12:53:48 +02:00
email: { email },
username: email,
2023-08-23 08:24:12 +02:00
profile: { givenName: firstName, familyName: lastName },
2023-06-29 12:53:48 +02:00
};
if (organization) {
payload.organization = { orgId: organization };
}
return userService.addHumanUser(
password
? {
...payload,
password: { password },
}
: payload,
{}
);
}
export async function verifyTOTPRegistration(
code: string,
userId: string,
token?: string
): Promise<VerifyTOTPRegistrationResponse> {
let userService;
if (token) {
const authConfig: ZitadelServerOptions = {
name: "zitadel login",
apiUrl: process.env.ZITADEL_API_URL ?? "",
token: token,
};
const sessionUser = initializeServer(authConfig);
userService = user.getUser(sessionUser);
} else {
userService = user.getUser(server);
}
return userService.verifyTOTPRegistration({ code, userId }, {});
}
2024-04-17 15:19:19 +02:00
export async function getUserByID(
userId: string
): Promise<GetUserByIDResponse> {
const userService = user.getUser(server);
return userService.getUserByID({ userId }, {});
}
2024-03-25 13:39:23 +01:00
export async function listUsers(
userName: string,
organizationId: string
): Promise<ListUsersResponse> {
2024-03-12 11:35:19 +01:00
const userService = user.getUser(server);
return userService.listUsers(
{
2024-03-28 09:03:18 +01:00
queries: organizationId
? [
{
userNameQuery: {
userName,
method: TextQueryMethod.TEXT_QUERY_METHOD_EQUALS,
},
},
{
organizationIdQuery: {
2024-03-25 13:39:23 +01:00
organizationId,
2024-03-28 09:03:18 +01:00
},
},
]
: [
{
userNameQuery: {
userName,
method: TextQueryMethod.TEXT_QUERY_METHOD_EQUALS,
},
},
],
2024-03-12 11:35:19 +01:00
},
{}
);
}
2024-04-03 15:16:06 +02:00
export async function getOrgByDomain(
domain: string
): Promise<GetOrgByDomainGlobalResponse> {
const mgmtService = management.getManagement(server);
return mgmtService.getOrgByDomainGlobal({ domain }, {});
}
2023-07-27 11:05:42 +02:00
export async function startIdentityProviderFlow(
server: ZitadelServer,
2023-08-23 08:24:12 +02:00
{ idpId, urls }: StartIdentityProviderIntentRequest
): Promise<StartIdentityProviderIntentResponse> {
2023-07-27 11:05:42 +02:00
const userService = user.getUser(server);
2023-08-23 08:24:12 +02:00
return userService.startIdentityProviderIntent({
2023-07-27 11:05:42 +02:00
idpId,
2023-08-21 17:00:29 +02:00
urls,
2023-07-27 11:05:42 +02:00
});
}
2023-07-28 15:23:17 +02:00
export async function retrieveIdentityProviderInformation(
server: ZitadelServer,
2023-08-23 08:24:12 +02:00
{ idpIntentId, idpIntentToken }: RetrieveIdentityProviderIntentRequest
): Promise<RetrieveIdentityProviderIntentResponse> {
2023-07-28 15:23:17 +02:00
const userService = user.getUser(server);
2023-08-23 08:24:12 +02:00
return userService.retrieveIdentityProviderIntent({
idpIntentId,
idpIntentToken,
2023-07-28 15:23:17 +02:00
});
}
export async function getAuthRequest(
server: ZitadelServer,
{ authRequestId }: GetAuthRequestRequest
): Promise<GetAuthRequestResponse> {
const oidcService = oidc.getOidc(server);
return oidcService.getAuthRequest({
authRequestId,
});
}
export async function createCallback(
server: ZitadelServer,
req: CreateCallbackRequest
): Promise<CreateCallbackResponse> {
const oidcService = oidc.getOidc(server);
return oidcService.createCallback(req);
}
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(
userId: string,
token?: string
): Promise<any> {
let userService;
if (token) {
const authConfig: ZitadelServerOptions = {
name: "zitadel login",
apiUrl: process.env.ZITADEL_API_URL ?? "",
token: token,
};
const sessionUser = initializeServer(authConfig);
userService = user.getUser(sessionUser);
} else {
userService = user.getUser(server);
}
2023-06-15 13:58:32 +02:00
return userService.createPasskeyRegistrationLink({
2023-06-15 13:58:32 +02:00
userId,
returnCode: {},
});
}
/**
*
* @param server
* @param userId the id of the user where the email should be set
* @param domain the domain on which the factor is registered
* @returns the newly set email
*/
export async function registerU2F(
userId: string,
domain: string
): Promise<any> {
const userservice = user.getUser(server);
return userservice.registerU2F({
userId,
domain,
});
}
/**
*
* @param server
* @param userId the id of the user where the email should be set
* @param domain the domain on which the factor is registered
* @returns the newly set email
*/
export async function verifyU2FRegistration(
request: VerifyU2FRegistrationRequest
): Promise<any> {
const userservice = user.getUser(server);
return userservice.verifyU2FRegistration(request, {});
}
/**
*
* @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-27 18:08:22 +02:00
code: { id: string; code: string },
domain: 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-27 18:08:22 +02:00
domain,
// authenticator:
2023-06-13 17:43:37 +02:00
});
2023-06-12 10:38:28 +02:00
}
2023-06-28 10:47:22 +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 listAuthenticationMethodTypes(
userId: string
2023-07-04 09:34:07 +02:00
): Promise<ListAuthenticationMethodTypesResponse> {
2023-06-28 10:47:22 +02:00
const userservice = user.getUser(server);
return userservice.listAuthenticationMethodTypes({
userId,
});
}
2023-04-21 13:49:15 +02:00
export { server };