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: Use the correct defaults for the invite token expiry #10344

Merged
merged 1 commit into from
Nov 28, 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
31 changes: 31 additions & 0 deletions integration-tests/http/__tests__/invite/admin/invite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,37 @@ medusaIntegrationTestRunner({
expect(e.response.data.message).toEqual("Unauthorized")
})
})

it("should fail to accept with an expired token", async () => {
jest.useFakeTimers()

const signup = await api.post("/auth/user/emailpass/register", {
email: "test@medusa-commerce.com",
password: "secret_password",
})

// Advance time by 25 hours
jest.advanceTimersByTime(25 * 60 * 60 * 1000)

await api
.post(
`/admin/invites/accept?token=${invite.token}`,
{
first_name: "Another Test",
last_name: "User",
},
{
headers: { authorization: `Bearer ${signup.data.token}` },
}
)
.catch((e) => {
expect(e.response.status).toEqual(401)
expect(e.response.data.message).toEqual("Unauthorized")
})

jest.useRealTimers()
})

it("should resend an invite", async () => {
const resendResponse = (
await api.post(`/admin/invites/${invite.id}/resend`, {}, adminHeaders)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { IUserModuleService } from "@medusajs/framework/types/dist/user"
import { IUserModuleService } from "@medusajs/framework/types"
import { Modules, UserEvents } from "@medusajs/framework/utils"
import {
MockEventBusService,
moduleIntegrationTestRunner,
} from "@medusajs/test-utils"
import jwt, { JwtPayload } from "jsonwebtoken"

jest.setTimeout(30000)

const today = new Date()
const expireDate = new Date(today.setDate(today.getDate() + 10))
const expireDate = new Date().setMilliseconds(
new Date().getMilliseconds() + 60 * 60 * 24
)

const defaultInviteData = [
{
Expand Down Expand Up @@ -111,6 +113,11 @@ moduleIntegrationTestRunner<IUserModuleService>({
id,
})
)

const tokenContent = jwt.decode(invite.token) as JwtPayload
expect(tokenContent.exp).toBeLessThanOrEqual(
Date.now() / 1000 + 60 * 60 * 24
)
})

it("should throw an error when an invite with the given id does not exist", async () => {
Expand Down
9 changes: 4 additions & 5 deletions packages/modules/user/src/services/user-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ type InjectedDependencies = {
inviteService: ModulesSdkTypes.IMedusaInternalService<any>
}

// 1 day
const DEFAULT_VALID_INVITE_DURATION = 60 * 60 * 24 * 1000
const DEFAULT_VALID_INVITE_DURATION_SECONDS = 60 * 60 * 24
export default class UserModuleService
extends MedusaService<{
User: {
Expand Down Expand Up @@ -60,7 +59,7 @@ export default class UserModuleService
jwtSecret: moduleDeclaration["jwt_secret"],
expiresIn:
parseInt(moduleDeclaration["valid_duration"]) ||
DEFAULT_VALID_INVITE_DURATION,
DEFAULT_VALID_INVITE_DURATION_SECONDS,
}

if (!this.config.jwtSecret) {
Expand Down Expand Up @@ -153,7 +152,7 @@ export default class UserModuleService
return {
id: invite.id,
expires_at: new Date().setMilliseconds(
new Date().getMilliseconds() + this.config.expiresIn
new Date().getMilliseconds() + this.config.expiresIn * 1000
),
token: this.generateToken({ id: invite.id, email: invite.email }),
}
Expand Down Expand Up @@ -325,7 +324,7 @@ export default class UserModuleService
return {
id: invite.id,
expires_at: new Date().setMilliseconds(
new Date().getMilliseconds() + this.config.expiresIn
new Date().getMilliseconds() + this.config.expiresIn * 1000
),
token: this.generateToken({ id: invite.id, email: invite.email }),
}
Expand Down
Loading