Skip to content

Commit

Permalink
refactor: add a function to generate cookies for reply (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy-Schmitz authored Nov 25, 2023
1 parent 767c490 commit 8ff1528
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
14 changes: 3 additions & 11 deletions services/auth/login.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { verifyValueWithHash, secureCookie } from "../../utils/credential.js";
import { verifyValueWithHash } from "../../utils/credential.js";
import { makeAccesstoken, makeRefreshtoken } from "../../utils/jwt.js";
import { refreshCookie, fgpCookie } from "../../utils/cookies.js";
import config from "../../config.js";

export const loginHandler = async function (request, reply) {
Expand Down Expand Up @@ -47,18 +48,9 @@ export const loginHandler = async function (request, reply) {
access_token: userAccessToken.token,
access_token_expiry: userAccessToken.expiration,
};
const expireDate = new Date();
expireDate.setTime(expireDate.getTime() + 7 * 24 * 60 * 60 * 1000); // TODO: Make configurable now, set to 7 days

reply.headers({
"set-cookie": [
`userRefreshToken=${userRefreshToken.token}; Path=/; Expires=${expireDate}; SameSite=${
config.SAMESITE
}; HttpOnly; ${secureCookie()}`,
`Fgp=${userAccessToken.userFingerprint}; Path=/; Max-Age=3600; SameSite=${
config.SAMESITE
}; HttpOnly; ${secureCookie()}`,
],
"set-cookie": [refreshCookie(userRefreshToken.token), fgpCookie(userAccessToken.userFingerprint)],
"x-authc-app-origin": config.APPLICATIONORIGIN,
});

Expand Down
14 changes: 3 additions & 11 deletions services/auth/profile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createHash, secureCookie } from "../../utils/credential.js";
import { createHash } from "../../utils/credential.js";
import { makeAccesstoken, makeRefreshtoken } from "../../utils/jwt.js";
import config from "../../config.js";
import { refreshCookie, fgpCookie } from "../../utils/cookies.js";

export const userProfileHandler = async function (request, reply) {
try {
Expand Down Expand Up @@ -50,18 +51,9 @@ export const userProfileHandler = async function (request, reply) {
access_token: userAccessToken.token,
access_token_expiry: userAccessToken.expiration,
};
const expireDate = new Date();
expireDate.setTime(expireDate.getTime() + 7 * 24 * 60 * 60 * 1000); // TODO: Make configurable now, set to 7 days

reply.headers({
"set-cookie": [
`userRefreshToken=${userRefreshToken.token}; Path=/; Expires=${expireDate}; SameSite=${
config.SAMESITE
}; HttpOnly; ${secureCookie()}`,
`Fgp=${userAccessToken.userFingerprint}; Path=/; Max-Age=3600; SameSite=${
config.SAMESITE
}; HttpOnly; ${secureCookie()}`,
],
"set-cookie": [refreshCookie(userRefreshToken.token), fgpCookie(userAccessToken.userFingerprint)],
"x-authc-app-origin": config.APPLICATIONORIGIN,
});

Expand Down
13 changes: 2 additions & 11 deletions services/auth/refresh.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { makeAccesstoken, makeRefreshtoken, validateJWT } from "../../utils/jwt.js";
import config from "../../config.js";
import { parse } from "cookie";
import { secureCookie } from "../../utils/credential.js";
import { refreshCookie, fgpCookie } from "../../utils/cookies.js";

export const tokenRefreshHandler = async function (request, reply) {
try {
Expand Down Expand Up @@ -37,18 +37,9 @@ export const tokenRefreshHandler = async function (request, reply) {
access_token: userAccessToken.token,
access_token_expiry: userAccessToken.expiration,
};
const expireDate = new Date();
expireDate.setTime(expireDate.getTime() + 7 * 24 * 60 * 60 * 1000); // TODO: Make configurable now, set to 7 days

reply.headers({
"set-cookie": [
`userRefreshToken=${userRefreshToken.token}; Path=/; Expires=${expireDate}; SameSite=${
config.SAMESITE
}; HttpOnly; ${secureCookie()}`,
`Fgp=${userAccessToken.userFingerprint}; Path=/; Max-Age=3600; SameSite=${
config.SAMESITE
}; HttpOnly; ${secureCookie()}`,
],
"set-cookie": [refreshCookie(userRefreshToken.token), fgpCookie(userAccessToken.userFingerprint)],
"x-authc-app-origin": config.APPLICATIONORIGIN,
});

Expand Down
10 changes: 2 additions & 8 deletions services/auth/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { randomUUID } from "crypto";
import { createId } from "@paralleldrive/cuid2";
import { makeAccesstoken, makeRefreshtoken } from "../../utils/jwt.js";
import config from "../../config.js";
import { refreshCookie, fgpCookie } from "../../utils/cookies.js";

export const registrationHandler = async function (request, reply) {
try {
Expand Down Expand Up @@ -53,14 +54,7 @@ export const registrationHandler = async function (request, reply) {
expireDate.setTime(expireDate.getTime() + 7 * 24 * 60 * 60 * 1000); // TODO: Make configurable now, set to 7 days

reply.headers({
"set-cookie": [
`userRefreshToken=${
userRefreshToken.token
}; Path=/; Expires=${expireDate}; SameSite=None; HttpOnly; ${secureCookie()}`,
`Fgp=${userAccessToken.userFingerprint}; Path=/; Max-Age=3600; SameSite=${
config.SAMESITE
}; HttpOnly; ${secureCookie()}`,
],
"set-cookie": [refreshCookie(userRefreshToken.token), fgpCookie(userAccessToken.userFingerprint)],
"x-authc-app-origin": config.REGISTRATIONORIGIN,
});

Expand Down
13 changes: 13 additions & 0 deletions utils/cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { secureCookie } from "./credential.js";
import config from "../config.js";

export function refreshCookie(token) {
const expireDate = new Date();
expireDate.setTime(expireDate.getTime() + 7 * 24 * 60 * 60 * 1000); // TODO: Make configurable now, set to 7 days
return `userRefreshToken=${token}; Path=/; Expires=${expireDate}; SameSite=${
config.SAMESITE
}; HttpOnly; ${secureCookie()}`;
}
export function fgpCookie(fingerprint) {
return `Fgp=${fingerprint}; Path=/; Max-Age=3600; SameSite=${config.SAMESITE}; HttpOnly; ${secureCookie()}`;
}

0 comments on commit 8ff1528

Please sign in to comment.