Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add handler for in-store customer token #236

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-meals-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

add handler for creating in-store customer token
42 changes: 42 additions & 0 deletions src/oauth/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,46 @@ describe("OAuth2Server", () => {
});
});
});

describe("POST /:projectKey/in-store/key=:storeKey/customers/token", () => {
it("should return a token for in-store customer access", async () => {
const projectKey = "test-project";
const storeKey = "test-store";

storage.add(projectKey, "customer", {
...getBaseResourceProperties(),
email: "j.doe@example.org",
password: hashPassword("password"),
addresses: [],
authenticationMode: "password",
isEmailVerified: true,
stores: [
{
typeId: "store",
key: storeKey,
},
],
});

const response = await supertest(app)
.post(`/${projectKey}/in-store/key=${storeKey}/customers/token`)
.auth("validClientId", "validClientSecret")
.query({
grant_type: "password",
username: "j.doe@example.org",
password: "password",
scope: `${projectKey}:manage_my_profile`,
})
.send();

expect(response.status).toBe(200);
expect(response.body).toEqual({
scope: expect.stringMatching(/customer_id:([^\s]+)/),
access_token: expect.stringMatching(/\S{8,}==$/),
refresh_token: expect.stringMatching(/test-project:\S{8,}==$/),
expires_in: 172800,
token_type: "Bearer",
});
});
});
});
51 changes: 44 additions & 7 deletions src/oauth/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,52 @@ export class OAuth2Server {
response: Response,
next: NextFunction,
) {
return next(
new CommercetoolsError<InvalidClientError>(
const projectKey = request.params.projectKey;
const storeKey = request.params.storeKey;
const grantType = request.query.grant_type || request.body.grant_type;
if (!grantType) {
return next(
new CommercetoolsError<InvalidRequestError>(
{
code: "invalid_request",
message: "Missing required parameter: grant_type.",
},
400,
),
);
}

if (grantType === "password") {
const username = request.query.username || request.body.username;
const password = hashPassword(
request.query.password || request.body.password,
);
const scope =
request.query.scope?.toString() || request.body.scope?.toString();

const result = this.customerRepository.query(
{ projectKey, storeKey },
{
code: "invalid_client",
message: "Not implemented yet in commercetools-mock",
where: [`email = "${username}"`, `password = "${password}"`],
},
401,
),
);
);

if (result.count === 0) {
return next(
new CommercetoolsError<any>(
{
code: "invalid_customer_account_credentials",
message: "Customer account with the given credentials not found.",
},
400,
),
);
}

const customer = result.results[0];
const token = this.store.getCustomerToken(projectKey, customer.id, scope);
return response.status(200).send(token);
}
}

async anonymousTokenHandler(
Expand Down