Skip to content

Commit 316476d

Browse files
committed
+ Added and Modified files for OAuth changes
1 parent 1fbe5a7 commit 316476d

25 files changed

+1490
-19
lines changed

docs/AccessTokenResponse.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# CyberSource.AccessTokenResponse
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
**accessToken** | **String** | Created JWT token. | [optional]
7+
**tokenType** | **String** | Bearer. | [optional]
8+
**refreshToken** | **String** | Newly created JWT token for initial request or if refresh token expired, else the same refresh token as in the request. | [optional]
9+
**expiresIn** | **Number** | Number of seconds left till the access token gets expired. | [optional]
10+
**scope** | **String** | List of permissions for APIs. | [optional]
11+
**refreshTokenExpiresIn** | **Number** | Number of seconds left till the refresh token gets expired. | [optional]
12+
**clientStatus** | **String** | Successful response can be returned only if client status is active. | [optional]
13+
14+

docs/BadRequestError.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CyberSource.BadRequestError
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
**error** | **String** | | [optional]
7+
**errorDescription** | **String** | | [optional]
8+
9+

docs/CreateAccessTokenRequest.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# CyberSource.CreateAccessTokenRequest
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
**clientId** | **String** | The client ID that you received when you registered your application in the CyberSource Business Center. This identifies your client application. |
7+
**clientSecret** | **String** | The client secret that you received when you registered your application in the CyberSource Business Center. |
8+
**grantType** | **String** | The grant type value determines which type of flow is used to obtain the access token. The first time your application requests a token, use the value `authorization_code`. When you use this value, you must also include the `code` field in the request. For subsequent request, use `refresh_token`. When you use this value, you must also include the `refresh_token` field in the request. Valid values: - authorization_code - refresh_token | [optional]
9+
**code** | **String** | This value is sent by CyberSource in the redirect URL. For security reasons, the code expires in 10 minutes. If it expires, you must repeat the redirect to request another. Conditional. This value is equired if grant_type is `authorization_code`. | [optional]
10+
**refreshToken** | **String** | Conditional. Required if grant_type is `refresh_token`. | [optional]
11+
12+

docs/OAuthApi.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# CyberSource.OAuthApi
2+
3+
All URIs are relative to *https://api-ma.Cybersource.com*
4+
5+
Method | HTTP request | Description
6+
------------- | ------------- | -------------
7+
[**createAccessToken**](OAuthApi.md#createAccessToken) | **POST** /oauth2/v3/token | Create access token and refresh token
8+
9+
10+
<a name="createAccessToken"></a>
11+
# **createAccessToken**
12+
> AccessTokenResponse createAccessToken(createAccessTokenRequest, opts)
13+
14+
Create access token and refresh token
15+
16+
This request is used by technology partners to obtain an access token and a refresh token, which are contained in the response. The partner can then use the access token for authentication when submitting API requests to CyberSource on behalf of the merchant. The request must include the authorization code that was included in the URL redirect response from CyberSource (see [full documentation](https://developer.cybersource.com/api/developer-guides/OAuth/cybs_extend_intro.html)). Access tokens expire after 15 minutes. The refresh token is used to create a new access token, it expires after one year.
17+
18+
### Example
19+
```javascript
20+
var CyberSource = require('CyberSource');
21+
22+
var apiInstance = new CyberSource.OAuthApi();
23+
24+
var createAccessTokenRequest = new CyberSource.CreateAccessTokenRequest(); // CreateAccessTokenRequest | Request payload
25+
26+
var opts = {
27+
'vCClientCorrelationId': "vCClientCorrelationId_example" // String | We recommended that you submit this header with a unique value in every client request to this endpoint. It is sent back in the response header and logged both in the request log and response log.
28+
};
29+
30+
var callback = function(error, data, response) {
31+
if (error) {
32+
console.error(error);
33+
} else {
34+
console.log('API called successfully. Returned data: ' + data);
35+
}
36+
};
37+
apiInstance.createAccessToken(createAccessTokenRequest, opts, callback);
38+
```
39+
40+
### Parameters
41+
42+
Name | Type | Description | Notes
43+
------------- | ------------- | ------------- | -------------
44+
**createAccessTokenRequest** | [**CreateAccessTokenRequest**](CreateAccessTokenRequest.md)| Request payload |
45+
**vCClientCorrelationId** | **String**| We recommended that you submit this header with a unique value in every client request to this endpoint. It is sent back in the response header and logged both in the request log and response log. | [optional]
46+
47+
### Return type
48+
49+
[**AccessTokenResponse**](AccessTokenResponse.md)
50+
51+
### Authorization
52+
53+
No authorization required
54+
55+
### HTTP request headers
56+
57+
- **Content-Type**: application/x-www-form-urlencoded
58+
- **Accept**: application/json;charset=utf-8
59+

docs/ResourceNotFoundError.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CyberSource.ResourceNotFoundError
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
**error** | **String** | | [optional]
7+
**errorDescription** | **String** | | [optional]
8+
9+

docs/UnauthorizedClientError.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CyberSource.UnauthorizedClientError
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
**error** | **String** | | [optional]
7+
**errorDescription** | **String** | | [optional]
8+
9+

generator/cybersource-javascript-template/ApiClient.mustache

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,9 @@
385385
386386
var token = Authorization.getToken(this.merchantConfig, this.logger);
387387
388-
var clientId = getClientId();
388+
// var clientId = getClientId();
389389
390-
headerParams['v-c-client-id'] = clientId;
390+
// headerParams['v-c-client-id'] = clientId;
391391
392392
// if (this.merchantConfig.getSolutionId() != null && this.merchantConfig.getSolutionId() != '') {
393393
// headerParams['v-c-solution-id'] = this.merchantConfig.getSolutionId();
@@ -423,6 +423,11 @@
423423
this.logger.info('User-Agent : ' + headerParams['User-Agent']);
424424
this.logger.info(this.constants.END_TRANSACTION);
425425
}
426+
else if (this.merchantConfig.getAuthenticationType().toLowerCase() === this.constants.OAUTH) {
427+
token = 'Bearer ' + token;
428+
headerParams['Authorization'] = token;
429+
this.logger.info(this.constants.AUTHORIZATION + ' : ' + token);
430+
}
426431

427432
return headerParams;
428433
}
@@ -468,6 +473,7 @@
468473
var proxyPort = this.merchantConfig.getProxyPort();
469474
var proxyUser = this.merchantConfig.getProxyUser();
470475
var proxyPassword = this.merchantConfig.getProxyPassword();
476+
var enableClientCert = this.merchantConfig.getEnableClientCert();
471477
472478
var request = superagent(httpMethod, url);
473479
@@ -483,6 +489,17 @@
483489
request.proxy(proxy);
484490
}
485491

492+
var fslib = require('fs');
493+
var pathlib = require('path');
494+
495+
if(enableClientCert)
496+
{
497+
var certFile = pathlib.resolve(pathlib.join(this.merchantConfig.getClientCertDir(), this.merchantConfig.getSSLClientCert()));
498+
var keyFile = pathlib.resolve(pathlib.join(this.merchantConfig.getClientCertDir(), this.merchantConfig.getPrivateKey()));
499+
request.cert(fslib.readFileSync(certFile));
500+
request.key(fslib.readFileSync(keyFile));
501+
}
502+
486503
// apply authentications
487504
this.applyAuthToRequest(request, authNames);
488505

@@ -502,7 +519,11 @@
502519
|| httpMethod.toLowerCase() === this.constants.PUT) {
503520
bodyParam = JSON.stringify(bodyParam, null, 0);
504521
}
505-
headerParams = this.callAuthenticationHeader(httpMethod, requestTarget, bodyParam, headerParams);
522+
523+
if (this.merchantConfig.getAuthenticationType().toLowerCase() !== this.constants.MUTUAL_AUTH)
524+
{
525+
headerParams = this.callAuthenticationHeader(httpMethod, requestTarget, bodyParam, headerParams);
526+
}
506527

507528
// set header parameters
508529
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
@@ -521,7 +542,9 @@
521542
}
522543

523544
if (contentType === 'application/x-www-form-urlencoded') {
524-
request.send(querystring.stringify(this.normalizeParams(formParams)));
545+
request.type('form');
546+
formParams = bodyParam
547+
request.send(JSON.parse(formParams));
525548
} else if (contentType == 'multipart/form-data') {
526549
var _formParams = this.normalizeParams(formParams);
527550
for (var key in _formParams) {

src/ApiClient.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@
387387

388388
var token = Authorization.getToken(this.merchantConfig, this.logger);
389389

390-
var clientId = getClientId();
390+
// var clientId = getClientId();
391391

392-
headerParams['v-c-client-id'] = clientId;
392+
// headerParams['v-c-client-id'] = clientId;
393393

394394
// if (this.merchantConfig.getSolutionId() != null && this.merchantConfig.getSolutionId() != '') {
395395
// headerParams['v-c-solution-id'] = this.merchantConfig.getSolutionId();
@@ -425,6 +425,11 @@
425425
this.logger.info('User-Agent : ' + headerParams['User-Agent']);
426426
this.logger.info(this.constants.END_TRANSACTION);
427427
}
428+
else if (this.merchantConfig.getAuthenticationType().toLowerCase() === this.constants.OAUTH) {
429+
token = 'Bearer ' + token;
430+
headerParams['Authorization'] = token;
431+
this.logger.info(this.constants.AUTHORIZATION + ' : ' + token);
432+
}
428433

429434
return headerParams;
430435
}
@@ -470,6 +475,7 @@
470475
var proxyPort = this.merchantConfig.getProxyPort();
471476
var proxyUser = this.merchantConfig.getProxyUser();
472477
var proxyPassword = this.merchantConfig.getProxyPassword();
478+
var enableClientCert = this.merchantConfig.getEnableClientCert();
473479

474480
var request = superagent(httpMethod, url);
475481

@@ -484,6 +490,17 @@
484490
}
485491
request.proxy(proxy);
486492
}
493+
494+
var fslib = require('fs');
495+
var pathlib = require('path');
496+
497+
if(enableClientCert)
498+
{
499+
var certFile = pathlib.resolve(pathlib.join(this.merchantConfig.getClientCertDir(), this.merchantConfig.getSSLClientCert()));
500+
var keyFile = pathlib.resolve(pathlib.join(this.merchantConfig.getClientCertDir(), this.merchantConfig.getPrivateKey()));
501+
request.cert(fslib.readFileSync(certFile));
502+
request.key(fslib.readFileSync(keyFile));
503+
}
487504

488505
// apply authentications
489506
this.applyAuthToRequest(request, authNames);
@@ -504,7 +521,10 @@
504521
|| httpMethod.toLowerCase() === this.constants.PUT) {
505522
bodyParam = JSON.stringify(bodyParam, null, 0);
506523
}
507-
headerParams = this.callAuthenticationHeader(httpMethod, requestTarget, bodyParam, headerParams);
524+
525+
if (this.merchantConfig.getAuthenticationType().toLowerCase() !== this.constants.MUTUAL_AUTH) {
526+
headerParams = this.callAuthenticationHeader(httpMethod, requestTarget, bodyParam, headerParams);
527+
}
508528

509529
// set header parameters
510530
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
@@ -523,7 +543,9 @@
523543
}
524544

525545
if (contentType === 'application/x-www-form-urlencoded') {
526-
request.send(querystring.stringify(this.normalizeParams(formParams)));
546+
request.type('form');
547+
formParams = bodyParam
548+
request.send(JSON.parse(formParams));
527549
} else if (contentType == 'multipart/form-data') {
528550
var _formParams = this.normalizeParams(formParams);
529551
for (var key in _formParams) {

src/api/OAuthApi.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* OAuth2 API
3+
* OAuth2 Token Service (OAuth2)
4+
*
5+
* OpenAPI spec version: v3
6+
*
7+
* NOTE: This class is auto generated by the swagger code generator program.
8+
* https://github.com/swagger-api/swagger-codegen.git
9+
*
10+
* Swagger Codegen version: 2.3.0
11+
*
12+
* Do not edit the class manually.
13+
*
14+
*/
15+
16+
(function(root, factory) {
17+
if (typeof define === 'function' && define.amd) {
18+
// AMD. Register as an anonymous module.
19+
define(['ApiClient', 'model/AccessTokenResponse', 'model/BadRequestError', 'model/CreateAccessTokenRequest', 'model/ResourceNotFoundError', 'model/UnauthorizedClientError'], factory);
20+
} else if (typeof module === 'object' && module.exports) {
21+
// CommonJS-like environments that support module.exports, like Node.
22+
module.exports = factory(require('../ApiClient'), require('../model/AccessTokenResponse'), require('../model/BadRequestError'), require('../model/CreateAccessTokenRequest'), require('../model/ResourceNotFoundError'), require('../model/UnauthorizedClientError'));
23+
} else {
24+
// Browser globals (root is window)
25+
if (!root.CyberSource) {
26+
root.CyberSource = {};
27+
}
28+
root.CyberSource.OAuthApi = factory(root.CyberSource.ApiClient, root.CyberSource.AccessTokenResponse, root.CyberSource.BadRequestError, root.CyberSource.CreateAccessTokenRequest, root.CyberSource.ResourceNotFoundError, root.CyberSource.UnauthorizedClientError);
29+
}
30+
}(this, function(ApiClient, AccessTokenResponse, BadRequestError, CreateAccessTokenRequest, ResourceNotFoundError, UnauthorizedClientError) {
31+
'use strict';
32+
33+
/**
34+
* OAuth service.
35+
* @module api/OAuthApi
36+
* @version v3
37+
*/
38+
39+
/**
40+
* Constructs a new OAuthApi.
41+
* @alias module:api/OAuthApi
42+
* @class
43+
* @param {module:ApiClient} apiClient Optional API client implementation to use,
44+
* default to {@link module:ApiClient#instance} if unspecified.
45+
*/
46+
var exports = function(configObject, apiClient) {
47+
this.apiClient = apiClient || ApiClient.instance;
48+
49+
this.apiClient.setConfiguration(configObject);
50+
51+
52+
/**
53+
* Callback function to receive the result of the createAccessToken operation.
54+
* @callback module:api/OAuthApi~createAccessTokenCallback
55+
* @param {String} error Error message, if any.
56+
* @param {module:model/AccessTokenResponse} data The data returned by the service call.
57+
* @param {String} response The complete HTTP response.
58+
*/
59+
60+
/**
61+
* Create access token and refresh token
62+
* This request is used by technology partners to obtain an access token and a refresh token, which are contained in the response. The partner can then use the access token for authentication when submitting API requests to CyberSource on behalf of the merchant. The request must include the authorization code that was included in the URL redirect response from CyberSource (see [full documentation](https://developer.cybersource.com/api/developer-guides/OAuth/cybs_extend_intro.html)). Access tokens expire after 15 minutes. The refresh token is used to create a new access token, it expires after one year.
63+
* @param {module:model/CreateAccessTokenRequest} createAccessTokenRequest Request payload
64+
* @param {Object} opts Optional parameters
65+
* @param {String} opts.vCClientCorrelationId We recommended that you submit this header with a unique value in every client request to this endpoint. It is sent back in the response header and logged both in the request log and response log.
66+
* @param {module:api/OAuthApi~createAccessTokenCallback} callback The callback function, accepting three arguments: error, data, response
67+
* data is of type: {@link module:model/AccessTokenResponse}
68+
*/
69+
this.createAccessToken = function(createAccessTokenRequest, opts, callback) {
70+
opts = opts || {};
71+
var postBody = createAccessTokenRequest;
72+
73+
// verify the required parameter 'createAccessTokenRequest' is set
74+
if (createAccessTokenRequest === undefined || createAccessTokenRequest === null) {
75+
throw new Error("Missing the required parameter 'createAccessTokenRequest' when calling createAccessToken");
76+
}
77+
78+
79+
var pathParams = {
80+
};
81+
var queryParams = {
82+
};
83+
var headerParams = {
84+
'v-c-client-correlation-id': opts['vCClientCorrelationId']
85+
};
86+
var formParams = {
87+
};
88+
89+
var authNames = [];
90+
var contentTypes = ['application/x-www-form-urlencoded'];
91+
var accepts = ['application/json;charset=utf-8'];
92+
var returnType = AccessTokenResponse;
93+
94+
return this.apiClient.callApi(
95+
'/oauth2/v3/token', 'POST',
96+
pathParams, queryParams, headerParams, formParams, postBody,
97+
authNames, contentTypes, accepts, returnType, callback
98+
);
99+
}
100+
};
101+
102+
return exports;
103+
}));

src/authentication/core/Authorization.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var Constants = require('../util/Constants');
44
var HttpSingToken = require('../http/HTTPSigToken');
55
var JWTSigToken = require('../jwt/JWTSigToken');
6+
var OAuthToken = require('../oauth/OAuthToken');
67
var ApiException = require('../util/ApiException');
78

89
/**
@@ -14,6 +15,7 @@ exports.getToken = function(merchantConfig, logger){
1415
var authenticationType = merchantConfig.getAuthenticationType().toLowerCase();
1516
var httpSigToken;
1617
var jwtSingToken;
18+
var oauthToken;
1719

1820
if(authenticationType === Constants.HTTP) {
1921
httpSigToken = HttpSingToken.getToken(merchantConfig, logger);
@@ -23,6 +25,10 @@ exports.getToken = function(merchantConfig, logger){
2325
jwtSingToken = JWTSigToken.getToken(merchantConfig, logger);
2426
return jwtSingToken;
2527
}
28+
else if(authenticationType === Constants.OAUTH) {
29+
oauthToken = OAuthToken.getToken(merchantConfig, logger);
30+
return oauthToken;
31+
}
2632
else{
2733
ApiException.ApiException(Constants.AUTH_ERROR, logger);
2834
}

0 commit comments

Comments
 (0)