Skip to content

Commit

Permalink
Merge pull request #1155 from sesamyab/ma/expires
Browse files Browse the repository at this point in the history
fix: update expires
  • Loading branch information
markusahlstrand authored Dec 12, 2024
2 parents cc42604 + e3ee2f3 commit 58ca6ed
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
9 changes: 7 additions & 2 deletions src/authentication-flows/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
getUserByEmailAndProvider,
getUsersByEmail,
} from "../utils/users";
import { CODE_EXPIRATION_TIME } from "../constants";
import {
CODE_EXPIRATION_TIME,
LOGIN_SESSION_EXPIRATION_TIME,
} from "../constants";
import generateOTP from "../utils/otp";
import { sendResetPassword } from "../controllers/email";
import { createLogMessage } from "../utils/create-log-message";
Expand Down Expand Up @@ -59,7 +62,9 @@ export async function requestPasswordReset(
}

const loginSession = await ctx.env.data.logins.create(client.tenant.id, {
expires_at: new Date(Date.now() + CODE_EXPIRATION_TIME).toISOString(),
expires_at: new Date(
Date.now() + LOGIN_SESSION_EXPIRATION_TIME,
).toISOString(),
authParams: {
client_id: client.id,
username: email,
Expand Down
7 changes: 5 additions & 2 deletions src/authentication-flows/passwordless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import generateOTP from "../utils/otp";
import {
CODE_EXPIRATION_TIME,
EMAIL_VERIFICATION_EXPIRATION_TIME,
UNIVERSAL_AUTH_SESSION_EXPIRES_IN_SECONDS,
} from "../constants";
import {
Expand Down Expand Up @@ -77,7 +78,7 @@ export async function validateCode(
};
}

await env.data.codes.remove(client.tenant.id, code.code_id);
await env.data.codes.used(client.tenant.id, code.code_id);

const emailUser = await getPrimaryUserByEmailAndProvider({
userAdapter: env.data.users,
Expand Down Expand Up @@ -167,7 +168,9 @@ export async function sendEmailVerificationEmail({
code_id,
code_type: "email_verification",
login_id: loginSession.login_id,
expires_at: new Date(Date.now() + CODE_EXPIRATION_TIME).toISOString(),
expires_at: new Date(
Date.now() + EMAIL_VERIFICATION_EXPIRATION_TIME,
).toISOString(),
});

await sendValidateEmailAddress(env, client, user.email, code_id, state);
Expand Down
4 changes: 2 additions & 2 deletions src/authentication-flows/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function ticketAuth(
ctx.set("connection", realm);

const code = await env.data.codes.get(tenant_id, ticketId, "ticket");
if (!code) {
if (!code || code.used_at) {
throw new HTTPException(403, { message: "Ticket not found" });
}

Expand All @@ -45,7 +45,7 @@ export async function ticketAuth(
const client = await getClient(ctx.env, login.authParams.client_id);
ctx.set("client_id", login.authParams.client_id);

await env.data.codes.remove(tenant_id, ticketId);
await env.data.codes.used(tenant_id, ticketId);

const provider = getProviderFromRealm(realm);

Expand Down
18 changes: 3 additions & 15 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
export const ACCESS_TOKEN_EXPIRE_IN_SECONDS = 60 * 60 * 24; // 24 hours
export const MONTH_IN_SECONDS = 30 * 24 * 60 * 60;

// export const headers = {
// accessControlAllowHeaders: "Access-Control-Allow-Headers",
// accessControlAllowOrigin: "Access-Control-Allow-Origin",
// accessControlAllowMethod: "Access-Control-Allow-Methods",
// accessControlAllowCredentials: "Access-Control-Allow-Credentials",
// accessControlExposeHeaders: "Access-Control-Expose-Headers",
// cacheControl: "cache-control",
// contentType: "content-type",
// contentRange: "content-range",
// location: "location",
// setCookie: "set-cookie",
// tenantId: "tenant-id",
// };

export const UNIVERSAL_AUTH_SESSION_EXPIRES_IN_SECONDS = 60 * 60 * 24; // 1 day
export const OAUTH2_CODE_EXPIRES_IN_SECONDS = 5 * 60; // 5 minutes

export const CODE_EXPIRATION_TIME = 24 * 60 * 60 * 1000;
export const CODE_EXPIRATION_TIME = 30 * 60 * 1000; // 30 minutes
export const EMAIL_VERIFICATION_EXPIRATION_TIME = 7 * 24 * 60 * 60 * 1000; // One week
export const LOGIN_SESSION_EXPIRATION_TIME = 24 * 60 * 60 * 1000; // 24 hours

export const CLIENT_ID = process.env.CLIENT_ID || "default";
9 changes: 7 additions & 2 deletions src/routes/universal-login/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ import {
requestPasswordReset,
} from "../../authentication-flows/password";
import { CustomException } from "../../models/CustomError";
import { CODE_EXPIRATION_TIME } from "../../constants";
import {
CODE_EXPIRATION_TIME,
EMAIL_VERIFICATION_EXPIRATION_TIME,
} from "../../constants";
import {
Client,
Login,
Expand Down Expand Up @@ -1493,7 +1496,9 @@ export const loginRoutes = new OpenAPIHono<{ Bindings: Env; Variables: Var }>()
code_id: generateOTP(),
code_type: "email_verification",
login_id: session.login_id,
expires_at: new Date(Date.now() + CODE_EXPIRATION_TIME).toISOString(),
expires_at: new Date(
Date.now() + EMAIL_VERIFICATION_EXPIRATION_TIME,
).toISOString(),
});

await sendSignupValidateEmailAddress(
Expand Down

0 comments on commit 58ca6ed

Please sign in to comment.