keycloak_theme/src/lib/getKcContext/KcContextBase.ts

337 lines
9.4 KiB
TypeScript
Raw Normal View History

2021-06-23 08:16:51 +02:00
import type { PageId } from "../../bin/build-keycloak-theme/generateFtl";
import type { KcLanguageTag } from "../i18n/KcLanguageTag";
2021-10-11 20:56:43 +02:00
import { assert } from "tsafe/assert";
import type { Equals } from "tsafe";
2021-06-23 08:16:51 +02:00
import type { MessageKey } from "../i18n/useKcMessage";
import type { LanguageLabel } from "../i18n/KcLanguageTag";
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)
*/
2021-06-23 08:16:51 +02:00
export type KcContextBase =
| KcContextBase.Login
| KcContextBase.Register
| KcContextBase.RegisterUserProfile
| KcContextBase.Info
| KcContextBase.Error
| KcContextBase.LoginResetPassword
| KcContextBase.LoginVerifyEmail
| KcContextBase.Terms
| KcContextBase.LoginOtp
2021-12-28 00:08:25 +03:00
| KcContextBase.LoginUpdatePassword
| KcContextBase.LoginUpdateProfile
2022-01-01 18:44:05 +02:00
| KcContextBase.LoginIdpLinkConfirm
| KcContextBase.LoginPageExpired;
2021-06-23 08:16:51 +02:00
export declare namespace KcContextBase {
export type Common = {
2021-03-04 21:14:54 +01:00
url: {
loginAction: string;
resourcesPath: string;
resourcesCommonPath: string;
loginRestartFlowUrl: string;
2021-03-02 22:48:36 +01:00
loginUrl: string;
2021-03-04 21:14:54 +01:00
};
realm: {
name: string;
2021-03-04 18:15:48 +01:00
displayName?: string;
2021-03-04 21:14:54 +01:00
displayNameHtml?: string;
internationalizationEnabled: boolean;
registrationEmailAsUsername: boolean;
2021-03-04 21:14:54 +01:00
};
/** Undefined if !realm.internationalizationEnabled */
locale?: {
supported: {
url: string;
2021-03-04 21:14:54 +01:00
languageTag: KcLanguageTag;
/** Is determined by languageTag. Ex: languageTag === "en" => label === "English"
* or getLanguageLabel(languageTag) === label
*/
//label: LanguageLabel;
}[];
current: LanguageLabel;
};
2021-03-04 21:14:54 +01:00
auth?: {
showUsername: boolean;
showResetCredentials: boolean;
showTryAnotherWayLink: boolean;
2021-03-07 14:57:53 +01:00
attemptedUsername?: string;
2021-03-04 21:14:54 +01:00
};
scripts: string[];
message?: {
type: "success" | "warning" | "error" | "info";
summary: string;
};
client: {
clientId: string;
name?: string;
description?: string;
};
2021-03-04 21:14:54 +01:00
isAppInitiatedAction: boolean;
2021-10-11 03:25:02 +02:00
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
};
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;
2021-09-17 22:57:38 +02:00
rememberMe?: boolean;
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
};
};
2021-10-11 03:25:02 +02:00
export type RegisterCommon = Common & {
2021-03-04 21:14:54 +01:00
url: {
registrationAction: string;
};
passwordRequired: boolean;
recaptchaRequired: boolean;
2021-03-05 14:50:46 +01:00
recaptchaSiteKey?: string;
2021-06-15 17:32:03 +02:00
social: {
displayInfo: boolean;
providers?: {
loginUrl: string;
alias: string;
providerId: string;
displayName: string;
}[];
2021-06-15 17:32:03 +02:00
};
2021-03-04 18:15:48 +01:00
};
2021-03-04 21:14:54 +01:00
2021-10-11 03:25:02 +02:00
export type Register = RegisterCommon & {
pageId: "register.ftl";
register: {
formData: {
firstName?: string;
displayName?: string;
lastName?: string;
email?: string;
username?: string;
};
2021-10-11 03:25:02 +02:00
};
};
export type RegisterUserProfile = RegisterCommon & {
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
};
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;
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
};
export type Error = Common & {
2021-03-06 23:03:03 +01:00
pageId: "error.ftl";
client?: {
baseUrl?: string;
};
message: NonNullable<Common["message"]>;
2021-03-06 22:41:36 +01:00
};
2021-03-04 21:14:54 +01:00
export type LoginResetPassword = Common & {
2021-03-07 14:57:53 +01:00
pageId: "login-reset-password.ftl";
realm: {
loginWithEmailAllowed: boolean;
};
2021-03-07 14:57:53 +01:00
};
export type LoginVerifyEmail = Common & {
2021-03-07 15:37:37 +01:00
pageId: "login-verify-email.ftl";
};
2021-04-08 15:41:40 +02:00
export type Terms = Common & {
pageId: "terms.ftl";
};
2021-05-01 14:55:58 +02:00
export type LoginOtp = Common & {
pageId: "login-otp.ftl";
otpLogin: {
userOtpCredentials: { id: string; userLabel: string }[];
};
2021-05-01 14:55:58 +02:00
};
2021-12-28 00:08:25 +03:00
export type LoginUpdatePassword = Common & {
pageId: "login-update-password.ftl";
username: string;
};
2021-06-14 19:06:31 +02:00
export type LoginUpdateProfile = Common & {
pageId: "login-update-profile.ftl";
user: {
editUsernameAllowed: boolean;
username?: string;
email?: string;
firstName?: string;
lastName?: string;
};
};
2021-06-14 21:19:46 +02:00
export type LoginIdpLinkConfirm = Common & {
pageId: "login-idp-link-confirm.ftl";
idpAlias: string;
};
2022-01-01 18:44:05 +02:00
export type LoginPageExpired = Common & {
pageId: "login-page-expired.ftl";
};
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";
};
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
};
}
2021-10-11 03:41:05 +02:00
assert<Equals<KcContextBase["pageId"], PageId>>();