Skip to content

Commit

Permalink
Merge pull request #209 from shainegordon/main
Browse files Browse the repository at this point in the history
fix: update how cookies are read and extracted from NextCookies
  • Loading branch information
aiji42 authored Aug 16, 2022
2 parents ca2b1f1 + a24d318 commit 87985c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
41 changes: 21 additions & 20 deletions src/__tests__/cognito.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { handleFallback } from '../handle-fallback'
import { Fallback } from '../types'
import { decodeProtectedHeader, jwtVerify } from 'jose'
import fetchMock from 'fetch-mock'
import {
Cookies,
NextCookies
} from 'next/dist/server/web/spec-extension/cookies'

vi.mock('jose', () => ({
importJWK: vi.fn(),
Expand Down Expand Up @@ -59,12 +63,13 @@ describe('makeCognitoInspector', () => {
})

test('has no cookies', async () => {
const cookies = new Cookies()
await makeCognitoInspector(fallback, cognitoParams)(
{ cookies: {} } as NextRequest,
{ cookies } as NextRequest,
event
)

expect(handleFallback).toBeCalledWith(fallback, { cookies: {} }, event)
expect(handleFallback).toBeCalledWith(fallback, { cookies }, event)
})

test('has the firebase cookie', async () => {
Expand All @@ -74,11 +79,11 @@ describe('makeCognitoInspector', () => {
;(jwtVerify as Mock).mockReturnValue(
new Promise((resolve) => resolve(true))
)
const cookies = new Cookies()
cookies.set('CognitoIdentityServiceProvider.yyy.userName.idToken', 'x.x.x')
await makeCognitoInspector(fallback, cognitoParams)(
{
cookies: {
'CognitoIdentityServiceProvider.yyy.userName.idToken': 'x.x.x'
}
cookies
} as unknown as NextRequest,
event
)
Expand All @@ -99,15 +104,15 @@ describe('makeCognitoInspector', () => {
})
)
)
const cookies = new Cookies()
cookies.set('CognitoIdentityServiceProvider.yyy.userName.idToken', 'x.x.x')
await makeCognitoInspector(
fallback,
cognitoParams,
(res) => !!res.email_verified
)(
{
cookies: {
'CognitoIdentityServiceProvider.yyy.userName.idToken': 'x.x.x'
}
cookies
} as unknown as NextRequest,
event
)
Expand All @@ -123,21 +128,19 @@ describe('makeCognitoInspector', () => {
new Promise((resolve, reject) => reject(false))
)
const token = 'x.y.z'
const cookies = new Cookies()
cookies.set('CognitoIdentityServiceProvider.yyy.userName.idToken', token)
await makeCognitoInspector(fallback, cognitoParams)(
{
cookies: {
'CognitoIdentityServiceProvider.yyy.userName.idToken': token
}
cookies
} as unknown as NextRequest,
event
)

expect(handleFallback).toBeCalledWith(
fallback,
{
cookies: {
'CognitoIdentityServiceProvider.yyy.userName.idToken': token
}
cookies
},
event
)
Expand All @@ -148,21 +151,19 @@ describe('makeCognitoInspector', () => {
kid: 'kid3'
})
const token = 'x.y.z'
const cookies = new Cookies()
cookies.set('CognitoIdentityServiceProvider.yyy.userName.idToken', token)
await makeCognitoInspector(fallback, cognitoParams)(
{
cookies: {
'CognitoIdentityServiceProvider.yyy.userName.idToken': token
}
cookies
} as unknown as NextRequest,
event
)

expect(handleFallback).toBeCalledWith(
fallback,
{
cookies: {
'CognitoIdentityServiceProvider.yyy.userName.idToken': token
}
cookies
},
event
)
Expand Down
7 changes: 5 additions & 2 deletions src/cognito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ const verifyCognitoAuthenticatedUser = async (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
customHandler?: (payload: any) => boolean
): Promise<boolean> => {
const token = Object.entries(req.cookies).find(([key]) =>
const tokenKey = [...req.cookies.keys()].find((key) =>
new RegExp(
`CognitoIdentityServiceProvider\\.${clientId}\\..+\\.idToken`
).test(key)
)?.[1]
)
if (!tokenKey) return false
const token = req.cookies.get(tokenKey)

if (!token) return false

const { keys }: { keys: JWK[] } = await fetch(
Expand Down

1 comment on commit 87985c7

@vercel
Copy link

@vercel vercel bot commented on 87985c7 Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-fortress – ./

next-fortress-git-main-aiji42.vercel.app
next-fortress-aiji42.vercel.app
next-fortress.vercel.app

Please sign in to comment.