-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,49 +4,51 @@ import { handleFallback } from '../handle-fallback' | |
import { Fallback } from '../types' | ||
import fetchMock from 'fetch-mock' | ||
import { decodeProtectedHeader, jwtVerify } from 'jose' | ||
import { toJwk } from 'js-x509-utils' | ||
|
||
jest.mock('jose', () => ({ | ||
importJWK: jest.fn(), | ||
decodeProtectedHeader: jest.fn(), | ||
jwtVerify: jest.fn() | ||
})) | ||
|
||
fetchMock.get( | ||
'https://www.googleapis.com/service_accounts/v1/jwk/[email protected]', | ||
{ | ||
status: 200, | ||
body: { | ||
keys: [ | ||
{ | ||
kid: 'kid1', | ||
n: 'n2', | ||
kty: 'RSA', | ||
use: 'sig', | ||
e: 'AQAB', | ||
alg: 'RS256' | ||
}, | ||
{ | ||
kid: 'kid2', | ||
n: 'n2', | ||
kty: 'RSA', | ||
use: 'sig', | ||
e: 'AQAB', | ||
alg: 'RS256' | ||
} | ||
] | ||
jest.mock('js-x509-utils', () => ({ | ||
toJwk: jest.fn() | ||
})) | ||
|
||
fetchMock | ||
.get( | ||
'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]', | ||
{ | ||
status: 200, | ||
body: { | ||
kid1: 'xxxxxxxxxx', | ||
kid2: 'yyyyyyyyyy' | ||
} | ||
} | ||
} | ||
) | ||
) | ||
.get( | ||
'https://www.googleapis.com/identitytoolkit/v3/relyingparty/publicKeys', | ||
{ | ||
status: 200, | ||
body: { | ||
kid3: 'zzzzzzzzzz' | ||
} | ||
} | ||
) | ||
|
||
jest.mock('../handle-fallback', () => ({ | ||
handleFallback: jest.fn() | ||
})) | ||
|
||
const fallback: Fallback = { type: 'redirect', destination: '/foo' } | ||
|
||
const originalEnv = { ...process.env } | ||
|
||
describe('makeFirebaseInspector', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
process.env = originalEnv | ||
}) | ||
|
||
test('has no cookies', async () => { | ||
|
@@ -71,6 +73,53 @@ describe('makeFirebaseInspector', () => { | |
expect(handleFallback).not.toBeCalled() | ||
}) | ||
|
||
test('has the firebase cookie by custom key', async () => { | ||
process.env = { | ||
...process.env, | ||
FORTRESS_FIREBASE_COOKIE_KEY: 'session' | ||
} | ||
;(decodeProtectedHeader as jest.Mock).mockReturnValue({ | ||
kid: 'kid1' | ||
}) | ||
;(jwtVerify as jest.Mock).mockReturnValue( | ||
new Promise((resolve) => resolve(true)) | ||
) | ||
await makeFirebaseInspector(fallback)({ | ||
cookies: { | ||
session: 'x.x.x' | ||
} | ||
} as unknown as NextRequest) | ||
|
||
expect(handleFallback).not.toBeCalled() | ||
}) | ||
|
||
test('has the firebase cookie and passed custom handler', async () => { | ||
;(decodeProtectedHeader as jest.Mock).mockReturnValue({ | ||
kid: 'kid1' | ||
}) | ||
;(jwtVerify as jest.Mock).mockReturnValue( | ||
new Promise((resolve) => | ||
resolve({ | ||
payload: { | ||
firebase: { | ||
sign_in_provider: 'google.com' | ||
} | ||
} | ||
}) | ||
) | ||
) | ||
await makeFirebaseInspector( | ||
fallback, | ||
(res) => res.firebase.sign_in_provider === 'google.com' | ||
)({ | ||
cookies: { | ||
__fortressFirebaseSession: 'x.x.x' | ||
} | ||
} as unknown as NextRequest) | ||
|
||
expect(handleFallback).not.toBeCalled() | ||
}) | ||
|
||
test("has the firebase cookie, but it's not valid.", async () => { | ||
;(decodeProtectedHeader as jest.Mock).mockReturnValue({ | ||
kid: 'kid1' | ||
|
@@ -116,5 +165,28 @@ describe('makeFirebaseInspector', () => { | |
}, | ||
undefined | ||
) | ||
expect(toJwk).toBeCalledWith(undefined, 'pem') | ||
}) | ||
|
||
test('session cookie mode', async () => { | ||
process.env = { | ||
...process.env, | ||
FORTRESS_FIREBASE_MODE: 'session' | ||
} | ||
;(decodeProtectedHeader as jest.Mock).mockReturnValue({ | ||
kid: 'kid3' | ||
}) | ||
;(jwtVerify as jest.Mock).mockReturnValue( | ||
new Promise((resolve) => resolve(true)) | ||
) | ||
const token = 'x.y.z' | ||
await makeFirebaseInspector(fallback)({ | ||
cookies: { | ||
__fortressFirebaseSession: token | ||
} | ||
} as unknown as NextRequest) | ||
|
||
expect(handleFallback).not.toBeCalled() | ||
expect(toJwk).toBeCalledWith('zzzzzzzzzz', 'pem') | ||
}) | ||
}) |