bwh-auth 0.2.1 → 0.3.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/README.md CHANGED
@@ -23,6 +23,27 @@ The companion Laravel API package is
23
23
  - `PasskeyLoginButton`
24
24
  - `PasskeySection`
25
25
 
26
+ ## Relying-party helpers
27
+
28
+ `relyingApplicationsFrom(value)` validates the sibling-application list an identity provider
29
+ reports into one that is safe to render, and `safeApplicationHref(url)` is the scheme check
30
+ behind it. Neither touches the DOM, so an app can feed them a `#app-initial-data` payload, an
31
+ Inertia shared prop, or anything else it already has.
32
+
33
+ ```ts
34
+ import { relyingApplicationsFrom } from 'bwh-auth';
35
+
36
+ const applications = relyingApplicationsFrom(initialData.applications);
37
+ ```
38
+
39
+ Rendering these entries means putting provider-supplied text into an `href`. `javascript:`
40
+ and `data:` URLs pass a server-side URL-validity check but execute rather than navigate, so
41
+ the scheme is parsed rather than prefix-matched — a `startsWith` test can be walked past with
42
+ leading control characters or unexpected case — and the **parsed** form is returned, so what
43
+ is rendered is exactly what was validated. Unrecognised entries are dropped rather than
44
+ throwing: this is navigation chrome, and one malformed entry must not take down the page it
45
+ appears on.
46
+
26
47
  ## Install
27
48
 
28
49
  Install the published package from npm:
package/dist/index.cjs CHANGED
@@ -45,7 +45,9 @@ __export(index_exports, {
45
45
  getDefaultPasskeyName: () => getDefaultPasskeyName,
46
46
  isAbortError: () => isAbortError,
47
47
  isConditionalMediationAvailable: () => isConditionalMediationAvailable,
48
- registerPasskey: () => registerPasskey
48
+ registerPasskey: () => registerPasskey,
49
+ relyingApplicationsFrom: () => relyingApplicationsFrom,
50
+ safeApplicationHref: () => safeApplicationHref
49
51
  });
50
52
  module.exports = __toCommonJS(index_exports);
51
53
 
@@ -831,6 +833,35 @@ function PasskeySection({ endpoints = {}, components, onSuccess, onError }) {
831
833
  ] })
832
834
  ] });
833
835
  }
836
+
837
+ // src/relying-applications.ts
838
+ function safeApplicationHref(url) {
839
+ try {
840
+ const parsed = new URL(url);
841
+ return parsed.protocol === "https:" || parsed.protocol === "http:" ? parsed.toString() : null;
842
+ } catch {
843
+ return null;
844
+ }
845
+ }
846
+ function relyingApplicationsFrom(value) {
847
+ if (!Array.isArray(value)) {
848
+ return [];
849
+ }
850
+ return value.flatMap((entry) => {
851
+ if (typeof entry !== "object" || entry === null) {
852
+ return [];
853
+ }
854
+ const { key, name, url } = entry;
855
+ if (typeof key !== "string" || typeof name !== "string" || typeof url !== "string") {
856
+ return [];
857
+ }
858
+ if (key === "" || name.trim() === "") {
859
+ return [];
860
+ }
861
+ const href = safeApplicationHref(url);
862
+ return href === null ? [] : [{ key, name: name.trim(), url: href }];
863
+ });
864
+ }
834
865
  // Annotate the CommonJS export names for ESM import in node:
835
866
  0 && (module.exports = {
836
867
  ChangePasswordForm,
@@ -848,5 +879,7 @@ function PasskeySection({ endpoints = {}, components, onSuccess, onError }) {
848
879
  getDefaultPasskeyName,
849
880
  isAbortError,
850
881
  isConditionalMediationAvailable,
851
- registerPasskey
882
+ registerPasskey,
883
+ relyingApplicationsFrom,
884
+ safeApplicationHref
852
885
  });
package/dist/index.d.cts CHANGED
@@ -137,6 +137,44 @@ interface PasskeySectionProps {
137
137
  }
138
138
  declare function PasskeySection({ endpoints, components, onSuccess, onError }: PasskeySectionProps): react_jsx_runtime.JSX.Element;
139
139
 
140
+ /**
141
+ * The sibling applications an identity provider reports for the signed-in person.
142
+ *
143
+ * Every relying party receives this list the same way — server-side, injected per request
144
+ * from the session rather than compiled into a bundle — but each one carries it into the
145
+ * page differently: a `#app-initial-data` script tag, an Inertia shared prop, a Blade loop.
146
+ * What they cannot afford to do differently is decide which entries are safe to render, so
147
+ * that decision lives here and the transport stays the application's business.
148
+ */
149
+ interface RelyingApplication {
150
+ key: string;
151
+ name: string;
152
+ url: string;
153
+ }
154
+ /**
155
+ * Reduce a URL from the wire to one that is safe to put in an `href`, or reject it.
156
+ *
157
+ * These arrive as text in the page and end up as a link the browser will follow, so the
158
+ * scheme is what matters: `javascript:` and `data:` both pass a server-side URL validity
159
+ * check but execute rather than navigate. Parsing and allowing only http(s) is stronger
160
+ * than matching a prefix — it normalises away the leading control characters, mixed case,
161
+ * and escapes that a hand-rolled test can be walked past — and it returns the *parsed*
162
+ * form, so what gets rendered is exactly what was validated.
163
+ *
164
+ * Absolute URLs only, deliberately. These point at other origins; a relative one means the
165
+ * provider told us something unexpected, and guessing a base for it would be inventing a
166
+ * destination rather than validating one.
167
+ */
168
+ declare function safeApplicationHref(url: string): string | null;
169
+ /**
170
+ * Validate whatever the page handed over into a list that is safe to render.
171
+ *
172
+ * Unrecognised entries are dropped rather than throwing. This is navigation chrome: one
173
+ * malformed entry, or a provider that has grown a field, must not be able to take down the
174
+ * page it appears on. Anything that is not a well-formed list degrades to an empty menu.
175
+ */
176
+ declare function relyingApplicationsFrom(value: unknown): RelyingApplication[];
177
+
140
178
  declare function getCsrfToken(explicitToken?: string): string;
141
179
  declare function base64urlToArrayBuffer(b64: string): ArrayBuffer;
142
180
  declare function arrayBufferToBase64url(buffer: ArrayBuffer): string;
@@ -163,4 +201,4 @@ declare function isConditionalMediationAvailable(): Promise<boolean>;
163
201
  declare function authenticateWithPasskey({ endpoints, mediation, signal }?: AuthenticateWithPasskeyOptions): Promise<PasskeyAuthenticationResult>;
164
202
  declare function registerPasskey({ endpoints, name, signal }?: RegisterPasskeyOptions): Promise<PasskeyRegistrationResult>;
165
203
 
166
- export { type AuthButtonComponent, type AuthButtonComponentInput, type AuthButtonComponentProps, type AuthButtonSize, type AuthButtonVariant, type AuthComponentInput, type AuthComponentOverrides, type AuthComponentSet, type AuthComponentSuperset, type AuthComponents, type AuthContainerComponentProps, type AuthEndpointConfig, type AuthInputComponentProps, type AuthJsonResponse, type AuthLabelComponentProps, type AuthSignupField, type AuthSignupValues, type AuthValidationErrors, ChangePasswordForm, LoginForm, type Passkey, PasskeyLoginButton, PasskeySection, PasswordResetRequestForm, ResetPasswordForm, SignupForm, TwoFactorForm, arrayBufferToBase64url, authenticateWithPasskey, base64urlToArrayBuffer, getCsrfToken, getDefaultPasskeyName, isAbortError, isConditionalMediationAvailable, registerPasskey };
204
+ export { type AuthButtonComponent, type AuthButtonComponentInput, type AuthButtonComponentProps, type AuthButtonSize, type AuthButtonVariant, type AuthComponentInput, type AuthComponentOverrides, type AuthComponentSet, type AuthComponentSuperset, type AuthComponents, type AuthContainerComponentProps, type AuthEndpointConfig, type AuthInputComponentProps, type AuthJsonResponse, type AuthLabelComponentProps, type AuthSignupField, type AuthSignupValues, type AuthValidationErrors, ChangePasswordForm, LoginForm, type Passkey, PasskeyLoginButton, PasskeySection, PasswordResetRequestForm, type RelyingApplication, ResetPasswordForm, SignupForm, TwoFactorForm, arrayBufferToBase64url, authenticateWithPasskey, base64urlToArrayBuffer, getCsrfToken, getDefaultPasskeyName, isAbortError, isConditionalMediationAvailable, registerPasskey, relyingApplicationsFrom, safeApplicationHref };
package/dist/index.d.ts CHANGED
@@ -137,6 +137,44 @@ interface PasskeySectionProps {
137
137
  }
138
138
  declare function PasskeySection({ endpoints, components, onSuccess, onError }: PasskeySectionProps): react_jsx_runtime.JSX.Element;
139
139
 
140
+ /**
141
+ * The sibling applications an identity provider reports for the signed-in person.
142
+ *
143
+ * Every relying party receives this list the same way — server-side, injected per request
144
+ * from the session rather than compiled into a bundle — but each one carries it into the
145
+ * page differently: a `#app-initial-data` script tag, an Inertia shared prop, a Blade loop.
146
+ * What they cannot afford to do differently is decide which entries are safe to render, so
147
+ * that decision lives here and the transport stays the application's business.
148
+ */
149
+ interface RelyingApplication {
150
+ key: string;
151
+ name: string;
152
+ url: string;
153
+ }
154
+ /**
155
+ * Reduce a URL from the wire to one that is safe to put in an `href`, or reject it.
156
+ *
157
+ * These arrive as text in the page and end up as a link the browser will follow, so the
158
+ * scheme is what matters: `javascript:` and `data:` both pass a server-side URL validity
159
+ * check but execute rather than navigate. Parsing and allowing only http(s) is stronger
160
+ * than matching a prefix — it normalises away the leading control characters, mixed case,
161
+ * and escapes that a hand-rolled test can be walked past — and it returns the *parsed*
162
+ * form, so what gets rendered is exactly what was validated.
163
+ *
164
+ * Absolute URLs only, deliberately. These point at other origins; a relative one means the
165
+ * provider told us something unexpected, and guessing a base for it would be inventing a
166
+ * destination rather than validating one.
167
+ */
168
+ declare function safeApplicationHref(url: string): string | null;
169
+ /**
170
+ * Validate whatever the page handed over into a list that is safe to render.
171
+ *
172
+ * Unrecognised entries are dropped rather than throwing. This is navigation chrome: one
173
+ * malformed entry, or a provider that has grown a field, must not be able to take down the
174
+ * page it appears on. Anything that is not a well-formed list degrades to an empty menu.
175
+ */
176
+ declare function relyingApplicationsFrom(value: unknown): RelyingApplication[];
177
+
140
178
  declare function getCsrfToken(explicitToken?: string): string;
141
179
  declare function base64urlToArrayBuffer(b64: string): ArrayBuffer;
142
180
  declare function arrayBufferToBase64url(buffer: ArrayBuffer): string;
@@ -163,4 +201,4 @@ declare function isConditionalMediationAvailable(): Promise<boolean>;
163
201
  declare function authenticateWithPasskey({ endpoints, mediation, signal }?: AuthenticateWithPasskeyOptions): Promise<PasskeyAuthenticationResult>;
164
202
  declare function registerPasskey({ endpoints, name, signal }?: RegisterPasskeyOptions): Promise<PasskeyRegistrationResult>;
165
203
 
166
- export { type AuthButtonComponent, type AuthButtonComponentInput, type AuthButtonComponentProps, type AuthButtonSize, type AuthButtonVariant, type AuthComponentInput, type AuthComponentOverrides, type AuthComponentSet, type AuthComponentSuperset, type AuthComponents, type AuthContainerComponentProps, type AuthEndpointConfig, type AuthInputComponentProps, type AuthJsonResponse, type AuthLabelComponentProps, type AuthSignupField, type AuthSignupValues, type AuthValidationErrors, ChangePasswordForm, LoginForm, type Passkey, PasskeyLoginButton, PasskeySection, PasswordResetRequestForm, ResetPasswordForm, SignupForm, TwoFactorForm, arrayBufferToBase64url, authenticateWithPasskey, base64urlToArrayBuffer, getCsrfToken, getDefaultPasskeyName, isAbortError, isConditionalMediationAvailable, registerPasskey };
204
+ export { type AuthButtonComponent, type AuthButtonComponentInput, type AuthButtonComponentProps, type AuthButtonSize, type AuthButtonVariant, type AuthComponentInput, type AuthComponentOverrides, type AuthComponentSet, type AuthComponentSuperset, type AuthComponents, type AuthContainerComponentProps, type AuthEndpointConfig, type AuthInputComponentProps, type AuthJsonResponse, type AuthLabelComponentProps, type AuthSignupField, type AuthSignupValues, type AuthValidationErrors, ChangePasswordForm, LoginForm, type Passkey, PasskeyLoginButton, PasskeySection, PasswordResetRequestForm, type RelyingApplication, ResetPasswordForm, SignupForm, TwoFactorForm, arrayBufferToBase64url, authenticateWithPasskey, base64urlToArrayBuffer, getCsrfToken, getDefaultPasskeyName, isAbortError, isConditionalMediationAvailable, registerPasskey, relyingApplicationsFrom, safeApplicationHref };
package/dist/index.js CHANGED
@@ -780,6 +780,35 @@ function PasskeySection({ endpoints = {}, components, onSuccess, onError }) {
780
780
  ] })
781
781
  ] });
782
782
  }
783
+
784
+ // src/relying-applications.ts
785
+ function safeApplicationHref(url) {
786
+ try {
787
+ const parsed = new URL(url);
788
+ return parsed.protocol === "https:" || parsed.protocol === "http:" ? parsed.toString() : null;
789
+ } catch {
790
+ return null;
791
+ }
792
+ }
793
+ function relyingApplicationsFrom(value) {
794
+ if (!Array.isArray(value)) {
795
+ return [];
796
+ }
797
+ return value.flatMap((entry) => {
798
+ if (typeof entry !== "object" || entry === null) {
799
+ return [];
800
+ }
801
+ const { key, name, url } = entry;
802
+ if (typeof key !== "string" || typeof name !== "string" || typeof url !== "string") {
803
+ return [];
804
+ }
805
+ if (key === "" || name.trim() === "") {
806
+ return [];
807
+ }
808
+ const href = safeApplicationHref(url);
809
+ return href === null ? [] : [{ key, name: name.trim(), url: href }];
810
+ });
811
+ }
783
812
  export {
784
813
  ChangePasswordForm,
785
814
  LoginForm,
@@ -796,5 +825,7 @@ export {
796
825
  getDefaultPasskeyName,
797
826
  isAbortError,
798
827
  isConditionalMediationAvailable,
799
- registerPasskey
828
+ registerPasskey,
829
+ relyingApplicationsFrom,
830
+ safeApplicationHref
800
831
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bwh-auth",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Shared React auth UI and browser WebAuthn helpers for BWH applications.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -30,6 +30,7 @@
30
30
  "scripts": {
31
31
  "build": "tsup src/index.ts --format esm,cjs --dts --dts-resolve",
32
32
  "release": "node scripts/release.mjs",
33
+ "test": "vitest run",
33
34
  "typecheck": "tsc --noEmit",
34
35
  "prepare": "pnpm run build"
35
36
  },
@@ -39,6 +40,7 @@
39
40
  "react-dom": "^18.3.0 || ^19.0.0"
40
41
  },
41
42
  "devDependencies": {
43
+ "@types/node": "^26.3.0",
42
44
  "@types/react": "^19.2.14",
43
45
  "@types/react-dom": "^19.2.3",
44
46
  "esbuild": "0.28.2",
@@ -46,6 +48,7 @@
46
48
  "react": "^19.2.6",
47
49
  "react-dom": "^19.2.6",
48
50
  "tsup": "^8.5.1",
49
- "typescript": "^5.9.3"
51
+ "typescript": "^5.9.3",
52
+ "vitest": "^3.2.7"
50
53
  }
51
54
  }