Skip to content

Commit a95b60a

Browse files
committed
added maskPassword function
1 parent abfff47 commit a95b60a

File tree

4 files changed

+102
-18
lines changed

4 files changed

+102
-18
lines changed

lib/types/operation.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ Operation.prototype.getHeaderParams = function (args) {
491491
return headers;
492492
};
493493

494-
Operation.prototype.urlify = function (args) {
494+
Operation.prototype.urlify = function (args, maskPasswords) {
495495
var formParams = {};
496496
var requestUrl = this.path.replace(/#.*/, ''); // remove URL fragment
497497
var querystring = ''; // grab params from the args, build the querystring along the way
@@ -500,14 +500,19 @@ Operation.prototype.urlify = function (args) {
500500
var param = this.parameters[i];
501501

502502
if (typeof args[param.name] !== 'undefined') {
503+
var isPassword;
504+
if(param.type === 'string' && param.format === 'password' && maskPasswords) {
505+
isPassword = true;
506+
}
507+
503508
if (param.in === 'path') {
504509
var reg = new RegExp('\{' + param.name + '\}', 'gi');
505510
var value = args[param.name];
506511

507512
if (Array.isArray(value)) {
508-
value = this.encodePathCollection(param.collectionFormat, param.name, value);
513+
value = this.encodePathCollection(param.collectionFormat, param.name, value, isPassword);
509514
} else {
510-
value = this.encodePathParam(value);
515+
value = this.encodePathParam(value, isPassword);
511516
}
512517

513518
requestUrl = requestUrl.replace(reg, value);
@@ -522,12 +527,12 @@ Operation.prototype.urlify = function (args) {
522527
var qp = args[param.name];
523528

524529
if (Array.isArray(qp)) {
525-
querystring += this.encodeQueryCollection(param.collectionFormat, param.name, qp);
530+
querystring += this.encodeQueryCollection(param.collectionFormat, param.name, qp, isPassword);
526531
} else {
527-
querystring += this.encodeQueryKey(param.name) + '=' + this.encodeQueryParam(args[param.name]);
532+
querystring += this.encodeQueryKey(param.name) + '=' + this.encodeQueryParam(args[param.name], isPassword);
528533
}
529534
} else {
530-
querystring += this.encodeQueryKey(param.name) + '=' + this.encodeQueryParam(args[param.name]);
535+
querystring += this.encodeQueryKey(param.name) + '=' + this.encodeQueryParam(args[param.name], isPassword);
531536
}
532537
} else if (param.in === 'formData') {
533538
formParams[param.name] = args[param.name];
@@ -835,7 +840,7 @@ Operation.prototype.execute = function (arg1, arg2, arg3, arg4, parent) {
835840
for (attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
836841

837842
var body = this.getBody(contentTypeHeaders, args, opts);
838-
var url = this.urlify(args);
843+
var url = this.urlify(args, opts.maskPasswords);
839844

840845
if(url.indexOf('.{format}') > 0) {
841846
if(headers) {
@@ -1015,7 +1020,7 @@ Operation.prototype.matchesAccept = function(accepts) {
10151020
};
10161021

10171022
Operation.prototype.asCurl = function (args1, args2) {
1018-
var opts = {mock: true};
1023+
var opts = {mock: true, maskPasswords: true};
10191024
if (typeof args2 === 'object') {
10201025
for (var argKey in args2) {
10211026
opts[argKey] = args2[argKey];
@@ -1082,14 +1087,14 @@ Operation.prototype.asCurl = function (args1, args2) {
10821087
if (Array.isArray(paramValue)) {
10831088
if(parameter.collectionFormat === 'multi') {
10841089
for(var v in paramValue) {
1085-
body += '-F ' + this.encodeQueryKey(parameter.name) + '=' + paramValue[v] + ' ';
1090+
body += '-F ' + this.encodeQueryKey(parameter.name) + '=' + mask(paramValue[v], parameter.format) + ' ';
10861091
}
10871092
}
10881093
else {
1089-
body += '-F ' + this.encodeQueryCollection(parameter.collectionFormat, parameter.name, paramValue) + ' ';
1094+
body += '-F ' + this.encodeQueryCollection(parameter.collectionFormat, parameter.name, mask(paramValue, parameter.format)) + ' ';
10901095
}
10911096
} else {
1092-
body += '-F ' + this.encodeQueryKey(parameter.name) + '=' + paramValue + ' ';
1097+
body += '-F ' + this.encodeQueryKey(parameter.name) + '=' + mask(paramValue, parameter.format) + ' ';
10931098
}
10941099
}
10951100
}
@@ -1120,7 +1125,7 @@ Operation.prototype.asCurl = function (args1, args2) {
11201125
return 'curl ' + (results.join(' ')) + ' \'' + obj.url + '\'';
11211126
};
11221127

1123-
Operation.prototype.encodePathCollection = function (type, name, value) {
1128+
Operation.prototype.encodePathCollection = function (type, name, value, maskPasswords) {
11241129
var encoded = '';
11251130
var i;
11261131
var separator = '';
@@ -1137,9 +1142,9 @@ Operation.prototype.encodePathCollection = function (type, name, value) {
11371142

11381143
for (i = 0; i < value.length; i++) {
11391144
if (i === 0) {
1140-
encoded = this.encodeQueryParam(value[i]);
1145+
encoded = this.encodeQueryParam(value[i], maskPasswords);
11411146
} else {
1142-
encoded += separator + this.encodeQueryParam(value[i]);
1147+
encoded += separator + this.encodeQueryParam(value[i], maskPasswords);
11431148
}
11441149
}
11451150

@@ -1197,13 +1202,23 @@ Operation.prototype.encodeQueryKey = function (arg) {
11971202
.replace('%5B','[').replace('%5D', ']').replace('%24', '$');
11981203
};
11991204

1200-
Operation.prototype.encodeQueryParam = function (arg) {
1205+
Operation.prototype.encodeQueryParam = function (arg, maskPasswords) {
1206+
if(maskPasswords) {
1207+
return "******";
1208+
}
12011209
return encodeURIComponent(arg);
12021210
};
12031211

12041212
/**
12051213
* TODO revisit, might not want to leave '/'
12061214
**/
1207-
Operation.prototype.encodePathParam = function (pathParam) {
1208-
return encodeURIComponent(pathParam);
1215+
Operation.prototype.encodePathParam = function (pathParam, maskPasswords) {
1216+
return encodeURIComponent(pathParam, maskPasswords);
12091217
};
1218+
1219+
var mask = function(value, format) {
1220+
if(typeof format === 'string' && format === 'password') {
1221+
return '******';
1222+
}
1223+
return value;
1224+
}

test/browser/http.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ describe('yaml http', function () {
111111
it('should call the catch-function when executing an invalid api-call', function(done) {
112112
var petId = -100;
113113
petstoreWithPromise.pet.getPetById({petId: petId}).then(function (success) {
114-
console.log('why?');
115114
console.log(success);
116115
done();
117116
}).catch(function(error) {

test/client.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,42 @@ describe('SwaggerClient', function () {
14711471
});
14721472
});
14731473

1474+
it('should keep password format', function(done) {
1475+
var spec = {
1476+
schemes: ['https'],
1477+
paths: {
1478+
'/v2/nada': {
1479+
get: {
1480+
operationId: 'getNothing',
1481+
tags: [ 'test' ],
1482+
parameters: [{
1483+
in: 'query',
1484+
name: 'password',
1485+
type: 'string',
1486+
format: 'password',
1487+
required: true
1488+
}],
1489+
responses: {
1490+
default: {
1491+
description: 'ok'
1492+
}
1493+
}
1494+
}
1495+
}
1496+
}
1497+
};
1498+
1499+
new SwaggerClient({
1500+
url: 'http://localhost:8000',
1501+
spec: spec,
1502+
usePromise: true
1503+
}).then(function(client) {
1504+
expect(client.apis.test.operations.getNothing.parameters[0].format).toBe('password');
1505+
expect(client.apis.test.operations.getNothing.asCurl({password: 'hidden!'})).toBe('curl -X GET --header \'Accept: application/json\' \'https://localhost:8000/v2/nada?password=******\'');
1506+
done();
1507+
});
1508+
});
1509+
14741510
it('should honor schemes', function(done) {
14751511
var spec = {
14761512
schemes: ['https'],

test/help.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,40 @@ describe('help options', function () {
509509
});
510510
});
511511

512+
it('masks passwords in curl example', function (done) {
513+
var spec = {
514+
basePath: '/v2',
515+
paths: {
516+
'/test': {
517+
post: {
518+
tags: [ 'test' ],
519+
operationId: 'sample',
520+
parameters: [
521+
{
522+
in: 'query',
523+
name: 'password',
524+
type: 'string',
525+
format: 'password',
526+
required: true
527+
}
528+
]
529+
}
530+
}
531+
}
532+
};
533+
534+
var client = new SwaggerClient({
535+
url: 'http://petstore.swagger.io/v2/swagger.json',
536+
spec: spec,
537+
success: function () {
538+
var msg = client.test.sample.asCurl({password: 'hidden!'});
539+
expect(msg).toBe('curl -X POST --header \'Content-Type: application/json\' --header \'Accept: application/json\' \'http://petstore.swagger.io/v2/test?password=******\'');
540+
541+
done();
542+
}
543+
});
544+
});
545+
512546

513547
it('shows curl for multipart/form-data with array parameters', function (done) {
514548
var spec = {

0 commit comments

Comments
 (0)