@labdigital/commercetools-mock 2.23.1 → 2.24.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 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +76 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/password.ts +15 -0
- package/src/repositories/customer/index.ts +30 -1
- package/src/repositories/my-customer.ts +41 -1
- package/src/services/customer.ts +9 -0
- package/src/services/my-customer.test.ts +47 -0
- package/src/services/my-customer.ts +10 -0
package/dist/index.cjs
CHANGED
|
@@ -79,10 +79,14 @@ var import_express = __toESM(require("express"), 1);
|
|
|
79
79
|
// src/lib/password.ts
|
|
80
80
|
var import_uuid = require("uuid");
|
|
81
81
|
var PWRESET_SECRET = "pwreset";
|
|
82
|
+
var EMAIL_VERIFY_SECRET = "emailverifysecret";
|
|
82
83
|
var hashPassword = (clearPassword) => Buffer.from(clearPassword).toString("base64");
|
|
83
84
|
var createPasswordResetToken = (customer) => Buffer.from(`${customer.id}:${PWRESET_SECRET}:${(0, import_uuid.v4)()}`).toString(
|
|
84
85
|
"base64"
|
|
85
86
|
);
|
|
87
|
+
var createEmailVerifyToken = (customer) => Buffer.from(`${customer.id}:${EMAIL_VERIFY_SECRET}:${(0, import_uuid.v4)()}`).toString(
|
|
88
|
+
"base64"
|
|
89
|
+
);
|
|
86
90
|
var validatePasswordResetToken = (token) => {
|
|
87
91
|
const items = Buffer.from(token, "base64").toString("utf-8").split(":");
|
|
88
92
|
const [customerId, secret] = items;
|
|
@@ -91,6 +95,14 @@ var validatePasswordResetToken = (token) => {
|
|
|
91
95
|
}
|
|
92
96
|
return customerId;
|
|
93
97
|
};
|
|
98
|
+
var validateEmailVerifyToken = (token) => {
|
|
99
|
+
const items = Buffer.from(token, "base64").toString("utf-8").split(":");
|
|
100
|
+
const [customerId, secret] = items;
|
|
101
|
+
if (secret !== EMAIL_VERIFY_SECRET) {
|
|
102
|
+
return void 0;
|
|
103
|
+
}
|
|
104
|
+
return customerId;
|
|
105
|
+
};
|
|
94
106
|
|
|
95
107
|
// src/oauth/helpers.ts
|
|
96
108
|
var getBearerToken = (request) => {
|
|
@@ -3449,6 +3461,29 @@ var CustomerRepository = class extends AbstractResourceRepository {
|
|
|
3449
3461
|
value: token
|
|
3450
3462
|
};
|
|
3451
3463
|
}
|
|
3464
|
+
verifyEmailToken(context, id) {
|
|
3465
|
+
const results = this._storage.query(context.projectKey, this.getTypeId(), {
|
|
3466
|
+
where: [`id="${id.toLocaleLowerCase()}"`]
|
|
3467
|
+
});
|
|
3468
|
+
if (results.count === 0) {
|
|
3469
|
+
throw new CommercetoolsError({
|
|
3470
|
+
code: "ResourceNotFound",
|
|
3471
|
+
message: `The Customer with ID '${id}' was not found.`
|
|
3472
|
+
});
|
|
3473
|
+
}
|
|
3474
|
+
const expiresAt = new Date(Date.now() + 30 * 60);
|
|
3475
|
+
const customer = results.results[0];
|
|
3476
|
+
const rest = getBaseResourceProperties();
|
|
3477
|
+
const token = createEmailVerifyToken(customer);
|
|
3478
|
+
return {
|
|
3479
|
+
id: rest.id,
|
|
3480
|
+
createdAt: rest.createdAt,
|
|
3481
|
+
lastModifiedAt: rest.lastModifiedAt,
|
|
3482
|
+
customerId: customer.id,
|
|
3483
|
+
expiresAt: expiresAt.toISOString(),
|
|
3484
|
+
value: token
|
|
3485
|
+
};
|
|
3486
|
+
}
|
|
3452
3487
|
};
|
|
3453
3488
|
|
|
3454
3489
|
// src/repositories/customer-group.ts
|
|
@@ -3761,6 +3796,31 @@ var MyCustomerRepository = class extends CustomerRepository {
|
|
|
3761
3796
|
this._storage.add(context.projectKey, "customer", customer);
|
|
3762
3797
|
return customer;
|
|
3763
3798
|
}
|
|
3799
|
+
confirmEmail(context, resetPassword) {
|
|
3800
|
+
const { tokenValue } = resetPassword;
|
|
3801
|
+
const customerId = validateEmailVerifyToken(tokenValue);
|
|
3802
|
+
if (!customerId) {
|
|
3803
|
+
throw new CommercetoolsError({
|
|
3804
|
+
code: "ResourceNotFound",
|
|
3805
|
+
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3806
|
+
});
|
|
3807
|
+
}
|
|
3808
|
+
const customer = this._storage.get(
|
|
3809
|
+
context.projectKey,
|
|
3810
|
+
"customer",
|
|
3811
|
+
customerId
|
|
3812
|
+
);
|
|
3813
|
+
if (!customer) {
|
|
3814
|
+
throw new CommercetoolsError({
|
|
3815
|
+
code: "ResourceNotFound",
|
|
3816
|
+
message: `The Customer with ID 'Token(${tokenValue})' was not found.`
|
|
3817
|
+
});
|
|
3818
|
+
}
|
|
3819
|
+
customer.isEmailVerified = true;
|
|
3820
|
+
customer.version += 1;
|
|
3821
|
+
this._storage.add(context.projectKey, "customer", customer);
|
|
3822
|
+
return customer;
|
|
3823
|
+
}
|
|
3764
3824
|
deleteMe(context) {
|
|
3765
3825
|
const results = this._storage.query(
|
|
3766
3826
|
context.projectKey,
|
|
@@ -7471,6 +7531,14 @@ var CustomerService = class extends AbstractService {
|
|
|
7471
7531
|
);
|
|
7472
7532
|
return response.status(200).send(token);
|
|
7473
7533
|
});
|
|
7534
|
+
parent.post("/email-token", (request, response) => {
|
|
7535
|
+
const id = request.body.id;
|
|
7536
|
+
const token = this.repository.verifyEmailToken(
|
|
7537
|
+
getRepositoryContext(request),
|
|
7538
|
+
id
|
|
7539
|
+
);
|
|
7540
|
+
return response.status(200).send(token);
|
|
7541
|
+
});
|
|
7474
7542
|
}
|
|
7475
7543
|
};
|
|
7476
7544
|
|
|
@@ -7576,6 +7644,7 @@ var MyCustomerService = class extends AbstractService {
|
|
|
7576
7644
|
router.post("/login", this.signIn.bind(this));
|
|
7577
7645
|
router.post("/password", this.changePassword.bind(this));
|
|
7578
7646
|
router.post("/password/reset", this.resetPassword.bind(this));
|
|
7647
|
+
router.post("/email/confirm", this.emailConfirm.bind(this));
|
|
7579
7648
|
parent.use(`/${basePath}`, router);
|
|
7580
7649
|
}
|
|
7581
7650
|
getMe(request, response) {
|
|
@@ -7633,6 +7702,13 @@ var MyCustomerService = class extends AbstractService {
|
|
|
7633
7702
|
);
|
|
7634
7703
|
return response.status(200).send(customer);
|
|
7635
7704
|
}
|
|
7705
|
+
emailConfirm(request, response) {
|
|
7706
|
+
const customer = this.repository.confirmEmail(
|
|
7707
|
+
getRepositoryContext(request),
|
|
7708
|
+
request.body
|
|
7709
|
+
);
|
|
7710
|
+
return response.status(200).send(customer);
|
|
7711
|
+
}
|
|
7636
7712
|
signIn(request, response) {
|
|
7637
7713
|
const { email, password } = request.body;
|
|
7638
7714
|
const encodedPassword = hashPassword(password);
|