diff --git a/src/Collection.js b/src/Collection.js index 81dde79..a50caed 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -168,14 +168,7 @@ class Collection { value: host, type: 'string' })) - // add a variable for each of the JWT JS 3rd party libraries - const libJSRSASign = { - id: Utils.GenID('libJSRSASign'), - key: 'libJSRSASign', - value: null, - type: 'string' - } - variables.push(libJSRSASign) + return variables } diff --git a/src/CollectionAdvanced.js b/src/CollectionAdvanced.js index 4c5763e..a88e75f 100644 --- a/src/CollectionAdvanced.js +++ b/src/CollectionAdvanced.js @@ -2,6 +2,7 @@ require('dotenv').config() const fs = require('fs') const Utils = require('./Utils') +const { uniq } = require('lodash') const Collection = require('./Collection') @@ -67,6 +68,25 @@ class CollectionAdvanced extends Collection { // PRIVATE + /** + * Extracts all server URLs as variables + */ + getVariables () { + const variables = uniq(Object.values(this.openapi.paths).flatMap(endpoints => ( + Object.values(endpoints).map(endpoint => this.server(endpoint).host) + ))).map(host => ({ + id: Utils.GenID(host), + key: host, // .replace(/\./g, '_'), + value: host, + type: 'string' + })) + // add a variable for each of the JWT JS 3rd party libraries + const libJSRSASign = this.variableLibJaRsaSign() + + variables.push(libJSRSASign) + return variables + } + injectUtilities (localCollection) { // insert Utils folder at top level item // TODO: Add proper description @@ -188,6 +208,19 @@ class CollectionAdvanced extends Collection { return [] } + variableLibJaRsaSign () { + const scriptString = String(fs.readFileSync('./src/events/libJsRSASign.min.js')) + const libJSRSASign = { + id: Utils.GenID('libJSRSASign'), + key: 'libJSRSASign', + value: scriptString, + type: 'string' + } + const hash = this.calculateHash(scriptString) + libJSRSASign.id = Utils.GenID(hash) // RB: to big for uuidv5 + return libJSRSASign + } + /** * Creates a pre-request event for collection */ diff --git a/src/events/collectionPreReqScript.js b/src/events/collectionPreReqScript.js index 84fbf02..6c26342 100644 --- a/src/events/collectionPreReqScript.js +++ b/src/events/collectionPreReqScript.js @@ -157,12 +157,13 @@ function get_jwt_assertion () { navigator = {} window = {} /* eslint-disable no-eval */ + eval(libJSRSASign) // UUID const uuid = require('uuid') - const private_key_encrypted = pm.environment.get('private_key_encrypted') + const private_key_encrypted = pm.environment.get('private_key_encrypted').replace(/\\n/g, '') const private_key_passphrase = pm.environment.get('private_key_passphrase') const private_key = KEYUTIL.getKey(private_key_encrypted, private_key_passphrase) @@ -178,6 +179,7 @@ function get_jwt_assertion () { const iat = KJUR.jws.IntDate.get('now') const header = { alg: 'RS512', typ: 'JWT', kid: kid } + // console.log(`header: ${JSON.stringify(header)}`) const claims = { @@ -190,11 +192,10 @@ function get_jwt_assertion () { iat: iat } - const jwt = KJUR.jws.JWS.sign(null, header, claims, private_key) + // console.log(`claim set: ${JSON.stringify(claims)}`) - // console.log(`header: ${JSON.stringify(header)}`) - // console.log(`claim set: ${JSON.stringify(claims)}`) - // console.log('JWT Assertion: ', jwt) + const jwt = KJUR.jws.JWS.sign(null, header, claims, private_key) + // console.log('JWT Assertion: ', jwt) return jwt } diff --git a/tests/OpenAPICollection.test.js b/tests/OpenAPICollection.test.js index fe45669..b53274d 100644 --- a/tests/OpenAPICollection.test.js +++ b/tests/OpenAPICollection.test.js @@ -27,7 +27,7 @@ describe('.convert', () => { const openAPISpec = await openapi.process() const collection = new Collection(openAPISpec, locale).process() expect(collection.item).toHaveLength(52) - expect(collection.variable).toHaveLength(3 + 1) + expect(collection.variable).toHaveLength(3) expect(collection.item[0].item[0].request.url.host).toBe('{{account.box.com}}') })