Update Cookie Expiry Logic

This commit is contained in:
Benjamin Toby
2025-03-23 07:52:28 +01:00
parent 870bc7fd17
commit e829ebd2ed
25 changed files with 289 additions and 345 deletions
+5 -3
View File
@@ -12,6 +12,8 @@ import {
PackageUserLoginRequestBody,
} from "../../types";
import debugLog from "../../utils/logging/debug-log";
import numberfy from "../../utils/numberfy";
import grabCookieExpiryDate from "../../utils/grab-cookie-expirt-date";
type Param = {
key?: string;
@@ -65,6 +67,7 @@ export default async function loginUser({
}: Param): Promise<APILoginFunctionReturn> {
const grabedHostNames = grabHostNames({ userId: user_id || apiUserID });
const { host, port, scheme } = grabedHostNames;
const COOKIE_EXPIRY_DATE = grabCookieExpiryDate();
const defaultTempLoginFieldName = "temp_login_code";
const emailLoginTempCodeFieldName = email_login
@@ -117,7 +120,6 @@ export default async function loginUser({
* Initialize HTTP response variable
*/
/** @type {import("../../types").APILoginFunctionReturn} */
let httpResponse: import("../../types").APILoginFunctionReturn = {
success: false,
};
@@ -268,10 +270,10 @@ export default async function loginUser({
}
response?.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true${
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Expires=${COOKIE_EXPIRY_DATE}${
secureCookie ? ";Secure=true" : ""
}`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true;Expires=${COOKIE_EXPIRY_DATE}`,
]);
if (debug) {
+4 -3
View File
@@ -1,5 +1,4 @@
import http from "http";
import https from "https";
import fs from "fs";
import path from "path";
import encrypt from "../../functions/dsql/encrypt";
@@ -13,6 +12,7 @@ import {
} from "../../functions/backend/auth/write-auth-files";
import getAuthCookieNames from "../../functions/backend/cookies/get-auth-cookie-names";
import { APILoginFunctionReturn } from "../../types";
import grabCookieExpiryDate from "../../utils/grab-cookie-expirt-date";
type Param = {
key?: string;
@@ -51,6 +51,7 @@ export default async function reauthUser({
*/
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
const COOKIE_EXPIRY_DATE = grabCookieExpiryDate();
const finalEncryptionKey =
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
@@ -190,10 +191,10 @@ export default async function reauthUser({
const csrfName = cookieNames.csrfCookieName;
response?.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true${
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Expires=${COOKIE_EXPIRY_DATE}${
secureCookie ? ";Secure=true" : ""
}`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true;Expires=${COOKIE_EXPIRY_DATE}`,
]);
if (httpResponse.csrf) {
@@ -4,6 +4,7 @@ import path from "path";
import encrypt from "../../../functions/dsql/encrypt";
import grabHostNames from "../../../utils/grab-host-names";
import apiGithubLogin from "../../../functions/api/users/social/api-github-login";
import grabCookieExpiryDate from "../../../utils/grab-cookie-expirt-date";
interface FunctionReturn {
success: boolean;
@@ -59,6 +60,7 @@ export default async function githubAuth({
*/
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
const COOKIE_EXPIRY_DATE = grabCookieExpiryDate();
if (!code || code?.match(/ /)) {
return {
@@ -230,10 +232,10 @@ export default async function githubAuth({
const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`;
response.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true${
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Expires=${COOKIE_EXPIRY_DATE}${
secureCookie ? ";Secure=true" : ""
}`,
`${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true;Expires=${COOKIE_EXPIRY_DATE}`,
]);
}
@@ -5,6 +5,7 @@ import apiGoogleLogin from "../../../functions/api/users/social/api-google-login
import getAuthCookieNames from "../../../functions/backend/cookies/get-auth-cookie-names";
import { writeAuthFile } from "../../../functions/backend/auth/write-auth-files";
import { APILoginFunctionReturn } from "../../../types";
import grabCookieExpiryDate from "../../../utils/grab-cookie-expirt-date";
type Param = {
key?: string;
@@ -40,6 +41,7 @@ export default async function googleAuth({
userId: apiUserID || process.env.DSQL_API_USER_ID,
});
const { host, port, scheme, user_id } = grabedHostNames;
const COOKIE_EXPIRY_DATE = grabCookieExpiryDate();
const finalEncryptionKey =
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
@@ -194,10 +196,10 @@ export default async function googleAuth({
const csrfName = cookieNames.csrfCookieName;
response?.setHeader("Set-Cookie", [
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true${
`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;;Expires=${COOKIE_EXPIRY_DATE}${
secureCookie ? ";Secure=true" : ""
}`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`,
`${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true;;Expires=${COOKIE_EXPIRY_DATE}`,
]);
}