@flowerforce/flowerbase 1.0.3-beta.5 → 1.0.3-beta.6
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/dist/auth/plugins/jwt.d.ts.map +1 -1
- package/dist/auth/plugins/jwt.js +2 -1
- package/dist/auth/utils.d.ts.map +1 -1
- package/dist/auth/utils.js +9 -0
- package/dist/shared/handleUserRegistration.d.ts.map +1 -1
- package/dist/shared/handleUserRegistration.js +2 -1
- package/dist/utils/crypto/index.d.ts.map +1 -1
- package/dist/utils/crypto/index.js +3 -0
- package/package.json +1 -1
- package/src/auth/plugins/jwt.ts +3 -0
- package/src/auth/utils.ts +15 -0
- package/src/shared/handleUserRegistration.ts +3 -1
- package/src/utils/crypto/index.ts +5 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../../src/auth/plugins/jwt.ts"],"names":[],"mappings":"AAIA,KAAK,OAAO,GAAG;IACb,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED;;;;;;;GAOG;iUAC8C,OAAO;AAAxD,
|
|
1
|
+
{"version":3,"file":"jwt.d.ts","sourceRoot":"","sources":["../../../src/auth/plugins/jwt.ts"],"names":[],"mappings":"AAIA,KAAK,OAAO,GAAG;IACb,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED;;;;;;;GAOG;iUAC8C,OAAO;AAAxD,wBAwDE"}
|
package/dist/auth/plugins/jwt.js
CHANGED
|
@@ -43,10 +43,11 @@ exports.default = (0, fastify_plugin_1.default)(function (fastify, opts) {
|
|
|
43
43
|
fastify.decorate('createAccessToken', function (user) {
|
|
44
44
|
const id = user._id.toString();
|
|
45
45
|
const userDataId = user.user_data._id.toString();
|
|
46
|
-
const user_data = Object.assign({ _id: userDataId, id: userDataId }, user.user_data);
|
|
46
|
+
const user_data = Object.assign({ _id: userDataId, id: userDataId, email: user.email }, user.user_data);
|
|
47
47
|
return this.jwt.sign({
|
|
48
48
|
typ: 'access',
|
|
49
49
|
id,
|
|
50
|
+
data: user_data,
|
|
50
51
|
user_data: user_data,
|
|
51
52
|
custom_data: user_data
|
|
52
53
|
}, {
|
package/dist/auth/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/auth/utils.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY;;;;;;;;;;;;;CASxB,CAAA;AAED,eAAO,MAAM,YAAY;;;;;;;;;;;;;CASxB,CAAA;AAED,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;CAUhC,CAAA;AAED,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;CAS/B,CAAA;AAED,oBAAY,cAAc;IACxB,KAAK,WAAW;IAChB,YAAY,cAAc;IAC1B,OAAO,aAAa;IACpB,OAAO,aAAa;IACpB,KAAK,gBAAgB;IACrB,aAAa,WAAW;IACxB,UAAU,sBAAsB;CACjC;AAED,oBAAY,WAAW;IACrB,mBAAmB,wBAAwB;IAC3C,aAAa,mCAAmC;IAChD,oBAAoB,sCAAsC;CAC3D;AAED,MAAM,WAAW,UAAU;IACzB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,SAAS,EAAE,MAAM,CAAA;IACjB,gBAAgB,EAAE,aAAa,CAAA;IAC/B,iBAAiB,EAAE,cAAc,CAAA;CAClC;AAED,UAAU,MAAM;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;CAClB;AACD,UAAU,aAAa;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;CACf;AAED,UAAU,cAAc;IACtB,IAAI,EAAE,iBAAiB,CAAC;IACxB,IAAI,EAAE,iBAAiB,CAAC;IACxB,QAAQ,EAAE,O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rB,8BAA8B,EAAE,MAAM,CAAA;CACvC;AAED,eAAO,MAAM,aAAa,mBAAmB,CAAA;AAE7C;;;GAGG;AACH,eAAO,MAAM,cAAc,QAAO,UAGjC,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,kBAAkB,QAAO,oBAGrC,CAAA;AAED,eAAO,MAAM,aAAa,GACxB,qBAAqB,MAAM,EAC3B,OAAO,MAAM,EACb,SAAS,MAAM;;;;;CA2DhB,CAAA"}
|
package/dist/auth/utils.js
CHANGED
|
@@ -88,10 +88,19 @@ const getMailConfig = (resetPasswordConfig, token, tokenId) => {
|
|
|
88
88
|
const ENV_PREFIX = 'ENV';
|
|
89
89
|
const { from, subject, mailToken } = mailConfig;
|
|
90
90
|
const [fromPrefix, fromPath] = from.split('.');
|
|
91
|
+
if (!fromPath) {
|
|
92
|
+
throw new Error(`Invalid fromPath: ${fromPath}`);
|
|
93
|
+
}
|
|
91
94
|
const currentSender = (_a = (fromPrefix === ENV_PREFIX ? process.env[fromPath] : from)) !== null && _a !== void 0 ? _a : '';
|
|
92
95
|
const [subjectPrefix, subjectPath] = subject.split('.');
|
|
96
|
+
if (!subjectPath) {
|
|
97
|
+
throw new Error(`Invalid subjectPath: ${subjectPath}`);
|
|
98
|
+
}
|
|
93
99
|
const currentSubject = (_b = (subjectPrefix === ENV_PREFIX ? process.env[subjectPath] : subject)) !== null && _b !== void 0 ? _b : '';
|
|
94
100
|
const [mailTokenPrefix, mailTokenPath] = mailToken.split('.');
|
|
101
|
+
if (!mailTokenPath) {
|
|
102
|
+
throw new Error(`Invalid mailTokenPath: ${mailTokenPath}`);
|
|
103
|
+
}
|
|
95
104
|
const currentMailToken = (_c = (mailTokenPrefix === 'ENV' ? process.env[mailTokenPath] : mailToken)) !== null && _c !== void 0 ? _c : '';
|
|
96
105
|
const link = `${resetPasswordUrl}/${token}/${tokenId}`;
|
|
97
106
|
const body = `<body style="font-family: Arial, sans-serif; background-color: #f4f4f4; text-align: center; padding: 20px;">
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleUserRegistration.d.ts","sourceRoot":"","sources":["../../src/shared/handleUserRegistration.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"handleUserRegistration.d.ts","sourceRoot":"","sources":["../../src/shared/handleUserRegistration.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAA;AAE9E;;;;;;GAMG;AACH,QAAA,MAAM,sBAAsB,EAAE,sBAkD7B,CAAA;AAED,eAAe,sBAAsB,CAAA"}
|
|
@@ -25,7 +25,8 @@ const handleUserRegistration = (app, opt) => (_a) => __awaiter(void 0, [_a], voi
|
|
|
25
25
|
throw new Error('only run_as_system');
|
|
26
26
|
}
|
|
27
27
|
const { authCollection } = constants_1.AUTH_CONFIG;
|
|
28
|
-
const
|
|
28
|
+
const mongo = app === null || app === void 0 ? void 0 : app.mongo;
|
|
29
|
+
const db = mongo.client.db(constants_1.DB_NAME);
|
|
29
30
|
const hashedPassword = yield (0, crypto_1.hashPassword)(password);
|
|
30
31
|
const existingUser = yield (db === null || db === void 0 ? void 0 : db.collection(authCollection).findOne({ email }));
|
|
31
32
|
if (existingUser) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/crypto/index.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAU,WAAW,MAAM,oBAInD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAU,WAAW,MAAM,EAAE,gBAAgB,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/crypto/index.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAU,WAAW,MAAM,oBAInD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAU,WAAW,MAAM,EAAE,gBAAgB,MAAM,qBAU9E,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,eAAW,WAExC,CAAA"}
|
|
@@ -35,6 +35,9 @@ exports.hashPassword = hashPassword;
|
|
|
35
35
|
*/
|
|
36
36
|
const comparePassword = (plaintext, storedPassword) => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
37
|
const [storedHash, storedSalt] = storedPassword.split('.');
|
|
38
|
+
if (!storedHash || !storedSalt) {
|
|
39
|
+
throw new Error(`Invalid stored password: ${storedPassword}`);
|
|
40
|
+
}
|
|
38
41
|
const storedBuffer = Buffer.from(storedHash, 'hex');
|
|
39
42
|
const buffer = (yield scrypt(plaintext, storedSalt, 64));
|
|
40
43
|
return node_crypto_1.default.timingSafeEqual(buffer, storedBuffer);
|
package/package.json
CHANGED
package/src/auth/plugins/jwt.ts
CHANGED
|
@@ -37,12 +37,15 @@ export default fp(async function (fastify, opts: Options) {
|
|
|
37
37
|
const user_data = {
|
|
38
38
|
_id: userDataId,
|
|
39
39
|
id: userDataId,
|
|
40
|
+
email: user.email,
|
|
40
41
|
...user.user_data
|
|
41
42
|
}
|
|
43
|
+
|
|
42
44
|
return this.jwt.sign(
|
|
43
45
|
{
|
|
44
46
|
typ: 'access',
|
|
45
47
|
id,
|
|
48
|
+
data: user_data,
|
|
46
49
|
user_data: user_data,
|
|
47
50
|
custom_data: user_data
|
|
48
51
|
},
|
package/src/auth/utils.ts
CHANGED
|
@@ -142,11 +142,26 @@ export const getMailConfig = (
|
|
|
142
142
|
const { from, subject, mailToken } = mailConfig
|
|
143
143
|
|
|
144
144
|
const [fromPrefix, fromPath] = from.split('.')
|
|
145
|
+
|
|
146
|
+
if (!fromPath) {
|
|
147
|
+
throw new Error(`Invalid fromPath: ${fromPath}`)
|
|
148
|
+
}
|
|
149
|
+
|
|
145
150
|
const currentSender = (fromPrefix === ENV_PREFIX ? process.env[fromPath] : from) ?? ''
|
|
146
151
|
const [subjectPrefix, subjectPath] = subject.split('.')
|
|
152
|
+
|
|
153
|
+
if (!subjectPath) {
|
|
154
|
+
throw new Error(`Invalid subjectPath: ${subjectPath}`)
|
|
155
|
+
}
|
|
156
|
+
|
|
147
157
|
const currentSubject =
|
|
148
158
|
(subjectPrefix === ENV_PREFIX ? process.env[subjectPath] : subject) ?? ''
|
|
149
159
|
const [mailTokenPrefix, mailTokenPath] = mailToken.split('.')
|
|
160
|
+
|
|
161
|
+
if (!mailTokenPath) {
|
|
162
|
+
throw new Error(`Invalid mailTokenPath: ${mailTokenPath}`)
|
|
163
|
+
}
|
|
164
|
+
|
|
150
165
|
const currentMailToken =
|
|
151
166
|
(mailTokenPrefix === 'ENV' ? process.env[mailTokenPath] : mailToken) ?? ''
|
|
152
167
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FastifyMongoObject } from "@fastify/mongodb/types"
|
|
1
2
|
import { PROVIDER_TYPE } from "../auth/utils"
|
|
2
3
|
import { AUTH_CONFIG, DB_NAME } from "../constants"
|
|
3
4
|
import { hashPassword } from "../utils/crypto"
|
|
@@ -18,7 +19,8 @@ const handleUserRegistration: HandleUserRegistration = (app, opt) => async ({ em
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
const { authCollection } = AUTH_CONFIG
|
|
21
|
-
const
|
|
22
|
+
const mongo: FastifyMongoObject = app?.mongo
|
|
23
|
+
const db = mongo.client.db(DB_NAME)
|
|
22
24
|
const hashedPassword = await hashPassword(password)
|
|
23
25
|
|
|
24
26
|
const existingUser = await db?.collection(authCollection!).findOne({ email })
|
|
@@ -22,6 +22,11 @@ export const hashPassword = async (plaintext: string) => {
|
|
|
22
22
|
*/
|
|
23
23
|
export const comparePassword = async (plaintext: string, storedPassword: string) => {
|
|
24
24
|
const [storedHash, storedSalt] = storedPassword.split('.')
|
|
25
|
+
|
|
26
|
+
if (!storedHash || !storedSalt) {
|
|
27
|
+
throw new Error(`Invalid stored password: ${storedPassword}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
25
30
|
const storedBuffer = Buffer.from(storedHash, 'hex')
|
|
26
31
|
const buffer = (await scrypt(plaintext, storedSalt, 64)) as Buffer
|
|
27
32
|
return crypto.timingSafeEqual(buffer, storedBuffer)
|