@axiumine/koa-utils 5.8.0 → 5.9.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
@@ -28,7 +28,7 @@ The remaining twelve are declared **optional** in `peerDependenciesMeta`. There
28
28
 
29
29
  | Optional peer | Required by |
30
30
  |---|---|
31
- | `@socketlabs/email` | `email/SocketLabsLib`, `lib/access/createResetPwdFlow`, `lib/access/createVerifyEmailFlow`, `lib/access/verifyEmailMailer`, `koa/router/verifyEmail`, and the `signUp` / `login*` / `resetPwd` / `updatePassword` / `emailChangeHashVerify` mutations |
31
+ | `@socketlabs/email` | `email/SocketLabsLib`, `lib/access/createResetPwdFlow`, `lib/access/createVerifyEmailFlow`, `lib/access/resetPwdMailer`, `lib/access/verifyEmailMailer`, `koa/router/verifyEmail`, and the `signUp` / `login*` / `resetPwd` / `updatePassword` / `emailChangeHashVerify` mutations |
32
32
  | `clamscan` | `files/scanVirus`, `files/uploadTempImage`, `files/uploadTempPdf` |
33
33
  | `file-type` | `files/validateMimeType`, `files/validateMimeTypeImages`, `files/validateJpgPngMimeType`, `files/uploadTempImage`, `files/uploadTempPdf` — loaded lazily, at first call |
34
34
  | `fs-extra` | the `files/move*` and `files/validate*` helpers, `files/uploadTempImage`, `files/uploadTempPdf` |
@@ -100,7 +100,26 @@ SendResponse {
100
100
  sendSubscriptionEmail(emailTo: string, otp: string): Promise<boolean>;
101
101
  sendResetPwdConfirmation(emailTo: string, name?: string): Promise<boolean>;
102
102
  sendResetPwdConfirmationHash(email: string, name: string, hash: string): Promise<boolean>;
103
- sendEmailReset(emailTo: string, hash: string, name?: string): Promise<boolean>;
103
+ /**
104
+ * Send the password-reset link.
105
+ *
106
+ * `linkBase` and `linkPath` are here for the same reason they are on {@link sendEmailVerify}, and the
107
+ * pair has to move together: an account that confirmed its address on one front end and then resets its
108
+ * password on another has been sent to two different sites for two halves of the same login. Both
109
+ * default to the previous behaviour — `APP_DOMAIN` and `/x/reset` — so no existing caller changes.
110
+ *
111
+ * ⚠️ Unlike the verification link, this one is consumed by a **front-end route**, not by a backend
112
+ * router: the page reads the email and the hash out of its own path and calls `updatePwd`. `linkPath`
113
+ * therefore names a route in whatever app `linkBase` serves, and the two have to agree with that app's
114
+ * router rather than with a service mount point.
115
+ *
116
+ * @param emailTo recipient, also embedded in the link (URI-encoded)
117
+ * @param hash the reset hash stored on the account
118
+ * @param name optional greeting name
119
+ * @param linkBase scheme + host the link points at; defaults to `APP_DOMAIN`
120
+ * @param linkPath front-end route prefix that renders the new-password form; defaults to `/x/reset`
121
+ */
122
+ sendEmailReset(emailTo: string, hash: string, name?: string, linkBase?: string, linkPath?: string): Promise<boolean>;
104
123
  /**
105
124
  * fallback footer
106
125
  */
@@ -256,10 +256,31 @@ SendResponse {
256
256
  return false;
257
257
  }
258
258
  }
259
- async sendEmailReset(emailTo, hash, name = '') {
259
+ /**
260
+ * Send the password-reset link.
261
+ *
262
+ * `linkBase` and `linkPath` are here for the same reason they are on {@link sendEmailVerify}, and the
263
+ * pair has to move together: an account that confirmed its address on one front end and then resets its
264
+ * password on another has been sent to two different sites for two halves of the same login. Both
265
+ * default to the previous behaviour — `APP_DOMAIN` and `/x/reset` — so no existing caller changes.
266
+ *
267
+ * ⚠️ Unlike the verification link, this one is consumed by a **front-end route**, not by a backend
268
+ * router: the page reads the email and the hash out of its own path and calls `updatePwd`. `linkPath`
269
+ * therefore names a route in whatever app `linkBase` serves, and the two have to agree with that app's
270
+ * router rather than with a service mount point.
271
+ *
272
+ * @param emailTo recipient, also embedded in the link (URI-encoded)
273
+ * @param hash the reset hash stored on the account
274
+ * @param name optional greeting name
275
+ * @param linkBase scheme + host the link points at; defaults to `APP_DOMAIN`
276
+ * @param linkPath front-end route prefix that renders the new-password form; defaults to `/x/reset`
277
+ */
278
+ async sendEmailReset(emailTo, hash, name = '', linkBase = this.linkBase, linkPath = '/x/reset') {
260
279
  const subject = `Password reset for ${this.platformName}`;
261
280
  const encodedEmail = encodeURI(emailTo);
262
- const link = `${this.linkBase}/x/reset/${encodedEmail}/${hash}`;
281
+ const base = linkBase.replace(/\/+$/, '');
282
+ const path = `/${linkPath.replace(/^\/+/, '')}`;
283
+ const link = `${base}${path}/${encodedEmail}/${hash}`;
263
284
  const linkHtml = this.StringObj.makeLink(link);
264
285
  const nameFixed = this.fixName(name);
265
286
  const textBody = `Hi${nameFixed}, you or someone else has requested a password reset. You can change your login password
@@ -1,3 +1,4 @@
1
+ import { IResetPwdMailer } from "../../../lib/access/resetPwdMailer.mjs";
1
2
  import { TGetResetPwd } from "../../../private/lib/access/db/getResetPwd.mjs";
2
3
  import { TSaveResetReq } from "../../../private/lib/access/db/saveResetReq.mjs";
3
4
  import { GraphQLNonNull } from 'graphql';
@@ -9,6 +10,15 @@ export interface IResetPwdArgs {
9
10
  export interface IResetPwdDeps {
10
11
  getResetPwd: TGetResetPwd;
11
12
  saveResetReq: TSaveResetReq;
13
+ /**
14
+ * Where the link in the mail points. Defaults to SocketLabs on `APP_DOMAIN`, which is what every
15
+ * caller got before the option existed.
16
+ *
17
+ * Supply one when the process serves more than one front end: the link host is the only part of this
18
+ * flow that differs per audience, and it is not derivable from the account — two collections behind
19
+ * one service look identical by the time the resolver has an email and a hash.
20
+ */
21
+ mailer?: IResetPwdMailer;
12
22
  }
13
23
  /**
14
24
  * Take the user email and send an email with a link for change the password.
@@ -1,4 +1,4 @@
1
- import { SocketLabsLib } from "../../../email/SocketLabsLib.mjs";
1
+ import { socketLabsResetPwdMailer } from "../../../lib/access/resetPwdMailer.mjs";
2
2
  import { checkEmailLen } from "../../../lib/checkEmailLen.mjs";
3
3
  import { EMAIL_HASH_LEN } from "../../../lib/Constants.mjs";
4
4
  import { DateLib } from "../../../lib/DateLib.mjs";
@@ -93,8 +93,8 @@ export const createResetPwdMutation = (deps) => ({
93
93
  const mail = pendingMail;
94
94
  if (mail !== null) {
95
95
  try {
96
- const SocketLabsObj = new SocketLabsLib();
97
- void SocketLabsObj.sendEmailReset(uEmail, mail.hash, mail.name).catch((e) => {
96
+ const mailer = deps.mailer ?? socketLabsResetPwdMailer;
97
+ void mailer.sendEmailReset(uEmail, mail.hash, mail.name).catch((e) => {
98
98
  Sentry.captureException(e);
99
99
  });
100
100
  }
@@ -1,10 +1,19 @@
1
1
  import { TResetPwdMutation } from '../../graphQL/schema/mutations/resetPwd.mjs';
2
2
  import { TUpdatePasswordMutation } from '../../graphQL/schema/mutations/updatePassword.mjs';
3
3
  import { IResetPwdPaths, TAccessModel } from './accessPaths.mjs';
4
+ import { IResetPwdMailer } from './resetPwdMailer.mjs';
4
5
  /** What the factory needs: the account model, plus any path that differs from the default layout. */
5
6
  export interface ICreateResetPwdFlowArgs {
6
7
  model: TAccessModel;
7
8
  paths?: Partial<IResetPwdPaths>;
9
+ /**
10
+ * Where the reset link points. Omitted means SocketLabs on `APP_DOMAIN`, the pre-existing behaviour.
11
+ *
12
+ * Pass `createResetPwdMailer(base, path)` when this process mails more than one audience. The flow is
13
+ * already per-model, so per-flow is the right granularity: the collection and the front end that
14
+ * serves it are the same choice made twice.
15
+ */
16
+ mailer?: IResetPwdMailer;
8
17
  }
9
18
  /** The two mutations the flow exposes, bound to the model and paths passed in. */
10
19
  export interface IResetPwdFlow {
@@ -30,4 +39,4 @@ export interface IResetPwdFlow {
30
39
  * })
31
40
  * ```
32
41
  */
33
- export declare const createResetPwdFlow: ({ model, paths }: ICreateResetPwdFlowArgs) => IResetPwdFlow;
42
+ export declare const createResetPwdFlow: ({ model, paths, mailer }: ICreateResetPwdFlowArgs) => IResetPwdFlow;
@@ -24,13 +24,14 @@ import { resolveResetPwdPaths } from './accessPaths.mjs';
24
24
  * })
25
25
  * ```
26
26
  */
27
- export const createResetPwdFlow = ({ model, paths }) => {
27
+ export const createResetPwdFlow = ({ model, paths, mailer }) => {
28
28
  const resolved = resolveResetPwdPaths(paths);
29
29
  const getResetPwd = createGetResetPwd(model, resolved);
30
30
  return {
31
31
  resetPwd: createResetPwdMutation({
32
32
  getResetPwd,
33
- saveResetReq: createSaveResetReq(model, resolved)
33
+ saveResetReq: createSaveResetReq(model, resolved),
34
+ mailer
34
35
  }),
35
36
  updatePassword: createUpdatePasswordMutation({
36
37
  getResetPwd,
@@ -0,0 +1,44 @@
1
+ /**
2
+ * The one message the password-reset flow sends.
3
+ *
4
+ * A single-method interface looks like ceremony next to `IVerifyEmailMailer`'s six, and it is not: the
5
+ * link in this mail carries the host the account will finish the reset on, and the resolver that sends it
6
+ * has no way to know which of a deployment's front ends the account belongs to. Before this seam existed
7
+ * the host was `APP_DOMAIN`, read once in `SocketLabsLib`'s constructor — correct for a deployment with
8
+ * one front end, and wrong the moment a second one authenticates against the same backend, because every
9
+ * customer's reset link then points at the operator panel.
10
+ *
11
+ * Structural, so `SocketLabsLib` itself satisfies it and a test double is an object literal.
12
+ *
13
+ * The resolver never awaits the returned promise (see `createResetPwdMutation` for the three reasons), so
14
+ * an implementation must not rely on its rejection being observed by the caller — the flow attaches its
15
+ * own Sentry handler and nothing else looks at it.
16
+ */
17
+ export interface IResetPwdMailer {
18
+ sendEmailReset(email: string, hash: string, name: string): Promise<unknown>;
19
+ }
20
+ /**
21
+ * SocketLabs on the default link base, constructed per call.
22
+ *
23
+ * Per call rather than once at module load for the reason `socketLabsVerifyEmailMailer` gives: the
24
+ * constructor reads `process.env`, and hoisting it to import time pins whatever the environment held when
25
+ * the module was first resolved.
26
+ */
27
+ export declare const socketLabsResetPwdMailer: IResetPwdMailer;
28
+ /**
29
+ * A mailer that points the reset link at a specific host and route.
30
+ *
31
+ * The intended use is one call per audience: a deployment serving a customer site next to an operator
32
+ * panel builds one of these per front end and passes it to that tier's `createResetPwdFlow`. Both
33
+ * arguments are configuration rather than a pre-joined URL — `sendEmailReset` normalises a trailing slash
34
+ * on the base and a missing leading slash on the path.
35
+ *
36
+ * Both are optional so a caller can pass `process.env.SOMETHING` straight through: an unset variable
37
+ * arrives as `undefined` and the default takes over — `APP_DOMAIN` for the base, `/x/reset` for the path.
38
+ * Normalising that to `''` at the call site instead would produce a link with no host at all.
39
+ *
40
+ * ⚠️ `linkPath` names a **front-end route**, not a backend mount. It has to match the route that renders
41
+ * the new-password form in the app `linkBase` serves; there is nothing on the server side that would fail
42
+ * loudly if it does not, because the link is only ever followed by a person.
43
+ */
44
+ export declare const createResetPwdMailer: (linkBase?: string, linkPath?: string) => IResetPwdMailer;
@@ -0,0 +1,31 @@
1
+ import { SocketLabsLib } from "../../email/SocketLabsLib.mjs";
2
+ /**
3
+ * SocketLabs on the default link base, constructed per call.
4
+ *
5
+ * Per call rather than once at module load for the reason `socketLabsVerifyEmailMailer` gives: the
6
+ * constructor reads `process.env`, and hoisting it to import time pins whatever the environment held when
7
+ * the module was first resolved.
8
+ */
9
+ export const socketLabsResetPwdMailer = {
10
+ sendEmailReset: (email, hash, name) => new SocketLabsLib().sendEmailReset(email, hash, name)
11
+ };
12
+ /**
13
+ * A mailer that points the reset link at a specific host and route.
14
+ *
15
+ * The intended use is one call per audience: a deployment serving a customer site next to an operator
16
+ * panel builds one of these per front end and passes it to that tier's `createResetPwdFlow`. Both
17
+ * arguments are configuration rather than a pre-joined URL — `sendEmailReset` normalises a trailing slash
18
+ * on the base and a missing leading slash on the path.
19
+ *
20
+ * Both are optional so a caller can pass `process.env.SOMETHING` straight through: an unset variable
21
+ * arrives as `undefined` and the default takes over — `APP_DOMAIN` for the base, `/x/reset` for the path.
22
+ * Normalising that to `''` at the call site instead would produce a link with no host at all.
23
+ *
24
+ * ⚠️ `linkPath` names a **front-end route**, not a backend mount. It has to match the route that renders
25
+ * the new-password form in the app `linkBase` serves; there is nothing on the server side that would fail
26
+ * loudly if it does not, because the link is only ever followed by a person.
27
+ */
28
+ export const createResetPwdMailer = (linkBase, linkPath) => ({
29
+ sendEmailReset: (email, hash, name) => new SocketLabsLib().sendEmailReset(email, hash, name, linkBase, linkPath)
30
+ });
31
+ //# sourceMappingURL=resetPwdMailer.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiumine/koa-utils",
3
- "version": "5.8.0",
3
+ "version": "5.9.0",
4
4
  "description": "Koa Utils",
5
5
  "type": "module",
6
6
  "repository": {
@@ -72,6 +72,10 @@
72
72
  "import": "./dist/lib/access/verifyEmailMailer.mjs",
73
73
  "types": "./dist/lib/access/verifyEmailMailer.d.mts"
74
74
  },
75
+ "./lib/access/resetPwdMailer": {
76
+ "import": "./dist/lib/access/resetPwdMailer.mjs",
77
+ "types": "./dist/lib/access/resetPwdMailer.d.mts"
78
+ },
75
79
  "./lib/MongoDB/IOnboarding": {
76
80
  "import": "./dist/lib/MongoDB/IOnboarding.mjs",
77
81
  "types": "./dist/lib/MongoDB/IOnboarding.d.mts"