@structured-id/ui-core 3.2.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@structured-id/ui-core",
3
- "version": "3.2.0",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -297,6 +297,92 @@ describe("SidRegistrationForm", () => {
297
297
  vi.useRealTimers();
298
298
  });
299
299
 
300
+ describe("password rules", () => {
301
+ // A q-input that shows the first rule its value fails, as QInput does.
302
+ const ruleStubs = {
303
+ ...quasarStubs,
304
+ "q-input": {
305
+ props: ["modelValue", "rules"],
306
+ emits: ["update:modelValue"],
307
+ computed: {
308
+ failed(): string {
309
+ const self = this as unknown as {
310
+ modelValue?: string;
311
+ rules?: ((v: string) => true | string)[];
312
+ };
313
+ for (const rule of self.rules ?? []) {
314
+ const result = rule(self.modelValue ?? "");
315
+ if (result !== true) return result;
316
+ }
317
+ return "";
318
+ },
319
+ },
320
+ template:
321
+ '<label><input :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)" /><span class="failed">{{ failed }}</span></label>',
322
+ },
323
+ };
324
+ const ceDefault = {
325
+ minLength: 8,
326
+ minUppercase: 1,
327
+ minLowercase: 1,
328
+ minDigit: 1,
329
+ minSymbol: 0,
330
+ };
331
+
332
+ async function passwordError(
333
+ value: string,
334
+ passwordPolicyFn: () => Promise<typeof ceDefault>,
335
+ ): Promise<string> {
336
+ const w = mount(SidRegistrationForm, {
337
+ props: { registerFn, passwordPolicyFn, showConfirmPassword: false },
338
+ global: { stubs: ruleStubs },
339
+ });
340
+ await flushPromises();
341
+ const password = w.find('[data-test="password"]');
342
+ await password.find("input").setValue(value);
343
+ return password.find(".failed").text();
344
+ }
345
+
346
+ // The rules are the server's policy: an 8-character password with an
347
+ // uppercase letter, a lowercase letter and a digit meets the CE default.
348
+ it("accepts a password that meets the server policy", async () => {
349
+ expect(
350
+ await passwordError("HrenVam1", () => Promise.resolve(ceDefault)),
351
+ ).toBe("");
352
+ });
353
+
354
+ // Each minimum names itself: a missing uppercase letter is not reported
355
+ // as a short password.
356
+ it("names the rule a password fails", async () => {
357
+ const policy = () => Promise.resolve(ceDefault);
358
+ expect(await passwordError("hrenvam1", policy)).toBe(
359
+ "At least 1 uppercase letter",
360
+ );
361
+ expect(await passwordError("HrenVamX", policy)).toBe("At least 1 digit");
362
+ expect(await passwordError("HrenVa1", policy)).toBe(
363
+ "At least 8 characters",
364
+ );
365
+ });
366
+
367
+ // Symbols are the printable ASCII characters that are not letters or
368
+ // digits, as the proof counts them; a space is not one.
369
+ it("checks the symbol minimum", async () => {
370
+ const policy = () => Promise.resolve({ ...ceDefault, minSymbol: 1 });
371
+ expect(await passwordError("Hren Vam1", policy)).toBe(
372
+ "At least 1 symbol",
373
+ );
374
+ expect(await passwordError("Hren-Vam1", policy)).toBe("");
375
+ });
376
+
377
+ // Until the server answers only an empty password is refused: the
378
+ // proof, not the form, enforces the policy.
379
+ it("checks only presence until the policy is known", async () => {
380
+ const never = () => new Promise<typeof ceDefault>(() => {});
381
+ expect(await passwordError("a", never)).toBe("");
382
+ expect(await passwordError("", never)).toBe("This field is required");
383
+ });
384
+ });
385
+
300
386
  it("renders footer slot", () => {
301
387
  const w = mount(SidRegistrationForm, {
302
388
  props: { registerFn },
@@ -217,6 +217,14 @@ import { ref, computed, watch, useSlots } from "vue";
217
217
  import { SidPasswordCeremony, SidPrincipalInput } from "../../quasar";
218
218
  import type { CeremonyOutcome } from "../../quasar";
219
219
  import type { PasswordOperationProgress } from "../composables/useAuth";
220
+ import { getPasswordPolicy } from "../composables/useAuthApi";
221
+ import {
222
+ defaultPasswordRuleText,
223
+ failedPasswordRule,
224
+ usePasswordPolicy,
225
+ type PasswordPolicy,
226
+ type PasswordRuleText,
227
+ } from "../composables/usePasswordPolicy";
220
228
 
221
229
  // Minimal QInput shape — we only use validate(). Avoids importing 'quasar' at
222
230
  // type level (it's a peer dep, not present during ui-core typecheck).
@@ -254,8 +262,10 @@ const props = withDefaults(
254
262
  confirmPasswordLabel?: string;
255
263
  /** Password required validation text */
256
264
  passwordRequiredText?: string;
257
- /** Password min length validation text */
258
- passwordMinLengthText?: string;
265
+ /** What a password that fails a policy minimum is told. */
266
+ passwordRuleText?: PasswordRuleText;
267
+ /** Loads the server's password policy. */
268
+ passwordPolicyFn?: () => Promise<PasswordPolicy>;
259
269
  /** Passwords mismatch validation text */
260
270
  passwordsMismatchText?: string;
261
271
  /** Invite code input label */
@@ -303,8 +313,6 @@ const props = withDefaults(
303
313
  ) => Promise<void>;
304
314
  /** What the ceremony says once the account is created. */
305
315
  acceptedText?: string;
306
- /** Min password length (default 12 — NIST SP 800-63B 4th draft baseline). */
307
- passwordMinLength?: number;
308
316
  /** Show confirm-password field with match validation. Default true. */
309
317
  showConfirmPassword?: boolean;
310
318
  /** Min username length. Registration default 6 (premium feature gating);
@@ -319,8 +327,8 @@ const props = withDefaults(
319
327
  passwordLabel: "Password",
320
328
  confirmPasswordLabel: "Confirm password",
321
329
  passwordRequiredText: REQUIRED_FIELD_TEXT,
322
- passwordMinLengthText: "At least 12 characters with mixed case + digit",
323
- passwordMinLength: 12,
330
+ passwordRuleText: defaultPasswordRuleText,
331
+ passwordPolicyFn: getPasswordPolicy,
324
332
  passwordsMismatchText: "Passwords do not match",
325
333
  inviteCodeLabel: "Invite code",
326
334
  inviteCodeRequiredText: "Invite code required",
@@ -380,16 +388,14 @@ let announce: (() => void) | undefined;
380
388
  const passwordRef = ref<ValidatableInput | null>(null);
381
389
  const confirmRef = ref<ValidatableInput | null>(null);
382
390
 
383
- // Password complexity (matches sid-pake-core ZKPP gadget A defaults).
384
- // Server enforces via OPAQUE-ZKPP zero-knowledge proof; client also checks
385
- // to give immediate UX feedback.
391
+ const { policy } = usePasswordPolicy(props.passwordPolicyFn);
392
+
386
393
  const passwordRules = computed(() => [
387
394
  (v: string) => !!v || props.passwordRequiredText,
388
- (v: string) =>
389
- v.length >= props.passwordMinLength || props.passwordMinLengthText,
390
- (v: string) => /[a-z]/.test(v) || props.passwordMinLengthText,
391
- (v: string) => /[A-Z]/.test(v) || props.passwordMinLengthText,
392
- (v: string) => /[0-9]/.test(v) || props.passwordMinLengthText,
395
+ (v: string) => {
396
+ const failed = policy.value && failedPasswordRule(v, policy.value);
397
+ return !failed || props.passwordRuleText(failed.rule, failed.count);
398
+ },
393
399
  ]);
394
400
 
395
401
  // Re-validate confirm field whenever password changes (eager match feedback).
@@ -6,3 +6,4 @@ export * from "./useEnrollmentApi";
6
6
  export * from "./useIdentityApi";
7
7
  export * from "./useSecurityLevel";
8
8
  export * from "./useCurrentPasswordRequirement";
9
+ export * from "./usePasswordPolicy";
@@ -57,6 +57,7 @@ import {
57
57
  type OpaqueZkppRegistrationFinishResponse,
58
58
  type PasswordChangeChallengeResponse,
59
59
  type PasswordChangeExecuteResponse,
60
+ type GetZkppPolicyResponse,
60
61
  } from "@structured-id/proto/sid/v1/authn/auth";
61
62
  import type {
62
63
  PasswordHistoryContext,
@@ -574,6 +575,12 @@ export async function completePasswordReset(
574
575
  return response;
575
576
  }
576
577
 
578
+ /** The password policy the server's proof checks a new password against. */
579
+ export async function getPasswordPolicy(): Promise<GetZkppPolicyResponse> {
580
+ const { response } = await client().getZkppPolicy({});
581
+ return response;
582
+ }
583
+
577
584
  // Recovery shard RPCs (downloadRecoveryShard, uploadRecoveryShard, getRecoveryInfo)
578
585
  // are EE/SaaS-only — defined in proto-ee, not in CE proto.
579
586
 
@@ -0,0 +1,109 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { flushPromises } from "@vue/test-utils";
3
+ import {
4
+ defaultPasswordRuleText,
5
+ failedPasswordRule,
6
+ usePasswordPolicy,
7
+ } from "./usePasswordPolicy";
8
+
9
+ const ceDefault = {
10
+ minLength: 8,
11
+ minUppercase: 1,
12
+ minLowercase: 1,
13
+ minDigit: 1,
14
+ minSymbol: 0,
15
+ };
16
+
17
+ describe("failedPasswordRule", () => {
18
+ it("passes a password that meets every minimum", () => {
19
+ expect(failedPasswordRule("HrenVam1", ceDefault)).toBeNull();
20
+ });
21
+
22
+ // The first failed minimum is reported, in a fixed order, with the
23
+ // policy's number for it.
24
+ it("reports the first failed minimum with its number", () => {
25
+ expect(failedPasswordRule("hV1", ceDefault)).toEqual({
26
+ rule: "length",
27
+ count: 8,
28
+ });
29
+ expect(failedPasswordRule("hrenvam1", ceDefault)).toEqual({
30
+ rule: "uppercase",
31
+ count: 1,
32
+ });
33
+ expect(failedPasswordRule("HRENVAM1", ceDefault)).toEqual({
34
+ rule: "lowercase",
35
+ count: 1,
36
+ });
37
+ expect(failedPasswordRule("HrenVamX", ceDefault)).toEqual({
38
+ rule: "digit",
39
+ count: 1,
40
+ });
41
+ });
42
+
43
+ // Counts, not presence: a minimum of 2 needs two of the class.
44
+ it("counts the characters of a class", () => {
45
+ const policy = { ...ceDefault, minDigit: 2 };
46
+ expect(failedPasswordRule("HrenVam1", policy)?.rule).toBe("digit");
47
+ expect(failedPasswordRule("HrenVam12", policy)).toBeNull();
48
+ });
49
+
50
+ // The proof's classes are ASCII: a non-ASCII letter is none of them, a
51
+ // space is not a symbol, and every other printable ASCII non-alphanumeric
52
+ // character is.
53
+ it("classifies characters as the proof does", () => {
54
+ const policy = {
55
+ minLength: 0,
56
+ minUppercase: 0,
57
+ minLowercase: 0,
58
+ minDigit: 0,
59
+ minSymbol: 1,
60
+ };
61
+ expect(failedPasswordRule(" ", policy)?.rule).toBe("symbol");
62
+ expect(failedPasswordRule("Ж", policy)?.rule).toBe("symbol");
63
+ for (const symbol of "!/:@[`{~") {
64
+ expect(failedPasswordRule(symbol, policy)).toBeNull();
65
+ }
66
+ expect(
67
+ failedPasswordRule("ЖЖЖЖЖЖЖЖ", { ...ceDefault, minLength: 8 })?.rule,
68
+ ).toBe("uppercase");
69
+ });
70
+
71
+ // Length counts characters, so a character outside the BMP is one.
72
+ it("counts length in characters", () => {
73
+ const policy = { ...ceDefault, minUppercase: 0, minDigit: 0, minLength: 3 };
74
+ expect(failedPasswordRule("a😀b", policy)).toBeNull();
75
+ expect(failedPasswordRule("a😀", policy)?.rule).toBe("length");
76
+ });
77
+ });
78
+
79
+ describe("defaultPasswordRuleText", () => {
80
+ it("names the rule and its number", () => {
81
+ expect(defaultPasswordRuleText("length", 8)).toBe("At least 8 characters");
82
+ expect(defaultPasswordRuleText("uppercase", 1)).toBe(
83
+ "At least 1 uppercase letter",
84
+ );
85
+ expect(defaultPasswordRuleText("lowercase", 2)).toBe(
86
+ "At least 2 lowercase letters",
87
+ );
88
+ expect(defaultPasswordRuleText("digit", 2)).toBe("At least 2 digits");
89
+ expect(defaultPasswordRuleText("symbol", 1)).toBe("At least 1 symbol");
90
+ });
91
+ });
92
+
93
+ describe("usePasswordPolicy", () => {
94
+ it("holds the policy once loaded", async () => {
95
+ const { policy } = usePasswordPolicy(() => Promise.resolve(ceDefault));
96
+ expect(policy.value).toBeNull();
97
+ await flushPromises();
98
+ expect(policy.value).toEqual(ceDefault);
99
+ });
100
+
101
+ // A policy that cannot be loaded leaves the checks to the server.
102
+ it("stays unknown when the policy cannot be loaded", async () => {
103
+ const { policy } = usePasswordPolicy(() =>
104
+ Promise.reject(new Error("unavailable")),
105
+ );
106
+ await flushPromises();
107
+ expect(policy.value).toBeNull();
108
+ });
109
+ });
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The server's password policy, as a form checks a new password against it.
3
+ * The proof enforces the policy; these checks only tell the user early.
4
+ */
5
+ import { readonly, ref } from "vue";
6
+ import type { GetZkppPolicyResponse } from "@structured-id/proto/sid/v1/authn/auth";
7
+ import { getPasswordPolicy } from "./useAuthApi";
8
+
9
+ /** The minimums of a password policy. */
10
+ export type PasswordPolicy = Pick<
11
+ GetZkppPolicyResponse,
12
+ "minLength" | "minUppercase" | "minLowercase" | "minDigit" | "minSymbol"
13
+ >;
14
+
15
+ /** A minimum a password can fail. */
16
+ export type PasswordRule =
17
+ "length" | "uppercase" | "lowercase" | "digit" | "symbol";
18
+
19
+ /** What a failed rule says, given the policy's number for it. */
20
+ export type PasswordRuleText = (rule: PasswordRule, count: number) => string;
21
+
22
+ export const defaultPasswordRuleText: PasswordRuleText = (rule, count) => {
23
+ switch (rule) {
24
+ case "length":
25
+ return `At least ${count} characters`;
26
+ case "uppercase":
27
+ return `At least ${count} uppercase ${count === 1 ? "letter" : "letters"}`;
28
+ case "lowercase":
29
+ return `At least ${count} lowercase ${count === 1 ? "letter" : "letters"}`;
30
+ case "digit":
31
+ return `At least ${count} ${count === 1 ? "digit" : "digits"}`;
32
+ case "symbol":
33
+ return `At least ${count} ${count === 1 ? "symbol" : "symbols"}`;
34
+ }
35
+ };
36
+
37
+ // The character classes the proof counts: ASCII letters and digits, and the
38
+ // printable ASCII characters that are neither (a space is not a symbol).
39
+ const UPPERCASE = /[A-Z]/g;
40
+ const LOWERCASE = /[a-z]/g;
41
+ const DIGIT = /[0-9]/g;
42
+ const SYMBOL = /[!-/:-@[-`{-~]/g;
43
+
44
+ const count = (value: string, pattern: RegExp) =>
45
+ value.match(pattern)?.length ?? 0;
46
+
47
+ /**
48
+ * The first minimum `password` fails, or null when it meets them all. Length
49
+ * is counted in characters: the proof counts UTF-8 bytes, never fewer, so a
50
+ * password that passes here is never refused there for its length.
51
+ */
52
+ export function failedPasswordRule(
53
+ password: string,
54
+ policy: PasswordPolicy,
55
+ ): { rule: PasswordRule; count: number } | null {
56
+ const checks: [PasswordRule, number, number][] = [
57
+ ["length", [...password].length, policy.minLength],
58
+ ["uppercase", count(password, UPPERCASE), policy.minUppercase],
59
+ ["lowercase", count(password, LOWERCASE), policy.minLowercase],
60
+ ["digit", count(password, DIGIT), policy.minDigit],
61
+ ["symbol", count(password, SYMBOL), policy.minSymbol],
62
+ ];
63
+ for (const [rule, has, needs] of checks) {
64
+ if (has < needs) return { rule, count: needs };
65
+ }
66
+ return null;
67
+ }
68
+
69
+ /**
70
+ * Load the policy once. Until it is known (or when it cannot be loaded)
71
+ * `policy` stays null: the operation itself then meets the server, which
72
+ * refuses a password its policy does not admit.
73
+ */
74
+ export function usePasswordPolicy(
75
+ load: () => Promise<PasswordPolicy> = getPasswordPolicy,
76
+ ) {
77
+ const policy = ref<PasswordPolicy | null>(null);
78
+ load().then(
79
+ (p) => (policy.value = p),
80
+ () => undefined,
81
+ );
82
+ return { policy: readonly(policy) };
83
+ }