Skip to content

[JavaScript] Add authentications support to JavaScript client #2056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
* The base path to put in front of every API call's (relative) path.
*/
this.basePath = '{{basePath}}'.replace(/\/+$/, '');

{{=< >=}}
this.authentications = {<#authMethods><#isBasic>
'<name>': {type: 'basic'}</isBasic><#isApiKey>
'<name>': {type: 'apiKey', in: <#isKeyInHeader>'header'</isKeyInHeader><^isKeyInHeader>'query'</isKeyInHeader>, name: '<keyParamName>'}</isApiKey><#isOAuth>
'<name>': {type: 'oauth2'}</isOAuth><#hasMore>,</hasMore></authMethods>
};
<={{ }}=>
/**
* The default HTTP headers to be included for all API calls.
*/
Expand Down Expand Up @@ -158,6 +164,42 @@
}
};

ApiClient.prototype.applyAuthToRequest = function applyAuthToRequest(request, authNames) {
var _this = this;
authNames.forEach(function(authName) {
var auth = _this.authentications[authName];
switch (auth.type) {
case 'basic':
if (auth.username || auth.password) {
request.auth(auth.username || '', auth.password || '');
}
break;
case 'apiKey':
if (auth.apiKey) {
var data = {};
if (auth.apiKeyPrefix) {
data[auth.name] = auth.apiKeyPrefix + ' ' + auth.apiKey;
} else {
data[auth.name] = auth.apiKey;
}
if (auth.in === 'header') {
request.set(data);
} else {
request.query(data);
}
}
break;
case 'oauth2':
if (auth.accessToken) {
request.set({'Authorization': 'Bearer ' + auth.accessToken});
}
break;
default:
throw new Error('Unknown authentication type: ' + auth.type);
}
});
};

ApiClient.prototype.deserialize = function deserialize(response, returnType) {
if (response == null || returnType == null) {
return null;
Expand All @@ -172,12 +214,15 @@
};

ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams,
queryParams, headerParams, formParams, bodyParam, contentTypes, accepts,
returnType{{^usePromises}}, callback{{/usePromises}}) {
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes,
accepts, returnType{{^usePromises}}, callback{{/usePromises}}) {
var _this = this;
var url = this.buildUrl(path, pathParams);
var request = superagent(httpMethod, url);

// apply authentications
this.applyAuthToRequest(request, authNames);

// set query parameters
request.query(this.normalizeParams(queryParams));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@
'<baseName>': <#collectionFormat>this.buildCollectionParam(<paramName>, '<collectionFormat>')</collectionFormat><^collectionFormat><paramName></collectionFormat><#hasMore>,</hasMore></formParams>
};

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

return this.apiClient.callApi(
'<&path>', '<httpMethod>',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType<^usePromises>, callback</usePromises>
authNames, contentTypes, accepts, returnType<^usePromises>, callback</usePromises>
);
<={{ }}=>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"devDependencies": {
"mocha": "~2.3.4",
"sinon": "1.17.3",
"expect.js": "~0.3.1"
}
}
1 change: 1 addition & 0 deletions samples/client/petstore/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"devDependencies": {
"mocha": "~2.3.4",
"sinon": "1.17.3",
"expect.js": "~0.3.1"
}
}
48 changes: 46 additions & 2 deletions samples/client/petstore/javascript/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
*/
this.basePath = 'http://petstore.swagger.io/v2'.replace(/\/+$/, '');

this.authentications = {
'petstore_auth': {type: 'oauth2'},
'api_key': {type: 'apiKey', in: 'header', name: 'api_key'}
};

/**
* The default HTTP headers to be included for all API calls.
*/
Expand Down Expand Up @@ -158,6 +163,42 @@
}
};

ApiClient.prototype.applyAuthToRequest = function applyAuthToRequest(request, authNames) {
var _this = this;
authNames.forEach(function(authName) {
var auth = _this.authentications[authName];
switch (auth.type) {
case 'basic':
if (auth.username || auth.password) {
request.auth(auth.username || '', auth.password || '');
}
break;
case 'apiKey':
if (auth.apiKey) {
var data = {};
if (auth.apiKeyPrefix) {
data[auth.name] = auth.apiKeyPrefix + ' ' + auth.apiKey;
} else {
data[auth.name] = auth.apiKey;
}
if (auth.in === 'header') {
request.set(data);
} else {
request.query(data);
}
}
break;
case 'oauth2':
if (auth.accessToken) {
request.set({'Authorization': 'Bearer ' + auth.accessToken});
}
break;
default:
throw new Error('Unknown authentication type: ' + auth.type);
}
});
};

ApiClient.prototype.deserialize = function deserialize(response, returnType) {
if (response == null || returnType == null) {
return null;
Expand All @@ -172,12 +213,15 @@
};

ApiClient.prototype.callApi = function callApi(path, httpMethod, pathParams,
queryParams, headerParams, formParams, bodyParam, contentTypes, accepts,
returnType, callback) {
queryParams, headerParams, formParams, bodyParam, authNames, contentTypes,
accepts, returnType, callback) {
var _this = this;
var url = this.buildUrl(path, pathParams);
var request = superagent(httpMethod, url);

// apply authentications
this.applyAuthToRequest(request, authNames);

// set query parameters
request.query(this.normalizeParams(queryParams));

Expand Down
30 changes: 20 additions & 10 deletions samples/client/petstore/javascript/src/api/PetApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@
var formParams = {
};

var authNames = ['petstore_auth'];
var contentTypes = ['application/json', 'application/xml'];
var accepts = ['application/json', 'application/xml'];
var returnType = null;

return this.apiClient.callApi(
'/pet', 'PUT',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand All @@ -73,14 +74,15 @@
var formParams = {
};

var authNames = ['petstore_auth'];
var contentTypes = ['application/json', 'application/xml'];
var accepts = ['application/json', 'application/xml'];
var returnType = null;

return this.apiClient.callApi(
'/pet', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand All @@ -107,14 +109,15 @@
var formParams = {
};

var authNames = ['petstore_auth'];
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var returnType = [Pet];

return this.apiClient.callApi(
'/pet/findByStatus', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand All @@ -141,14 +144,15 @@
var formParams = {
};

var authNames = ['petstore_auth'];
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var returnType = [Pet];

return this.apiClient.callApi(
'/pet/findByTags', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand Down Expand Up @@ -180,14 +184,15 @@
var formParams = {
};

var authNames = ['api_key'];
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var returnType = Pet;

return this.apiClient.callApi(
'/pet/{petId}', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand Down Expand Up @@ -222,14 +227,15 @@
'status': status
};

var authNames = ['petstore_auth'];
var contentTypes = ['application/x-www-form-urlencoded'];
var accepts = ['application/json', 'application/xml'];
var returnType = null;

return this.apiClient.callApi(
'/pet/{petId}', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand Down Expand Up @@ -262,14 +268,15 @@
var formParams = {
};

var authNames = ['petstore_auth'];
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var returnType = null;

return this.apiClient.callApi(
'/pet/{petId}', 'DELETE',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand Down Expand Up @@ -304,14 +311,15 @@
'file': file
};

var authNames = ['petstore_auth'];
var contentTypes = ['multipart/form-data'];
var accepts = ['application/json', 'application/xml'];
var returnType = null;

return this.apiClient.callApi(
'/pet/{petId}/uploadImage', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand Down Expand Up @@ -343,14 +351,15 @@
var formParams = {
};

var authNames = ['api_key'];
var contentTypes = [];
var accepts = ['application/json', 'application/xml'];
var returnType = 'String';

return this.apiClient.callApi(
'/pet/{petId}?testing_byte_array=true', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand All @@ -375,14 +384,15 @@
var formParams = {
};

var authNames = ['petstore_auth'];
var contentTypes = ['application/json', 'application/xml'];
var accepts = ['application/json', 'application/xml'];
var returnType = null;

return this.apiClient.callApi(
'/pet?testing_byte_array=true', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, returnType, callback
authNames, contentTypes, accepts, returnType, callback
);

}
Expand Down
Loading