@labdigital/commercetools-mock 2.26.1 → 2.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +65 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +65 -52
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/password.ts +12 -5
- package/src/repositories/customer/index.ts +52 -5
- package/src/repositories/my-customer.ts +1 -41
- package/src/services/customer.test.ts +62 -2
- package/src/services/customer.ts +31 -18
- package/src/services/my-customer.ts +1 -1
package/dist/index.cjs
CHANGED
|
@@ -81,18 +81,21 @@ var import_uuid = require("uuid");
|
|
|
81
81
|
var PWRESET_SECRET = "pwreset";
|
|
82
82
|
var EMAIL_VERIFY_SECRET = "emailverifysecret";
|
|
83
83
|
var hashPassword = (clearPassword) => Buffer.from(clearPassword).toString("base64");
|
|
84
|
-
var createPasswordResetToken = (customer) => Buffer.from(
|
|
85
|
-
|
|
86
|
-
);
|
|
84
|
+
var createPasswordResetToken = (customer, expiresAt) => Buffer.from(
|
|
85
|
+
`${customer.id}:${PWRESET_SECRET}:${expiresAt.getTime()}`
|
|
86
|
+
).toString("base64");
|
|
87
87
|
var createEmailVerifyToken = (customer) => Buffer.from(`${customer.id}:${EMAIL_VERIFY_SECRET}:${(0, import_uuid.v4)()}`).toString(
|
|
88
88
|
"base64"
|
|
89
89
|
);
|
|
90
90
|
var validatePasswordResetToken = (token) => {
|
|
91
91
|
const items = Buffer.from(token, "base64").toString("utf-8").split(":");
|
|
92
|
-
const [customerId, secret] = items;
|
|
92
|
+
const [customerId, secret, time] = items;
|
|
93
93
|
if (secret !== PWRESET_SECRET) {
|
|
94
94
|
return void 0;
|
|
95
95
|
}
|
|
96
|
+
if (parseInt(time) < (/* @__PURE__ */ new Date()).getTime()) {
|
|
97
|
+
return void 0;
|
|
98
|
+
}
|
|
96
99
|
return customerId;
|
|
97
100
|
};
|
|
98
101
|
var validateEmailVerifyToken = (token) => {
|
|
@@ -3455,20 +3458,21 @@ var CustomerRepository = class extends AbstractResourceRepository {
|
|
|
3455
3458
|
};
|
|
3456
3459
|
return this.saveNew(context, resource);
|
|
3457
3460
|
}
|
|
3458
|
-
passwordResetToken(context,
|
|
3461
|
+
passwordResetToken(context, request) {
|
|
3459
3462
|
const results = this._storage.query(context.projectKey, this.getTypeId(), {
|
|
3460
|
-
where: [`email="${email.toLocaleLowerCase()}"`]
|
|
3463
|
+
where: [`email="${request.email.toLocaleLowerCase()}"`]
|
|
3461
3464
|
});
|
|
3462
3465
|
if (results.count === 0) {
|
|
3463
3466
|
throw new CommercetoolsError({
|
|
3464
3467
|
code: "ResourceNotFound",
|
|
3465
|
-
message: `The Customer with ID '${email}' was not found.`
|
|
3468
|
+
message: `The Customer with ID '${request.email}' was not found.`
|
|
3466
3469
|
});
|
|
3467
3470
|
}
|
|
3468
|
-
const
|
|
3471
|
+
const ttlMinutes = request.ttlMinutes ?? 34560;
|
|
3472
|
+
const expiresAt = new Date((/* @__PURE__ */ new Date()).getTime() + ttlMinutes * 60 * 1e3);
|
|
3469
3473
|
const customer = results.results[0];
|
|
3470
3474
|
const rest = getBaseResourceProperties();
|
|
3471
|
-
const token = createPasswordResetToken(customer);
|
|
3475
|
+
const token = createPasswordResetToken(customer, expiresAt);
|
|
3472
3476
|
return {
|
|
3473
3477
|
id: rest.id,
|
|
3474
3478
|
createdAt: rest.createdAt,
|
|
@@ -3478,6 +3482,31 @@ var CustomerRepository = class extends AbstractResourceRepository {
|
|
|
3478
3482
|
value: token
|
|
3479
3483
|
};
|
|
3480
3484
|
}
|
|
3485
|
+
passwordReset(context, resetPassword) {
|
|
3486
|
+
const { newPassword, tokenValue } = resetPassword;
|
|
3487
|
+
const customerId = validatePasswordResetToken(tokenValue);
|
|
3488
|
+
if (!customerId) {
|
|
3489
|
+
throw new CommercetoolsError({
|
|
3490
|
+
code: "ResourceNotFound",
|
|
3491
|
+
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3492
|
+
});
|
|
3493
|
+
}
|
|
3494
|
+
const customer = this._storage.get(
|
|
3495
|
+
context.projectKey,
|
|
3496
|
+
"customer",
|
|
3497
|
+
customerId
|
|
3498
|
+
);
|
|
3499
|
+
if (!customer) {
|
|
3500
|
+
throw new CommercetoolsError({
|
|
3501
|
+
code: "ResourceNotFound",
|
|
3502
|
+
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3503
|
+
});
|
|
3504
|
+
}
|
|
3505
|
+
customer.password = hashPassword(newPassword);
|
|
3506
|
+
customer.version += 1;
|
|
3507
|
+
this._storage.add(context.projectKey, "customer", customer);
|
|
3508
|
+
return customer;
|
|
3509
|
+
}
|
|
3481
3510
|
verifyEmailToken(context, id) {
|
|
3482
3511
|
const results = this._storage.query(context.projectKey, this.getTypeId(), {
|
|
3483
3512
|
where: [`id="${id.toLocaleLowerCase()}"`]
|
|
@@ -3860,31 +3889,6 @@ var MyCustomerRepository = class extends CustomerRepository {
|
|
|
3860
3889
|
}
|
|
3861
3890
|
return;
|
|
3862
3891
|
}
|
|
3863
|
-
resetPassword(context, resetPassword) {
|
|
3864
|
-
const { newPassword, tokenValue } = resetPassword;
|
|
3865
|
-
const customerId = validatePasswordResetToken(tokenValue);
|
|
3866
|
-
if (!customerId) {
|
|
3867
|
-
throw new CommercetoolsError({
|
|
3868
|
-
code: "ResourceNotFound",
|
|
3869
|
-
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3870
|
-
});
|
|
3871
|
-
}
|
|
3872
|
-
const customer = this._storage.get(
|
|
3873
|
-
context.projectKey,
|
|
3874
|
-
"customer",
|
|
3875
|
-
customerId
|
|
3876
|
-
);
|
|
3877
|
-
if (!customer) {
|
|
3878
|
-
throw new CommercetoolsError({
|
|
3879
|
-
code: "ResourceNotFound",
|
|
3880
|
-
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3881
|
-
});
|
|
3882
|
-
}
|
|
3883
|
-
customer.password = hashPassword(newPassword);
|
|
3884
|
-
customer.version += 1;
|
|
3885
|
-
this._storage.add(context.projectKey, "customer", customer);
|
|
3886
|
-
return customer;
|
|
3887
|
-
}
|
|
3888
3892
|
};
|
|
3889
3893
|
|
|
3890
3894
|
// src/repositories/my-order.ts
|
|
@@ -7527,6 +7531,11 @@ var CustomerService = class extends AbstractService {
|
|
|
7527
7531
|
getBasePath() {
|
|
7528
7532
|
return "customers";
|
|
7529
7533
|
}
|
|
7534
|
+
extraRoutes(parent) {
|
|
7535
|
+
parent.post("/password-token", this.passwordResetToken.bind(this));
|
|
7536
|
+
parent.post("/password/reset", this.passwordReset.bind(this));
|
|
7537
|
+
parent.post("/email-token", this.confirmEmailToken.bind(this));
|
|
7538
|
+
}
|
|
7530
7539
|
post(request, response) {
|
|
7531
7540
|
const draft = request.body;
|
|
7532
7541
|
const resource = this.repository.create(
|
|
@@ -7539,23 +7548,27 @@ var CustomerService = class extends AbstractService {
|
|
|
7539
7548
|
};
|
|
7540
7549
|
return response.status(this.createStatusCode).send(result);
|
|
7541
7550
|
}
|
|
7542
|
-
|
|
7543
|
-
|
|
7544
|
-
|
|
7545
|
-
|
|
7546
|
-
|
|
7547
|
-
|
|
7548
|
-
|
|
7549
|
-
|
|
7550
|
-
|
|
7551
|
-
|
|
7552
|
-
|
|
7553
|
-
|
|
7554
|
-
|
|
7555
|
-
|
|
7556
|
-
|
|
7557
|
-
|
|
7558
|
-
|
|
7551
|
+
passwordResetToken(request, response) {
|
|
7552
|
+
const customer = this.repository.passwordResetToken(
|
|
7553
|
+
getRepositoryContext(request),
|
|
7554
|
+
request.body
|
|
7555
|
+
);
|
|
7556
|
+
return response.status(200).send(customer);
|
|
7557
|
+
}
|
|
7558
|
+
passwordReset(request, response) {
|
|
7559
|
+
const customer = this.repository.passwordReset(
|
|
7560
|
+
getRepositoryContext(request),
|
|
7561
|
+
request.body
|
|
7562
|
+
);
|
|
7563
|
+
return response.status(200).send(customer);
|
|
7564
|
+
}
|
|
7565
|
+
confirmEmailToken(request, response) {
|
|
7566
|
+
const id = request.body.id;
|
|
7567
|
+
const token = this.repository.verifyEmailToken(
|
|
7568
|
+
getRepositoryContext(request),
|
|
7569
|
+
id
|
|
7570
|
+
);
|
|
7571
|
+
return response.status(200).send(token);
|
|
7559
7572
|
}
|
|
7560
7573
|
};
|
|
7561
7574
|
|
|
@@ -7713,7 +7726,7 @@ var MyCustomerService = class extends AbstractService {
|
|
|
7713
7726
|
return response.status(200).send(customer);
|
|
7714
7727
|
}
|
|
7715
7728
|
resetPassword(request, response) {
|
|
7716
|
-
const customer = this.repository.
|
|
7729
|
+
const customer = this.repository.passwordReset(
|
|
7717
7730
|
getRepositoryContext(request),
|
|
7718
7731
|
request.body
|
|
7719
7732
|
);
|