571 lines
16 KiB
TypeScript
Raw Normal View History

2023-04-27 11:52:02 +02:00
import type { LoginThemePageId, ThemeType } from "keycloakify/bin/keycloakify/generateFtl";
2021-10-11 20:56:43 +02:00
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
2023-03-19 14:48:01 +01:00
import type { MessageKey } from "../i18n/i18n";
2021-02-23 15:32:37 +01:00
type ExtractAfterStartingWith<Prefix extends string, StrEnum> = StrEnum extends `${Prefix}${infer U}` ? U : never;
2021-02-23 15:32:37 +01:00
/** Take theses type definition with a grain of salt.
* Some values might be undefined on some pages.
* (ex: url.loginAction is undefined on error.ftl)
*/
2023-03-18 18:27:50 +01:00
export type KcContext =
| KcContext.Login
| KcContext.Register
| KcContext.RegisterUserProfile
| KcContext.Info
| KcContext.Error
| KcContext.LoginResetPassword
| KcContext.LoginVerifyEmail
| KcContext.Terms
| KcContext.LoginOtp
| KcContext.LoginUsername
| KcContext.WebauthnAuthenticate
| KcContext.LoginPassword
| KcContext.LoginUpdatePassword
| KcContext.LoginUpdateProfile
| KcContext.LoginIdpLinkConfirm
| KcContext.LoginIdpLinkEmail
| KcContext.LoginPageExpired
| KcContext.LoginConfigTotp
| KcContext.LogoutConfirm
| KcContext.UpdateUserProfile
2023-03-27 13:02:44 +03:00
| KcContext.IdpReviewUserProfile
2023-03-31 17:38:22 +02:00
| KcContext.UpdateEmail
2023-04-20 20:51:46 +02:00
| KcContext.SelectAuthenticator
| KcContext.SamlPostForm;
2023-03-18 18:27:50 +01:00
export declare namespace KcContext {
2023-03-18 16:17:33 +01:00
export type Common = {
keycloakifyVersion: string;
2023-04-27 11:52:02 +02:00
themeType: "login";
2023-06-08 23:09:14 +02:00
themeName: string;
2023-03-18 16:17:33 +01:00
url: {
loginAction: string;
resourcesPath: string;
resourcesCommonPath: string;
loginRestartFlowUrl: string;
loginUrl: string;
};
2023-03-18 16:17:33 +01:00
realm: {
name: string;
displayName?: string;
displayNameHtml?: string;
internationalizationEnabled: boolean;
registrationEmailAsUsername: boolean;
};
/** Undefined if !realm.internationalizationEnabled */
locale?: {
supported: {
url: string;
label: string;
languageTag: string;
}[];
currentLanguageTag: string;
2021-10-11 03:25:02 +02:00
};
2023-03-18 16:17:33 +01:00
auth?: {
showUsername?: boolean;
showResetCredentials?: boolean;
showTryAnotherWayLink?: boolean;
attemptedUsername?: string;
};
scripts: string[];
message?: {
type: "success" | "warning" | "error" | "info";
summary: string;
};
client: {
clientId: string;
name?: string;
description?: string;
};
isAppInitiatedAction: boolean;
messagesPerField: {
printIfExists: <T>(fieldName: string, x: T) => T | undefined;
existsError: (fieldName: string) => boolean;
get: (fieldName: string) => string;
exists: (fieldName: string) => boolean;
};
};
2021-03-04 21:14:54 +01:00
2023-04-20 20:51:46 +02:00
export type SamlPostForm = Common & {
pageId: "saml-post-form.ftl";
samlPost: {
url: string;
SAMLRequest?: string;
SAMLResponse?: string;
2023-05-02 16:53:43 +03:00
relayState?: string;
2023-04-20 20:51:46 +02:00
};
};
2023-03-18 16:17:33 +01:00
export type Login = Common & {
2021-03-06 22:41:36 +01:00
pageId: "login.ftl";
2021-03-04 21:14:54 +01:00
url: {
loginResetCredentialsUrl: string;
registrationUrl: string;
};
realm: {
loginWithEmailAllowed: boolean;
rememberMe: boolean;
2021-03-06 23:03:03 +01:00
password: boolean;
2021-03-04 21:14:54 +01:00
resetPasswordAllowed: boolean;
2021-03-04 23:24:43 +01:00
registrationAllowed: boolean;
2021-03-04 21:14:54 +01:00
};
auth: {
selectedCredential?: string;
};
registrationDisabled: boolean;
login: {
2021-03-04 18:15:48 +01:00
username?: string;
rememberMe?: string;
password?: string;
2021-03-04 21:14:54 +01:00
};
usernameEditDisabled: boolean;
social: {
displayInfo: boolean;
providers?: {
loginUrl: string;
alias: string;
providerId: string;
displayName: string;
}[];
2021-03-04 21:14:54 +01:00
};
};
2023-03-17 20:40:29 +01:00
export type Register = RegisterUserProfile.CommonWithLegacy & {
2021-10-11 03:25:02 +02:00
pageId: "register.ftl";
register: {
formData: {
firstName?: string;
displayName?: string;
lastName?: string;
email?: string;
username?: string;
};
2021-10-11 03:25:02 +02:00
};
};
2023-03-17 20:40:29 +01:00
export type RegisterUserProfile = RegisterUserProfile.CommonWithLegacy & {
2021-10-11 03:25:02 +02:00
pageId: "register-user-profile.ftl";
profile: {
2021-10-11 20:56:43 +02:00
context: "REGISTRATION_PROFILE";
attributes: Attribute[];
attributesByName: Record<string, Attribute>;
};
2021-10-11 03:25:02 +02:00
};
2023-03-17 20:40:29 +01:00
export namespace RegisterUserProfile {
2023-03-18 16:17:33 +01:00
export type CommonWithLegacy = Common & {
2023-03-17 20:40:29 +01:00
url: {
registrationAction: string;
};
passwordRequired: boolean;
recaptchaRequired: boolean;
recaptchaSiteKey?: string;
social: {
displayInfo: boolean;
providers?: {
loginUrl: string;
alias: string;
providerId: string;
displayName: string;
}[];
};
};
}
2023-03-18 16:17:33 +01:00
export type Info = Common & {
2021-03-06 22:41:36 +01:00
pageId: "info.ftl";
2021-03-06 14:42:56 +01:00
messageHeader?: string;
2023-03-19 14:48:01 +01:00
requiredActions?: ExtractAfterStartingWith<"requiredAction.", MessageKey>[];
2021-03-06 14:42:56 +01:00
skipLink: boolean;
pageRedirectUri?: string;
actionUri?: string;
client: {
baseUrl?: string;
};
2021-03-06 14:42:56 +01:00
};
2023-03-18 16:17:33 +01:00
export type Error = Common & {
2021-03-06 23:03:03 +01:00
pageId: "error.ftl";
client?: {
baseUrl?: string;
};
2023-03-18 16:17:33 +01:00
message: NonNullable<Common["message"]>;
2021-03-06 22:41:36 +01:00
};
2021-03-04 21:14:54 +01:00
2023-03-18 16:17:33 +01:00
export type LoginResetPassword = Common & {
2021-03-07 14:57:53 +01:00
pageId: "login-reset-password.ftl";
realm: {
loginWithEmailAllowed: boolean;
};
url: {
loginResetCredentialsUrl: string;
2023-03-27 21:03:11 +02:00
};
2021-03-07 14:57:53 +01:00
};
2023-03-18 16:17:33 +01:00
export type LoginVerifyEmail = Common & {
2021-03-07 15:37:37 +01:00
pageId: "login-verify-email.ftl";
2022-07-06 02:05:08 +02:00
//NOTE: Optional because maybe it wasn't defined in older keycloak versions.
user?: {
email: string;
};
2021-03-07 15:37:37 +01:00
};
2023-03-18 16:17:33 +01:00
export type Terms = Common & {
2021-04-08 15:41:40 +02:00
pageId: "terms.ftl";
};
2023-03-18 16:17:33 +01:00
export type LoginOtp = Common & {
2021-05-01 14:55:58 +02:00
pageId: "login-otp.ftl";
otpLogin: {
userOtpCredentials: { id: string; userLabel: string }[];
};
2021-05-01 14:55:58 +02:00
};
2023-03-18 16:17:33 +01:00
export type LoginUsername = Common & {
pageId: "login-username.ftl";
url: {
loginResetCredentialsUrl: string;
registrationUrl: string;
};
realm: {
loginWithEmailAllowed: boolean;
rememberMe: boolean;
password: boolean;
resetPasswordAllowed: boolean;
registrationAllowed: boolean;
};
registrationDisabled: boolean;
login: {
username?: string;
rememberMe?: string;
};
usernameHidden?: boolean;
social: {
displayInfo: boolean;
providers?: {
loginUrl: string;
alias: string;
providerId: string;
displayName: string;
}[];
};
};
2023-03-18 16:17:33 +01:00
export type LoginPassword = Common & {
2022-10-04 15:01:08 -04:00
pageId: "login-password.ftl";
url: {
loginResetCredentialsUrl: string;
registrationUrl: string;
};
realm: {
resetPasswordAllowed: boolean;
};
auth?: {
showUsername?: boolean;
showResetCredentials?: boolean;
showTryAnotherWayLink?: boolean;
attemptedUsername?: string;
};
social: {
displayInfo: boolean;
};
login: {
password?: string;
};
};
2023-03-18 16:17:33 +01:00
export type WebauthnAuthenticate = Common & {
pageId: "webauthn-authenticate.ftl";
authenticators: {
2023-03-17 20:40:29 +01:00
authenticators: WebauthnAuthenticate.WebauthnAuthenticator[];
};
challenge: string;
// I hate this:
userVerification: UserVerificationRequirement | "not specified";
rpId: string;
createTimeout: string;
isUserIdentified: "true" | "false";
shouldDisplayAuthenticators: boolean;
social: {
displayInfo: boolean;
};
login: {};
};
2023-03-17 20:40:29 +01:00
export namespace WebauthnAuthenticate {
export type WebauthnAuthenticator = {
credentialId: string;
transports: {
2023-03-18 06:14:05 +01:00
iconClass: string;
2023-03-19 14:48:01 +01:00
displayNameProperties: MessageKey[];
2023-03-17 20:40:29 +01:00
};
label: string;
createdAt: string;
};
}
2023-03-18 16:17:33 +01:00
export type LoginUpdatePassword = Common & {
2021-12-28 00:08:25 +03:00
pageId: "login-update-password.ftl";
username: string;
};
2023-03-18 16:17:33 +01:00
export type LoginUpdateProfile = Common & {
2021-06-14 19:06:31 +02:00
pageId: "login-update-profile.ftl";
user: {
editUsernameAllowed: boolean;
username?: string;
email?: string;
firstName?: string;
lastName?: string;
};
};
2023-03-18 16:17:33 +01:00
export type LoginIdpLinkConfirm = Common & {
2021-06-14 21:19:46 +02:00
pageId: "login-idp-link-confirm.ftl";
idpAlias: string;
};
2022-01-01 18:44:05 +02:00
2023-03-18 16:17:33 +01:00
export type LoginIdpLinkEmail = Common & {
2022-04-22 17:54:47 +03:00
pageId: "login-idp-link-email.ftl";
brokerContext: {
username: string;
};
2022-04-22 17:54:47 +03:00
idpAlias: string;
};
2023-03-18 16:17:33 +01:00
export type LoginPageExpired = Common & {
2022-01-01 18:44:05 +02:00
pageId: "login-page-expired.ftl";
};
2022-06-28 14:37:17 -04:00
2023-03-18 16:17:33 +01:00
export type LoginConfigTotp = Common & {
2022-06-28 14:37:17 -04:00
pageId: "login-config-totp.ftl";
mode?: "qr" | "manual" | undefined | null;
totp: {
totpSecretEncoded: string;
qrUrl: string;
policy: {
algorithm: "HmacSHA1" | "HmacSHA256" | "HmacSHA512";
digits: number;
lookAheadWindow: number;
} & (
| {
type: "totp";
period: number;
}
| {
type: "hotp";
initialCounter: number;
}
);
supportedApplications: string[];
2022-06-28 14:37:17 -04:00
totpSecretQrCode: string;
manualUrl: string;
totpSecret: string;
otpCredentials: { id: string; userLabel: string }[];
};
};
2022-06-17 00:47:55 +02:00
2023-03-18 16:17:33 +01:00
export type LogoutConfirm = Common & {
2022-06-17 00:47:55 +02:00
pageId: "logout-confirm.ftl";
url: {
logoutConfirmAction: string;
};
client: {
baseUrl?: string;
};
logoutConfirm: {
code: string;
skipLink?: boolean;
};
};
2022-09-09 02:07:29 +02:00
2023-03-18 16:17:33 +01:00
export type UpdateUserProfile = Common & {
2022-09-09 02:07:29 +02:00
pageId: "update-user-profile.ftl";
profile: {
attributes: Attribute[];
attributesByName: Record<string, Attribute>;
};
};
2022-09-09 12:55:57 +02:00
2023-03-18 16:17:33 +01:00
export type IdpReviewUserProfile = Common & {
2022-09-09 12:55:57 +02:00
pageId: "idp-review-user-profile.ftl";
profile: {
2022-09-09 14:37:27 +02:00
context: "IDP_REVIEW";
2022-09-09 12:55:57 +02:00
attributes: Attribute[];
attributesByName: Record<string, Attribute>;
};
};
2023-03-27 13:02:44 +03:00
export type UpdateEmail = Common & {
pageId: "update-email.ftl";
email: {
value?: string;
};
};
2023-03-31 17:38:22 +02:00
export type SelectAuthenticator = Common & {
pageId: "select-authenticator.ftl";
auth: {
authenticationSelections: SelectAuthenticator.AuthenticationSelection[];
};
};
export namespace SelectAuthenticator {
export type AuthenticationSelection = {
authExecId: string;
displayName:
| "otp-display-name"
| "password-display-name"
| "auth-username-form-display-name"
| "auth-username-password-form-display-name"
| "webauthn-display-name"
| "webauthn-passwordless-display-name";
helpText:
| "otp-help-text"
| "password-help-text"
| "auth-username-form-help-text"
| "auth-username-password-form-help-text"
| "webauthn-help-text"
| "webauthn-passwordless-help-text";
iconCssClass?:
| "kcAuthenticatorDefaultClass"
| "kcAuthenticatorPasswordClass"
| "kcAuthenticatorOTPClass"
| "kcAuthenticatorWebAuthnClass"
| "kcAuthenticatorWebAuthnPasswordlessClass";
};
}
2021-03-04 21:14:54 +01:00
}
2021-02-23 15:32:37 +01:00
2021-10-11 20:56:43 +02:00
export type Attribute = {
name: string;
displayName?: string;
required: boolean;
value?: string;
group?: string;
groupDisplayHeader?: string;
groupDisplayDescription?: string;
readOnly: boolean;
validators: Validators;
annotations: Record<string, string>;
groupAnnotations: Record<string, string>;
2021-11-01 22:28:53 +01:00
autocomplete?:
| "on"
| "off"
| "name"
| "honorific-prefix"
| "given-name"
| "additional-name"
| "family-name"
| "honorific-suffix"
| "nickname"
| "email"
| "username"
| "new-password"
| "current-password"
| "one-time-code"
| "organization-title"
| "organization"
| "street-address"
| "address-line1"
| "address-line2"
| "address-line3"
| "address-level4"
| "address-level3"
| "address-level2"
| "address-level1"
| "country"
| "country-name"
| "postal-code"
| "cc-name"
| "cc-given-name"
| "cc-additional-name"
| "cc-family-name"
| "cc-number"
| "cc-exp"
| "cc-exp-month"
| "cc-exp-year"
| "cc-csc"
| "cc-type"
| "transaction-currency"
| "transaction-amount"
| "language"
| "bday"
| "bday-day"
| "bday-month"
| "bday-year"
| "sex"
| "tel"
| "tel-country-code"
| "tel-national"
| "tel-area-code"
| "tel-local"
| "tel-extension"
| "impp"
| "url"
| "photo";
2021-10-11 20:56:43 +02:00
};
export type Validators = Partial<{
length: Validators.DoIgnoreEmpty & Validators.Range;
double: Validators.DoIgnoreEmpty & Validators.Range;
integer: Validators.DoIgnoreEmpty & Validators.Range;
email: Validators.DoIgnoreEmpty;
"up-immutable-attribute": {};
"up-attribute-required-by-metadata-value": {};
"up-username-has-value": {};
"up-duplicate-username": {};
"up-username-mutation": {};
"up-email-exists-as-username": {};
"up-blank-attribute-value": Validators.ErrorMessage & {
"fail-on-null": boolean;
2021-10-11 20:56:43 +02:00
};
"up-duplicate-email": {};
"local-date": Validators.DoIgnoreEmpty;
pattern: Validators.DoIgnoreEmpty & Validators.ErrorMessage & { pattern: string };
"person-name-prohibited-characters": Validators.DoIgnoreEmpty & Validators.ErrorMessage;
2021-10-11 20:56:43 +02:00
uri: Validators.DoIgnoreEmpty;
"username-prohibited-characters": Validators.DoIgnoreEmpty & Validators.ErrorMessage;
/** Made up validator that only exists in Keycloakify */
_compareToOther: Validators.DoIgnoreEmpty &
Validators.ErrorMessage & {
name: string;
shouldBe: "equal" | "different";
};
2022-03-18 00:46:12 +01:00
options: Validators.Options;
2021-10-11 20:56:43 +02:00
}>;
export declare namespace Validators {
export type DoIgnoreEmpty = {
"ignore.empty.value"?: boolean;
2021-10-11 20:56:43 +02:00
};
export type ErrorMessage = {
"error-message"?: string;
};
2021-10-11 20:56:43 +02:00
export type Range = {
/** "0", "1", "2"... yeah I know, don't tell me */
min?: `${number}`;
max?: `${number}`;
2021-10-11 20:56:43 +02:00
};
2022-03-18 00:46:12 +01:00
export type Options = {
options: string[];
};
2021-10-11 20:56:43 +02:00
}
2023-04-20 20:51:46 +02:00
{
type Got = KcContext["pageId"];
type Expected = LoginThemePageId;
type OnlyInGot = Exclude<Got, Expected>;
type OnlyInExpected = Exclude<Expected, Got>;
assert<Equals<OnlyInGot, never>>();
assert<Equals<OnlyInExpected, never>>();
}
2023-04-27 11:52:02 +02:00
assert<KcContext["themeType"] extends ThemeType ? true : false>();