Skip to content

Commit a47cddd

Browse files
committed
Merge pull request #2056 from xhh/javascript-auth
[JavaScript] Add authentications support to JavaScript client
2 parents a87ce31 + 7beb737 commit a47cddd

File tree

10 files changed

+357
-29
lines changed

10 files changed

+357
-29
lines changed

modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
* The base path to put in front of every API call's (relative) path.
2121
*/
2222
this.basePath = '{{basePath}}'.replace(/\/+$/, '');
23-
23+
{{=< >=}}
24+
this.authentications = {<#authMethods><#isBasic>
25+
'<name>': {type: 'basic'}</isBasic><#isApiKey>
26+
'<name>': {type: 'apiKey', in: <#isKeyInHeader>'header'</isKeyInHeader><^isKeyInHeader>'query'</isKeyInHeader>, name: '<keyParamName>'}</isApiKey><#isOAuth>
27+
'<name>': {type: 'oauth2'}</isOAuth><#hasMore>,</hasMore></authMethods>
28+
};
29+
<={{ }}=>
2430
/**
2531
* The default HTTP headers to be included for all API calls.
2632
*/
@@ -158,6 +164,42 @@
158164
}
159165
};
160166

167+
ApiClient.prototype.applyAuthToRequest = function applyAuthToRequest(request, authNames) {
168+
var _this = this;
169+
authNames.forEach(function(authName) {
170+
var auth = _this.authentications[authName];
171+
switch (auth.type) {
172+
case 'basic':
173+
if (auth.username || auth.password) {
174+
request.auth(auth.username || '', auth.password || '');
175+
}
176+
break;
177+
case 'apiKey':
178+
if (auth.apiKey) {
179+
var data = {};
180+
if (auth.apiKeyPrefix) {
181+
data[auth.name] = auth.apiKeyPrefix + ' ' + auth.apiKey;
182+
} else {
183+
data[auth.name] = auth.apiKey;
184+
}
185+
if (auth.in === 'header') {
186+
request.set(data);
187+
} else {
188+
request.query(data);
189+
}
190+
}
191+
break;
192+
case 'oauth2':
193+
if (auth.accessToken) {
194+
request.set({'Authorization': 'Bearer ' + auth.accessToken});
195+
}
196+
break;
197+
default:
198+
throw new Error('Unknown authentication type: ' + auth.type);
199+
}
200+
});
201+
};
202+
161203
ApiClient.prototype.deserialize = function deserialize(response, returnType) {
162204
if (response == null || returnType == null) {
163205
return null;
@@ -172,12 +214,15 @@
172214
};
173215

174216
ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams,
175-
queryParams, headerParams, formParams, bodyParam, contentTypes, accepts,
176-
returnType{{^usePromises}}, callback{{/usePromises}}) {
217+
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes,
218+
accepts, returnType{{^usePromises}}, callback{{/usePromises}}) {
177219
var _this = this;
178220
var url = this.buildUrl(path, pathParams);
179221
var request = superagent(httpMethod, url);
180222
223+
// apply authentications
224+
this.applyAuthToRequest(request, authNames);
225+
181226
// set query parameters
182227
request.query(this.normalizeParams(queryParams));
183228

modules/swagger-codegen/src/main/resources/Javascript/api.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
'<baseName>': <#collectionFormat>this.buildCollectionParam(<paramName>, '<collectionFormat>')</collectionFormat><^collectionFormat><paramName></collectionFormat><#hasMore>,</hasMore></formParams>
5252
};
5353

54+
var authNames = [<#authMethods>'<name>'<#hasMore>, </hasMore></authMethods>];
5455
var contentTypes = [<#consumes>'<mediaType>'<#hasMore>, </hasMore></consumes>];
5556
var accepts = [<#produces>'<mediaType>'<#hasMore>, </hasMore></produces>];
5657
var returnType = <#returnType><&returnType></returnType><^returnType>null</returnType>;
5758

5859
return this.apiClient.callApi(
5960
'<&path>', '<httpMethod>',
6061
pathParams, queryParams, headerParams, formParams, postBody,
61-
contentTypes, accepts, returnType<^usePromises>, callback</usePromises>
62+
authNames, contentTypes, accepts, returnType<^usePromises>, callback</usePromises>
6263
);
6364
<={{ }}=>
6465
}

modules/swagger-codegen/src/main/resources/Javascript/package.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"devDependencies": {
1414
"mocha": "~2.3.4",
15+
"sinon": "1.17.3",
1516
"expect.js": "~0.3.1"
1617
}
1718
}

samples/client/petstore/javascript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"devDependencies": {
1414
"mocha": "~2.3.4",
15+
"sinon": "1.17.3",
1516
"expect.js": "~0.3.1"
1617
}
1718
}

samples/client/petstore/javascript/src/ApiClient.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
*/
2222
this.basePath = 'http://petstore.swagger.io/v2'.replace(/\/+$/, '');
2323

24+
this.authentications = {
25+
'petstore_auth': {type: 'oauth2'},
26+
'api_key': {type: 'apiKey', in: 'header', name: 'api_key'}
27+
};
28+
2429
/**
2530
* The default HTTP headers to be included for all API calls.
2631
*/
@@ -158,6 +163,42 @@
158163
}
159164
};
160165

166+
ApiClient.prototype.applyAuthToRequest = function applyAuthToRequest(request, authNames) {
167+
var _this = this;
168+
authNames.forEach(function(authName) {
169+
var auth = _this.authentications[authName];
170+
switch (auth.type) {
171+
case 'basic':
172+
if (auth.username || auth.password) {
173+
request.auth(auth.username || '', auth.password || '');
174+
}
175+
break;
176+
case 'apiKey':
177+
if (auth.apiKey) {
178+
var data = {};
179+
if (auth.apiKeyPrefix) {
180+
data[auth.name] = auth.apiKeyPrefix + ' ' + auth.apiKey;
181+
} else {
182+
data[auth.name] = auth.apiKey;
183+
}
184+
if (auth.in === 'header') {
185+
request.set(data);
186+
} else {
187+
request.query(data);
188+
}
189+
}
190+
break;
191+
case 'oauth2':
192+
if (auth.accessToken) {
193+
request.set({'Authorization': 'Bearer ' + auth.accessToken});
194+
}
195+
break;
196+
default:
197+
throw new Error('Unknown authentication type: ' + auth.type);
198+
}
199+
});
200+
};
201+
161202
ApiClient.prototype.deserialize = function deserialize(response, returnType) {
162203
if (response == null || returnType == null) {
163204
return null;
@@ -172,12 +213,15 @@
172213
};
173214

174215
ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams,
175-
queryParams, headerParams, formParams, bodyParam, contentTypes, accepts,
176-
returnType, callback) {
216+
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes,
217+
accepts, returnType, callback) {
177218
var _this = this;
178219
var url = this.buildUrl(path, pathParams);
179220
var request = superagent(httpMethod, url);
180221

222+
// apply authentications
223+
this.applyAuthToRequest(request, authNames);
224+
181225
// set query parameters
182226
request.query(this.normalizeParams(queryParams));
183227

samples/client/petstore/javascript/src/api/PetApi.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@
4141
var formParams = {
4242
};
4343

44+
var authNames = ['petstore_auth'];
4445
var contentTypes = ['application/json', 'application/xml'];
4546
var accepts = ['application/json', 'application/xml'];
4647
var returnType = null;
4748

4849
return this.apiClient.callApi(
4950
'/pet', 'PUT',
5051
pathParams, queryParams, headerParams, formParams, postBody,
51-
contentTypes, accepts, returnType, callback
52+
authNames, contentTypes, accepts, returnType, callback
5253
);
5354

5455
}
@@ -73,14 +74,15 @@
7374
var formParams = {
7475
};
7576

77+
var authNames = ['petstore_auth'];
7678
var contentTypes = ['application/json', 'application/xml'];
7779
var accepts = ['application/json', 'application/xml'];
7880
var returnType = null;
7981

8082
return this.apiClient.callApi(
8183
'/pet', 'POST',
8284
pathParams, queryParams, headerParams, formParams, postBody,
83-
contentTypes, accepts, returnType, callback
85+
authNames, contentTypes, accepts, returnType, callback
8486
);
8587

8688
}
@@ -107,14 +109,15 @@
107109
var formParams = {
108110
};
109111

112+
var authNames = ['petstore_auth'];
110113
var contentTypes = [];
111114
var accepts = ['application/json', 'application/xml'];
112115
var returnType = [Pet];
113116

114117
return this.apiClient.callApi(
115118
'/pet/findByStatus', 'GET',
116119
pathParams, queryParams, headerParams, formParams, postBody,
117-
contentTypes, accepts, returnType, callback
120+
authNames, contentTypes, accepts, returnType, callback
118121
);
119122

120123
}
@@ -141,14 +144,15 @@
141144
var formParams = {
142145
};
143146

147+
var authNames = ['petstore_auth'];
144148
var contentTypes = [];
145149
var accepts = ['application/json', 'application/xml'];
146150
var returnType = [Pet];
147151

148152
return this.apiClient.callApi(
149153
'/pet/findByTags', 'GET',
150154
pathParams, queryParams, headerParams, formParams, postBody,
151-
contentTypes, accepts, returnType, callback
155+
authNames, contentTypes, accepts, returnType, callback
152156
);
153157

154158
}
@@ -180,14 +184,15 @@
180184
var formParams = {
181185
};
182186

187+
var authNames = ['api_key'];
183188
var contentTypes = [];
184189
var accepts = ['application/json', 'application/xml'];
185190
var returnType = Pet;
186191

187192
return this.apiClient.callApi(
188193
'/pet/{petId}', 'GET',
189194
pathParams, queryParams, headerParams, formParams, postBody,
190-
contentTypes, accepts, returnType, callback
195+
authNames, contentTypes, accepts, returnType, callback
191196
);
192197

193198
}
@@ -222,14 +227,15 @@
222227
'status': status
223228
};
224229

230+
var authNames = ['petstore_auth'];
225231
var contentTypes = ['application/x-www-form-urlencoded'];
226232
var accepts = ['application/json', 'application/xml'];
227233
var returnType = null;
228234

229235
return this.apiClient.callApi(
230236
'/pet/{petId}', 'POST',
231237
pathParams, queryParams, headerParams, formParams, postBody,
232-
contentTypes, accepts, returnType, callback
238+
authNames, contentTypes, accepts, returnType, callback
233239
);
234240

235241
}
@@ -262,14 +268,15 @@
262268
var formParams = {
263269
};
264270

271+
var authNames = ['petstore_auth'];
265272
var contentTypes = [];
266273
var accepts = ['application/json', 'application/xml'];
267274
var returnType = null;
268275

269276
return this.apiClient.callApi(
270277
'/pet/{petId}', 'DELETE',
271278
pathParams, queryParams, headerParams, formParams, postBody,
272-
contentTypes, accepts, returnType, callback
279+
authNames, contentTypes, accepts, returnType, callback
273280
);
274281

275282
}
@@ -304,14 +311,15 @@
304311
'file': file
305312
};
306313

314+
var authNames = ['petstore_auth'];
307315
var contentTypes = ['multipart/form-data'];
308316
var accepts = ['application/json', 'application/xml'];
309317
var returnType = null;
310318

311319
return this.apiClient.callApi(
312320
'/pet/{petId}/uploadImage', 'POST',
313321
pathParams, queryParams, headerParams, formParams, postBody,
314-
contentTypes, accepts, returnType, callback
322+
authNames, contentTypes, accepts, returnType, callback
315323
);
316324

317325
}
@@ -343,14 +351,15 @@
343351
var formParams = {
344352
};
345353

354+
var authNames = ['api_key'];
346355
var contentTypes = [];
347356
var accepts = ['application/json', 'application/xml'];
348357
var returnType = 'String';
349358

350359
return this.apiClient.callApi(
351360
'/pet/{petId}?testing_byte_array=true', 'GET',
352361
pathParams, queryParams, headerParams, formParams, postBody,
353-
contentTypes, accepts, returnType, callback
362+
authNames, contentTypes, accepts, returnType, callback
354363
);
355364

356365
}
@@ -375,14 +384,15 @@
375384
var formParams = {
376385
};
377386

387+
var authNames = ['petstore_auth'];
378388
var contentTypes = ['application/json', 'application/xml'];
379389
var accepts = ['application/json', 'application/xml'];
380390
var returnType = null;
381391

382392
return this.apiClient.callApi(
383393
'/pet?testing_byte_array=true', 'POST',
384394
pathParams, queryParams, headerParams, formParams, postBody,
385-
contentTypes, accepts, returnType, callback
395+
authNames, contentTypes, accepts, returnType, callback
386396
);
387397

388398
}

0 commit comments

Comments
 (0)