Skip to content

Commit 50e2c47

Browse files
Add X-INSTANCE-API-KEY support
Signed-off-by: Trevor Dolby <trevor.dolby@ibm.com>
1 parent 870322d commit 50e2c47

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

rest-fetch.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,13 @@ async function uploadFileFormData(restEndPoint, user, pass, timeout, data, filen
156156
formData.append('field_separator', data.field_separator);
157157
formData.append('text_qualifier', data.text_qualifier);
158158

159+
const authHeaderName = (user === 'X-INSTANCE-API-KEY') ? 'X-INSTANCE-API-KEY' : 'Authorization';
160+
const authHeaderValue = (user === 'X-INSTANCE-API-KEY') ? pass : 'Basic ' + btoa(user + ':' + pass);
161+
159162
const options = {
160163
method: method,
161164
headers: {
162-
'Authorization': 'Basic ' + btoa(user + ':' + pass),
165+
authHeaderName: authHeaderValue,
163166
'Accept': 'application/json',
164167
...formData.getHeaders()
165168
},
@@ -321,12 +324,16 @@ async function custom(restEndPoint,user,pass,timeout,jsonBody,formBody,type,call
321324
//Authentication
322325
if(user!==undefined)
323326
{
324-
options.auth={};
325-
options.auth.username = user;
326-
options.auth.password = pass;
327-
const base64Credentials = Buffer.from(`${user}:${pass}`).toString('base64');
328-
options.headers['Authorization']= `Basic ${base64Credentials}`;
329-
327+
if(user != "X-INSTANCE-API-KEY")
328+
{
329+
options.auth={};
330+
options.auth.username = user;
331+
options.auth.password = pass;
332+
const base64Credentials = Buffer.from(`${user}:${pass}`).toString('base64');
333+
options.headers['Authorization']= `Basic ${base64Credentials}`;
334+
} else {
335+
options.headers['X-INSTANCE-API-KEY'] = pass;
336+
}
330337
}
331338

332339
//Body Content

sync-rest-fetch.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,13 @@ async function uploadFileFormData(restEndPoint, user, pass, timeout, data, filen
138138
formData.append('field_separator', data.field_separator);
139139
formData.append('text_qualifier', data.text_qualifier);
140140

141+
const authHeaderName = (user === 'X-INSTANCE-API-KEY') ? 'X-INSTANCE-API-KEY' : 'Authorization';
142+
const authHeaderValue = (user === 'X-INSTANCE-API-KEY') ? pass : 'Basic ' + Buffer.from(user + ':' + pass).toString('base64');
143+
141144
const options = {
142145
method: method,
143146
headers: {
144-
'Authorization': 'Basic ' + Buffer.from(user + ':' + pass).toString('base64'),
147+
authHeaderName: authHeaderValue,
145148
'Accept': 'application/json',
146149
...formData.getHeaders()
147150
},
@@ -171,7 +174,11 @@ async function custom(restEndPoint, user, pass, timeout, data, contentType, meth
171174
};
172175

173176
if(user!==undefined && user!==null && user.length>0){
174-
options.headers["Authorization"]='Basic ' + Buffer.from(user + ':' + pass).toString('base64');
177+
if ( user != "X-INSTANCE-API-KEY" ) {
178+
options.headers["Authorization"]='Basic ' + Buffer.from(user + ':' + pass).toString('base64');
179+
} else {
180+
options.headers["X-INSTANCE-API-KEY"]=pass;
181+
}
175182
}
176183

177184
if (data) {
@@ -227,10 +234,13 @@ async function custom(restEndPoint, user, pass, timeout, data, contentType, meth
227234
async function downloadFile(restEndPoint, user, pass, timeout, destFile) {
228235
debug("GET File from: " + restEndPoint);
229236

237+
const authHeaderName = (user === 'X-INSTANCE-API-KEY') ? 'X-INSTANCE-API-KEY' : 'Authorization';
238+
const authHeaderValue = (user === 'X-INSTANCE-API-KEY') ? pass : 'Basic ' + Buffer.from(user + ':' + pass).toString('base64');
239+
230240
const options = {
231241
method: 'GET',
232242
headers: {
233-
'Authorization': 'Basic ' + Buffer.from(user + ':' + pass).toString('base64'),
243+
authHeaderName: authHeaderValue,
234244
'Accept': 'application/octet-stream',
235245
},
236246
timeout: timeout * 1000,

wmiocli.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,27 @@ function checkOptions() {
130130
}
131131
}
132132

133-
if (program.opts().user == undefined) {
134-
tenantUser = readFromConsole('Please type your tenant User ID: ');
135-
}
136-
else {
137-
tenantUser = program.opts().user
138-
}
139133

140-
if (program.opts().password == undefined) {
141-
tenantPw = readFromConsole('Please type your tenant User Password: ', true);
134+
if (program.opts().apikey == undefined) {
135+
if (program.opts().user == undefined) {
136+
tenantUser = readFromConsole('Please type your tenant User ID: ');
137+
}
138+
else {
139+
tenantUser = program.opts().user
140+
}
141+
142+
if (program.opts().password == undefined) {
143+
tenantPw = readFromConsole('Please type your tenant User Password: ', true);
144+
}
145+
else {
146+
tenantPw = program.opts().password
147+
}
142148
}
143149
else {
144-
tenantPw = program.opts().password
150+
// Picked up in rest-fetch and used for the auth header
151+
tenantUser = "X-INSTANCE-API-KEY"
152+
tenantPw = program.opts().apikey
145153
}
146-
147154
}
148155

149156
function debug(message) {
@@ -158,10 +165,11 @@ program
158165
.version(versionNo)
159166

160167
//required options
161-
.option('-d, --domain <tenantDomain>', 'Tenant Doamin Name, e.g. "tenant.int-aws-us.webmethods.io"')
168+
.option('-d, --domain <tenantDomain>', 'Tenant Domain Name, e.g. "tenant.int-aws-us.webmethods.io"')
162169
.option('-u, --user <userid>', 'Tenant User ID')
163170
.option('-p, --password <password>', 'Tenant User Password')
164171
//.requiredOption('-p, --password <password>', 'Tenant User Password')
172+
.option('-k, --apikey <apikey>', 'API key for X-INSTANCE-API-KEY header (replaces -u/-p)')
165173

166174
//Positional optoins
167175
.option('-s, --start <position>', 'Index of where to start the return of data (default 0)')

0 commit comments

Comments
 (0)