@axiumine/koa-utils 5.1.0 → 5.1.1
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 +0 -18
- package/dist/graphQL/schema/mutations/emailChangeHashVerify.mjs +4 -1
- package/dist/graphQL/schema/mutations/emailChangeHashVerify.mjs.map +1 -1
- package/dist/private/lib/access/db/removeResetReq.mjs +6 -1
- package/dist/private/lib/access/db/removeResetReq.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,24 +24,6 @@ yarn add @node-rs/bcrypt @sentry/node @socketlabs/email clamscan dotenv \
|
|
|
24
24
|
reflect-metadata sequelize sequelize-typescript sharp uuid
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
## Upgrading to 5.1.0 — data migration required
|
|
28
|
-
|
|
29
|
-
5.1.0 changes `account.disabled` from `type: String` to `type: Boolean` in both user schemas (it always was `boolean` in the TypeScript interfaces). Every version up to and including 5.0.3 could store the *strings* `'true'`/`'false'` in that field, and `'false'` is truthy — so a user explicitly marked **not** disabled was refused login with a 403. The schema fix repairs hydrated reads, but `.lean()` reads bypass Mongoose casting and still see the raw string, so the stored values have to be rewritten.
|
|
30
|
-
|
|
31
|
-
The script is not part of the published tarball (`files: ["dist"]`). Take it from the [repository](https://github.com/Axiumine/koa-utils/blob/main/scripts/migrate-account-disabled-to-boolean.mjs) and run it once per database, before deploying 5.1.0:
|
|
32
|
-
|
|
33
|
-
```bash
|
|
34
|
-
# dry run — reports what it would change, writes nothing
|
|
35
|
-
MONGO_URI='mongodb://user:pass@host:27017/dbname' node scripts/migrate-account-disabled-to-boolean.mjs
|
|
36
|
-
|
|
37
|
-
# apply
|
|
38
|
-
MONGO_URI='mongodb://user:pass@host:27017/dbname' node scripts/migrate-account-disabled-to-boolean.mjs --apply
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
It maps `'true'` → `true` and `'false'` → `false`, removes empty strings, and leaves any other value untouched while reporting it and exiting with code `2`. Re-runs are idempotent. A database only ever written by 5.1.0 or later needs nothing. Full details in [`CHANGELOG.md`](CHANGELOG.md).
|
|
42
|
-
|
|
43
|
-
5.1.0 also moves the password-reset token to its own field, `account.resetHash`. Reset links issued before the upgrade stop working — the window is bounded by the 60-minute reset expiry, so it closes an hour after deploy. No migration needed for that one.
|
|
44
|
-
|
|
45
27
|
## Usage
|
|
46
28
|
|
|
47
29
|
No barrel — import each helper from its explicit subpath.
|
|
@@ -34,8 +34,11 @@ export const emailChangeHashVerify = {
|
|
|
34
34
|
const { email, hash } = args;
|
|
35
35
|
const uEmail = email.toLowerCase();
|
|
36
36
|
// search if the email exists
|
|
37
|
+
// Every field this resolver reads must be listed. account.email.requestTimes was missing, and
|
|
38
|
+
// because this is a .lean() read the absent key made handleHashMismatch throw 500 on *every*
|
|
39
|
+
// wrong hash: the strike counter never advanced and the owner never got the wrongHash warning.
|
|
37
40
|
const user = await UserBase.findOne({ 'account.email.newEmailTmp': uEmail })
|
|
38
|
-
.select('_id account.email.hash account.email.dateLastReq account.deleted account.disabled')
|
|
41
|
+
.select('_id account.email.hash account.email.dateLastReq account.email.requestTimes account.deleted account.disabled')
|
|
39
42
|
.lean();
|
|
40
43
|
// if email not found, return
|
|
41
44
|
if (user === null) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emailChangeHashVerify.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/emailChangeHashVerify.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,yCAAgC;AACxD,OAAO,EAAE,SAAS,EAAE,mCAA0B;AAC9C,OAAO,EAAE,QAAQ,EAAE,0CAAoC;AACvD,OAAO,eAAe,2DAAkD;AACxE,OAAO,EAAE,WAAW,EAAE,uDAA8C;AACpE,OAAO,EAAE,kBAAkB,EAAE,2CAAqC;AAClE,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAQvE;;;GAGG;AACH,KAAK,UAAU,kBAAkB,CAChC,GAAmB,EACnB,MAAc,EACd,YAAgC,EAChC,aAA4B;IAE5B,cAAc;IACd,OAAO,CAAC,KAAK,CAAC,aAAa,CAAC,CAAA;IAE5B,IAAI,OAAO,YAAY,KAAK,WAAW,EAAE,CAAC;QACzC,MAAM,kBAAkB,EAAE,CAAA;IAC3B,CAAC;IAED,MAAM,WAAW,CAAC,GAAG,CAAC,CAAA;IACtB,+BAA+B;IAC/B,aAAa,CAAC,SAAS,CAAC,MAAM,EAAE,YAAY,CAAC,CAAA;IAC7C,OAAO,KAAK,CAAA;AACb,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACpC,WAAW,EAAE,oDAAoD;IACjE,IAAI,EAAE,IAAI,cAAc,CAAC,cAAc,CAAC;IACxC,IAAI,EAAE;QACL,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,cAAc,CAAC,aAAa,CAAC,EAAE;QAClD,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,cAAc,CAAC,aAAa,CAAC,EAAE;KACjD;IACD,KAAK,CAAC,OAAO,CAAC,CAAU,EAAE,IAAW;QACpC,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,IAAI,CAAA;QAC5B,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,EAAE,CAAA;QAElC,6BAA6B;QAC7B,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,EAAE,2BAA2B,EAAE,MAAM,EAAE,CAAC;aAC1E,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"emailChangeHashVerify.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/emailChangeHashVerify.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,yCAAgC;AACxD,OAAO,EAAE,SAAS,EAAE,mCAA0B;AAC9C,OAAO,EAAE,QAAQ,EAAE,0CAAoC;AACvD,OAAO,eAAe,2DAAkD;AACxE,OA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tF,sFAAsF;iBACjF,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;gBAC/B,OAAO,KAAK,CAAA;YACb,CAAC;YACD,yCAAyC;iBACpC,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;gBAChC,+BAA+B;gBAC/B,aAAa,CAAC,eAAe,CAAC,MAAM,CAAC,CAAA;gBACrC,OAAO,KAAK,CAAA;YACb,CAAC;iBAAM,CAAC;gBACP,sCAAsC;gBAEtC,iEAAiE;gBACjE,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,cAAc,CAAC,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC,CAAA;gBAEpE,IAAI,GAAG,KAAK,CAAC,EAAE,CAAC;oBACf,mBAAmB;oBACnB,MAAM,eAAe,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;oBACvC,OAAO,IAAI,CAAA;gBACZ,CAAC;qBAAM,CAAC;oBACP,OAAO,KAAK,CAAA;gBACb,CAAC;YACF,CAAC;QACF,CAAC;aAAM,CAAC;YACP,OAAO,kBAAkB,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,YAAY,EAAE,aAAa,CAAC,CAAA;QACtF,CAAC;IACF,CAAC;CACD,CAAA","sourcesContent":["import { SocketLabsLib } from '@email/SocketLabsLib.mjs'\nimport { StringLib } from '@lib/StringLib.mjs'\nimport { UserBase } from '@models/MongoDB/UserBase.mjs'\nimport confirmNewEmail from '@private/lib/access/db/confirmNewEmail.mjs'\nimport { incReqTimes } from '@private/lib/access/db/incReqTimes.mjs'\nimport { throwInternalError } from '@throw/throwInternalError.mjs'\nimport { GraphQLBoolean, GraphQLNonNull, GraphQLString } from 'graphql'\nimport { Types } from 'mongoose'\n\ninterface IArgs {\n\temail: string\n\thash: string\n}\n\n/**\n * The submitted hash does not match the stored one: count the attempt and warn the account owner.\n * Extracted from resolve() only to keep it inside the max-lines-per-function budget — behaviour unchanged.\n */\nasync function handleHashMismatch(\n\tuId: Types.ObjectId,\n\tuEmail: string,\n\trequestTimes: number | undefined,\n\tSocketLabsObj: SocketLabsLib\n) {\n\t// hash failed\n\tconsole.debug('HASH FAILED')\n\n\tif (typeof requestTimes === 'undefined') {\n\t\tthrow throwInternalError()\n\t}\n\n\tawait incReqTimes(uId)\n\t// noinspection ES6MissingAwait\n\tSocketLabsObj.wrongHash(uEmail, requestTimes)\n\treturn false\n}\n\n/**\n * Change email - Verify match between email and hash\n */\nexport const emailChangeHashVerify = {\n\tdescription: 'Change email - Verify match between email and hash',\n\ttype: new GraphQLNonNull(GraphQLBoolean),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) },\n\t\thash: { type: new GraphQLNonNull(GraphQLString) }\n\t},\n\tasync resolve(_: unknown, args: IArgs) {\n\t\tconst { email, hash } = args\n\t\tconst uEmail = email.toLowerCase()\n\n\t\t// search if the email exists\n\t\t// Every field this resolver reads must be listed. account.email.requestTimes was missing, and\n\t\t// because this is a .lean() read the absent key made handleHashMismatch throw 500 on *every*\n\t\t// wrong hash: the strike counter never advanced and the owner never got the wrongHash warning.\n\t\tconst user = await UserBase.findOne({ 'account.email.newEmailTmp': uEmail })\n\t\t\t.select('_id account.email.hash account.email.dateLastReq account.email.requestTimes account.deleted account.disabled')\n\t\t\t.lean()\n\n\t\t// if email not found, return\n\t\tif (user === null) {\n\t\t\t// <- not found or wrong, do not say to the user the real problem !\n\t\t\tconsole.debug('email NOT found ', uEmail)\n\n\t\t\treturn false // @fixme throw\n\t\t}\n\t\tconsole.debug('email found')\n\n\t\tconst SocketLabsObj = new SocketLabsLib()\n\t\tconst StrLibObj = new StringLib()\n\n\t\tconsole.debug('comparing hash ')\n\t\tconst accountEmail = user.account.email\n\t\tif (hash === accountEmail.hash) {\n\t\t\tif (typeof accountEmail.dateLastReq === 'undefined') {\n\t\t\t\t// @fixme sentry 'dateLastReq is undefined'\n\t\t\t\tthrow throwInternalError()\n\t\t\t}\n\n\t\t\tconsole.debug('activation link hash is valid')\n\t\t\t/****************************************************\n\t\t\t * if dateLastReq too old then 3 days send email\n\t\t\t */\n\n\t\t\tconst now = new Date()\n\t\t\tconst day3ago = new Date(now.setDate(now.getDate() - 3))\n\t\t\tconst tsReq = StrLibObj.isoToTimestamp(accountEmail.dateLastReq)\n\t\t\tconst ts3DayAgo = StrLibObj.isoToTimestamp(day3ago)\n\n\t\t\tif (ts3DayAgo > tsReq) {\n\t\t\t\t// dateLastReq too old then 3 days\n\t\t\t\tconsole.debug('but link is too old')\n\n\t\t\t\t// noinspection ES6MissingAwait\n\t\t\t\tSocketLabsObj.hashReqTooOld(uEmail)\n\t\t\t\treturn false\n\t\t\t}\n\n\t\t\t// account is deleted -> maybe by admin for any reason\n\t\t\t// Read raw: this is a .lean() query, so Mongoose casting never runs and these are real\n\t\t\t// booleans only on data that scripts/migrate-account-disabled-to-boolean.mjs has been\n\t\t\t// through. A legacy string 'false' is truthy and blocks here — migrate, don't coerce.\n\t\t\telse if (user.account.deleted) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\t// if account is disabled, for any reason\n\t\t\telse if (user.account.disabled) {\n\t\t\t\t// noinspection ES6MissingAwait\n\t\t\t\tSocketLabsObj.accountDisabled(uEmail)\n\t\t\t\treturn false\n\t\t\t} else {\n\t\t\t\t// valid request. is this email free ?\n\n\t\t\t\t// meanwhile, has some user registered with this email address ??\n\t\t\t\tconst qty = await UserBase.countDocuments({ 'login.email': uEmail })\n\n\t\t\t\tif (qty === 0) {\n\t\t\t\t\t// accept new email\n\t\t\t\t\tawait confirmNewEmail(user._id, uEmail)\n\t\t\t\t\treturn true\n\t\t\t\t} else {\n\t\t\t\t\treturn false\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\treturn handleHashMismatch(user._id, uEmail, accountEmail.requestTimes, SocketLabsObj)\n\t\t}\n\t}\n}\n"]}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { UserBase } from "../../../../graphQL/models/MongoDB/UserBase.mjs";
|
|
2
2
|
export default async function removeResetReq(session, email) {
|
|
3
|
-
|
|
3
|
+
// No { upsert: true }. An upsert on a clearing operation is backwards: when the email matched no
|
|
4
|
+
// document MongoDB did not no-op, it inserted one keyed by login.email — and updateOne skips
|
|
5
|
+
// validators, so that row bypassed every required field in the schema (login.password,
|
|
6
|
+
// account.email.valid, account.registrationDate). A no-op is the only correct answer to "clear the
|
|
7
|
+
// reset state of a user that does not exist".
|
|
8
|
+
return UserBase.updateOne({ 'login.email': email }, { $unset: { 'account.resetDateReq': '', 'account.resetHash': '' } })
|
|
4
9
|
.session(session)
|
|
5
10
|
.exec();
|
|
6
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"removeResetReq.mjs","sourceRoot":"","sources":["../../../../../src/private/lib/access/db/removeResetReq.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,wDAAoC;AAGvD,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,cAAc,CAAC,OAAsB,EAAE,KAAa;IACjF,OAAO,QAAQ,CAAC,SAAS,
|
|
1
|
+
{"version":3,"file":"removeResetReq.mjs","sourceRoot":"","sources":["../../../../../src/private/lib/access/db/removeResetReq.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,wDAAoC;AAGvD,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,cAAc,CAAC,OAAsB,EAAE,KAAa;IACjF,iGAAiG;IACjG,6FAA6F;IAC7F,uFAAuF;IACvF,mGAAmG;IACnG,8CAA8C;IAC9C,OAAO,QAAQ,CAAC,SAAS,CAAC,EAAE,aAAa,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,sBAAsB,EAAE,EAAE,EAAE,mBAAmB,EAAE,EAAE,EAAE,EAAE,CAAC;SACtH,OAAO,CAAC,OAAO,CAAC;SAChB,IAAI,EAAE,CAAA;AACT,CAAC","sourcesContent":["import { UserBase } from '@models/MongoDB/UserBase.mjs'\nimport { ClientSession } from 'mongoose'\n\nexport default async function removeResetReq(session: ClientSession, email: string) {\n\t// No { upsert: true }. An upsert on a clearing operation is backwards: when the email matched no\n\t// document MongoDB did not no-op, it inserted one keyed by login.email — and updateOne skips\n\t// validators, so that row bypassed every required field in the schema (login.password,\n\t// account.email.valid, account.registrationDate). A no-op is the only correct answer to \"clear the\n\t// reset state of a user that does not exist\".\n\treturn UserBase.updateOne({ 'login.email': email }, { $unset: { 'account.resetDateReq': '', 'account.resetHash': '' } })\n\t\t.session(session)\n\t\t.exec()\n}\n"]}
|