import { Cookie , CookieJar } from 'tough-cookie' ; import { JWK } from 'jose' ; import { signing as deriveKey } from '../../../src/auth0-session/utils/hkdf' ; import { generateCookieValue } from '../../../src/auth0-session/transient-store' ; import { IncomingMessage , request as nodeHttpRequest } from 'http' ; import { request as nodeHttpsRequest } from 'https' ; import { ConfigParameters } from '../../../src/auth0-session' ; const secret = '__test_session_secret__' ; const clientId = '__test_client_id__' ; export const defaultConfig = { secret , clientID : clientId , issuerBaseURL : 'https://op.example.com' , routes : { callback : '/callback' } } ; export const toSignedCookieJar = ( cookies , url ) : CookieJar => { const cookieJar = new CookieJar ( ) ; const jwk = JWK . asKey ( deriveKey ( secret ) ) ; for ( const [ key , value ] of Object . entries ( cookies ) ) { cookieJar . setCookieSync ( `${ key }=${ generateCookieValue ( key , value , jwk ) }` , url ) ; } return cookieJar ; } ; export const toCookieJar = ( cookies , url ) : CookieJar => { const cookieJar = new CookieJar ( ) ; for ( const [ key , value ] of Object . entries ( cookies ) ) { cookieJar . setCookieSync ( `${ key }=${ value }` , url ) ; } return cookieJar ; } ; export const fromCookieJar = ( cookieJar , url ) : { [ key ] : string ; } => cookieJar . getCookiesSync ( url ) . reduce ( ( obj , { key , value } ) => { obj [ key ] = value . split ( '.' ) [ 0 ] ; return obj ; } , { } ) ; export const getCookie = ( findKey , cookieJar , url ) : Cookie | undefined => cookieJar . getCookiesSync ( url ) . find ( ( { key } ) => key === findKey ) ; const request = ( url , method = 'GET' , { body , cookieJar , fullResponse } ) : Promise < { [ key ] : string ; } | string | { data : { [ key ] : string ; } | string ; res : IncomingMessage ; } > => new Promise ( ( resolve , reject ) => { const { pathname , port , protocol , search = '' } = new URL ( url ) ; const req = ( protocol === 'https:' ? nodeHttpsRequest : nodeHttpRequest ) ( { method , host : 'localhost' , port , path : pathname + search , protocol , rejectUnauthorized : false } , ( res ) => { if ( res . statusCode && ( res . statusCode < 200 || res . statusCode >= 400 ) ) { return reject ( new Error ( res . statusMessage ) ) ; } const buffers = [ ] ; res . on ( 'data' , ( chunk ) => { buffers . push ( chunk ) ; } ) ; res . on ( 'end' , ( ) => { const str = Buffer . concat ( buffers ) . toString ( ) ; let data ; try { data = str ? JSON . parse ( str ) : str ; } catch ( e ) { data = str ; } if ( fullResponse ) { resolve ( { res , data } ) ; } else { resolve ( data ) ; } } ) ; if ( cookieJar ) { ( res . headers [ 'set-cookie' ] || [ ] ) . forEach ( ( cookie ) => cookieJar . setCookieSync ( cookie , url ) ) ; } } ) ; req . setHeader ( 'content-type' , 'application/json' ) ; if ( cookieJar ) { req . setHeader ( 'cookie' , cookieJar . getCookieStringSync ( url ) ) ; } req . on ( 'error' , reject ) ; if ( body ) { req . write ( JSON . stringify ( body ) ) ; } req . end ( ) ; } ) ; export const get = async ( baseURL , path , { cookieJar , fullResponse } = { } ) : Promise < any | Response > => { return request ( `${ baseURL }${ path }` , 'GET' , { cookieJar , fullResponse } ) ; } ; export const post = async ( baseURL , path , { cookieJar , body , fullResponse } ) : Promise < any | Response > => request ( `${ baseURL }${ path }` , 'POST' , { body , cookieJar , fullResponse } ) ;