Skip to content

Commit

Permalink
chore: Update NEXTAUTH_SECRET configuration in cypress.config.ts and …
Browse files Browse the repository at this point in the history
…route.ts
  • Loading branch information
Diegogtz03 committed Jun 8, 2024
1 parent e2cb2ff commit 18ac198
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 98 deletions.
1 change: 1 addition & 0 deletions knowx/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ export default defineConfig({
GITHUB_PASSWORD: process.env.EMAIL_SERVER_PASSWORD,
COOKIE_NAME: "next-auth.session-token",
SITE_NAME: process.env.NEXTAUTH_URL,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
},
})
87 changes: 4 additions & 83 deletions knowx/cypress/e2e/login/login.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,94 +15,15 @@ describe("Auth Redirect", () => {
})
})

describe("Login using github", () => {
describe("Authorization Check", () => {
// beforeEach(() => {
// cy.rewriteHeaders()
// })

it("passes", () => {
cy.visit("http://localhost:3000/auth")

const username = Cypress.env("GITHUB_USERNAME")
const password = Cypress.env("GITHUB_PASSWORD")
const loginUrl = Cypress.env("SITE_NAME") + "/auth"
const cookieName = Cypress.env("COOKIE_NAME")
const socialLoginOptions = {
username,
password,
loginUrl,
headless: false,
logs: true,
isPopup: true,
loginSelector: "#provider-button-github",
postLoginSelector: "#provider-button-github",
screenshotOnError: true,
loginSelectorDelay: 2000,
}

cy.clearCookies()

cy.task("GitHubSocialLogin", socialLoginOptions).then((results: any) => {
results["cookies"].forEach((cookie: any) => {
if (cookie.domain.includes(cookieName)) {
cy.setCookie(cookie.name, cookie.value, {
domain: cookie.domain,
expiry: cookie.expires,
httpOnly: cookie.httpOnly,
path: cookie.path,
secure: cookie.secure,
})
}
})
cy.window().then((window) => {
Object.keys(results.ssd).forEach((key) =>
window.sessionStorage.setItem(key, results.ssd[key]),
)
Object.keys(results.lsd).forEach((key) =>
window.localStorage.setItem(key, results.lsd[key]),
)
})
})

// cy.get("#provider-button-github").click()

// cy.origin("https://github.com", () => {
// cy.get("#login_field").should("exist")

// // cy.get("#login_field").type(Cypress.env("GITHUB_USERNAME"))

// cy.task("generateOTP", Cypress.env("OTP_SECRET")).then((otp) => {
// cy.get("#login_field")
// .type(otp as string)
// .wait(15000)
// })

// cy.get("#password").type(Cypress.env("GITHUB_PASSWORD"))

// cy.task("proxiedmail").then((proxiedmail) => {})

// cy.task("generateOTP", Cypress.env("OTP_SECRET")).then((otp) => {
// // cy.get('input[name="otp"]').type(otp as string)
// cy.get("#password").type(otp as string)
// })

// cy.get('input[type="submit"]').click()

// cy.get('button[name="authorize"]')
// .contains("Authorize")
// .then((btn) => {
// cy.get('button[name="authorize"]').contains("Authorize").click()
// })

// cy.get('text[name="otp"]').then((otp) => {
// cy.task("generateOTP", {
// secret: Cypress.env("OTP_SECRET"),
// }).then((otp) => {
// cy.get('input[name="otp"]').type(otp as string)
// })
// })

// cy.get('button[name="authorize"]').contains("Authorize").click()

cy.login()
cy.visit("http://localhost:3000/auth")
cy.url().should("eq", "http://localhost:3000/dashboard")
})
})
61 changes: 56 additions & 5 deletions knowx/cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/// <reference types="cypress" />
// / <reference types="cypress" />

declare namespace Cypress {
interface Chainable {
rewriteHeaders(): void
import hkdf from "@panva/hkdf"
import { EncryptJWT, JWTPayload } from "jose"

declare global {
namespace Cypress {
interface Chainable {
rewriteHeaders(): void
login(): void
}
}
}

// commands.ts
// Origin: https://www.tomoliver.net/posts/cypress-samesite-problem
Cypress.Commands.add("rewriteHeaders", () => {
cy.intercept("*", (req) =>
Expand All @@ -25,3 +30,49 @@ Cypress.Commands.add("rewriteHeaders", () => {
}),
)
})

async function getDerivedEncryptionKey(secret: string) {
return await hkdf(
"sha256",
secret,
"",
"NextAuth.js Generated Encryption Key",
32,
)
}

export async function encode(
token: JWTPayload,
secret: string,
): Promise<string> {
const maxAge = 30 * 24 * 60 * 60
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)
}

Cypress.Commands.add("login", () => {
const payload = {
name: "Testing",
email: "[email protected]",
picture: "https://avatars.githubusercontent.com/u/65473367?v=",
iat: new Date().getTime(),
exp: new Date().getTime() + 30 * 24 * 60 * 60 * 1000,
}

cy.wrap(null)
.then(() => {
return encode(payload, Cypress.env("NEXTAUTH_SECRET"))
})
.then((encryptedToken) =>
cy.setCookie("next-auth.session-token", encryptedToken, {
expiry: new Date().setDate(new Date().getDate() + 2),
path: "/",
sameSite: "lax",
}),
)
})
Loading

0 comments on commit 18ac198

Please sign in to comment.