@axiumine/koa-utils 5.3.0 → 5.4.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 +2 -0
- package/dist/graphQL/models/MongoDB/log/LogStatsGraphql.d.mts +2 -1
- package/dist/graphQL/models/MongoDB/log/LogStatsGraphql.mjs.map +1 -1
- package/dist/graphQL/schema/mutations/emailChangeHashVerify.d.mts +3 -3
- package/dist/graphQL/schema/mutations/emailChangeHashVerify.mjs +54 -58
- package/dist/graphQL/schema/mutations/emailChangeHashVerify.mjs.map +1 -1
- package/dist/graphQL/schema/mutations/login4Ever.d.mts +3 -3
- package/dist/graphQL/schema/mutations/login4Ever.mjs.map +1 -1
- package/dist/graphQL/schema/mutations/loginAdmin.d.mts +3 -3
- package/dist/graphQL/schema/mutations/loginAdmin.mjs.map +1 -1
- package/dist/graphQL/schema/mutations/loginRememberme.d.mts +3 -3
- package/dist/graphQL/schema/mutations/loginRememberme.mjs.map +1 -1
- package/dist/graphQL/schema/mutations/resetPwd.d.mts +3 -3
- package/dist/graphQL/schema/mutations/resetPwd.mjs.map +1 -1
- package/dist/graphQL/schema/mutations/signUp.d.mts +3 -3
- package/dist/graphQL/schema/mutations/signUp.mjs.map +1 -1
- package/dist/graphQL/schema/mutations/updatePassword.d.mts +3 -3
- package/dist/graphQL/schema/mutations/updatePassword.mjs +47 -38
- package/dist/graphQL/schema/mutations/updatePassword.mjs.map +1 -1
- package/dist/lib/access/accessPaths.d.mts +4 -0
- package/dist/lib/access/accessPaths.mjs +2 -0
- package/dist/lib/access/accessPaths.mjs.map +1 -1
- package/dist/lib/db/log/logGlobalError.d.mts +2 -2
- package/dist/lib/db/log/logGlobalError.mjs.map +1 -1
- package/dist/private/lib/access/db/getResetPwd.mjs +15 -1
- package/dist/private/lib/access/db/getResetPwd.mjs.map +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -92,6 +92,8 @@ const { resetPwd, updatePassword } = createResetPwdFlow({
|
|
|
92
92
|
name: 'profile.fullName',
|
|
93
93
|
resetDateReq: 'resetPwd.resetDateReq',
|
|
94
94
|
resetHash: 'resetPwd.resetHash',
|
|
95
|
+
deleted: 'state.gone', // a set flag makes the reset answer exactly as it does for an unknown address
|
|
96
|
+
disabled: 'state.locked',
|
|
95
97
|
resetClear: ['resetPwd'] // paths to $unset — a container, not its members, on a strict subdocument
|
|
96
98
|
}
|
|
97
99
|
})
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Types } from 'mongoose';
|
|
2
|
-
|
|
2
|
+
/** Document shape of the `LogStatsGraphql` model. Exported so a consumer re-exporting the model can name its type. */
|
|
3
|
+
export interface IStatsGraphqlSchema {
|
|
3
4
|
_id: Types.ObjectId;
|
|
4
5
|
u: number;
|
|
5
6
|
n: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LogStatsGraphql.mjs","sourceRoot":"","sources":["../../../../../src/graphQL/models/MongoDB/log/LogStatsGraphql.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAS,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"LogStatsGraphql.mjs","sourceRoot":"","sources":["../../../../../src/graphQL/models/MongoDB/log/LogStatsGraphql.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAS,MAAM,UAAU,CAAA;AAa/C,MAAM,kBAAkB,GAAG,IAAI,MAAM,CACpC;IACC,CAAC,EAAE;QACF,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;KACd;IACD,CAAC,EAAE;QACF,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;KACd;IACD,CAAC,EAAE;QACF,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;KACd;IACD,CAAC,EAAE;QACF,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,IAAI;KACd;IACD,CAAC,EAAE;QACF,IAAI,EAAE,IAAI;QACV,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,oDAAoD;KAC9E;IACD,GAAG,EAAE;QACJ,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,KAAK;KACf;CACD,EACD;IACC,UAAU,EAAE,iBAAiB;CAC7B,CACD,CAAA;AAED,MAAM,eAAe,GAAG,KAAK,CAAsB,iBAAiB,EAAE,kBAAkB,CAAC,CAAA;AACzF,OAAO,EAAE,eAAe,EAAE,CAAA","sourcesContent":["import { model, Schema, Types } from 'mongoose'\n\n/** Document shape of the `LogStatsGraphql` model. Exported so a consumer re-exporting the model can name its type. */\nexport interface IStatsGraphqlSchema {\n\t_id: Types.ObjectId\n\tu: number // user\n\tn: string // query name\n\ti: Date // inserted\n\ts: number // status\n\tm: number // msTot\n\t__v?: number\n}\n\nconst StatsGraphqlSchema = new Schema<IStatsGraphqlSchema>(\n\t{\n\t\tu: {\n\t\t\ttype: Number,\n\t\t\trequired: true\n\t\t},\n\t\tn: {\n\t\t\ttype: String,\n\t\t\trequired: true\n\t\t},\n\t\ts: {\n\t\t\ttype: Number,\n\t\t\trequired: true\n\t\t},\n\t\tm: {\n\t\t\ttype: Number,\n\t\t\trequired: true\n\t\t},\n\t\ti: {\n\t\t\ttype: Date,\n\t\t\trequired: false,\n\t\t\tdefault: () => new Date() // Ensure a new Date is generated for each document.\n\t\t},\n\t\t__v: {\n\t\t\ttype: Number,\n\t\t\trequired: false\n\t\t}\n\t},\n\t{\n\t\tcollection: 'logStatsGraphql'\n\t}\n)\n\nconst LogStatsGraphql = model<IStatsGraphqlSchema>('LogStatsGraphql', StatsGraphqlSchema)\nexport { LogStatsGraphql }\n"]}
|
|
@@ -2,7 +2,8 @@ import { IVerifyEmailPaths, TAccessModel } from "../../../lib/access/accessPaths
|
|
|
2
2
|
import { TConfirmNewEmail } from "../../../private/lib/access/db/confirmNewEmail.mjs";
|
|
3
3
|
import { TIncReqTimes } from "../../../private/lib/access/db/incReqTimes.mjs";
|
|
4
4
|
import { GraphQLNonNull } from 'graphql';
|
|
5
|
-
|
|
5
|
+
/** Arguments accepted by the `emailChangeHashVerify` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */
|
|
6
|
+
export interface IEmailChangeHashVerifyArgs {
|
|
6
7
|
email: string;
|
|
7
8
|
hash: string;
|
|
8
9
|
}
|
|
@@ -31,10 +32,9 @@ export declare const createEmailChangeHashVerifyMutation: (deps: IEmailChangeHas
|
|
|
31
32
|
type: GraphQLNonNull<import("graphql").GraphQLScalarType<string, string>>;
|
|
32
33
|
};
|
|
33
34
|
};
|
|
34
|
-
resolve(_: unknown, args:
|
|
35
|
+
resolve(_: unknown, args: IEmailChangeHashVerifyArgs): Promise<boolean>;
|
|
35
36
|
};
|
|
36
37
|
/** Shape of the bound mutation, for the modules that take it as a dependency. */
|
|
37
38
|
export type TEmailChangeHashVerifyMutation = ReturnType<typeof createEmailChangeHashVerifyMutation>;
|
|
38
39
|
/** `UserBase`-bound default — the mutation every existing consumer already imports. */
|
|
39
40
|
export declare const emailChangeHashVerify: TEmailChangeHashVerifyMutation;
|
|
40
|
-
export {};
|
|
@@ -11,17 +11,62 @@ import { GraphQLBoolean, GraphQLNonNull, GraphQLString } from 'graphql';
|
|
|
11
11
|
* The submitted hash does not match the stored one: count the attempt and warn the account owner.
|
|
12
12
|
* Extracted from resolve() only to keep it inside the max-lines-per-function budget — behaviour unchanged.
|
|
13
13
|
*/
|
|
14
|
-
async function handleHashMismatch(
|
|
14
|
+
async function handleHashMismatch(ctx, uId, requestTimes) {
|
|
15
15
|
// hash failed
|
|
16
16
|
console.debug('HASH FAILED');
|
|
17
17
|
if (typeof requestTimes === 'undefined') {
|
|
18
18
|
throw throwInternalError();
|
|
19
19
|
}
|
|
20
|
-
await
|
|
20
|
+
await ctx.deps.incReqTimes(uId);
|
|
21
21
|
// noinspection ES6MissingAwait
|
|
22
|
-
|
|
22
|
+
ctx.socketLabs.wrongHash(ctx.uEmail, requestTimes);
|
|
23
23
|
return false;
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* The submitted hash matches: enforce the freshness/account guards and, if the target email is still
|
|
27
|
+
* free, accept it. Extracted from resolve() only to keep it inside the max-lines-per-function budget.
|
|
28
|
+
*/
|
|
29
|
+
async function handleValidHash(ctx, uId, dateLastReq) {
|
|
30
|
+
const { deps, user, uEmail, socketLabs } = ctx;
|
|
31
|
+
const { paths } = deps;
|
|
32
|
+
if (typeof dateLastReq === 'undefined') {
|
|
33
|
+
// @fixme sentry 'dateLastReq is undefined'
|
|
34
|
+
throw throwInternalError();
|
|
35
|
+
}
|
|
36
|
+
console.debug('activation link hash is valid');
|
|
37
|
+
// if dateLastReq is older than 3 days, warn and stop
|
|
38
|
+
const StrLibObj = new StringLib();
|
|
39
|
+
const now = new Date();
|
|
40
|
+
const day3ago = new Date(now.setDate(now.getDate() - 3));
|
|
41
|
+
if (StrLibObj.isoToTimestamp(day3ago) > StrLibObj.isoToTimestamp(dateLastReq)) {
|
|
42
|
+
// dateLastReq too old then 3 days
|
|
43
|
+
console.debug('but link is too old');
|
|
44
|
+
// noinspection ES6MissingAwait
|
|
45
|
+
socketLabs.hashReqTooOld(uEmail);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
// account is deleted -> maybe by admin for any reason
|
|
49
|
+
// Read raw: this is a .lean() query, so Mongoose casting never runs and these are real booleans
|
|
50
|
+
// only on data that scripts/migrate-account-disabled-to-boolean.mjs has been through. A legacy
|
|
51
|
+
// string 'false' is truthy and blocks here — migrate, don't coerce.
|
|
52
|
+
if (readPath(user, paths.deleted)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
// if account is disabled, for any reason
|
|
56
|
+
if (readPath(user, paths.disabled)) {
|
|
57
|
+
// noinspection ES6MissingAwait
|
|
58
|
+
socketLabs.accountDisabled(uEmail);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
// valid request. meanwhile, has some user registered with this email address ??
|
|
62
|
+
const qty = await deps.model.countDocuments({ [paths.email]: uEmail });
|
|
63
|
+
if (qty > 0) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
// accept new email
|
|
67
|
+
await deps.confirmNewEmail(uId, uEmail);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
25
70
|
/**
|
|
26
71
|
* Change email - Verify match between email and hash
|
|
27
72
|
*
|
|
@@ -38,8 +83,7 @@ export const createEmailChangeHashVerifyMutation = (deps) => ({
|
|
|
38
83
|
},
|
|
39
84
|
async resolve(_, args) {
|
|
40
85
|
const { paths } = deps;
|
|
41
|
-
const
|
|
42
|
-
const uEmail = email.toLowerCase();
|
|
86
|
+
const uEmail = args.email.toLowerCase();
|
|
43
87
|
// search if the email exists
|
|
44
88
|
// Every field this resolver reads must be listed. account.email.requestTimes was missing, and
|
|
45
89
|
// because this is a .lean() read the absent key made handleHashMismatch throw 500 on *every*
|
|
@@ -48,68 +92,20 @@ export const createEmailChangeHashVerifyMutation = (deps) => ({
|
|
|
48
92
|
.findOne({ [paths.newEmailTmp]: uEmail })
|
|
49
93
|
.select(buildProjection([paths.hash, paths.dateLastReq, paths.requestTimes, paths.deleted, paths.disabled]))
|
|
50
94
|
.lean();
|
|
51
|
-
// if email not found, return
|
|
95
|
+
// if email not found, return (do not tell the user the real problem !)
|
|
52
96
|
if (user === null) {
|
|
53
|
-
// <- not found or wrong, do not say to the user the real problem !
|
|
54
97
|
console.debug('email NOT found ', uEmail);
|
|
55
98
|
return false; // @fixme throw
|
|
56
99
|
}
|
|
57
100
|
console.debug('email found');
|
|
58
|
-
const
|
|
59
|
-
const StrLibObj = new StringLib();
|
|
101
|
+
const ctx = { deps, user, uEmail, socketLabs: new SocketLabsLib() };
|
|
60
102
|
console.debug('comparing hash ');
|
|
61
103
|
const uId = readPath(user, '_id');
|
|
62
104
|
const dateLastReq = readPath(user, paths.dateLastReq);
|
|
63
|
-
if (hash === readPath(user, paths.hash)) {
|
|
64
|
-
|
|
65
|
-
// @fixme sentry 'dateLastReq is undefined'
|
|
66
|
-
throw throwInternalError();
|
|
67
|
-
}
|
|
68
|
-
console.debug('activation link hash is valid');
|
|
69
|
-
/****************************************************
|
|
70
|
-
* if dateLastReq too old then 3 days send email
|
|
71
|
-
*/
|
|
72
|
-
const now = new Date();
|
|
73
|
-
const day3ago = new Date(now.setDate(now.getDate() - 3));
|
|
74
|
-
const tsReq = StrLibObj.isoToTimestamp(dateLastReq);
|
|
75
|
-
const ts3DayAgo = StrLibObj.isoToTimestamp(day3ago);
|
|
76
|
-
if (ts3DayAgo > tsReq) {
|
|
77
|
-
// dateLastReq too old then 3 days
|
|
78
|
-
console.debug('but link is too old');
|
|
79
|
-
// noinspection ES6MissingAwait
|
|
80
|
-
SocketLabsObj.hashReqTooOld(uEmail);
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
// account is deleted -> maybe by admin for any reason
|
|
84
|
-
// Read raw: this is a .lean() query, so Mongoose casting never runs and these are real
|
|
85
|
-
// booleans only on data that scripts/migrate-account-disabled-to-boolean.mjs has been
|
|
86
|
-
// through. A legacy string 'false' is truthy and blocks here — migrate, don't coerce.
|
|
87
|
-
else if (readPath(user, paths.deleted)) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
// if account is disabled, for any reason
|
|
91
|
-
else if (readPath(user, paths.disabled)) {
|
|
92
|
-
// noinspection ES6MissingAwait
|
|
93
|
-
SocketLabsObj.accountDisabled(uEmail);
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
// valid request. is this email free ?
|
|
98
|
-
// meanwhile, has some user registered with this email address ??
|
|
99
|
-
const qty = await deps.model.countDocuments({ [paths.email]: uEmail });
|
|
100
|
-
if (qty === 0) {
|
|
101
|
-
// accept new email
|
|
102
|
-
await deps.confirmNewEmail(uId, uEmail);
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
return handleHashMismatch(deps.incReqTimes, uId, uEmail, readPath(user, paths.requestTimes), SocketLabsObj);
|
|
105
|
+
if (args.hash === readPath(user, paths.hash)) {
|
|
106
|
+
return handleValidHash(ctx, uId, dateLastReq);
|
|
112
107
|
}
|
|
108
|
+
return handleHashMismatch(ctx, uId, readPath(user, paths.requestTimes));
|
|
113
109
|
}
|
|
114
110
|
});
|
|
115
111
|
/** `UserBase`-bound default — the mutation every existing consumer already imports. */
|
|
@@ -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,0BAA0B,EAAmC,4CAAmC;AACzG,OAAO,EAAE,SAAS,EAAE,mCAA0B;AAC9C,OAAO,EAAE,QAAQ,EAAE,0CAAoC;AACvD,OAAO,eAAqC,2DAAkD;AAC9F,OAAO,EAAE,WAAW,EAAgB,uDAA8C;AAClF,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,kDAAyC;AAC7E,OAAO,EAAE,kBAAkB,EAAE,2CAAqC;AAClE,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAgBvE;;;GAGG;AACH,KAAK,UAAU,kBAAkB,CAChC,aAA2B,EAC3B,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,aAAa,CAAC,GAAG,CAAC,CAAA;IACxB,+BAA+B;IAC/B,aAAa,CAAC,SAAS,CAA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CAAC;YACD,yCAAyC;iBACpC,IAAI,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACzC,+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,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC,CAAA;gBAEtE,IAAI,GAAG,KAAK,CAAC,EAAE,CAAC;oBACf,mBAAmB;oBACnB,MAAM,IAAI,CAAC,eAAe,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,CACxB,IAAI,CAAC,WAAW,EAChB,GAAG,EACH,MAAM,EACN,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,YAAY,CAAuB,EACxD,aAAa,CACb,CAAA;QACF,CAAC;IACF,CAAC;CACD,CAAC,CAAA;AAKF,uFAAuF;AACvF,MAAM,CAAC,MAAM,qBAAqB,GAAmC,mCAAmC,CAAC;IACxG,KAAK,EAAE,QAAQ;IACf,KAAK,EAAE,0BAA0B;IACjC,eAAe;IACf,WAAW;CACX,CAAC,CAAA","sourcesContent":["import { SocketLabsLib } from '@email/SocketLabsLib.mjs'\nimport { DEFAULT_VERIFY_EMAIL_PATHS, IVerifyEmailPaths, TAccessModel } from '@lib/access/accessPaths.mjs'\nimport { StringLib } from '@lib/StringLib.mjs'\nimport { UserBase } from '@models/MongoDB/UserBase.mjs'\nimport confirmNewEmail, { TConfirmNewEmail } from '@private/lib/access/db/confirmNewEmail.mjs'\nimport { incReqTimes, TIncReqTimes } from '@private/lib/access/db/incReqTimes.mjs'\nimport { buildProjection, readPath } from '@private/lib/access/pathTools.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/** Model, field map and writers the resolver needs, all bound together by the caller. */\nexport interface IEmailChangeHashVerifyDeps {\n\tmodel: TAccessModel\n\tpaths: IVerifyEmailPaths\n\tconfirmNewEmail: TConfirmNewEmail\n\tincReqTimes: TIncReqTimes\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\tincReqTimesFn: TIncReqTimes,\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 incReqTimesFn(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 *\n * The collection and every field path come from `deps`, so the same resolver serves any account model.\n * The projection is built from the same map the reads use, which is what keeps a read field from going\n * missing — the exact bug that made every wrong hash answer 500 through 5.1.0.\n */\nexport const createEmailChangeHashVerifyMutation = (deps: IEmailChangeHashVerifyDeps) => ({\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 { paths } = deps\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 deps.model\n\t\t\t.findOne({ [paths.newEmailTmp]: uEmail })\n\t\t\t.select(buildProjection([paths.hash, paths.dateLastReq, paths.requestTimes, paths.deleted, paths.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 uId = readPath(user, '_id') as Types.ObjectId\n\t\tconst dateLastReq = readPath(user, paths.dateLastReq) as Date | undefined\n\t\tif (hash === readPath(user, paths.hash)) {\n\t\t\tif (typeof 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(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 (readPath(user, paths.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 (readPath(user, paths.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 deps.model.countDocuments({ [paths.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 deps.confirmNewEmail(uId, 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(\n\t\t\t\tdeps.incReqTimes,\n\t\t\t\tuId,\n\t\t\t\tuEmail,\n\t\t\t\treadPath(user, paths.requestTimes) as number | undefined,\n\t\t\t\tSocketLabsObj\n\t\t\t)\n\t\t}\n\t}\n})\n\n/** Shape of the bound mutation, for the modules that take it as a dependency. */\nexport type TEmailChangeHashVerifyMutation = ReturnType<typeof createEmailChangeHashVerifyMutation>\n\n/** `UserBase`-bound default — the mutation every existing consumer already imports. */\nexport const emailChangeHashVerify: TEmailChangeHashVerifyMutation = createEmailChangeHashVerifyMutation({\n\tmodel: UserBase,\n\tpaths: DEFAULT_VERIFY_EMAIL_PATHS,\n\tconfirmNewEmail,\n\tincReqTimes\n})\n"]}
|
|
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,0BAA0B,EA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EAAE,KAAK,CAAC,WAAW,CAAqB,CAAA;QAEzE,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YAC9C,OAAO,eAAe,CAAC,GAAG,EAAE,GAAG,EAAE,WAAW,CAAC,CAAA;QAC9C,CAAC;QACD,OAAO,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,YAAY,CAAuB,CAAC,CAAA;IAC9F,CAAC;CACD,CAAC,CAAA;AAKF,uFAAuF;AACvF,MAAM,CAAC,MAAM,qBAAqB,GAAmC,mCAAmC,CAAC;IACxG,KAAK,EAAE,QAAQ;IACf,KAAK,EAAE,0BAA0B;IACjC,eAAe;IACf,WAAW;CACX,CAAC,CAAA","sourcesContent":["import { SocketLabsLib } from '@email/SocketLabsLib.mjs'\nimport { DEFAULT_VERIFY_EMAIL_PATHS, IVerifyEmailPaths, TAccessModel } from '@lib/access/accessPaths.mjs'\nimport { StringLib } from '@lib/StringLib.mjs'\nimport { UserBase } from '@models/MongoDB/UserBase.mjs'\nimport confirmNewEmail, { TConfirmNewEmail } from '@private/lib/access/db/confirmNewEmail.mjs'\nimport { incReqTimes, TIncReqTimes } from '@private/lib/access/db/incReqTimes.mjs'\nimport { buildProjection, readPath } from '@private/lib/access/pathTools.mjs'\nimport { throwInternalError } from '@throw/throwInternalError.mjs'\nimport { GraphQLBoolean, GraphQLNonNull, GraphQLString } from 'graphql'\nimport { Types } from 'mongoose'\n\n/** Arguments accepted by the `emailChangeHashVerify` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */\nexport interface IEmailChangeHashVerifyArgs {\n\temail: string\n\thash: string\n}\n\n/** Model, field map and writers the resolver needs, all bound together by the caller. */\nexport interface IEmailChangeHashVerifyDeps {\n\tmodel: TAccessModel\n\tpaths: IVerifyEmailPaths\n\tconfirmNewEmail: TConfirmNewEmail\n\tincReqTimes: TIncReqTimes\n}\n\n/**\n * Everything the extracted branch handlers need, bundled so each stays within the max-params budget:\n * the bound deps, the lean document just read, the lowercased email and one shared SocketLabs client.\n */\ninterface IVerifyContext {\n\tdeps: IEmailChangeHashVerifyDeps\n\tuser: unknown\n\tuEmail: string\n\tsocketLabs: SocketLabsLib\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(ctx: IVerifyContext, uId: Types.ObjectId, requestTimes: number | undefined) {\n\t// hash failed\n\tconsole.debug('HASH FAILED')\n\n\tif (typeof requestTimes === 'undefined') {\n\t\tthrow throwInternalError()\n\t}\n\n\tawait ctx.deps.incReqTimes(uId)\n\t// noinspection ES6MissingAwait\n\tctx.socketLabs.wrongHash(ctx.uEmail, requestTimes)\n\treturn false\n}\n\n/**\n * The submitted hash matches: enforce the freshness/account guards and, if the target email is still\n * free, accept it. Extracted from resolve() only to keep it inside the max-lines-per-function budget.\n */\nasync function handleValidHash(ctx: IVerifyContext, uId: Types.ObjectId, dateLastReq: Date | undefined) {\n\tconst { deps, user, uEmail, socketLabs } = ctx\n\tconst { paths } = deps\n\n\tif (typeof dateLastReq === 'undefined') {\n\t\t// @fixme sentry 'dateLastReq is undefined'\n\t\tthrow throwInternalError()\n\t}\n\n\tconsole.debug('activation link hash is valid')\n\n\t// if dateLastReq is older than 3 days, warn and stop\n\tconst StrLibObj = new StringLib()\n\tconst now = new Date()\n\tconst day3ago = new Date(now.setDate(now.getDate() - 3))\n\n\tif (StrLibObj.isoToTimestamp(day3ago) > StrLibObj.isoToTimestamp(dateLastReq)) {\n\t\t// dateLastReq too old then 3 days\n\t\tconsole.debug('but link is too old')\n\n\t\t// noinspection ES6MissingAwait\n\t\tsocketLabs.hashReqTooOld(uEmail)\n\t\treturn false\n\t}\n\n\t// account is deleted -> maybe by admin for any reason\n\t// Read raw: this is a .lean() query, so Mongoose casting never runs and these are real booleans\n\t// only on data that scripts/migrate-account-disabled-to-boolean.mjs has been through. A legacy\n\t// string 'false' is truthy and blocks here — migrate, don't coerce.\n\tif (readPath(user, paths.deleted)) {\n\t\treturn false\n\t}\n\t// if account is disabled, for any reason\n\tif (readPath(user, paths.disabled)) {\n\t\t// noinspection ES6MissingAwait\n\t\tsocketLabs.accountDisabled(uEmail)\n\t\treturn false\n\t}\n\n\t// valid request. meanwhile, has some user registered with this email address ??\n\tconst qty = await deps.model.countDocuments({ [paths.email]: uEmail })\n\tif (qty > 0) {\n\t\treturn false\n\t}\n\n\t// accept new email\n\tawait deps.confirmNewEmail(uId, uEmail)\n\treturn true\n}\n\n/**\n * Change email - Verify match between email and hash\n *\n * The collection and every field path come from `deps`, so the same resolver serves any account model.\n * The projection is built from the same map the reads use, which is what keeps a read field from going\n * missing — the exact bug that made every wrong hash answer 500 through 5.1.0.\n */\nexport const createEmailChangeHashVerifyMutation = (deps: IEmailChangeHashVerifyDeps) => ({\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: IEmailChangeHashVerifyArgs) {\n\t\tconst { paths } = deps\n\t\tconst uEmail = args.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 deps.model\n\t\t\t.findOne({ [paths.newEmailTmp]: uEmail })\n\t\t\t.select(buildProjection([paths.hash, paths.dateLastReq, paths.requestTimes, paths.deleted, paths.disabled]))\n\t\t\t.lean()\n\n\t\t// if email not found, return (do not tell the user the real problem !)\n\t\tif (user === null) {\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 ctx: IVerifyContext = { deps, user, uEmail, socketLabs: new SocketLabsLib() }\n\n\t\tconsole.debug('comparing hash ')\n\t\tconst uId = readPath(user, '_id') as Types.ObjectId\n\t\tconst dateLastReq = readPath(user, paths.dateLastReq) as Date | undefined\n\n\t\tif (args.hash === readPath(user, paths.hash)) {\n\t\t\treturn handleValidHash(ctx, uId, dateLastReq)\n\t\t}\n\t\treturn handleHashMismatch(ctx, uId, readPath(user, paths.requestTimes) as number | undefined)\n\t}\n})\n\n/** Shape of the bound mutation, for the modules that take it as a dependency. */\nexport type TEmailChangeHashVerifyMutation = ReturnType<typeof createEmailChangeHashVerifyMutation>\n\n/** `UserBase`-bound default — the mutation every existing consumer already imports. */\nexport const emailChangeHashVerify: TEmailChangeHashVerifyMutation = createEmailChangeHashVerifyMutation({\n\tmodel: UserBase,\n\tpaths: DEFAULT_VERIFY_EMAIL_PATHS,\n\tconfirmNewEmail,\n\tincReqTimes\n})\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IContextLogin } from "../context/IContextLogin.mjs";
|
|
2
2
|
import { GraphQLNonNull } from 'graphql';
|
|
3
|
-
|
|
3
|
+
/** Arguments accepted by the `login4Ever` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */
|
|
4
|
+
export interface ILogin4EverArgs {
|
|
4
5
|
email: string;
|
|
5
6
|
password: string;
|
|
6
7
|
}
|
|
@@ -15,8 +16,7 @@ export declare const login4Ever: {
|
|
|
15
16
|
type: GraphQLNonNull<import("graphql").GraphQLScalarType<string, string>>;
|
|
16
17
|
};
|
|
17
18
|
};
|
|
18
|
-
resolve(_: unknown, args:
|
|
19
|
+
resolve(_: unknown, args: ILogin4EverArgs, ctx: IContextLogin): Promise<{
|
|
19
20
|
accessToken: string;
|
|
20
21
|
}>;
|
|
21
22
|
};
|
|
22
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"login4Ever.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/login4Ever.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,uCAA8B;AACtD,OAAO,EAAE,WAAW,EAAE,qCAA4B;AAClD,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,gCAAuB;AAC9F,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,2BAA2B,EAAE,kFAAyE;AAC/G,OAAO,EAAE,oBAAoB,EAAE,2EAAkE;AACjG,OAAO,EAAE,qBAAqB,EAAE,4EAAmE;AACnG,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,+BAA6B;AACjD,OAAO,EAAgB,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACrE,OAAO,QAAQ,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"login4Ever.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/login4Ever.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,uCAA8B;AACtD,OAAO,EAAE,WAAW,EAAE,qCAA4B;AAClD,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,gCAAuB;AAC9F,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,2BAA2B,EAAE,kFAAyE;AAC/G,OAAO,EAAE,oBAAoB,EAAE,2EAAkE;AACjG,OAAO,EAAE,qBAAqB,EAAE,4EAAmE;AACnG,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,+BAA6B;AACjD,OAAO,EAAgB,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACrE,OAAO,QAAQ,MAAM,UAAU,CAAA;AAQ/B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AAW,EAAE,YAAY,CAAC,CAAA;gBACvE,wCAAwC;gBACxC,eAAe,CAAC,GAAG,EAAE,YAAY,CAAC,CAAA;gBAClC,+CAA+C;YAChD,CAAC,CAAC,CAAA;QACH,CAAC;QAAC,OAAO,CAAU,EAAE,CAAC;YACrB,WAAW,GAAG,EAAE,CAAA;YAChB,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;YAC1B,eAAe,CAAC,CAAyB,CAAC,CAAA;QAC3C,CAAC;gBAAS,CAAC;YACV,MAAM,OAAO,CAAC,UAAU,EAAE,CAAA;QAC3B,CAAC;QACD,+DAA+D;QAC/D,OAAO,EAAE,WAAW,EAAE,CAAA;IACvB,CAAC;CACD,CAAA","sourcesContent":["import { IContextLogin } from '@context/IContextLogin.mjs'\nimport { checkEmailLen } from '@lib/checkEmailLen.mjs'\nimport { checkPwdLen } from '@lib/checkPwdLen.mjs'\nimport { setLoginCookies } from '@lib/setLoginCookies.mjs'\nimport { accessTokenExpiry, generateAccessToken, generateRefreshToken } from '@lib/tokens.mjs'\nimport { tryCatchRethrow } from '@lib/tryCatchRethrow.mjs'\nimport { checkUserLoginAuthorization } from '@private/graphQL/schema/mutations/checkUserLoginAuthorization.mjs'\nimport { setRedisLoginSession } from '@private/graphQL/schema/mutations/setRedisLoginSession.mjs'\nimport { updateLoginStats4ever } from '@private/graphQL/schema/mutations/updateLoginStats4ever.mjs'\nimport * as Sentry from '@sentry/node'\nimport { LoginType } from '@stypes/LoginType.mjs'\nimport { GraphQLError, GraphQLNonNull, GraphQLString } from 'graphql'\nimport mongoose from 'mongoose'\n\n/** Arguments accepted by the `login4Ever` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */\nexport interface ILogin4EverArgs {\n\temail: string\n\tpassword: string\n}\n\nexport const login4Ever = {\n\tdescription: 'login in platform WITHOUT remember me flag',\n\ttype: new GraphQLNonNull(LoginType),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) },\n\t\tpassword: { type: new GraphQLNonNull(GraphQLString) }\n\t},\n\tasync resolve(_: unknown, args: ILogin4EverArgs, ctx: IContextLogin) {\n\t\tconst { email, password } = args\n\n\t\t//console.debug('[login4Ever]')\n\n\t\tconst uEmail = email.toLowerCase().trim()\n\t\tcheckEmailLen(uEmail)\n\t\tcheckPwdLen(password)\n\t\t//console.debug('[login4Ever] data ok')\n\n\t\tlet accessToken = ''\n\n\t\tconst session = await mongoose.startSession()\n\t\ttry {\n\t\t\tawait session.withTransaction(async () => {\n\t\t\t\t// search if the email exists\n\t\t\t\tconst user = await checkUserLoginAuthorization(uEmail, password, session)\n\t\t\t\t//console.debug('[login4Ever] auth ok')\n\n\t\t\t\t// pwd valid\n\t\t\t\tconst uId = user.userId\n\t\t\t\tawait updateLoginStats4ever(uId, user.lastLogin, session)\n\t\t\t\t//console.debug('[login4Ever] update ok')\n\t\t\t\t// console.debug('[login] lastlogin ok')\n\n\t\t\t\taccessToken = generateAccessToken()\n\t\t\t\tconst refreshToken = generateRefreshToken()\n\t\t\t\tconst accTokenExp = accessTokenExpiry()\n\n\t\t\t\tawait setRedisLoginSession(uId, accessToken, accTokenExp, refreshToken)\n\t\t\t\t//console.debug('[login4Ever] redis ok')\n\t\t\t\tsetLoginCookies(ctx, refreshToken)\n\t\t\t\t//console.debug('[login4Ever] login cookie ok')\n\t\t\t})\n\t\t} catch (e: unknown) {\n\t\t\taccessToken = ''\n\t\t\tSentry.captureException(e)\n\t\t\ttryCatchRethrow(e as GraphQLError | Error)\n\t\t} finally {\n\t\t\tawait session.endSession()\n\t\t}\n\t\t//console.debug('[login4Ever] return accessToken', accessToken)\n\t\treturn { accessToken }\n\t}\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IContextLogin } from "../context/IContextLogin.mjs";
|
|
2
2
|
import { GraphQLNonNull } from 'graphql';
|
|
3
|
-
|
|
3
|
+
/** Arguments accepted by the `loginAdmin` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */
|
|
4
|
+
export interface ILoginAdminArgs {
|
|
4
5
|
email: string;
|
|
5
6
|
password: string;
|
|
6
7
|
rememberMe: boolean;
|
|
@@ -22,8 +23,7 @@ export declare const loginAdmin: {
|
|
|
22
23
|
type: GraphQLNonNull<import("graphql").GraphQLScalarType<boolean, boolean>>;
|
|
23
24
|
};
|
|
24
25
|
};
|
|
25
|
-
resolve(_: unknown, args:
|
|
26
|
+
resolve(_: unknown, args: ILoginAdminArgs, ctx: IContextLogin): Promise<{
|
|
26
27
|
accessToken: string;
|
|
27
28
|
}>;
|
|
28
29
|
};
|
|
29
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loginAdmin.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/loginAdmin.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,uCAA8B;AACtD,OAAO,EAAE,WAAW,EAAE,qCAA4B;AAClD,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,gCAAuB;AAC9F,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,gCAAgC,EAAE,uFAA8E;AACzH,OAAO,EAAE,oBAAoB,EAAE,2EAAkE;AACjG,OAAO,EAAE,qBAAqB,EAAE,4EAAmE;AACnG,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,+BAA6B;AACjD,OAAO,EAAE,cAAc,EAAgB,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACrF,OAAO,QAAQ,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"loginAdmin.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/loginAdmin.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,uCAA8B;AACtD,OAAO,EAAE,WAAW,EAAE,qCAA4B;AAClD,OAAO,EAAE,eAA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AAe,CAAC,KAAK,IAAI,EAAE;gBACxC,6BAA6B;gBAC7B,MAAM,IAAI,GAAG,MAAM,gCAAgC,CAAC,MAAM,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAA;gBAC9E,YAAY;gBACZ,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAA;gBACvB,MAAM,qBAAqB,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,CAAC,CAAA;gBACrE,wCAAwC;gBAExC,WAAW,GAAG,mBAAmB,EAAE,CAAA;gBACnC,MAAM,YAAY,GAAG,oBAAoB,EAAE,CAAA;gBAC3C,MAAM,WAAW,GAAG,iBAAiB,EAAE,CAAA;gBAEvC,MAAM,oBAAoB,CAAC,GAAG,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,CAAC,CAAA;gBACvE,eAAe,CAAC,GAAG,EAAE,YAAY,CAAC,CAAA;YACnC,CAAC,CAAC,CAAA;QACH,CAAC;QAAC,OAAO,CAAU,EAAE,CAAC;YACrB,WAAW,GAAG,EAAE,CAAA;YAChB,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;YAC1B,eAAe,CAAC,CAAyB,CAAC,CAAA;QAC3C,CAAC;gBAAS,CAAC;YACV,MAAM,OAAO,CAAC,UAAU,EAAE,CAAA;QAC3B,CAAC;QAED,OAAO;YACN,WAAW;SACX,CAAA;IACF,CAAC;CACD,CAAA","sourcesContent":["import { IContextLogin } from '@context/IContextLogin.mjs'\nimport { checkEmailLen } from '@lib/checkEmailLen.mjs'\nimport { checkPwdLen } from '@lib/checkPwdLen.mjs'\nimport { setLoginCookies } from '@lib/setLoginCookies.mjs'\nimport { accessTokenExpiry, generateAccessToken, generateRefreshToken } from '@lib/tokens.mjs'\nimport { tryCatchRethrow } from '@lib/tryCatchRethrow.mjs'\nimport { checkUserAdminLoginAuthorization } from '@private/graphQL/schema/mutations/checkUserAdminLoginAuthorization.mjs'\nimport { setRedisLoginSession } from '@private/graphQL/schema/mutations/setRedisLoginSession.mjs'\nimport { updateAdminLoginStats } from '@private/graphQL/schema/mutations/updateAdminLoginStats.mjs'\nimport * as Sentry from '@sentry/node'\nimport { LoginType } from '@stypes/LoginType.mjs'\nimport { GraphQLBoolean, GraphQLError, GraphQLNonNull, GraphQLString } from 'graphql'\nimport mongoose from 'mongoose'\n\n/** Arguments accepted by the `loginAdmin` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */\nexport interface ILoginAdminArgs {\n\temail: string\n\tpassword: string\n\trememberMe: boolean\n}\n\n/**\n *\n */\nexport const loginAdmin = {\n\tdescription: 'login admin in platform',\n\ttype: new GraphQLNonNull(LoginType),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) },\n\t\tpassword: { type: new GraphQLNonNull(GraphQLString) },\n\t\trememberMe: { type: new GraphQLNonNull(GraphQLBoolean) }\n\t},\n\tasync resolve(_: unknown, args: ILoginAdminArgs, ctx: IContextLogin) {\n\t\tconst { email, password, rememberMe } = args\n\n\t\tconst uEmail = email.toLowerCase().trim()\n\t\tcheckEmailLen(uEmail)\n\t\tcheckPwdLen(password)\n\n\t\tlet accessToken = ''\n\n\t\tconst session = await mongoose.startSession()\n\t\ttry {\n\t\t\tawait session.withTransaction(async () => {\n\t\t\t\t// search if the email exists\n\t\t\t\tconst user = await checkUserAdminLoginAuthorization(uEmail, password, session)\n\t\t\t\t// pwd valid\n\t\t\t\tconst uId = user.userId\n\t\t\t\tawait updateAdminLoginStats(uId, user.lastLogin, rememberMe, session)\n\t\t\t\t// console.debug('[login] lastlogin ok')\n\n\t\t\t\taccessToken = generateAccessToken()\n\t\t\t\tconst refreshToken = generateRefreshToken()\n\t\t\t\tconst accTokenExp = accessTokenExpiry()\n\n\t\t\t\tawait setRedisLoginSession(uId, accessToken, accTokenExp, refreshToken)\n\t\t\t\tsetLoginCookies(ctx, refreshToken)\n\t\t\t})\n\t\t} catch (e: unknown) {\n\t\t\taccessToken = ''\n\t\t\tSentry.captureException(e)\n\t\t\ttryCatchRethrow(e as GraphQLError | Error)\n\t\t} finally {\n\t\t\tawait session.endSession()\n\t\t}\n\n\t\treturn {\n\t\t\taccessToken\n\t\t}\n\t}\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { IContextLogin } from "../context/IContextLogin.mjs";
|
|
2
2
|
import { GraphQLNonNull } from 'graphql';
|
|
3
|
-
|
|
3
|
+
/** Arguments accepted by the `loginRememberme` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */
|
|
4
|
+
export interface ILoginRemembermeArgs {
|
|
4
5
|
email: string;
|
|
5
6
|
password: string;
|
|
6
7
|
rememberMe: boolean;
|
|
@@ -19,8 +20,7 @@ export declare const loginRememberme: {
|
|
|
19
20
|
type: GraphQLNonNull<import("graphql").GraphQLScalarType<boolean, boolean>>;
|
|
20
21
|
};
|
|
21
22
|
};
|
|
22
|
-
resolve(_: unknown, args:
|
|
23
|
+
resolve(_: unknown, args: ILoginRemembermeArgs, ctx: IContextLogin): Promise<{
|
|
23
24
|
accessToken: string;
|
|
24
25
|
}>;
|
|
25
26
|
};
|
|
26
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loginRememberme.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/loginRememberme.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,uCAA8B;AACtD,OAAO,EAAE,WAAW,EAAE,qCAA4B;AAClD,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,gCAAuB;AAC9F,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,2BAA2B,EAAE,kFAAyE;AAC/G,OAAO,EAAE,oBAAoB,EAAE,2EAAkE;AACjG,OAAO,EAAE,0BAA0B,EAAE,iFAAwE;AAC7G,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OA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Content":["import { IContextLogin } from '@context/IContextLogin.mjs'\nimport { checkEmailLen } from '@lib/checkEmailLen.mjs'\nimport { checkPwdLen } from '@lib/checkPwdLen.mjs'\nimport { setLoginCookies } from '@lib/setLoginCookies.mjs'\nimport { accessTokenExpiry, generateAccessToken, generateRefreshToken } from '@lib/tokens.mjs'\nimport { tryCatchRethrow } from '@lib/tryCatchRethrow.mjs'\nimport { checkUserLoginAuthorization } from '@private/graphQL/schema/mutations/checkUserLoginAuthorization.mjs'\nimport { setRedisLoginSession } from '@private/graphQL/schema/mutations/setRedisLoginSession.mjs'\nimport { updateLoginStatsRememberme } from '@private/graphQL/schema/mutations/updateLoginStatsRememberme.mjs'\nimport * as Sentry from '@sentry/node'\nimport { LoginType } from '@stypes/LoginType.mjs'\nimport { GraphQLBoolean, GraphQLError, GraphQLNonNull, GraphQLString } from 'graphql'\nimport mongoose from 'mongoose'\n\n/** Arguments accepted by the `loginRememberme` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */\nexport interface ILoginRemembermeArgs {\n\temail: string\n\tpassword: string\n\trememberMe: boolean\n}\n\nexport const loginRememberme = {\n\tdescription: 'login in platform with remember me flag',\n\ttype: new GraphQLNonNull(LoginType),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) },\n\t\tpassword: { type: new GraphQLNonNull(GraphQLString) },\n\t\trememberMe: { type: new GraphQLNonNull(GraphQLBoolean) }\n\t},\n\tasync resolve(_: unknown, args: ILoginRemembermeArgs, ctx: IContextLogin) {\n\t\tconst { email, password, rememberMe } = args\n\n\t\tconst uEmail = email.toLowerCase().trim()\n\t\tcheckEmailLen(uEmail)\n\t\tcheckPwdLen(password)\n\n\t\tlet accessToken = ''\n\n\t\tconst session = await mongoose.startSession()\n\t\ttry {\n\t\t\tawait session.withTransaction(async () => {\n\t\t\t\t// search if the email exists\n\t\t\t\tconst user = await checkUserLoginAuthorization(uEmail, password, session)\n\t\t\t\t// pwd valid\n\t\t\t\tconst uId = user.userId\n\t\t\t\tawait updateLoginStatsRememberme(uId, user.lastLogin, rememberMe, session)\n\t\t\t\t// console.debug('[login] lastlogin ok')\n\n\t\t\t\taccessToken = generateAccessToken()\n\t\t\t\tconst refreshToken = generateRefreshToken()\n\t\t\t\tconst accTokenExp = accessTokenExpiry()\n\n\t\t\t\tawait setRedisLoginSession(uId, accessToken, accTokenExp, refreshToken)\n\t\t\t\tsetLoginCookies(ctx, refreshToken)\n\t\t\t})\n\t\t} catch (e: unknown) {\n\t\t\taccessToken = ''\n\t\t\tSentry.captureException(e)\n\t\t\ttryCatchRethrow(e as GraphQLError | Error)\n\t\t} finally {\n\t\t\tawait session.endSession()\n\t\t}\n\n\t\treturn { accessToken }\n\t}\n}\n"]}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { TGetResetPwd } from "../../../private/lib/access/db/getResetPwd.mjs";
|
|
2
2
|
import { TSaveResetReq } from "../../../private/lib/access/db/saveResetReq.mjs";
|
|
3
3
|
import { GraphQLNonNull } from 'graphql';
|
|
4
|
-
|
|
4
|
+
/** Arguments accepted by the `resetPwd` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */
|
|
5
|
+
export interface IResetPwdArgs {
|
|
5
6
|
email: string;
|
|
6
7
|
}
|
|
7
8
|
/** Readers and writers the resolver needs, all bound to the same model + paths by the caller. */
|
|
@@ -27,10 +28,9 @@ export declare const createResetPwdMutation: (deps: IResetPwdDeps) => {
|
|
|
27
28
|
type: GraphQLNonNull<import("graphql").GraphQLScalarType<string, string>>;
|
|
28
29
|
};
|
|
29
30
|
};
|
|
30
|
-
resolve(_: unknown, args:
|
|
31
|
+
resolve(_: unknown, args: IResetPwdArgs): Promise<boolean>;
|
|
31
32
|
};
|
|
32
33
|
/** Shape of the bound mutation, for the modules that take it as a dependency. */
|
|
33
34
|
export type TResetPwdMutation = ReturnType<typeof createResetPwdMutation>;
|
|
34
35
|
/** `UserBase`-bound default — the mutation every existing consumer already imports. */
|
|
35
36
|
export declare const resetPwd: TResetPwd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gB;YACnB,CAAC,CAAC,CAAA;QACH,CAAC;QAAC,OAAO,CAAU,EAAE,CAAC;YACrB,eAAe,CAAC,CAAyB,CAAC,CAAA;QAC3C,CAAC;gBAAS,CAAC;YACV,MAAM,OAAO,CAAC,UAAU,EAAE,CAAA;QAC3B,CAAC;QAED,gFAAgF;QAChF,EAAE;QACF,qEAAqE;QACrE,+FAA+F;QAC/F,iGAAiG;QACjG,2FAA2F;QAC3F,+FAA+F;QAC/F,2CAA2C;QAC3C,iGAAiG;QACjG,yFAAyF;QACzF,+FAA+F;QAC/F,gDAAgD;QAChD,+FAA+F;QAC/F,6FAA6F;QAC7F,4FAA4F;QAC5F,uFAAuF;QACvF,+FAA+F;QAC/F,yFAAyF;QACzF,MAAM,IAAI,GAAG,WAAkC,CAAA;QAC/C,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACnB,IAAI,CAAC;gBACJ,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAA;gBACzC,KAAK,aAAa,CAAC,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAU,EAAE,EAAE;oBACpF,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;gBAC3B,CAAC,CAAC,CAAA;YACH,CAAC;YAAC,OAAO,CAAU,EAAE,CAAC;gBACrB,oEAAoE;gBACpE,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;YAC3B,CAAC;QACF,CAAC;QAED,OAAO,IAAI,CAAA;IACZ,CAAC;CACD,CAAC,CAAA;AAKF,uFAAuF;AACvF,MAAM,CAAC,MAAM,QAAQ,GAAsB,sBAAsB,CAAC,EAAE,WAAW,EAAE,YAAY,EAAE,CAAC,CAAA","sourcesContent":["import { SocketLabsLib } from '@email/SocketLabsLib.mjs'\nimport { checkEmailLen } from '@lib/checkEmailLen.mjs'\nimport { EMAIL_HASH_LEN } from '@lib/Constants.mjs'\nimport { DateLib } from '@lib/DateLib.mjs'\nimport { StringLib } from '@lib/StringLib.mjs'\nimport { tryCatchRethrow } from '@lib/tryCatchRethrow.mjs'\nimport { getResetPwd, TGetResetPwd } from '@private/lib/access/db/getResetPwd.mjs'\nimport { saveResetReq, TSaveResetReq } from '@private/lib/access/db/saveResetReq.mjs'\nimport * as Sentry from '@sentry/node'\nimport { GraphQLBoolean, GraphQLError, GraphQLNonNull, GraphQLString } from 'graphql'\nimport mongoose from 'mongoose'\n\ninterface IArgs {\n\temail: string\n}\n\n/** What the committed transaction leaves behind for the post-commit send. */\ninterface IPendingMail {\n\thash: string\n\tname: string\n}\n\n/** Readers and writers the resolver needs, all bound to the same model + paths by the caller. */\nexport interface IResetPwdDeps {\n\tgetResetPwd: TGetResetPwd\n\tsaveResetReq: TSaveResetReq\n}\n\n/**\n * Take the user email and send an email with a link for change the password.\n * for privacy, true is returned whatever happens: unknown address, first request, or a request\n * throttled because the previous one is less than 10 minutes old. The caller can never tell them apart,\n * and the mail is queued after the commit without being awaited so the response time does not tell\n * them apart either.\n *\n * The collection and field layout live entirely in the injected reader/writer, so the same resolver\n * serves any account model. Nothing about the flow itself changes.\n */\nexport const createResetPwdMutation = (deps: IResetPwdDeps) => ({\n\tdescription: 'send reset password link',\n\ttype: new GraphQLNonNull(GraphQLBoolean),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) }\n\t},\n\tasync resolve(_: unknown, args: IArgs) {\n\t\tconst { email } = args\n\n\t\tconst uEmail = email.toLowerCase().trim()\n\t\tcheckEmailLen(uEmail)\n\n\t\t// Set only when the transaction actually persisted a fresh reset request. The mail itself goes\n\t\t// out after the commit, and is not awaited — see the block below the transaction.\n\t\tlet pendingMail: IPendingMail | null = null\n\n\t\tconst session = await mongoose.startSession()\n\t\ttry {\n\t\t\tawait session.withTransaction(async () => {\n\t\t\t\t// console.debug('email exists ?')\n\t\t\t\t// email exists? -> retrieve whether there have already been reset requests -> return 4xx ??\n\t\t\t\tconst resetPwdVal = await deps.getResetPwd(session, uEmail)\n\n\t\t\t\t// don't reveal that the email doesn't exist, for privacy\n\t\t\t\tif (resetPwdVal === null) return\n\t\t\t\t// console.debug(\"the email exists\")\n\n\t\t\t\t// ok, email exists\n\n\t\t\t\t// -> wait 10 minutes from the last password reset request\n\n\t\t\t\t// if a password request already exists, read the date in seconds.\n\t\t\t\tconst lastReq = typeof resetPwdVal.resetDateReq !== 'undefined' ? new Date('' + resetPwdVal.resetDateReq) : null\n\n\t\t\t\t// request already made previously?\n\t\t\t\t// send a new email with the recovery link only if the last one is at least 10 minutes old\n\t\t\t\tconst nowDt = new Date()\n\n\t\t\t\t// The throttle is enforced silently. Throwing 429 here answered \"this address is\n\t\t\t\t// registered and has a reset pending\" to an unauthenticated caller, while an unknown\n\t\t\t\t// address returned true — an enumeration oracle for any address that had been through\n\t\t\t\t// this flow. Both cases now return true and send nothing.\n\t\t\t\tlet calculateHash = true\n\t\t\t\tif (lastReq !== null) {\n\t\t\t\t\t// console.debug('reset request already made')\n\t\t\t\t\t// console.debug('previous pwd reset request: ' + lastReq + ' it is now ' + nowDt)\n\t\t\t\t\tcalculateHash = DateLib.minElapsed(lastReq) >= 10\n\t\t\t\t} //else console.debug('first reset request')\n\n\t\t\t\tif (calculateHash) {\n\t\t\t\t\t// generate hash for password reset\n\t\t\t\t\tconst StrObj = new StringLib()\n\t\t\t\t\tconst hash = StrObj.randomString(EMAIL_HASH_LEN)\n\n\t\t\t\t\t// set hash and current reset date\n\t\t\t\t\tawait deps.saveResetReq(session, resetPwdVal._id, nowDt, hash)\n\n\t\t\t\t\t// the mail is queued after the commit, not here\n\t\t\t\t\tpendingMail = { hash, name: resetPwdVal.name }\n\t\t\t\t} // calculateHash\n\t\t\t})\n\t\t} catch (e: unknown) {\n\t\t\ttryCatchRethrow(e as GraphQLError | Error)\n\t\t} finally {\n\t\t\tawait session.endSession()\n\t\t}\n\n\t\t// Reached only on a committed transaction: tryCatchRethrow above always throws.\n\t\t//\n\t\t// Three reasons this send sits here and is deliberately not awaited:\n\t\t// 1. Timing. Awaiting a network round-trip to SocketLabs made the response measurably slower\n\t\t// exactly when the address was registered and not throttled — the same fact the removed 429\n\t\t// used to state outright. What is left is one extra updateOne, ~a millisecond against\n\t\t// internet jitter, and the 10-minute throttle caps an attacker at one sample per address,\n\t\t// so there is nothing to average out.\n\t\t// 2. Retries. session.withTransaction re-runs its callback on a transient error. With the send\n\t\t// inside, a retried commit mailed the user a second link and invalidated the first.\n\t\t// 3. Failure disclosure. A SocketLabs outage used to surface as a 500, which only an address\n\t\t// that actually exists could ever receive.\n\t\t// The cost is that a delivery failure is now visible in Sentry only, mail in flight is lost if\n\t\t// the process is killed before the request settles, and — the one a user feels — the hash is\n\t\t// already committed when the send fails, so the 10-minute throttle is armed for a link that\n\t\t// never arrived. Clearing resetHash/resetDateReq from the catch below to undo that was\n\t\t// considered and rejected: a rejected promise does not prove non-delivery, and a timeout after\n\t\t// SocketLabs accepted the message would kill a link already sitting in the user's inbox.\n\t\tconst mail = pendingMail as IPendingMail | null\n\t\tif (mail !== null) {\n\t\t\ttry {\n\t\t\t\tconst SocketLabsObj = new SocketLabsLib()\n\t\t\t\tvoid SocketLabsObj.sendEmailReset(uEmail, mail.hash, mail.name).catch((e: unknown) => {\n\t\t\t\t\tSentry.captureException(e)\n\t\t\t\t})\n\t\t\t} catch (e: unknown) {\n\t\t\t\t// the constructor, or a synchronous throw before the promise exists\n\t\t\t\tSentry.captureException(e)\n\t\t\t}\n\t\t}\n\n\t\treturn true\n\t}\n})\n\n/** Shape of the bound mutation, for the modules that take it as a dependency. */\nexport type TResetPwdMutation = ReturnType<typeof createResetPwdMutation>\n\n/** `UserBase`-bound default — the mutation every existing consumer already imports. */\nexport const resetPwd: TResetPwdMutation = createResetPwdMutation({ getResetPwd, saveResetReq })\n"]}
|
|
1
|
+
{"version":3,"file":"resetPwd.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/resetPwd.mts"],"names":[],"mapp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MAAM,OAAO,CAAC,UAAU,EAAE,CAAA;QAC3B,CAAC;QAED,gFAAgF;QAChF,EAAE;QACF,qEAAqE;QACrE,+FAA+F;QAC/F,iGAAiG;QACjG,2FAA2F;QAC3F,+FAA+F;QAC/F,2CAA2C;QAC3C,iGAAiG;QACjG,yFAAyF;QACzF,+FAA+F;QAC/F,gDAAgD;QAChD,+FAA+F;QAC/F,6FAA6F;QAC7F,4FAA4F;QAC5F,uFAAuF;QACvF,+FAA+F;QAC/F,yFAAyF;QACzF,MAAM,IAAI,GAAG,WAAkC,CAAA;QAC/C,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACnB,IAAI,CAAC;gBACJ,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAA;gBACzC,KAAK,aAAa,CAAC,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAU,EAAE,EAAE;oBACpF,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;gBAC3B,CAAC,CAAC,CAAA;YACH,CAAC;YAAC,OAAO,CAAU,EAAE,CAAC;gBACrB,oEAAoE;gBACpE,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;YAC3B,CAAC;QACF,CAAC;QAED,OAAO,IAAI,CAAA;IACZ,CAAC;CACD,CAAC,CAAA;AAKF,uFAAuF;AACvF,MAAM,CAAC,MAAM,QAAQ,GAAsB,sBAAsB,CAAC,EAAE,WAAW,EAAE,YAAY,EAAE,CAAC,CAAA","sourcesContent":["import { SocketLabsLib } from '@email/SocketLabsLib.mjs'\nimport { checkEmailLen } from '@lib/checkEmailLen.mjs'\nimport { EMAIL_HASH_LEN } from '@lib/Constants.mjs'\nimport { DateLib } from '@lib/DateLib.mjs'\nimport { StringLib } from '@lib/StringLib.mjs'\nimport { tryCatchRethrow } from '@lib/tryCatchRethrow.mjs'\nimport { getResetPwd, TGetResetPwd } from '@private/lib/access/db/getResetPwd.mjs'\nimport { saveResetReq, TSaveResetReq } from '@private/lib/access/db/saveResetReq.mjs'\nimport * as Sentry from '@sentry/node'\nimport { GraphQLBoolean, GraphQLError, GraphQLNonNull, GraphQLString } from 'graphql'\nimport mongoose from 'mongoose'\n\n/** Arguments accepted by the `resetPwd` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */\nexport interface IResetPwdArgs {\n\temail: string\n}\n\n/** What the committed transaction leaves behind for the post-commit send. */\ninterface IPendingMail {\n\thash: string\n\tname: string\n}\n\n/** Readers and writers the resolver needs, all bound to the same model + paths by the caller. */\nexport interface IResetPwdDeps {\n\tgetResetPwd: TGetResetPwd\n\tsaveResetReq: TSaveResetReq\n}\n\n/**\n * Take the user email and send an email with a link for change the password.\n * for privacy, true is returned whatever happens: unknown address, first request, or a request\n * throttled because the previous one is less than 10 minutes old. The caller can never tell them apart,\n * and the mail is queued after the commit without being awaited so the response time does not tell\n * them apart either.\n *\n * The collection and field layout live entirely in the injected reader/writer, so the same resolver\n * serves any account model. Nothing about the flow itself changes.\n */\nexport const createResetPwdMutation = (deps: IResetPwdDeps) => ({\n\tdescription: 'send reset password link',\n\ttype: new GraphQLNonNull(GraphQLBoolean),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) }\n\t},\n\tasync resolve(_: unknown, args: IResetPwdArgs) {\n\t\tconst { email } = args\n\n\t\tconst uEmail = email.toLowerCase().trim()\n\t\tcheckEmailLen(uEmail)\n\n\t\t// Set only when the transaction actually persisted a fresh reset request. The mail itself goes\n\t\t// out after the commit, and is not awaited — see the block below the transaction.\n\t\tlet pendingMail: IPendingMail | null = null\n\n\t\tconst session = await mongoose.startSession()\n\t\ttry {\n\t\t\tawait session.withTransaction(async () => {\n\t\t\t\t// console.debug('email exists ?')\n\t\t\t\t// email exists? -> retrieve whether there have already been reset requests -> return 4xx ??\n\t\t\t\tconst resetPwdVal = await deps.getResetPwd(session, uEmail)\n\n\t\t\t\t// don't reveal that the email doesn't exist, for privacy\n\t\t\t\tif (resetPwdVal === null) return\n\t\t\t\t// console.debug(\"the email exists\")\n\n\t\t\t\t// ok, email exists\n\n\t\t\t\t// -> wait 10 minutes from the last password reset request\n\n\t\t\t\t// if a password request already exists, read the date in seconds.\n\t\t\t\tconst lastReq = typeof resetPwdVal.resetDateReq !== 'undefined' ? new Date('' + resetPwdVal.resetDateReq) : null\n\n\t\t\t\t// request already made previously?\n\t\t\t\t// send a new email with the recovery link only if the last one is at least 10 minutes old\n\t\t\t\tconst nowDt = new Date()\n\n\t\t\t\t// The throttle is enforced silently. Throwing 429 here answered \"this address is\n\t\t\t\t// registered and has a reset pending\" to an unauthenticated caller, while an unknown\n\t\t\t\t// address returned true — an enumeration oracle for any address that had been through\n\t\t\t\t// this flow. Both cases now return true and send nothing.\n\t\t\t\tlet calculateHash = true\n\t\t\t\tif (lastReq !== null) {\n\t\t\t\t\t// console.debug('reset request already made')\n\t\t\t\t\t// console.debug('previous pwd reset request: ' + lastReq + ' it is now ' + nowDt)\n\t\t\t\t\tcalculateHash = DateLib.minElapsed(lastReq) >= 10\n\t\t\t\t} //else console.debug('first reset request')\n\n\t\t\t\tif (calculateHash) {\n\t\t\t\t\t// generate hash for password reset\n\t\t\t\t\tconst StrObj = new StringLib()\n\t\t\t\t\tconst hash = StrObj.randomString(EMAIL_HASH_LEN)\n\n\t\t\t\t\t// set hash and current reset date\n\t\t\t\t\tawait deps.saveResetReq(session, resetPwdVal._id, nowDt, hash)\n\n\t\t\t\t\t// the mail is queued after the commit, not here\n\t\t\t\t\tpendingMail = { hash, name: resetPwdVal.name }\n\t\t\t\t} // calculateHash\n\t\t\t})\n\t\t} catch (e: unknown) {\n\t\t\ttryCatchRethrow(e as GraphQLError | Error)\n\t\t} finally {\n\t\t\tawait session.endSession()\n\t\t}\n\n\t\t// Reached only on a committed transaction: tryCatchRethrow above always throws.\n\t\t//\n\t\t// Three reasons this send sits here and is deliberately not awaited:\n\t\t// 1. Timing. Awaiting a network round-trip to SocketLabs made the response measurably slower\n\t\t// exactly when the address was registered and not throttled — the same fact the removed 429\n\t\t// used to state outright. What is left is one extra updateOne, ~a millisecond against\n\t\t// internet jitter, and the 10-minute throttle caps an attacker at one sample per address,\n\t\t// so there is nothing to average out.\n\t\t// 2. Retries. session.withTransaction re-runs its callback on a transient error. With the send\n\t\t// inside, a retried commit mailed the user a second link and invalidated the first.\n\t\t// 3. Failure disclosure. A SocketLabs outage used to surface as a 500, which only an address\n\t\t// that actually exists could ever receive.\n\t\t// The cost is that a delivery failure is now visible in Sentry only, mail in flight is lost if\n\t\t// the process is killed before the request settles, and — the one a user feels — the hash is\n\t\t// already committed when the send fails, so the 10-minute throttle is armed for a link that\n\t\t// never arrived. Clearing resetHash/resetDateReq from the catch below to undo that was\n\t\t// considered and rejected: a rejected promise does not prove non-delivery, and a timeout after\n\t\t// SocketLabs accepted the message would kill a link already sitting in the user's inbox.\n\t\tconst mail = pendingMail as IPendingMail | null\n\t\tif (mail !== null) {\n\t\t\ttry {\n\t\t\t\tconst SocketLabsObj = new SocketLabsLib()\n\t\t\t\tvoid SocketLabsObj.sendEmailReset(uEmail, mail.hash, mail.name).catch((e: unknown) => {\n\t\t\t\t\tSentry.captureException(e)\n\t\t\t\t})\n\t\t\t} catch (e: unknown) {\n\t\t\t\t// the constructor, or a synchronous throw before the promise exists\n\t\t\t\tSentry.captureException(e)\n\t\t\t}\n\t\t}\n\n\t\treturn true\n\t}\n})\n\n/** Shape of the bound mutation, for the modules that take it as a dependency. */\nexport type TResetPwdMutation = ReturnType<typeof createResetPwdMutation>\n\n/** `UserBase`-bound default — the mutation every existing consumer already imports. */\nexport const resetPwd: TResetPwdMutation = createResetPwdMutation({ getResetPwd, saveResetReq })\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { GraphQLNonNull } from 'graphql';
|
|
2
|
-
|
|
2
|
+
/** Arguments accepted by the `signUp` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */
|
|
3
|
+
export interface ISignUpArgs {
|
|
3
4
|
email: string;
|
|
4
5
|
password: string;
|
|
5
6
|
}
|
|
@@ -14,6 +15,5 @@ export declare const signUp: {
|
|
|
14
15
|
type: GraphQLNonNull<import("graphql").GraphQLScalarType<string, string>>;
|
|
15
16
|
};
|
|
16
17
|
};
|
|
17
|
-
resolve(_: unknown, args:
|
|
18
|
+
resolve(_: unknown, args: ISignUpArgs): Promise<boolean>;
|
|
18
19
|
};
|
|
19
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"signUp.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/signUp.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,yCAAgC;AACxD,OAAO,EAAE,aAAa,EAAE,uCAA8B;AACtD,OAAO,EAAE,WAAW,EAAE,qCAA4B;AAClD,OAAO,EAAE,eAAe,EAAE,4CAAmC;AAC7D,OAAO,EAAE,SAAS,EAAE,sCAA6B;AACjD,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,2CAAqC;AAClE,OAAO,EAAE,cAAc,EAAgB,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACrF,OAAO,QAAQ,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"signUp.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/signUp.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,yCAAgC;AACxD,OAAO,EAAE,aAAa,EAAE,uCAA8B;AACtD,OAAO,EAAE,WAAW,EAAE,qCAA4B;AAClD,OAAO,EAAE,eAAe,EAAE,4CAAmC;AAC7D,OAAO,EAAE,SAAS,EAAE,sCAA6B;AACjD,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,2CAAqC;AAClE,OAAO,EAAE,cAAc,EAAgB,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA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gBAAgB,CAAC,CAAA;YAC9D,CAAC,CAAC,CAAA;QACH,CAAC;QAAC,OAAO,CAAU,EAAE,CAAC;YACrB,eAAe,CAAC,CAAyB,CAAC,CAAA;QAC3C,CAAC;gBAAS,CAAC;YACV,MAAM,OAAO,CAAC,UAAU,EAAE,CAAA;QAC3B,CAAC;QAED,OAAO,IAAI,CAAA;IACZ,CAAC;CACD,CAAA","sourcesContent":["import { SocketLabsLib } from '@email/SocketLabsLib.mjs'\nimport { checkEmailLen } from '@lib/checkEmailLen.mjs'\nimport { checkPwdLen } from '@lib/checkPwdLen.mjs'\nimport { registerNewUser } from '@lib/db/registerNewUser.mjs'\nimport { userExist } from '@lib/db/userExist.mjs'\nimport { tryCatchRethrow } from '@lib/tryCatchRethrow.mjs'\nimport { throwConflictError } from '@throw/throwConflictError.mjs'\nimport { GraphQLBoolean, GraphQLError, GraphQLNonNull, GraphQLString } from 'graphql'\nimport mongoose from 'mongoose'\n\n/** Arguments accepted by the `signUp` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */\nexport interface ISignUpArgs {\n\temail: string\n\tpassword: string\n}\n\nexport const signUp = {\n\tdescription: 'Sign Up',\n\ttype: new GraphQLNonNull(GraphQLBoolean),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) },\n\t\tpassword: { type: new GraphQLNonNull(GraphQLString) }\n\t},\n\tasync resolve(_: unknown, args: ISignUpArgs) {\n\t\tconst { email, password } = args\n\n\t\tconst uEmail = email.toLowerCase().trim()\n\t\tcheckEmailLen(uEmail)\n\t\tcheckPwdLen(password)\n\n\t\tconst session = await mongoose.startSession()\n\t\tlet uExist = false\n\t\ttry {\n\t\t\tawait session.withTransaction(async () => {\n\t\t\t\t/********************************************\n\t\t\t\t * First, search if the user does not exist\n\t\t\t\t */\n\t\t\t\tuExist = await userExist(uEmail, session)\n\t\t\t\tif (uExist) {\n\t\t\t\t\tconst SocketLabsObj = new SocketLabsLib()\n\t\t\t\t\tawait SocketLabsObj.emailAlreadyValid(uEmail)\n\t\t\t\t\tthrow throwConflictError()\n\t\t\t\t}\n\n\t\t\t\t/*********************************************\n\t\t\t\t * ok, new user. create user obj for DB. generate hash password\n\t\t\t\t */\n\t\t\t\tconst hashConfirmEmail = await registerNewUser(uEmail, password, session)\n\n\t\t\t\t/************************************\n\t\t\t\t * send email\n\t\t\t\t */\n\t\t\t\tconst SocketLabsObj = new SocketLabsLib()\n\t\t\t\tawait SocketLabsObj.sendEmailVerify(uEmail, hashConfirmEmail)\n\t\t\t})\n\t\t} catch (e: unknown) {\n\t\t\ttryCatchRethrow(e as GraphQLError | Error)\n\t\t} finally {\n\t\t\tawait session.endSession()\n\t\t}\n\n\t\treturn true\n\t}\n}\n"]}
|
|
@@ -2,7 +2,8 @@ import { TGetResetPwd } from "../../../private/lib/access/db/getResetPwd.mjs";
|
|
|
2
2
|
import { TRemoveResetReq } from "../../../private/lib/access/db/removeResetReq.mjs";
|
|
3
3
|
import { TUpdatePasswordDb } from "../../../private/lib/access/db/updatePasswordDb.mjs";
|
|
4
4
|
import { GraphQLNonNull } from 'graphql';
|
|
5
|
-
|
|
5
|
+
/** Arguments accepted by the `updatePassword` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */
|
|
6
|
+
export interface IUpdatePasswordArgs {
|
|
6
7
|
email: string;
|
|
7
8
|
hash: string;
|
|
8
9
|
password: string;
|
|
@@ -34,10 +35,9 @@ export declare const createUpdatePasswordMutation: (deps: IUpdatePasswordDeps) =
|
|
|
34
35
|
type: GraphQLNonNull<import("graphql").GraphQLScalarType<string, string>>;
|
|
35
36
|
};
|
|
36
37
|
};
|
|
37
|
-
resolve(_: unknown, args:
|
|
38
|
+
resolve(_: unknown, args: IUpdatePasswordArgs): Promise<boolean>;
|
|
38
39
|
};
|
|
39
40
|
/** Shape of the bound mutation, for the modules that take it as a dependency. */
|
|
40
41
|
export type TUpdatePasswordMutation = ReturnType<typeof createUpdatePasswordMutation>;
|
|
41
42
|
/** `UserBase`-bound default — the mutation every existing consumer already imports. */
|
|
42
43
|
export declare const updatePassword: TUpdatePasswordMutation;
|
|
43
|
-
export {};
|
|
@@ -11,6 +11,52 @@ import { throwForbiddenError } from "../../throw/throwForbiddenError.mjs";
|
|
|
11
11
|
import { throwInternalError } from "../../throw/throwInternalError.mjs";
|
|
12
12
|
import { GraphQLBoolean, GraphQLNonNull, GraphQLString } from 'graphql';
|
|
13
13
|
import mongoose from 'mongoose';
|
|
14
|
+
/**
|
|
15
|
+
* The transactional core: validate the reset hash, apply the new password and clear the request.
|
|
16
|
+
* Extracted from resolve() only to keep it inside the max-lines-per-function budget — behaviour
|
|
17
|
+
* unchanged. Returns the account holder's name so the caller can mail the post-commit confirmation.
|
|
18
|
+
*/
|
|
19
|
+
async function applyPasswordReset(deps, session, input) {
|
|
20
|
+
const { uEmail, hash, password } = input;
|
|
21
|
+
const resetPwd = await deps.getResetPwd(session, uEmail);
|
|
22
|
+
// check if email is present in db
|
|
23
|
+
if (resetPwd === null) {
|
|
24
|
+
throw throwForbiddenError(); // don't reveal whether the email is present, for privacy
|
|
25
|
+
}
|
|
26
|
+
// console.debug('--email present')
|
|
27
|
+
// No usable reset state. This is NOT an internal error as far as the caller is concerned:
|
|
28
|
+
// answering 500 here while an unknown address answers 403 turns this mutation into an
|
|
29
|
+
// account-enumeration oracle, because every registered account that has never requested a
|
|
30
|
+
// reset lands on this branch (getResetPwd yields resetHash === null whenever
|
|
31
|
+
// account.resetDateReq is undefined). Same 403 as the unknown-address path; the orphan
|
|
32
|
+
// state is reported to Sentry instead of to the caller.
|
|
33
|
+
if (resetPwd.resetHash === null) {
|
|
34
|
+
Sentry.captureMessage(`[updatePassword] no usable resetHash for ${uEmail}`);
|
|
35
|
+
throw throwForbiddenError();
|
|
36
|
+
}
|
|
37
|
+
/* c8 ignore next 4 -- defensive guard, resetDateReq cannot be null when resetHash is set */
|
|
38
|
+
if (resetPwd.resetDateReq === null) {
|
|
39
|
+
Sentry.captureMessage(`[updatePassword] resetHash set but resetDateReq missing for ${uEmail}`);
|
|
40
|
+
throw throwForbiddenError();
|
|
41
|
+
}
|
|
42
|
+
// check if hash is missing or invalid
|
|
43
|
+
if (resetPwd.resetHash !== hash) {
|
|
44
|
+
throw throwForbiddenError(); // don't reveal whether the email is present, for privacy
|
|
45
|
+
} // else console.debug('--hash valid')
|
|
46
|
+
// check if the request was made within 1 hour
|
|
47
|
+
const dt1 = new Date('' + resetPwd.resetDateReq);
|
|
48
|
+
if (DateLib.minElapsed(dt1) > 60) {
|
|
49
|
+
throw throwForbiddenError(); // The link is no longer valid
|
|
50
|
+
} // else console.debug('--link valid')
|
|
51
|
+
const update = await deps.updatePasswordDb(session, resetPwd._id, password);
|
|
52
|
+
if (!update) {
|
|
53
|
+
throw throwInternalError(); // "System error while updating the password."
|
|
54
|
+
} // else console.debug('--pwd updated')
|
|
55
|
+
// delete password reset request data from db
|
|
56
|
+
await deps.removeResetReq(session, uEmail);
|
|
57
|
+
// the confirmation email is sent after the commit, not here
|
|
58
|
+
return resetPwd.name;
|
|
59
|
+
}
|
|
14
60
|
/**
|
|
15
61
|
* Change the password of an account holding a valid, unexpired reset hash.
|
|
16
62
|
*
|
|
@@ -37,44 +83,7 @@ export const createUpdatePasswordMutation = (deps) => ({
|
|
|
37
83
|
const session = await mongoose.startSession();
|
|
38
84
|
try {
|
|
39
85
|
await session.withTransaction(async () => {
|
|
40
|
-
|
|
41
|
-
// check if email is present in db
|
|
42
|
-
if (resetPwd === null) {
|
|
43
|
-
throw throwForbiddenError(); // don't reveal whether the email is present, for privacy
|
|
44
|
-
}
|
|
45
|
-
// console.debug('--email present')
|
|
46
|
-
// No usable reset state. This is NOT an internal error as far as the caller is concerned:
|
|
47
|
-
// answering 500 here while an unknown address answers 403 turns this mutation into an
|
|
48
|
-
// account-enumeration oracle, because every registered account that has never requested a
|
|
49
|
-
// reset lands on this branch (getResetPwd yields resetHash === null whenever
|
|
50
|
-
// account.resetDateReq is undefined). Same 403 as the unknown-address path; the orphan
|
|
51
|
-
// state is reported to Sentry instead of to the caller.
|
|
52
|
-
if (resetPwd.resetHash === null) {
|
|
53
|
-
Sentry.captureMessage(`[updatePassword] no usable resetHash for ${uEmail}`);
|
|
54
|
-
throw throwForbiddenError();
|
|
55
|
-
}
|
|
56
|
-
/* c8 ignore next 4 -- defensive guard, resetDateReq cannot be null when resetHash is set */
|
|
57
|
-
if (resetPwd.resetDateReq === null) {
|
|
58
|
-
Sentry.captureMessage(`[updatePassword] resetHash set but resetDateReq missing for ${uEmail}`);
|
|
59
|
-
throw throwForbiddenError();
|
|
60
|
-
}
|
|
61
|
-
// check if hash is missing or invalid
|
|
62
|
-
if (resetPwd.resetHash !== hash) {
|
|
63
|
-
throw throwForbiddenError(); // don't reveal whether the email is present, for privacy
|
|
64
|
-
} // else console.debug('--hash valid')
|
|
65
|
-
// check if the request was made within 1 hour
|
|
66
|
-
const dt1 = new Date('' + resetPwd.resetDateReq);
|
|
67
|
-
if (DateLib.minElapsed(dt1) > 60) {
|
|
68
|
-
throw throwForbiddenError(); // The link is no longer valid
|
|
69
|
-
} // else console.debug('--link valid')
|
|
70
|
-
const update = await deps.updatePasswordDb(session, resetPwd._id, password);
|
|
71
|
-
if (!update) {
|
|
72
|
-
throw throwInternalError(); // "System error while updating the password."
|
|
73
|
-
} // else console.debug('--pwd updated')
|
|
74
|
-
// delete password reset request data from db
|
|
75
|
-
await deps.removeResetReq(session, uEmail);
|
|
76
|
-
// the confirmation email is sent after the commit, not here
|
|
77
|
-
confirmTo = resetPwd.name;
|
|
86
|
+
confirmTo = await applyPasswordReset(deps, session, { uEmail, hash, password });
|
|
78
87
|
});
|
|
79
88
|
}
|
|
80
89
|
catch (e) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updatePassword.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/updatePassword.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,yCAAgC;AACxD,OAAO,EAAE,aAAa,EAAE,uCAA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AAW;IACX,gBAAgB;IAChB,cAAc;CACd,CAAC,CAAA","sourcesContent":["import { SocketLabsLib } from '@email/SocketLabsLib.mjs'\nimport { checkEmailLen } from '@lib/checkEmailLen.mjs'\nimport { checkPwdLen } from '@lib/checkPwdLen.mjs'\nimport { DateLib } from '@lib/DateLib.mjs'\nimport { tryCatchRethrow } from '@lib/tryCatchRethrow.mjs'\nimport { getResetPwd, TGetResetPwd } from '@private/lib/access/db/getResetPwd.mjs'\nimport removeResetReq, { TRemoveResetReq } from '@private/lib/access/db/removeResetReq.mjs'\nimport updatePasswordDb, { TUpdatePasswordDb } from '@private/lib/access/db/updatePasswordDb.mjs'\nimport * as Sentry from '@sentry/node'\nimport { throwForbiddenError } from '@throw/throwForbiddenError.mjs'\nimport { throwInternalError } from '@throw/throwInternalError.mjs'\nimport { GraphQLBoolean, GraphQLError, GraphQLNonNull, GraphQLString } from 'graphql'\nimport mongoose from 'mongoose'\n\ninterface IArgs {\n\temail: string\n\thash: string\n\tpassword: string\n}\n\n/** Readers and writers the resolver needs, all bound to the same model + paths by the caller. */\nexport interface IUpdatePasswordDeps {\n\tgetResetPwd: TGetResetPwd\n\tupdatePasswordDb: TUpdatePasswordDb\n\tremoveResetReq: TRemoveResetReq\n}\n\n/**\n * Change the password of an account holding a valid, unexpired reset hash.\n *\n * The collection and field layout live entirely in the injected reader/writers, so the same resolver\n * serves any account model. In particular the cleanup unsets whatever `resetClear` names, which is not\n * necessarily the two leaf paths this resolver reads — see `removeResetReq`.\n */\nexport const createUpdatePasswordMutation = (deps: IUpdatePasswordDeps) => ({\n\tdescription: \"changes the user's password\",\n\ttype: new GraphQLNonNull(GraphQLBoolean),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) },\n\t\thash: { type: new GraphQLNonNull(GraphQLString) },\n\t\tpassword: { type: new GraphQLNonNull(GraphQLString) }\n\t},\n\tasync resolve(_: unknown, args: IArgs) {\n\t\tconst { email, hash, password } = args\n\n\t\tconst uEmail = email.trim().toLowerCase()\n\t\tcheckEmailLen(uEmail)\n\t\tcheckPwdLen(password)\n\n\t\t// Set only once the transaction has actually persisted the new password. Carries the user's\n\t\t// name to the post-commit confirmation email.\n\t\tlet confirmTo: string | null = null\n\n\t\tconst session = await mongoose.startSession()\n\n\t\ttry {\n\t\t\tawait session.withTransaction(async () => {\n\t\t\t\tconst resetPwd = await deps.getResetPwd(session, uEmail)\n\n\t\t\t\t// check if email is present in db\n\t\t\t\tif (resetPwd === null) {\n\t\t\t\t\tthrow throwForbiddenError() // don't reveal whether the email is present, for privacy\n\t\t\t\t}\n\t\t\t\t// console.debug('--email present')\n\n\t\t\t\t// No usable reset state. This is NOT an internal error as far as the caller is concerned:\n\t\t\t\t// answering 500 here while an unknown address answers 403 turns this mutation into an\n\t\t\t\t// account-enumeration oracle, because every registered account that has never requested a\n\t\t\t\t// reset lands on this branch (getResetPwd yields resetHash === null whenever\n\t\t\t\t// account.resetDateReq is undefined). Same 403 as the unknown-address path; the orphan\n\t\t\t\t// state is reported to Sentry instead of to the caller.\n\t\t\t\tif (resetPwd.resetHash === null) {\n\t\t\t\t\tSentry.captureMessage(`[updatePassword] no usable resetHash for ${uEmail}`)\n\t\t\t\t\tthrow throwForbiddenError()\n\t\t\t\t}\n\t\t\t\t/* c8 ignore next 4 -- defensive guard, resetDateReq cannot be null when resetHash is set */\n\t\t\t\tif (resetPwd.resetDateReq === null) {\n\t\t\t\t\tSentry.captureMessage(`[updatePassword] resetHash set but resetDateReq missing for ${uEmail}`)\n\t\t\t\t\tthrow throwForbiddenError()\n\t\t\t\t}\n\n\t\t\t\t// check if hash is missing or invalid\n\t\t\t\tif (resetPwd.resetHash !== hash) {\n\t\t\t\t\tthrow throwForbiddenError() // don't reveal whether the email is present, for privacy\n\t\t\t\t} // else console.debug('--hash valid')\n\n\t\t\t\t// check if the request was made within 1 hour\n\t\t\t\tconst dt1 = new Date('' + resetPwd.resetDateReq)\n\t\t\t\tif (DateLib.minElapsed(dt1) > 60) {\n\t\t\t\t\tthrow throwForbiddenError() // The link is no longer valid\n\t\t\t\t} // else console.debug('--link valid')\n\n\t\t\t\tconst update = await deps.updatePasswordDb(session, resetPwd._id, password)\n\t\t\t\tif (!update) {\n\t\t\t\t\tthrow throwInternalError() // \"System error while updating the password.\"\n\t\t\t\t} // else console.debug('--pwd updated')\n\n\t\t\t\t// delete password reset request data from db\n\t\t\t\tawait deps.removeResetReq(session, uEmail)\n\n\t\t\t\t// the confirmation email is sent after the commit, not here\n\t\t\t\tconfirmTo = resetPwd.name\n\t\t\t})\n\t\t} catch (e: unknown) {\n\t\t\ttryCatchRethrow(e as GraphQLError | Error)\n\t\t} finally {\n\t\t\tawait session.endSession()\n\t\t}\n\n\t\t// Reached only on a committed transaction: tryCatchRethrow above always throws.\n\t\t//\n\t\t// The send sits here rather than inside the callback because session.withTransaction re-runs\n\t\t// its callback on a transient error, and a retried commit therefore mailed the user a second\n\t\t// \"your password was changed\" notice. It is still awaited — unlike resetPwd there is no timing\n\t\t// oracle to close here, since reaching this point at all requires a valid reset hash.\n\t\t//\n\t\t// A delivery failure no longer fails the request. It used to abort the transaction and answer\n\t\t// 500, rolling back the password change with it — consistent, but it made the mail provider a\n\t\t// hard dependency: with SocketLabs down, nobody could complete a reset at all. The password is\n\t\t// committed by the time we get here, so the failure goes to Sentry and the caller still gets\n\t\t// true. The confirmation is a notice, not part of the operation.\n\t\t//\n\t\t// Don't \"restore\" the abort: there is no transaction left to abort at this point. Rethrowing\n\t\t// would report 500 for a password that is already live, and compensating would mean writing\n\t\t// back the old bcrypt hash, which this flow never reads. Moving the send back inside the\n\t\t// callback brings back the duplicate notice AND lets a mail go out for a commit that then\n\t\t// fails — an email cannot be recalled.\n\t\tconst name = confirmTo as string | null\n\t\tif (name !== null) {\n\t\t\ttry {\n\t\t\t\tconst SocketLabsObj = new SocketLabsLib()\n\t\t\t\tawait SocketLabsObj.sendResetPwdConfirmation(uEmail, name)\n\t\t\t} catch (e: unknown) {\n\t\t\t\tSentry.captureException(e)\n\t\t\t}\n\t\t}\n\n\t\treturn true\n\t}\n})\n\n/** Shape of the bound mutation, for the modules that take it as a dependency. */\nexport type TUpdatePasswordMutation = ReturnType<typeof createUpdatePasswordMutation>\n\n/** `UserBase`-bound default — the mutation every existing consumer already imports. */\nexport const updatePassword: TUpdatePasswordMutation = createUpdatePasswordMutation({\n\tgetResetPwd,\n\tupdatePasswordDb,\n\tremoveResetReq\n})\n"]}
|
|
1
|
+
{"version":3,"file":"updatePassword.mjs","sourceRoot":"","sources":["../../../../src/graphQL/schema/mutations/updatePassword.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,yCAAgC;AACxD,OAAO,EAAE,aAAa,EAAE,uCAA8B;AACtD,OAAO,EAAE,WAAW,EAAE,qCAA4B;AAClD,OAAO,EAAE,OAAO,EAAE,iCAAwB;AAC1C,OAAO,EAAE,eAAe,EAAE,yCAAgC;AAC1D,OAAO,EAAE,WAAW,EAAgB,uDAA8C;AAClF,OAAO,cAAmC,0DAAiD;AAC3F,OAAO,gBAAuC,4DAAmD;AACjG,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,mBAAmB,EAAE,4CAAsC;AACpE,OAAO,EAAE,kBAAkB,EAAE,2CAAqC;AAClE,OAAO,EAAE,cAAc,EAAgB,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACrF,OAAO,QAA2B,MAAM,UAAU,CAAA;AAuBlD;;;;GAIG;AACH,KAAK,UAAU,kBAAkB,CAChC,IAAyB,EACzB,OAAsB,EACtB,KAA2B;IAE3B,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAA;IACxC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IAExD,kCAAkC;IAClC,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACvB,MAAM,mBAAmB,EAAE,CAAA,CA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F,6FAA6F;QAC7F,+FAA+F;QAC/F,sFAAsF;QACtF,EAAE;QACF,8FAA8F;QAC9F,8FAA8F;QAC9F,+FAA+F;QAC/F,6FAA6F;QAC7F,iEAAiE;QACjE,EAAE;QACF,6FAA6F;QAC7F,4FAA4F;QAC5F,yFAAyF;QACzF,0FAA0F;QAC1F,uCAAuC;QACvC,MAAM,IAAI,GAAG,SAA0B,CAAA;QACvC,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACnB,IAAI,CAAC;gBACJ,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAA;gBACzC,MAAM,aAAa,CAAC,wBAAwB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;YAC3D,CAAC;YAAC,OAAO,CAAU,EAAE,CAAC;gBACrB,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAA;YAC3B,CAAC;QACF,CAAC;QAED,OAAO,IAAI,CAAA;IACZ,CAAC;CACD,CAAC,CAAA;AAKF,uFAAuF;AACvF,MAAM,CAAC,MAAM,cAAc,GAA4B,4BAA4B,CAAC;IACnF,WAAW;IACX,gBAAgB;IAChB,cAAc;CACd,CAAC,CAAA","sourcesContent":["import { SocketLabsLib } from '@email/SocketLabsLib.mjs'\nimport { checkEmailLen } from '@lib/checkEmailLen.mjs'\nimport { checkPwdLen } from '@lib/checkPwdLen.mjs'\nimport { DateLib } from '@lib/DateLib.mjs'\nimport { tryCatchRethrow } from '@lib/tryCatchRethrow.mjs'\nimport { getResetPwd, TGetResetPwd } from '@private/lib/access/db/getResetPwd.mjs'\nimport removeResetReq, { TRemoveResetReq } from '@private/lib/access/db/removeResetReq.mjs'\nimport updatePasswordDb, { TUpdatePasswordDb } from '@private/lib/access/db/updatePasswordDb.mjs'\nimport * as Sentry from '@sentry/node'\nimport { throwForbiddenError } from '@throw/throwForbiddenError.mjs'\nimport { throwInternalError } from '@throw/throwInternalError.mjs'\nimport { GraphQLBoolean, GraphQLError, GraphQLNonNull, GraphQLString } from 'graphql'\nimport mongoose, { ClientSession } from 'mongoose'\n\n/** Arguments accepted by the `updatePassword` mutation. Exported so a consumer re-exporting the bound mutation can name its type. */\nexport interface IUpdatePasswordArgs {\n\temail: string\n\thash: string\n\tpassword: string\n}\n\n/** Readers and writers the resolver needs, all bound to the same model + paths by the caller. */\nexport interface IUpdatePasswordDeps {\n\tgetResetPwd: TGetResetPwd\n\tupdatePasswordDb: TUpdatePasswordDb\n\tremoveResetReq: TRemoveResetReq\n}\n\n/** Normalised inputs the transaction body works on, bundled to keep it within the max-params budget. */\ninterface IUpdatePasswordInput {\n\tuEmail: string\n\thash: string\n\tpassword: string\n}\n\n/**\n * The transactional core: validate the reset hash, apply the new password and clear the request.\n * Extracted from resolve() only to keep it inside the max-lines-per-function budget — behaviour\n * unchanged. Returns the account holder's name so the caller can mail the post-commit confirmation.\n */\nasync function applyPasswordReset(\n\tdeps: IUpdatePasswordDeps,\n\tsession: ClientSession,\n\tinput: IUpdatePasswordInput\n): Promise<string> {\n\tconst { uEmail, hash, password } = input\n\tconst resetPwd = await deps.getResetPwd(session, uEmail)\n\n\t// check if email is present in db\n\tif (resetPwd === null) {\n\t\tthrow throwForbiddenError() // don't reveal whether the email is present, for privacy\n\t}\n\t// console.debug('--email present')\n\n\t// No usable reset state. This is NOT an internal error as far as the caller is concerned:\n\t// answering 500 here while an unknown address answers 403 turns this mutation into an\n\t// account-enumeration oracle, because every registered account that has never requested a\n\t// reset lands on this branch (getResetPwd yields resetHash === null whenever\n\t// account.resetDateReq is undefined). Same 403 as the unknown-address path; the orphan\n\t// state is reported to Sentry instead of to the caller.\n\tif (resetPwd.resetHash === null) {\n\t\tSentry.captureMessage(`[updatePassword] no usable resetHash for ${uEmail}`)\n\t\tthrow throwForbiddenError()\n\t}\n\t/* c8 ignore next 4 -- defensive guard, resetDateReq cannot be null when resetHash is set */\n\tif (resetPwd.resetDateReq === null) {\n\t\tSentry.captureMessage(`[updatePassword] resetHash set but resetDateReq missing for ${uEmail}`)\n\t\tthrow throwForbiddenError()\n\t}\n\n\t// check if hash is missing or invalid\n\tif (resetPwd.resetHash !== hash) {\n\t\tthrow throwForbiddenError() // don't reveal whether the email is present, for privacy\n\t} // else console.debug('--hash valid')\n\n\t// check if the request was made within 1 hour\n\tconst dt1 = new Date('' + resetPwd.resetDateReq)\n\tif (DateLib.minElapsed(dt1) > 60) {\n\t\tthrow throwForbiddenError() // The link is no longer valid\n\t} // else console.debug('--link valid')\n\n\tconst update = await deps.updatePasswordDb(session, resetPwd._id, password)\n\tif (!update) {\n\t\tthrow throwInternalError() // \"System error while updating the password.\"\n\t} // else console.debug('--pwd updated')\n\n\t// delete password reset request data from db\n\tawait deps.removeResetReq(session, uEmail)\n\n\t// the confirmation email is sent after the commit, not here\n\treturn resetPwd.name\n}\n\n/**\n * Change the password of an account holding a valid, unexpired reset hash.\n *\n * The collection and field layout live entirely in the injected reader/writers, so the same resolver\n * serves any account model. In particular the cleanup unsets whatever `resetClear` names, which is not\n * necessarily the two leaf paths this resolver reads — see `removeResetReq`.\n */\nexport const createUpdatePasswordMutation = (deps: IUpdatePasswordDeps) => ({\n\tdescription: \"changes the user's password\",\n\ttype: new GraphQLNonNull(GraphQLBoolean),\n\targs: {\n\t\temail: { type: new GraphQLNonNull(GraphQLString) },\n\t\thash: { type: new GraphQLNonNull(GraphQLString) },\n\t\tpassword: { type: new GraphQLNonNull(GraphQLString) }\n\t},\n\tasync resolve(_: unknown, args: IUpdatePasswordArgs) {\n\t\tconst { email, hash, password } = args\n\n\t\tconst uEmail = email.trim().toLowerCase()\n\t\tcheckEmailLen(uEmail)\n\t\tcheckPwdLen(password)\n\n\t\t// Set only once the transaction has actually persisted the new password. Carries the user's\n\t\t// name to the post-commit confirmation email.\n\t\tlet confirmTo: string | null = null\n\n\t\tconst session = await mongoose.startSession()\n\n\t\ttry {\n\t\t\tawait session.withTransaction(async () => {\n\t\t\t\tconfirmTo = await applyPasswordReset(deps, session, { uEmail, hash, password })\n\t\t\t})\n\t\t} catch (e: unknown) {\n\t\t\ttryCatchRethrow(e as GraphQLError | Error)\n\t\t} finally {\n\t\t\tawait session.endSession()\n\t\t}\n\n\t\t// Reached only on a committed transaction: tryCatchRethrow above always throws.\n\t\t//\n\t\t// The send sits here rather than inside the callback because session.withTransaction re-runs\n\t\t// its callback on a transient error, and a retried commit therefore mailed the user a second\n\t\t// \"your password was changed\" notice. It is still awaited — unlike resetPwd there is no timing\n\t\t// oracle to close here, since reaching this point at all requires a valid reset hash.\n\t\t//\n\t\t// A delivery failure no longer fails the request. It used to abort the transaction and answer\n\t\t// 500, rolling back the password change with it — consistent, but it made the mail provider a\n\t\t// hard dependency: with SocketLabs down, nobody could complete a reset at all. The password is\n\t\t// committed by the time we get here, so the failure goes to Sentry and the caller still gets\n\t\t// true. The confirmation is a notice, not part of the operation.\n\t\t//\n\t\t// Don't \"restore\" the abort: there is no transaction left to abort at this point. Rethrowing\n\t\t// would report 500 for a password that is already live, and compensating would mean writing\n\t\t// back the old bcrypt hash, which this flow never reads. Moving the send back inside the\n\t\t// callback brings back the duplicate notice AND lets a mail go out for a commit that then\n\t\t// fails — an email cannot be recalled.\n\t\tconst name = confirmTo as string | null\n\t\tif (name !== null) {\n\t\t\ttry {\n\t\t\t\tconst SocketLabsObj = new SocketLabsLib()\n\t\t\t\tawait SocketLabsObj.sendResetPwdConfirmation(uEmail, name)\n\t\t\t} catch (e: unknown) {\n\t\t\t\tSentry.captureException(e)\n\t\t\t}\n\t\t}\n\n\t\treturn true\n\t}\n})\n\n/** Shape of the bound mutation, for the modules that take it as a dependency. */\nexport type TUpdatePasswordMutation = ReturnType<typeof createUpdatePasswordMutation>\n\n/** `UserBase`-bound default — the mutation every existing consumer already imports. */\nexport const updatePassword: TUpdatePasswordMutation = createUpdatePasswordMutation({\n\tgetResetPwd,\n\tupdatePasswordDb,\n\tremoveResetReq\n})\n"]}
|
|
@@ -28,6 +28,10 @@ export interface IResetPwdPaths {
|
|
|
28
28
|
resetDateReq: string;
|
|
29
29
|
/** Password-reset token. Must stay disjoint from the email-verification hash slot. */
|
|
30
30
|
resetHash: string;
|
|
31
|
+
/** Account tombstone flag. A set value makes `getResetPwd` answer `null`, as for an unknown address. */
|
|
32
|
+
deleted: string;
|
|
33
|
+
/** Account lockout flag. A set value makes `getResetPwd` answer `null`, as for an unknown address. */
|
|
34
|
+
disabled: string;
|
|
31
35
|
/**
|
|
32
36
|
* Paths `removeResetReq` `$unset`s once a reset has been consumed.
|
|
33
37
|
*
|
|
@@ -5,6 +5,8 @@ export const DEFAULT_RESET_PWD_PATHS = Object.freeze({
|
|
|
5
5
|
name: 'personalData.name',
|
|
6
6
|
resetDateReq: 'account.resetDateReq',
|
|
7
7
|
resetHash: 'account.resetHash',
|
|
8
|
+
deleted: 'account.deleted',
|
|
9
|
+
disabled: 'account.disabled',
|
|
8
10
|
resetClear: Object.freeze(['account.resetDateReq', 'account.resetHash'])
|
|
9
11
|
});
|
|
10
12
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accessPaths.mjs","sourceRoot":"","sources":["../../../src/lib/access/accessPaths.mts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"accessPaths.mjs","sourceRoot":"","sources":["../../../src/lib/access/accessPaths.mts"],"names":[],"mappings":"AAmDA,8FAA8F;AAC9F,MAAM,CAAC,MAAM,uBAAuB,GAAmB,MAAM,CAAC,MAAM,CAAC;IACpE,KAAK,EAAE,aAAa;IACpB,QAAQ,EAAE,gBAAgB;IAC1B,IAAI,EAAE,mBAAmB;IACzB,YAAY,EAAE,sBAAsB;IACpC,SAAS,EAAE,mBAAmB;IAC9B,OAAO,EAAE,iBAAiB;IAC1B,QAAQ,EAAE,kBAAkB;IAC5B,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,sBAAsB,EAAE,mBAAmB,CAAC,CAAC;CACxE,CAAC,CAAA;AAEF;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAA+B;IACnE,OAAO,EAAE,GAAG,uBAAuB,EAAE,GAAG,KAAK,EAAE,CAAA;AAChD,CAAC;AAkCD,8FAA8F;AAC9F,MAAM,CAAC,MAAM,0BAA0B,GAAsB,MAAM,CAAC,MAAM,CAAC;IAC1E,KAAK,EAAE,aAAa;IACpB,KAAK,EAAE,qBAAqB;IAC5B,IAAI,EAAE,oBAAoB;IAC1B,WAAW,EAAE,2BAA2B;IACxC,YAAY,EAAE,4BAA4B;IAC1C,WAAW,EAAE,2BAA2B;IACxC,OAAO,EAAE,iBAAiB;IAC1B,QAAQ,EAAE,kBAAkB;IAC5B,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,oBAAoB,EAAE,2BAA2B,EAAE,4BAA4B,CAAC,CAAC;IAC7G,gBAAgB,EAAE,MAAM,CAAC,MAAM,CAAC;QAC/B,oBAAoB;QACpB,2BAA2B;QAC3B,4BAA4B;QAC5B,2BAA2B;KAC3B,CAAC;CACF,CAAC,CAAA;AAEF;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAAkC;IACzE,OAAO,EAAE,GAAG,0BAA0B,EAAE,GAAG,KAAK,EAAE,CAAA;AACnD,CAAC","sourcesContent":["import { Model } from 'mongoose'\n\n/**\n * Any mongoose model an access flow can be pointed at.\n *\n * Deliberately `Model<any>`: the flows only ever call `findOne`, `updateOne`, `countDocuments` and\n * `deleteOne` with computed field paths, so nothing here can be typed against a concrete document\n * shape without forcing every consumer to describe theirs. The `paths` map below is what pins the\n * contract instead — a wrong path is a runtime no-op, which is why the defaults are exported and the\n * flows are tested against them.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type TAccessModel = Model<any>\n\n/**\n * Field paths the password-reset flow reads and writes.\n *\n * Every value is a dotted mongo path into the account document. They are supplied as data rather\n * than hard-coded because `UserBase` is only one possible layout: a consumer whose accounts live in\n * another collection, or under another field tree, could not use `resetPwd` / `updatePassword` at\n * all while the paths were baked in.\n */\nexport interface IResetPwdPaths {\n\t/** Login address. Used as the lookup filter by `getResetPwd` and `removeResetReq`. */\n\temail: string\n\t/** Bcrypt hash slot `updatePassword` overwrites. */\n\tpassword: string\n\t/** Display name passed to the reset / confirmation emails. Missing value ⇒ empty string. */\n\tname: string\n\t/** When the pending reset was requested. Drives the 10-minute throttle and the 60-minute expiry. */\n\tresetDateReq: string\n\t/** Password-reset token. Must stay disjoint from the email-verification hash slot. */\n\tresetHash: string\n\t/** Account tombstone flag. A set value makes `getResetPwd` answer `null`, as for an unknown address. */\n\tdeleted: string\n\t/** Account lockout flag. A set value makes `getResetPwd` answer `null`, as for an unknown address. */\n\tdisabled: string\n\t/**\n\t * Paths `removeResetReq` `$unset`s once a reset has been consumed.\n\t *\n\t * Kept separate from `resetDateReq` / `resetHash` on purpose — it is NOT always the same list.\n\t * A layout that stores the request as one all-or-nothing subdocument, e.g.\n\t * `resetPwd: { resetDateReq, resetHash }` under `validationLevel: 'strict'` +\n\t * `validationAction: 'error'`, rejects a write that unsets a single member: the leftover document\n\t * fails validation. The only legal cleanup there is `$unset: { resetPwd: '' }` — one container\n\t * path, not two leaf paths. Deriving this list from the leaves would make that layout impossible\n\t * to express, and the flat default hides the problem completely.\n\t */\n\tresetClear: readonly string[]\n}\n\n/** The layout of `UserBase` — i.e. the behaviour of the flow before it took a `paths` map. */\nexport const DEFAULT_RESET_PWD_PATHS: IResetPwdPaths = Object.freeze({\n\temail: 'login.email',\n\tpassword: 'login.password',\n\tname: 'personalData.name',\n\tresetDateReq: 'account.resetDateReq',\n\tresetHash: 'account.resetHash',\n\tdeleted: 'account.deleted',\n\tdisabled: 'account.disabled',\n\tresetClear: Object.freeze(['account.resetDateReq', 'account.resetHash'])\n})\n\n/**\n * Merge a partial override over {@link DEFAULT_RESET_PWD_PATHS}.\n *\n * Plain spread: a key present with an explicit `undefined` value overrides the default with\n * `undefined`, so pass only the keys being changed.\n */\nexport function resolveResetPwdPaths(paths?: Partial<IResetPwdPaths>): IResetPwdPaths {\n\treturn { ...DEFAULT_RESET_PWD_PATHS, ...paths }\n}\n\n/**\n * Field paths the email-verification and email-change flows read and write.\n *\n * Same rationale as {@link IResetPwdPaths}. Note that `hash` here is the inbox-proof token\n * (activation and email change, 3-day life, `requestTimes` throttle) and must never be pointed at\n * the same field as `IResetPwdPaths.resetHash`: while the two shared one slot, a hash issued by\n * either flow authenticated the other, and an unauthenticated reset request killed pending\n * activation links.\n */\nexport interface IVerifyEmailPaths {\n\t/** Live login address — lookup filter, and the field `confirmNewEmail` writes on an email change. */\n\temail: string\n\t/** Boolean flag `enableEmailAccess` flips once a verification link is honoured. */\n\tvalid: string\n\t/** Verification / email-change token. Never the password-reset slot. */\n\thash: string\n\t/** When the current token was issued. Drives the 3-day link window. */\n\tdateLastReq: string\n\t/** Strike counter. Five wrong hashes delete the account. */\n\trequestTimes: string\n\t/** Address awaiting confirmation during an email change. */\n\tnewEmailTmp: string\n\t/** Account tombstone flag. */\n\tdeleted: string\n\t/** Account lockout flag. */\n\tdisabled: string\n\t/** Paths `enableEmailAccess` `$unset`s once a verification link is honoured. */\n\tverifyClear: readonly string[]\n\t/** Paths `confirmNewEmail` `$unset`s once a new address is accepted. */\n\temailChangeClear: readonly string[]\n}\n\n/** The layout of `UserBase` — i.e. the behaviour of the flow before it took a `paths` map. */\nexport const DEFAULT_VERIFY_EMAIL_PATHS: IVerifyEmailPaths = Object.freeze({\n\temail: 'login.email',\n\tvalid: 'account.email.valid',\n\thash: 'account.email.hash',\n\tdateLastReq: 'account.email.dateLastReq',\n\trequestTimes: 'account.email.requestTimes',\n\tnewEmailTmp: 'account.email.newEmailTmp',\n\tdeleted: 'account.deleted',\n\tdisabled: 'account.disabled',\n\tverifyClear: Object.freeze(['account.email.hash', 'account.email.dateLastReq', 'account.email.requestTimes']),\n\temailChangeClear: Object.freeze([\n\t\t'account.email.hash',\n\t\t'account.email.dateLastReq',\n\t\t'account.email.requestTimes',\n\t\t'account.email.newEmailTmp'\n\t])\n})\n\n/**\n * Merge a partial override over {@link DEFAULT_VERIFY_EMAIL_PATHS}.\n *\n * Plain spread: a key present with an explicit `undefined` value overrides the default with\n * `undefined`, so pass only the keys being changed.\n */\nexport function resolveVerifyEmailPaths(paths?: Partial<IVerifyEmailPaths>): IVerifyEmailPaths {\n\treturn { ...DEFAULT_VERIFY_EMAIL_PATHS, ...paths }\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
/** Payload accepted by `logGlobalError`. Exported so a consumer re-exporting the helper can name its type. */
|
|
2
|
+
export interface IGlobalError {
|
|
2
3
|
message: string;
|
|
3
4
|
stackArr: Array<string>;
|
|
4
5
|
}
|
|
5
6
|
export declare const logGlobalError: (log: IGlobalError) => void;
|
|
6
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logGlobalError.mjs","sourceRoot":"","sources":["../../../../src/lib/db/log/logGlobalError.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,+DAA8C;
|
|
1
|
+
{"version":3,"file":"logGlobalError.mjs","sourceRoot":"","sources":["../../../../src/lib/db/log/logGlobalError.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,+DAA8C;AAQvE,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,GAAiB;IACxD,IAAI,GAAG,CAAC,OAAO,KAAK,EAAE,IAAI,GAAG,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,OAAM;IAE3D,MAAM,cAAc,GAAG,IAAI,cAAc,CAAC,EAAE,CAAC,CAAA;IAC7C,IAAI,GAAG,CAAC,OAAO,KAAK,EAAE;QAAE,cAAc,CAAC,CAAC,GAAG,GAAG,CAAC,OAAO,CAAA;IACtD,IAAI,GAAG,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,cAAc,CAAC,CAAC,GAAG,GAAG,CAAC,QAAQ,CAAA;IAE9D,+BAA+B;AAChC,CAAC,CAAA","sourcesContent":["import { LogGlobalError } from '@models/MongoDB/log/LogGlobalError.mjs'\n\n/** Payload accepted by `logGlobalError`. Exported so a consumer re-exporting the helper can name its type. */\nexport interface IGlobalError {\n\tmessage: string\n\tstackArr: Array<string>\n}\n\nexport const logGlobalError = function (log: IGlobalError) {\n\tif (log.message === '' && log.stackArr.length === 0) return\n\n\tconst newGlobalError = new LogGlobalError({})\n\tif (log.message !== '') newGlobalError.m = log.message\n\tif (log.stackArr.length !== 0) newGlobalError.s = log.stackArr\n\n\t// return newGlobalError.save()\n}\n"]}
|
|
@@ -5,11 +5,25 @@ export const createGetResetPwd = (model, paths) => async function getResetPwd(se
|
|
|
5
5
|
let ret = null;
|
|
6
6
|
const queryRet = await model
|
|
7
7
|
.findOne({ [paths.email]: email })
|
|
8
|
-
.select(buildProjection([paths.name, paths.resetDateReq, paths.resetHash]))
|
|
8
|
+
.select(buildProjection([paths.name, paths.resetDateReq, paths.resetHash, paths.deleted, paths.disabled]))
|
|
9
9
|
.session(session)
|
|
10
10
|
.lean();
|
|
11
11
|
// if a reset request is found
|
|
12
12
|
if (queryRet !== null) {
|
|
13
|
+
// A tombstoned or locked-out account is not a reset target: without this, resetPwd mailed a
|
|
14
|
+
// live link to a deleted address and updatePassword overwrote the bcrypt hash of a disabled
|
|
15
|
+
// one, so re-enabling the account handed it back with an attacker-chosen password. `null` is
|
|
16
|
+
// the same answer an unknown address gets — resetPwd returns true and sends nothing,
|
|
17
|
+
// updatePassword answers the same 403 — so no new enumeration oracle is opened.
|
|
18
|
+
//
|
|
19
|
+
// The flags are read raw, exactly as assertVerifyEmailAllowed reads them. This is a .lean()
|
|
20
|
+
// read, so Mongoose casting never runs and these are what the driver found: a boolean once
|
|
21
|
+
// scripts/migrate-account-disabled-to-boolean.mjs has run. On un-migrated data a stored
|
|
22
|
+
// 'false' is a truthy string and blocks the reset; the fix is the migration, not a coercion
|
|
23
|
+
// here.
|
|
24
|
+
if (readPath(queryRet, paths.deleted) || readPath(queryRet, paths.disabled)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
13
27
|
const resetDateReq = readPath(queryRet, paths.resetDateReq);
|
|
14
28
|
let resetHash = null;
|
|
15
29
|
if (typeof resetDateReq !== 'undefined') {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getResetPwd.mjs","sourceRoot":"","sources":["../../../../../src/private/lib/access/db/getResetPwd.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAgC,+CAAmC;AACnG,OAAO,EAAE,QAAQ,EAAE,wDAAoC;AACvD,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,yBAAyC;AAW7E,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,KAAmB,EAAE,KAAqB,EAAE,EAAE,CAC/E,KAAK,UAAU,WAAW,CAAC,OAAsB,EAAE,KAAa;IAC/D,IAAI,GAAG,GAA2B,IAAI,CAAA;IAEtC,MAAM,QAAQ,GAAG,MAAM,KAAK;SAC1B,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC;SACjC,MAAM,CAAC,eAAe,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,YAAY,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"getResetPwd.mjs","sourceRoot":"","sources":["../../../../../src/private/lib/access/db/getResetPwd.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAgC,+CAAmC;AACnG,OAAO,EAAE,QAAQ,EAAE,wDAAoC;AACvD,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,yBAAyC;AAW7E,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,KAAmB,EAAE,KAAqB,EAAE,EAAE,CAC/E,KAAK,UAAU,WAAW,CAAC,OAAsB,EAAE,KAAa;IAC/D,IAAI,GAAG,GAA2B,IAAI,CAAA;IAEtC,MAAM,QAAQ,GAAG,MAAM,KAAK;SAC1B,OAAO,CAAC,EAAE,CAAC,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CAAC,IAAI,CAAwB,IAAI,EAAE;SAClE,CAAA;IACF,CAAC;IACD,OAAO,GAAG,CAAA;AACX,CAAC,CAAA;AAKF,wFAAwF;AACxF,MAAM,CAAC,MAAM,WAAW,GAAiB,iBAAiB,CAAC,QAAQ,EAAE,uBAAuB,CAAC,CAAA","sourcesContent":["import { DEFAULT_RESET_PWD_PATHS, IResetPwdPaths, TAccessModel } from '@lib/access/accessPaths.mjs'\nimport { UserBase } from '@models/MongoDB/UserBase.mjs'\nimport { buildProjection, readPath } from '@private/lib/access/pathTools.mjs'\nimport { ClientSession, Types } from 'mongoose'\n\n/** What the reset flow needs about an account, flattened out of whatever layout `paths` describes. */\nexport interface IResetPwdRecord {\n\t_id: Types.ObjectId\n\tresetDateReq: Date | undefined\n\tresetHash: string | null\n\tname: string\n}\n\nexport const createGetResetPwd = (model: TAccessModel, paths: IResetPwdPaths) =>\n\tasync function getResetPwd(session: ClientSession, email: string): Promise<IResetPwdRecord | null> {\n\t\tlet ret: IResetPwdRecord | null = null\n\n\t\tconst queryRet = await model\n\t\t\t.findOne({ [paths.email]: email })\n\t\t\t.select(buildProjection([paths.name, paths.resetDateReq, paths.resetHash, paths.deleted, paths.disabled]))\n\t\t\t.session(session)\n\t\t\t.lean()\n\n\t\t// if a reset request is found\n\t\tif (queryRet !== null) {\n\t\t\t// A tombstoned or locked-out account is not a reset target: without this, resetPwd mailed a\n\t\t\t// live link to a deleted address and updatePassword overwrote the bcrypt hash of a disabled\n\t\t\t// one, so re-enabling the account handed it back with an attacker-chosen password. `null` is\n\t\t\t// the same answer an unknown address gets — resetPwd returns true and sends nothing,\n\t\t\t// updatePassword answers the same 403 — so no new enumeration oracle is opened.\n\t\t\t//\n\t\t\t// The flags are read raw, exactly as assertVerifyEmailAllowed reads them. This is a .lean()\n\t\t\t// read, so Mongoose casting never runs and these are what the driver found: a boolean once\n\t\t\t// scripts/migrate-account-disabled-to-boolean.mjs has run. On un-migrated data a stored\n\t\t\t// 'false' is a truthy string and blocks the reset; the fix is the migration, not a coercion\n\t\t\t// here.\n\t\t\tif (readPath(queryRet, paths.deleted) || readPath(queryRet, paths.disabled)) {\n\t\t\t\treturn null\n\t\t\t}\n\n\t\t\tconst resetDateReq = readPath(queryRet, paths.resetDateReq) as Date | undefined\n\t\t\tlet resetHash: string | null = null\n\n\t\t\tif (typeof resetDateReq !== 'undefined') {\n\t\t\t\tconst storedHash = readPath(queryRet, paths.resetHash)\n\n\t\t\t\t// Never stringify, and never fall back to the email-verification hash slot. '' + undefined\n\t\t\t\t// yields the literal string \"undefined\", which passed updatePassword's null check and then\n\t\t\t\t// matched a caller sending that same literal as the hash argument — a reset with no secret\n\t\t\t\t// at all. The two fields must also stay disjoint: reading the verification slot here is\n\t\t\t\t// what let a hash issued by one flow authenticate the other. Anything but a stored string\n\t\t\t\t// => null, which updatePassword rejects with the same 403 it gives an unknown address.\n\t\t\t\tif (typeof storedHash === 'string') {\n\t\t\t\t\tresetHash = storedHash\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tret = {\n\t\t\t\t_id: queryRet._id as Types.ObjectId,\n\t\t\t\tresetDateReq,\n\t\t\t\tresetHash,\n\t\t\t\tname: (readPath(queryRet, paths.name) as string | undefined) || ''\n\t\t\t}\n\t\t}\n\t\treturn ret\n\t}\n\n/** Signature of the bound reader, for the modules that take it as a dependency. */\nexport type TGetResetPwd = ReturnType<typeof createGetResetPwd>\n\n/** `UserBase`-bound default — the behaviour every existing consumer already imports. */\nexport const getResetPwd: TGetResetPwd = createGetResetPwd(UserBase, DEFAULT_RESET_PWD_PATHS)\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axiumine/koa-utils",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.4.1",
|
|
4
4
|
"description": "Koa Utils",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"test:coverage": "yarn build && yarn build:tests && c8 mocha",
|
|
30
30
|
"qodana": "yarn test:coverage && mkdir -p .qodana/results && docker run --rm --env-file .env -v \"$PWD\":/data/project -v \"$PWD\"/.qodana/results:/data/results jetbrains/qodana-js:2026.1 --show-report",
|
|
31
31
|
"qodana:cli": "yarn test:coverage && set -a && . ./.env && set +a && qodana scan --show-report",
|
|
32
|
+
"reindex": "node .gitnexus/run.cjs analyze --no-stats",
|
|
32
33
|
"semgrep": "./scripts/semgrep.sh",
|
|
33
34
|
"semgrep:all": "./scripts/semgrep.sh --registry",
|
|
34
35
|
"semgrep:canary": "./scripts/semgrep-canary.sh",
|