Skip to content

Commit 1509df5

Browse files
authored
Merge pull request #918 from swagger-api/issue-917
added maskPassword function
2 parents 2ce72ba + a95b60a commit 1509df5

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) {
@@ -1017,7 +1022,7 @@ Operation.prototype.matchesAccept = function(accepts) {
10171022
};
10181023

10191024
Operation.prototype.asCurl = function (args1, args2) {
1020-
var opts = {mock: true};
1025+
var opts = {mock: true, maskPasswords: true};
10211026
if (typeof args2 === 'object') {
10221027
for (var argKey in args2) {
10231028
opts[argKey] = args2[argKey];
@@ -1084,14 +1089,14 @@ Operation.prototype.asCurl = function (args1, args2) {
10841089
if (Array.isArray(paramValue)) {
10851090
if(parameter.collectionFormat === 'multi') {
10861091
for(var v in paramValue) {
1087-
body += '-F ' + this.encodeQueryKey(parameter.name) + '=' + paramValue[v] + ' ';
1092+
body += '-F ' + this.encodeQueryKey(parameter.name) + '=' + mask(paramValue[v], parameter.format) + ' ';
10881093
}
10891094
}
10901095
else {
1091-
body += '-F ' + this.encodeQueryCollection(parameter.collectionFormat, parameter.name, paramValue) + ' ';
1096+
body += '-F ' + this.encodeQueryCollection(parameter.collectionFormat, parameter.name, mask(paramValue, parameter.format)) + ' ';
10921097
}
10931098
} else {
1094-
body += '-F ' + this.encodeQueryKey(parameter.name) + '=' + paramValue + ' ';
1099+
body += '-F ' + this.encodeQueryKey(parameter.name) + '=' + mask(paramValue, parameter.format) + ' ';
10951100
}
10961101
}
10971102
}
@@ -1122,7 +1127,7 @@ Operation.prototype.asCurl = function (args1, args2) {
11221127
return 'curl ' + (results.join(' ')) + ' \'' + obj.url + '\'';
11231128
};
11241129

1125-
Operation.prototype.encodePathCollection = function (type, name, value) {
1130+
Operation.prototype.encodePathCollection = function (type, name, value, maskPasswords) {
11261131
var encoded = '';
11271132
var i;
11281133
var separator = '';
@@ -1139,9 +1144,9 @@ Operation.prototype.encodePathCollection = function (type, name, value) {
11391144

11401145
for (i = 0; i < value.length; i++) {
11411146
if (i === 0) {
1142-
encoded = this.encodeQueryParam(value[i]);
1147+
encoded = this.encodeQueryParam(value[i], maskPasswords);
11431148
} else {
1144-
encoded += separator + this.encodeQueryParam(value[i]);
1149+
encoded += separator + this.encodeQueryParam(value[i], maskPasswords);
11451150
}
11461151
}
11471152

@@ -1199,13 +1204,23 @@ Operation.prototype.encodeQueryKey = function (arg) {
11991204
.replace('%5B','[').replace('%5D', ']').replace('%24', '$');
12001205
};
12011206

1202-
Operation.prototype.encodeQueryParam = function (arg) {
1207+
Operation.prototype.encodeQueryParam = function (arg, maskPasswords) {
1208+
if(maskPasswords) {
1209+
return "******";
1210+
}
12031211
return encodeURIComponent(arg);
12041212
};
12051213

12061214
/**
12071215
* TODO revisit, might not want to leave '/'
12081216
**/
1209-
Operation.prototype.encodePathParam = function (pathParam) {
1210-
return encodeURIComponent(pathParam);
1217+
Operation.prototype.encodePathParam = function (pathParam, maskPasswords) {
1218+
return encodeURIComponent(pathParam, maskPasswords);
12111219
};
1220+
1221+
var mask = function(value, format) {
1222+
if(typeof format === 'string' && format === 'password') {
1223+
return '******';
1224+
}
1225+
return value;
1226+
}

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)