Skip to content

Commit b44f37f

Browse files
authored
Merge pull request #139 from ibm-cloud-security/rebranding
Rebranding
2 parents 7eb6579 + 82ab395 commit b44f37f

18 files changed

+872
-237
lines changed

.eslintrc

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"root": true,
3+
"parserOptions": {
4+
"ecmaVersion": 2017,
5+
"sourceType": "module"
6+
},
7+
"env": {
8+
"browser": false,
9+
"node": true,
10+
"es6": true
11+
},
12+
13+
"plugins": [
14+
"node"
15+
],
16+
"extends": [
17+
"airbnb-base",
18+
"eslint:recommended",
19+
"plugin:node/recommended"
20+
],
21+
"rules": {
22+
"max-len": [
23+
2,
24+
130,
25+
4
26+
],
27+
"curly": [
28+
2,
29+
"all"
30+
],
31+
"operator-linebreak": [
32+
2,
33+
"after"
34+
],
35+
"indent": [
36+
0,
37+
"tab",
38+
{
39+
"SwitchCase": 1
40+
}
41+
],
42+
"quotes": [
43+
0,
44+
"single"
45+
],
46+
"no-mixed-spaces-and-tabs": 0,
47+
"space-unary-ops": [
48+
2,
49+
{
50+
"nonwords": false,
51+
"overrides": {}
52+
}
53+
],
54+
"keyword-spacing": [
55+
2,
56+
{}
57+
],
58+
"space-infix-ops": 0,
59+
"space-before-blocks": [
60+
2,
61+
"always"
62+
],
63+
"array-bracket-spacing": [
64+
0,
65+
"never",
66+
{
67+
"singleValue": true
68+
}
69+
],
70+
"space-in-parens": [
71+
2,
72+
"never"
73+
],
74+
"no-multiple-empty-lines": 2,
75+
"wrap-iife": 2,
76+
"valid-jsdoc": 2,
77+
"no-param-reassign": [
78+
2,
79+
{
80+
"props": false
81+
}
82+
],
83+
"no-plusplus": 0,
84+
"no-underscore-dangle": 0,
85+
"prefer-rest-params": 0,
86+
"comma-dangle": [
87+
"error",
88+
"never"
89+
],
90+
"semi": 2,
91+
"no-console": 2,
92+
"no-irregular-whitespace": 1,
93+
"no-sparse-arrays": 1,
94+
"object-curly-spacing": "off",
95+
"no-tabs": 0,
96+
"no-unreachable": 1,
97+
"valid-typeof": 1,
98+
"no-shadow": 1,
99+
100+
"no-path-concat": 1,
101+
"eqeqeq": [
102+
2,
103+
"smart"
104+
],
105+
"no-use-before-define": 1,
106+
"no-new-require": 1,
107+
// "no-undef": 2,
108+
109+
110+
"no-mixed-requires": 0,
111+
"no-multi-spaces": 0,
112+
// "no-unused-vars": [2, {"vars": "all", "args": "none"}],
113+
"dot-notation": 0,
114+
"new-cap": 0,
115+
"one-var": 0,
116+
"handle-callback-err": [
117+
2,
118+
"^.*(e|E)rr"
119+
],
120+
"node/no-unsupported-features/es-syntax": [1, {//warn
121+
"version": ">=8.11.0",
122+
"ignores": []
123+
}]
124+
},
125+
"globals": {
126+
"__dirname": true,
127+
"after": true,
128+
"afterEach": true,
129+
"assert": true,
130+
"before": true,
131+
"browser": true,
132+
"Buffer": true,
133+
"by": true,
134+
"console": true,
135+
"describe": true,
136+
"element": true,
137+
"emit": true,
138+
"it": true,
139+
"module": true,
140+
"process": true,
141+
"require": true,
142+
"setTimeout": true,
143+
"transclude": true,
144+
"templateUrl": true,
145+
"cwd": true,
146+
"__base": true
147+
}
148+
}

lib/self-service/self-service-manager.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const log4js = require("log4js");
1515
let request = require("request");
1616
const Q = require("q");
1717
const _ = require("underscore");
18-
const logger = log4js.getLogger("self-service-manager");
1918

19+
const logger = log4js.getLogger("self-service-manager");
2020
const VCAP_SERVICES = "VCAP_SERVICES";
2121
const VCAP_SERVICES_CREDENTIALS = "credentials";
2222
const VCAP_SERVICES_SERVICE_NAME1 = "AdvancedMobileAccess";
@@ -53,18 +53,18 @@ const PRODUCTION_IAM_TOKEN_URL = "https://iam.bluemix.net/oidc/token";
5353
const GENERAL_ERROR = "general_error";
5454

5555
const initError = "Failed to initialize self-service-manager.";
56-
const initErrorMsg = "Ensure your node.js app is either bound to an App ID service instance or pass required parameter to the constructor";
56+
const initErrorMsg = "Ensure your node.js app is either bound to an App ID service " +
57+
"instance or pass required parameter to the constructor";
5758

5859
/**
5960
* The constructor function, options can include: iamApiKey, tenantId, oauthServerUrl and managementUrl.
6061
* if iamApiKey, tenantId or oauthServerUrl not specify it will be taken from the App ID service binding json.
6162
* if iamToken is not passed to the SelfServiceManager APIs, the iamApiKey will be use to get iam tokens before every request.
6263
* if managementUrl not specify it will be taken from the App ID service binding json if exist or construct using the oauthServerUrl.
63-
* @param options
64+
* @param {object} options - options for the constructor
65+
* @return {undefined}
6466
*/
65-
function SelfServiceManager(options) {
66-
options = options || {};
67-
67+
function SelfServiceManager(options={}) {
6868
const vcapServices = JSON.parse(process.env[VCAP_SERVICES] || "{}");
6969
var vcapServiceCredentials = {};
7070
// Find App ID service config
@@ -76,13 +76,13 @@ function SelfServiceManager(options) {
7676
}
7777
}
7878
this.iamApiKey = options.iamApiKey || vcapServiceCredentials[API_KEY];
79-
if(this.iamApiKey) {
79+
if (this.iamApiKey) {
8080
logger.info("using user IAM API key [NOT SHOWING]");
8181
}
8282
this.managementUrl = options[MGMT_URL] || vcapServiceCredentials[MGMT_URL];
8383
let tenantId = options[TENANT_ID] || vcapServiceCredentials[TENANT_ID];
8484
this.tenantId = tenantId;
85-
85+
8686
if (!this.managementUrl) {
8787
if (!tenantId) {
8888
logger.error(initError);
@@ -96,7 +96,7 @@ function SelfServiceManager(options) {
9696
throw new Error(initError);
9797
}
9898
logger.info(OAUTH_SERVER_URL, oauthServerUrl);
99-
99+
100100
let serverUrl = oauthServerUrl.split(OAUTH_V3)[0];
101101
let serverDomain = serverUrl.split(APPID_AUTH);
102102
if (serverDomain[1]) {
@@ -106,9 +106,9 @@ function SelfServiceManager(options) {
106106
logger.error(initErrorMsg);
107107
throw new Error(initError);
108108
}
109-
this.managementUrl += MGMT_V4 + tenantId;
109+
this.managementUrl += MGMT_V4 + tenantId;
110110
}
111-
111+
112112
this.iamTokenUrl = options[IAM_TOKEN_URL] || PRODUCTION_IAM_TOKEN_URL;
113113
logger.info(IAM_TOKEN_URL, this.iamTokenUrl);
114114
logger.info(MGMT_URL, this.managementUrl);
@@ -121,7 +121,7 @@ function SelfServiceManager(options) {
121121
* @param {string=} iamToken, optional, if passed request to the server will use this token.
122122
* @return {Object} The created user SCIM.
123123
*/
124-
SelfServiceManager.prototype.signUp = function (userData, language='en', iamToken) {
124+
SelfServiceManager.prototype.signUp = function (userData, language = 'en', iamToken) {
125125
const deferred = Q.defer();
126126
let url = this.managementUrl + MGMT_SIGN_UP_PATH;
127127
_getIAMToken(iamToken, this.iamApiKey, this.iamTokenUrl).then(function (token) {
@@ -137,7 +137,7 @@ SelfServiceManager.prototype.signUp = function (userData, language='en', iamToke
137137
* @param {string} [language='en'] the user language code.
138138
* @return {Object} The user SCIM profile.
139139
*/
140-
SelfServiceManager.prototype.forgotPassword = function (email, language='en', iamToken) {
140+
SelfServiceManager.prototype.forgotPassword = function (email, language = 'en', iamToken) {
141141
const deferred = Q.defer();
142142
let url = this.managementUrl + MGMT_FORGOT_PASSWORD_PATH;
143143
_getIAMToken(iamToken, this.iamApiKey, this.iamTokenUrl).then(function (token) {
@@ -153,11 +153,11 @@ SelfServiceManager.prototype.forgotPassword = function (email, language='en', ia
153153
* @param {string=} iamToken, optional, if passed request to the server will use this token.
154154
* @return resolve if notification sent successfully, else reject with the error.
155155
*/
156-
SelfServiceManager.prototype.resendNotification = function (uuid, templateName, language='en', iamToken) {
156+
SelfServiceManager.prototype.resendNotification = function (uuid, templateName, language = 'en', iamToken) {
157157
const deferred = Q.defer();
158158
let url = this.managementUrl + MGMT_RESEND_PATH + templateName;
159159
_getIAMToken(iamToken, this.iamApiKey, this.iamTokenUrl).then(function (token) {
160-
_handleRequest(token, POST, url, {uuid: uuid} , {language: language}, RESEND_NOTIFICATION, deferred);
160+
_handleRequest(token, POST, url, {uuid: uuid}, {language: language}, RESEND_NOTIFICATION, deferred);
161161
}).catch(deferred.reject);
162162
return deferred.promise;
163163
};
@@ -172,7 +172,7 @@ SelfServiceManager.prototype.getSignUpConfirmationResult = function (context, ia
172172
const deferred = Q.defer();
173173
let url = this.managementUrl + MGMT_SIGN_UP_RESULT_PATH;
174174
_getIAMToken(iamToken, this.iamApiKey, this.iamTokenUrl).then(function (token) {
175-
_handleRequest(token, POST, url, {context: context} , {}, SIGN_UP_RESULT, deferred);
175+
_handleRequest(token, POST, url, {context: context}, {}, SIGN_UP_RESULT, deferred);
176176
}).catch(deferred.reject);
177177
return deferred.promise;
178178
};
@@ -187,7 +187,7 @@ SelfServiceManager.prototype.getForgotPasswordConfirmationResult = function (con
187187
const deferred = Q.defer();
188188
let url = this.managementUrl + MGMT_FORGOT_PASSWORD_RESULT_PATH;
189189
_getIAMToken(iamToken, this.iamApiKey, this.iamTokenUrl).then(function (token) {
190-
_handleRequest(token, POST, url, {context: context} , {}, FORGOT_PASSWORD_RESULT, deferred);
190+
_handleRequest(token, POST, url, {context: context}, {}, FORGOT_PASSWORD_RESULT, deferred);
191191
}).catch(deferred.reject);
192192
return deferred.promise;
193193
};
@@ -202,18 +202,18 @@ SelfServiceManager.prototype.getForgotPasswordConfirmationResult = function (con
202202
* @return {Object} The user SCIM profile.
203203
*/
204204

205-
SelfServiceManager.prototype.setUserNewPassword = function (uuid, newPassword, language='en', changedIpAddress=null, iamToken) {
205+
SelfServiceManager.prototype.setUserNewPassword = function (uuid, newPassword, language = 'en', changedIpAddress = null, iamToken) {
206206
const deferred = Q.defer();
207207
let url = this.managementUrl + MGMT_CHANGE_PASSWORD_PATH;
208208
_getIAMToken(iamToken, this.iamApiKey, this.iamTokenUrl).then(function (token) {
209-
let replacePasswordBody = {
209+
let replacePasswordBody = {
210210
uuid: uuid,
211211
newPassword: newPassword
212212
};
213213
if (changedIpAddress) {
214214
replacePasswordBody.changedIpAddress = changedIpAddress;
215215
}
216-
_handleRequest(token, POST, url, replacePasswordBody , {language: language}, CHANGE_USER_PASSWORD, deferred);
216+
_handleRequest(token, POST, url, replacePasswordBody, {language: language}, CHANGE_USER_PASSWORD, deferred);
217217
}).catch(deferred.reject);
218218
return deferred.promise;
219219
};
@@ -227,7 +227,7 @@ SelfServiceManager.prototype.getUserDetails = function (uuid, iamToken) {
227227
const deferred = Q.defer();
228228
let url = this.managementUrl + MGMT_USERS_PATH + uuid;
229229
_getIAMToken(iamToken, this.iamApiKey, this.iamTokenUrl).then(function (token) {
230-
_handleRequest(token, GET, url, {} , {}, GET_USER_DETAILS, deferred);
230+
_handleRequest(token, GET, url, {}, {}, GET_USER_DETAILS, deferred);
231231
}).catch(deferred.reject);
232232
return deferred.promise;
233233
};
@@ -247,7 +247,7 @@ SelfServiceManager.prototype.updateUserDetails = function (uuid, userData, iamTo
247247
return deferred.promise;
248248
};
249249

250-
let _handleRequest = function(iamToken, method, url, body, queryObject , action, deferred) {
250+
let _handleRequest = function (iamToken, method, url, body, queryObject, action, deferred) {
251251
let reqOptions = {
252252
url: url,
253253
method: method,
@@ -261,7 +261,7 @@ let _handleRequest = function(iamToken, method, url, body, queryObject , action,
261261
reqOptions.json = body;
262262
}
263263
request(reqOptions, function (err, response, responseBody) {
264-
if(!err && response.statusCode >= 200 && response.statusCode < 300) {
264+
if (!err && response.statusCode >= 200 && response.statusCode < 300) {
265265
logger.debug("request " + action + " success");
266266
logger.debug("response body: " + JSON.stringify(responseBody));
267267
deferred.resolve(responseBody);
@@ -284,26 +284,26 @@ let _handleRequest = function(iamToken, method, url, body, queryObject , action,
284284
});
285285
};
286286

287-
let _getIAMToken = function(iamToken, iamApiKey, iamTokenUrl) {
287+
let _getIAMToken = function (iamToken, iamApiKey, iamTokenUrl) {
288288
if (iamToken && _.isString(iamToken)) {
289289
return Promise.resolve(iamToken);
290290
}
291291
if (!iamApiKey) {
292292
return Promise.reject("You must pass 'iamToken' to self-service-manager APIs or specify 'iamApiKey' in selfServiceManager init options.");
293293
}
294294
var reqOptions = {
295-
url: iamTokenUrl,
295+
url: iamTokenUrl,
296296
method: "POST",
297297
headers: {
298298
"Content-Type": "application/x-www-form-urlencoded",
299299
"Accept": "application/json"
300300
},
301-
form : {
302-
"grant_type":"urn:ibm:params:oauth:grant-type:apikey",
303-
"apikey" : iamApiKey
301+
form: {
302+
"grant_type": "urn:ibm:params:oauth:grant-type:apikey",
303+
"apikey": iamApiKey
304304
}
305305
};
306-
return new Promise(function(resolve, reject) {
306+
return new Promise(function (resolve, reject) {
307307
request(reqOptions, function (error, response, body) {
308308
if (error) {
309309
logger.error("Obtained IAM token failure: " + error.message);

lib/strategies/api-strategy.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ function ApiStrategy(options) {
4040
ApiStrategy.STRATEGY_NAME = STRATEGY_NAME;
4141
ApiStrategy.DEFAULT_SCOPE = "appid_default";
4242

43-
ApiStrategy.prototype.authenticate = function (req, options) {
43+
ApiStrategy.prototype.authenticate = function (req, options={}) {
4444
var self = this;
4545
logger.debug("authenticate");
46-
options = options || {};
46+
4747
var requiredScope = ApiStrategy.DEFAULT_SCOPE;
4848
if (options.scope) {
4949
requiredScope += " " + options.scope;
@@ -93,7 +93,8 @@ ApiStrategy.prototype.authenticate = function (req, options) {
9393
}
9494
}
9595
if (!found) {
96-
logger.warn("access_token does not contain required scope. Expected ::", requiredScope, " Received ::", accessToken.scope);
96+
logger.warn("access_token does not contain required scope. Expected ::", requiredScope, " Received ::",
97+
accessToken.scope);
9798
return self.fail(buildWwwAuthenticateHeader(requiredScope, ERROR.INSUFFICIENT_SCOPE), 401);
9899
}
99100
}
@@ -117,15 +118,15 @@ ApiStrategy.prototype.authenticate = function (req, options) {
117118
}
118119
logger.debug("authentication success");
119120
return self.success(identityToken || null);
120-
}).catch(function (err) {
121+
}).catch(() => {
121122
logger.debug("authentication failed due to invalid identity token");
122123
return self.fail(buildWwwAuthenticateHeader(requiredScope, ERROR.INVALID_TOKEN), 401);
123124
});
124125
} else {
125126
logger.debug("authentication success: identity_token not found. Proceeding with access_token only");
126127
return self.success(identityToken || null);
127128
}
128-
}).catch(function (err) {
129+
}).catch(function () {
129130
logger.debug("authentication failed due to invalid access token");
130131
return self.fail(buildWwwAuthenticateHeader(requiredScope, ERROR.INVALID_TOKEN), 401);
131132
});

0 commit comments

Comments
 (0)