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

430 lines
10 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,
oidc,
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-08-22 13:46:58 +02:00
SetSessionRequest,
2024-03-12 11:35:19 +01:00
ListUsersResponse,
ListUsersRequest,
2023-06-06 17:11:49 +02:00
DeleteSessionResponse,
VerifyPasskeyRegistrationResponse,
2023-06-28 17:33:11 +02:00
LoginSettings,
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,
2023-08-23 08:24:12 +02:00
AddHumanUserRequest,
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
}
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);
}
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
}
2024-03-19 14:15:54 +01:00
export async function createSessionForLoginname(
server: ZitadelServer,
loginName: string,
2023-06-27 18:08:22 +02:00
password: string | undefined,
2023-08-22 13:15:33 +02:00
challenges: RequestChallenges | undefined
2023-05-24 16:27:35 +02:00
): Promise<CreateSessionResponse | undefined> {
const sessionService = session.getSession(server);
return password
? sessionService.createSession(
2023-06-28 11:29:56 +02:00
{
checks: { user: { loginName }, password: { password } },
challenges,
2024-03-18 16:30:44 +01:00
lifetime: {
seconds: 300,
nanos: 0,
},
2023-06-28 11:29:56 +02:00
},
{}
)
2023-06-28 17:33:11 +02:00
: sessionService.createSession(
2024-03-18 16:30:44 +01:00
{
checks: { user: { loginName } },
challenges,
lifetime: {
seconds: 300,
nanos: 0,
},
},
2023-06-28 17:33:11 +02: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-18 16:30:44 +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,
2023-06-28 17:33:11 +02:00
password: string | undefined,
2023-08-21 17:00:29 +02:00
webAuthN: { credentialAssertionData: any } | undefined,
2023-08-22 13:15:33 +02:00
challenges: RequestChallenges | undefined
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: {},
};
if (password && payload.checks) {
payload.checks.password = { password };
}
if (webAuthN && payload.checks) {
payload.checks.webAuthN = webAuthN;
}
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;
};
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-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
};
2023-07-28 15:23:17 +02:00
return userService
.addHumanUser(
password
? {
2023-06-29 12:53:48 +02:00
...payload,
password: { password },
}
2023-06-29 12:53:48 +02:00
: payload,
2023-05-24 16:27:35 +02:00
{}
)
2023-05-22 11:48:18 +02:00
.then((resp: AddHumanUserResponse) => {
return resp.userId;
});
}
2024-03-12 11:35:19 +01:00
export async function listUsers(userName: string): Promise<ListUsersResponse> {
// TODO limit for organization
const userService = user.getUser(server);
return userService.listUsers(
{
queries: [
{
userNameQuery: {
userName,
method: TextQueryMethod.TEXT_QUERY_METHOD_EQUALS,
},
},
],
},
{}
);
}
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(
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,
2023-06-27 18:08:22 +02:00
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 };