1- import { EventEmitter } from ' node:stream' ;
1+ import { EventEmitter } from " node:stream" ;
22
3- import * as jwt from ' jsonwebtoken' ;
4- import * as jwkToPem from ' jwk-to-pem' ;
5- import * as nock from ' nock' ;
6- import * as mockJWK from ' ../fixtures/credential/jwk.json' ;
7- import * as mockKey from ' ../fixtures/credential/key.json' ;
3+ import * as jwt from " jsonwebtoken" ;
4+ import jwkToPem from " jwk-to-pem" ;
5+ import nock from " nock" ;
6+ import * as mockJWK from " ../fixtures/credential/jwk.json" ;
7+ import * as mockKey from " ../fixtures/credential/key.json" ;
88
99// MockRequest mocks an https.Request.
1010export class MockRequest extends EventEmitter {
11- public method : ' POST' | ' GET' | ' OPTIONS' = ' POST' ;
11+ public method : " POST" | " GET" | " OPTIONS" = " POST" ;
1212
13- constructor (
14- readonly body : any ,
15- readonly headers : { [ name : string ] : string }
16- ) {
17- super ( )
13+ constructor ( readonly body : any , readonly headers : { [ name : string ] : string } ) {
14+ super ( ) ;
1815 }
1916
2017 public header ( name : string ) : string {
@@ -25,34 +22,34 @@ export class MockRequest extends EventEmitter {
2522// Creates a mock request with the given data and content-type.
2623export function mockRequest (
2724 data : any ,
28- contentType : string = ' application/json' ,
25+ contentType : string = " application/json" ,
2926 context : {
3027 authorization ?: string ;
3128 instanceIdToken ?: string ;
3229 appCheckToken ?: string ;
3330 } = { } ,
34- reqHeaders ?: Record < string , string > ,
31+ reqHeaders ?: Record < string , string >
3532) {
3633 const body : any = { } ;
37- if ( typeof data !== ' undefined' ) {
34+ if ( typeof data !== " undefined" ) {
3835 body . data = data ;
3936 }
4037
4138 const headers = {
42- ' content-type' : contentType ,
39+ " content-type" : contentType ,
4340 authorization : context . authorization ,
44- ' firebase-instance-id-token' : context . instanceIdToken ,
45- ' x-firebase-appcheck' : context . appCheckToken ,
46- origin : ' example.com' ,
41+ " firebase-instance-id-token" : context . instanceIdToken ,
42+ " x-firebase-appcheck" : context . appCheckToken ,
43+ origin : " example.com" ,
4744 ...reqHeaders ,
4845 } ;
4946
5047 return new MockRequest ( body , headers ) ;
5148}
5249
5350export const expectedResponseHeaders = {
54- ' Access-Control-Allow-Origin' : ' example.com' ,
55- Vary : ' Origin' ,
51+ " Access-Control-Allow-Origin" : " example.com" ,
52+ Vary : " Origin" ,
5653} ;
5754
5855/**
@@ -62,11 +59,11 @@ export const expectedResponseHeaders = {
6259export function mockFetchPublicKeys ( ) : nock . Scope {
6360 const mockedResponse = { [ mockKey . key_id ] : mockKey . public_key } ;
6461 const headers = {
65- ' cache-control' : ' public, max-age=1, must-revalidate, no-transform' ,
62+ " cache-control" : " public, max-age=1, must-revalidate, no-transform" ,
6663 } ;
6764
68- return nock ( ' https://www.googleapis.com:443' )
69- . get ( ' /robot/v1/metadata/x509/securetoken@system.gserviceaccount.com' )
65+ return nock ( " https://www.googleapis.com:443" )
66+ . get ( " /robot/v1/metadata/x509/securetoken@system.gserviceaccount.com" )
7067 . reply ( 200 , mockedResponse , headers ) ;
7168}
7269
@@ -78,12 +75,12 @@ export function generateIdToken(projectId: string): string {
7875 const options : jwt . SignOptions = {
7976 audience : projectId ,
8077 expiresIn : 60 * 60 , // 1 hour in seconds
81- issuer : ' https://securetoken.google.com/' + projectId ,
78+ issuer : " https://securetoken.google.com/" + projectId ,
8279 subject : mockKey . user_id ,
83- algorithm : ' RS256' ,
80+ algorithm : " RS256" ,
8481 header : {
8582 kid : mockKey . key_id ,
86- alg : ' RS256' ,
83+ alg : " RS256" ,
8784 } ,
8885 } ;
8986 return jwt . sign ( claims , mockKey . private_key , options ) ;
@@ -94,13 +91,13 @@ export function generateIdToken(projectId: string): string {
9491 */
9592export function generateUnsignedIdToken ( projectId : string ) : string {
9693 return [
97- { alg : ' RS256' , typ : ' JWT' } ,
94+ { alg : " RS256" , typ : " JWT" } ,
9895 { aud : projectId , sub : mockKey . user_id } ,
99- ' Invalid signature' ,
96+ " Invalid signature" ,
10097 ]
10198 . map ( ( str ) => JSON . stringify ( str ) )
102- . map ( ( str ) => Buffer . from ( str ) . toString ( ' base64' ) )
103- . join ( '.' ) ;
99+ . map ( ( str ) => Buffer . from ( str ) . toString ( " base64" ) )
100+ . join ( "." ) ;
104101}
105102
106103/**
@@ -113,27 +110,24 @@ export function mockFetchAppCheckPublicJwks(): nock.Scope {
113110 keys : [ { kty, use, alg, kid, n, e } ] ,
114111 } ;
115112
116- return nock ( ' https://firebaseappcheck.googleapis.com:443' )
117- . get ( ' /v1/jwks' )
113+ return nock ( " https://firebaseappcheck.googleapis.com:443" )
114+ . get ( " /v1/jwks" )
118115 . reply ( 200 , mockedResponse ) ;
119116}
120117
121118/**
122119 * Generates a mocked AppCheck token.
123120 */
124- export function generateAppCheckToken (
125- projectId : string ,
126- appId : string
127- ) : string {
121+ export function generateAppCheckToken ( projectId : string , appId : string ) : string {
128122 const claims = { } ;
129123 const options : jwt . SignOptions = {
130124 audience : [ `projects/${ projectId } ` ] ,
131125 expiresIn : 60 * 60 , // 1 hour in seconds
132126 issuer : `https://firebaseappcheck.googleapis.com/${ projectId } ` ,
133127 subject : appId ,
134128 header : {
135- alg : ' RS256' ,
136- typ : ' JWT' ,
129+ alg : " RS256" ,
130+ typ : " JWT" ,
137131 kid : mockJWK . kid ,
138132 } ,
139133 } ;
@@ -143,16 +137,13 @@ export function generateAppCheckToken(
143137/**
144138 * Generates a mocked, unsigned AppCheck token.
145139 */
146- export function generateUnsignedAppCheckToken (
147- projectId : string ,
148- appId : string
149- ) : string {
140+ export function generateUnsignedAppCheckToken ( projectId : string , appId : string ) : string {
150141 return [
151- { alg : ' RS256' , typ : ' JWT' } ,
142+ { alg : " RS256" , typ : " JWT" } ,
152143 { aud : [ `projects/${ projectId } ` ] , sub : appId } ,
153- ' Invalid signature' ,
144+ " Invalid signature" ,
154145 ]
155146 . map ( ( component ) => JSON . stringify ( component ) )
156- . map ( ( str ) => Buffer . from ( str ) . toString ( ' base64' ) )
157- . join ( '.' ) ;
147+ . map ( ( str ) => Buffer . from ( str ) . toString ( " base64" ) )
148+ . join ( "." ) ;
158149}
0 commit comments