mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
79 lines
2.7 KiB
TypeScript
79 lines
2.7 KiB
TypeScript
/// <reference types="cypress" />
|
|
// ***********************************************
|
|
// This example commands.ts shows you how to
|
|
// create various custom commands and overwrite
|
|
// existing commands.
|
|
//
|
|
// For more comprehensive examples of custom
|
|
// commands please read more here:
|
|
// https://on.cypress.io/custom-commands
|
|
// ***********************************************
|
|
//
|
|
//
|
|
// -- This is a parent command --
|
|
// Cypress.Commands.add('login', (email, password) => { ... })
|
|
//
|
|
//
|
|
// -- This is a child command --
|
|
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
|
|
//
|
|
//
|
|
// -- This is a dual command --
|
|
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
|
|
//
|
|
//
|
|
// -- This will overwrite an existing command --
|
|
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
|
|
//
|
|
// declare global {
|
|
// namespace Cypress {
|
|
// interface Chainable {
|
|
// login(email: string, password: string): Chainable<void>
|
|
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
|
|
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
|
|
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
import hkdf from "@panva/hkdf";
|
|
import { EncryptJWT, JWTPayload } from "jose";
|
|
|
|
// Function logic derived from https://github.com/nextauthjs/next-auth/blob/5c1826a8d1f8d8c2d26959d12375704b0a693bfc/packages/next-auth/src/jwt/index.ts#L113-L121
|
|
async function getDerivedEncryptionKey(secret: string) {
|
|
return await hkdf(
|
|
"sha256",
|
|
secret,
|
|
"",
|
|
"NextAuth.js Generated Encryption Key",
|
|
32
|
|
);
|
|
}
|
|
|
|
// Function logic derived from https://github.com/nextauthjs/next-auth/blob/5c1826a8d1f8d8c2d26959d12375704b0a693bfc/packages/next-auth/src/jwt/index.ts#L16-L25
|
|
export async function encode(
|
|
token: JWTPayload,
|
|
secret: string
|
|
): Promise<string> {
|
|
const maxAge = 30 * 24 * 60 * 60; // 30 days
|
|
const encryptionSecret = await getDerivedEncryptionKey(secret);
|
|
return await new EncryptJWT(token)
|
|
.setProtectedHeader({ alg: "dir", enc: "A256GCM" })
|
|
.setIssuedAt()
|
|
.setExpirationTime(Math.round(Date.now() / 1000 + maxAge))
|
|
.setJti("test")
|
|
.encrypt(encryptionSecret);
|
|
}
|
|
|
|
// @ts-ignore
|
|
Cypress.Commands.add("login", (userObj: JWTPayload) => {
|
|
// Generate and set a valid cookie from the fixture that next-auth can decrypt
|
|
cy.intercept("/api/auth/session", { fixture: "session.json" }).as("session");
|
|
cy.wrap(null)
|
|
.then(() => {
|
|
return encode(userObj, Cypress.env("NEXTAUTH_JWT_SECRET"));
|
|
})
|
|
.then((encryptedToken) => {
|
|
cy.setCookie("next-auth.session-token", encryptedToken);
|
|
});
|
|
});
|