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

fix: Validate identifier payload for reset password #9302

Merged
merged 3 commits into from
Oct 1, 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
16 changes: 12 additions & 4 deletions integration-tests/http/__tests__/auth/admin/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,23 @@ medusaIntegrationTestRunner({
describe("Reset password flows", () => {
it("should generate a reset password token", async () => {
const response = await api.post("/auth/user/emailpass/reset-password", {
email: "[email protected]",
identifier: "[email protected]",
})

expect(response.status).toEqual(201)
})

it("should fail if identifier is not provided", async () => {
const errResponse = await api
.post("/auth/user/emailpass/reset-password", {})
.catch((e) => e)

expect(errResponse.response.status).toEqual(400)
})

it("should fail to generate token for non-existing user, but still respond with 201", async () => {
const response = await api.post("/auth/user/emailpass/reset-password", {
email: "[email protected]",
identifier: "[email protected]",
})

expect(response.status).toEqual(201)
Expand All @@ -156,7 +164,7 @@ medusaIntegrationTestRunner({
it("should fail to generate token for existing user but no provider, but still respond with 201", async () => {
const response = await api.post(
"/auth/user/non-existing-provider/reset-password",
{ email: "[email protected]" }
{ identifier: "[email protected]" }
)

expect(response.status).toEqual(201)
Expand All @@ -165,7 +173,7 @@ medusaIntegrationTestRunner({
it("should fail to generate token for existing user but no provider, but still respond with 201", async () => {
const response = await api.post(
"/auth/user/non-existing-provider/reset-password",
{ email: "[email protected]" }
{ identifier: "[email protected]" }
)

expect(response.status).toEqual(201)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { ResetPasswordRequestType } from "../../../validators"

export const POST = async (
req: AuthenticatedMedusaRequest,
req: AuthenticatedMedusaRequest<ResetPasswordRequestType>,
res: MedusaResponse
) => {
const { auth_provider, actor_type } = req.params
const { identifier } = req.body
const { identifier } = req.validatedBody

const { http } = req.scope.resolve(
ContainerRegistrationKeys.CONFIG_MODULE
Expand Down
7 changes: 6 additions & 1 deletion packages/medusa/src/api/auth/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { authenticate, MiddlewareRoute } from "@medusajs/framework/http"
import { validateAndTransformBody } from "../utils/validate-body"
import { validateScopeProviderAssociation } from "./utils/validate-scope-provider-association"
import { validateToken } from "./utils/validate-token"
import { ResetPasswordRequest } from "./validators"

export const authRoutesMiddlewares: MiddlewareRoute[] = [
{
Expand Down Expand Up @@ -41,7 +43,10 @@ export const authRoutesMiddlewares: MiddlewareRoute[] = [
{
method: ["POST"],
matcher: "/auth/:actor_type/:auth_provider/reset-password",
middlewares: [validateScopeProviderAssociation()],
middlewares: [
validateScopeProviderAssociation(),
validateAndTransformBody(ResetPasswordRequest),
],
},
{
method: ["POST"],
Expand Down
6 changes: 6 additions & 0 deletions packages/medusa/src/api/auth/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from "zod"

export const ResetPasswordRequest = z.object({
identifier: z.string(),
})
export type ResetPasswordRequestType = z.infer<typeof ResetPasswordRequest>
Loading