@labdigital/commercetools-mock 2.26.0 → 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 +76 -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 +76 -52
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/password.ts +12 -5
- package/src/repositories/customer/actions.ts +18 -1
- 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) => {
|
|
@@ -3364,6 +3367,17 @@ var CustomerUpdateHandler = class extends AbstractUpdateHandler {
|
|
|
3364
3367
|
}
|
|
3365
3368
|
resource.custom.fields[name] = value;
|
|
3366
3369
|
}
|
|
3370
|
+
setCustomType(context, resource, { type, fields }) {
|
|
3371
|
+
if (type) {
|
|
3372
|
+
resource.custom = createCustomFields(
|
|
3373
|
+
{ type, fields },
|
|
3374
|
+
context.projectKey,
|
|
3375
|
+
this._storage
|
|
3376
|
+
);
|
|
3377
|
+
} else {
|
|
3378
|
+
resource.custom = void 0;
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3367
3381
|
setExternalId(_context, resource, { externalId }) {
|
|
3368
3382
|
resource.externalId = externalId;
|
|
3369
3383
|
}
|
|
@@ -3444,20 +3458,21 @@ var CustomerRepository = class extends AbstractResourceRepository {
|
|
|
3444
3458
|
};
|
|
3445
3459
|
return this.saveNew(context, resource);
|
|
3446
3460
|
}
|
|
3447
|
-
passwordResetToken(context,
|
|
3461
|
+
passwordResetToken(context, request) {
|
|
3448
3462
|
const results = this._storage.query(context.projectKey, this.getTypeId(), {
|
|
3449
|
-
where: [`email="${email.toLocaleLowerCase()}"`]
|
|
3463
|
+
where: [`email="${request.email.toLocaleLowerCase()}"`]
|
|
3450
3464
|
});
|
|
3451
3465
|
if (results.count === 0) {
|
|
3452
3466
|
throw new CommercetoolsError({
|
|
3453
3467
|
code: "ResourceNotFound",
|
|
3454
|
-
message: `The Customer with ID '${email}' was not found.`
|
|
3468
|
+
message: `The Customer with ID '${request.email}' was not found.`
|
|
3455
3469
|
});
|
|
3456
3470
|
}
|
|
3457
|
-
const
|
|
3471
|
+
const ttlMinutes = request.ttlMinutes ?? 34560;
|
|
3472
|
+
const expiresAt = new Date((/* @__PURE__ */ new Date()).getTime() + ttlMinutes * 60 * 1e3);
|
|
3458
3473
|
const customer = results.results[0];
|
|
3459
3474
|
const rest = getBaseResourceProperties();
|
|
3460
|
-
const token = createPasswordResetToken(customer);
|
|
3475
|
+
const token = createPasswordResetToken(customer, expiresAt);
|
|
3461
3476
|
return {
|
|
3462
3477
|
id: rest.id,
|
|
3463
3478
|
createdAt: rest.createdAt,
|
|
@@ -3467,6 +3482,31 @@ var CustomerRepository = class extends AbstractResourceRepository {
|
|
|
3467
3482
|
value: token
|
|
3468
3483
|
};
|
|
3469
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
|
+
}
|
|
3470
3510
|
verifyEmailToken(context, id) {
|
|
3471
3511
|
const results = this._storage.query(context.projectKey, this.getTypeId(), {
|
|
3472
3512
|
where: [`id="${id.toLocaleLowerCase()}"`]
|
|
@@ -3849,31 +3889,6 @@ var MyCustomerRepository = class extends CustomerRepository {
|
|
|
3849
3889
|
}
|
|
3850
3890
|
return;
|
|
3851
3891
|
}
|
|
3852
|
-
resetPassword(context, resetPassword) {
|
|
3853
|
-
const { newPassword, tokenValue } = resetPassword;
|
|
3854
|
-
const customerId = validatePasswordResetToken(tokenValue);
|
|
3855
|
-
if (!customerId) {
|
|
3856
|
-
throw new CommercetoolsError({
|
|
3857
|
-
code: "ResourceNotFound",
|
|
3858
|
-
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3859
|
-
});
|
|
3860
|
-
}
|
|
3861
|
-
const customer = this._storage.get(
|
|
3862
|
-
context.projectKey,
|
|
3863
|
-
"customer",
|
|
3864
|
-
customerId
|
|
3865
|
-
);
|
|
3866
|
-
if (!customer) {
|
|
3867
|
-
throw new CommercetoolsError({
|
|
3868
|
-
code: "ResourceNotFound",
|
|
3869
|
-
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3870
|
-
});
|
|
3871
|
-
}
|
|
3872
|
-
customer.password = hashPassword(newPassword);
|
|
3873
|
-
customer.version += 1;
|
|
3874
|
-
this._storage.add(context.projectKey, "customer", customer);
|
|
3875
|
-
return customer;
|
|
3876
|
-
}
|
|
3877
3892
|
};
|
|
3878
3893
|
|
|
3879
3894
|
// src/repositories/my-order.ts
|
|
@@ -7516,6 +7531,11 @@ var CustomerService = class extends AbstractService {
|
|
|
7516
7531
|
getBasePath() {
|
|
7517
7532
|
return "customers";
|
|
7518
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
|
+
}
|
|
7519
7539
|
post(request, response) {
|
|
7520
7540
|
const draft = request.body;
|
|
7521
7541
|
const resource = this.repository.create(
|
|
@@ -7528,23 +7548,27 @@ var CustomerService = class extends AbstractService {
|
|
|
7528
7548
|
};
|
|
7529
7549
|
return response.status(this.createStatusCode).send(result);
|
|
7530
7550
|
}
|
|
7531
|
-
|
|
7532
|
-
|
|
7533
|
-
|
|
7534
|
-
|
|
7535
|
-
|
|
7536
|
-
|
|
7537
|
-
|
|
7538
|
-
|
|
7539
|
-
|
|
7540
|
-
|
|
7541
|
-
|
|
7542
|
-
|
|
7543
|
-
|
|
7544
|
-
|
|
7545
|
-
|
|
7546
|
-
|
|
7547
|
-
|
|
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);
|
|
7548
7572
|
}
|
|
7549
7573
|
};
|
|
7550
7574
|
|
|
@@ -7702,7 +7726,7 @@ var MyCustomerService = class extends AbstractService {
|
|
|
7702
7726
|
return response.status(200).send(customer);
|
|
7703
7727
|
}
|
|
7704
7728
|
resetPassword(request, response) {
|
|
7705
|
-
const customer = this.repository.
|
|
7729
|
+
const customer = this.repository.passwordReset(
|
|
7706
7730
|
getRepositoryContext(request),
|
|
7707
7731
|
request.body
|
|
7708
7732
|
);
|