@@ -15,8 +15,8 @@ const log4js = require("log4js");
15
15
let request = require ( "request" ) ;
16
16
const Q = require ( "q" ) ;
17
17
const _ = require ( "underscore" ) ;
18
- const logger = log4js . getLogger ( "self-service-manager" ) ;
19
18
19
+ const logger = log4js . getLogger ( "self-service-manager" ) ;
20
20
const VCAP_SERVICES = "VCAP_SERVICES" ;
21
21
const VCAP_SERVICES_CREDENTIALS = "credentials" ;
22
22
const VCAP_SERVICES_SERVICE_NAME1 = "AdvancedMobileAccess" ;
@@ -53,18 +53,18 @@ const PRODUCTION_IAM_TOKEN_URL = "https://iam.bluemix.net/oidc/token";
53
53
const GENERAL_ERROR = "general_error" ;
54
54
55
55
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" ;
57
58
58
59
/**
59
60
* The constructor function, options can include: iamApiKey, tenantId, oauthServerUrl and managementUrl.
60
61
* if iamApiKey, tenantId or oauthServerUrl not specify it will be taken from the App ID service binding json.
61
62
* if iamToken is not passed to the SelfServiceManager APIs, the iamApiKey will be use to get iam tokens before every request.
62
63
* 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 }
64
66
*/
65
- function SelfServiceManager ( options ) {
66
- options = options || { } ;
67
-
67
+ function SelfServiceManager ( options = { } ) {
68
68
const vcapServices = JSON . parse ( process . env [ VCAP_SERVICES ] || "{}" ) ;
69
69
var vcapServiceCredentials = { } ;
70
70
// Find App ID service config
@@ -76,13 +76,13 @@ function SelfServiceManager(options) {
76
76
}
77
77
}
78
78
this . iamApiKey = options . iamApiKey || vcapServiceCredentials [ API_KEY ] ;
79
- if ( this . iamApiKey ) {
79
+ if ( this . iamApiKey ) {
80
80
logger . info ( "using user IAM API key [NOT SHOWING]" ) ;
81
81
}
82
82
this . managementUrl = options [ MGMT_URL ] || vcapServiceCredentials [ MGMT_URL ] ;
83
83
let tenantId = options [ TENANT_ID ] || vcapServiceCredentials [ TENANT_ID ] ;
84
84
this . tenantId = tenantId ;
85
-
85
+
86
86
if ( ! this . managementUrl ) {
87
87
if ( ! tenantId ) {
88
88
logger . error ( initError ) ;
@@ -96,7 +96,7 @@ function SelfServiceManager(options) {
96
96
throw new Error ( initError ) ;
97
97
}
98
98
logger . info ( OAUTH_SERVER_URL , oauthServerUrl ) ;
99
-
99
+
100
100
let serverUrl = oauthServerUrl . split ( OAUTH_V3 ) [ 0 ] ;
101
101
let serverDomain = serverUrl . split ( APPID_AUTH ) ;
102
102
if ( serverDomain [ 1 ] ) {
@@ -106,9 +106,9 @@ function SelfServiceManager(options) {
106
106
logger . error ( initErrorMsg ) ;
107
107
throw new Error ( initError ) ;
108
108
}
109
- this . managementUrl += MGMT_V4 + tenantId ;
109
+ this . managementUrl += MGMT_V4 + tenantId ;
110
110
}
111
-
111
+
112
112
this . iamTokenUrl = options [ IAM_TOKEN_URL ] || PRODUCTION_IAM_TOKEN_URL ;
113
113
logger . info ( IAM_TOKEN_URL , this . iamTokenUrl ) ;
114
114
logger . info ( MGMT_URL , this . managementUrl ) ;
@@ -121,7 +121,7 @@ function SelfServiceManager(options) {
121
121
* @param {string= } iamToken, optional, if passed request to the server will use this token.
122
122
* @return {Object } The created user SCIM.
123
123
*/
124
- SelfServiceManager . prototype . signUp = function ( userData , language = 'en' , iamToken ) {
124
+ SelfServiceManager . prototype . signUp = function ( userData , language = 'en' , iamToken ) {
125
125
const deferred = Q . defer ( ) ;
126
126
let url = this . managementUrl + MGMT_SIGN_UP_PATH ;
127
127
_getIAMToken ( iamToken , this . iamApiKey , this . iamTokenUrl ) . then ( function ( token ) {
@@ -137,7 +137,7 @@ SelfServiceManager.prototype.signUp = function (userData, language='en', iamToke
137
137
* @param {string } [language='en'] the user language code.
138
138
* @return {Object } The user SCIM profile.
139
139
*/
140
- SelfServiceManager . prototype . forgotPassword = function ( email , language = 'en' , iamToken ) {
140
+ SelfServiceManager . prototype . forgotPassword = function ( email , language = 'en' , iamToken ) {
141
141
const deferred = Q . defer ( ) ;
142
142
let url = this . managementUrl + MGMT_FORGOT_PASSWORD_PATH ;
143
143
_getIAMToken ( iamToken , this . iamApiKey , this . iamTokenUrl ) . then ( function ( token ) {
@@ -153,11 +153,11 @@ SelfServiceManager.prototype.forgotPassword = function (email, language='en', ia
153
153
* @param {string= } iamToken, optional, if passed request to the server will use this token.
154
154
* @return resolve if notification sent successfully, else reject with the error.
155
155
*/
156
- SelfServiceManager . prototype . resendNotification = function ( uuid , templateName , language = 'en' , iamToken ) {
156
+ SelfServiceManager . prototype . resendNotification = function ( uuid , templateName , language = 'en' , iamToken ) {
157
157
const deferred = Q . defer ( ) ;
158
158
let url = this . managementUrl + MGMT_RESEND_PATH + templateName ;
159
159
_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 ) ;
161
161
} ) . catch ( deferred . reject ) ;
162
162
return deferred . promise ;
163
163
} ;
@@ -172,7 +172,7 @@ SelfServiceManager.prototype.getSignUpConfirmationResult = function (context, ia
172
172
const deferred = Q . defer ( ) ;
173
173
let url = this . managementUrl + MGMT_SIGN_UP_RESULT_PATH ;
174
174
_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 ) ;
176
176
} ) . catch ( deferred . reject ) ;
177
177
return deferred . promise ;
178
178
} ;
@@ -187,7 +187,7 @@ SelfServiceManager.prototype.getForgotPasswordConfirmationResult = function (con
187
187
const deferred = Q . defer ( ) ;
188
188
let url = this . managementUrl + MGMT_FORGOT_PASSWORD_RESULT_PATH ;
189
189
_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 ) ;
191
191
} ) . catch ( deferred . reject ) ;
192
192
return deferred . promise ;
193
193
} ;
@@ -202,18 +202,18 @@ SelfServiceManager.prototype.getForgotPasswordConfirmationResult = function (con
202
202
* @return {Object } The user SCIM profile.
203
203
*/
204
204
205
- SelfServiceManager . prototype . setUserNewPassword = function ( uuid , newPassword , language = 'en' , changedIpAddress = null , iamToken ) {
205
+ SelfServiceManager . prototype . setUserNewPassword = function ( uuid , newPassword , language = 'en' , changedIpAddress = null , iamToken ) {
206
206
const deferred = Q . defer ( ) ;
207
207
let url = this . managementUrl + MGMT_CHANGE_PASSWORD_PATH ;
208
208
_getIAMToken ( iamToken , this . iamApiKey , this . iamTokenUrl ) . then ( function ( token ) {
209
- let replacePasswordBody = {
209
+ let replacePasswordBody = {
210
210
uuid : uuid ,
211
211
newPassword : newPassword
212
212
} ;
213
213
if ( changedIpAddress ) {
214
214
replacePasswordBody . changedIpAddress = changedIpAddress ;
215
215
}
216
- _handleRequest ( token , POST , url , replacePasswordBody , { language : language } , CHANGE_USER_PASSWORD , deferred ) ;
216
+ _handleRequest ( token , POST , url , replacePasswordBody , { language : language } , CHANGE_USER_PASSWORD , deferred ) ;
217
217
} ) . catch ( deferred . reject ) ;
218
218
return deferred . promise ;
219
219
} ;
@@ -227,7 +227,7 @@ SelfServiceManager.prototype.getUserDetails = function (uuid, iamToken) {
227
227
const deferred = Q . defer ( ) ;
228
228
let url = this . managementUrl + MGMT_USERS_PATH + uuid ;
229
229
_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 ) ;
231
231
} ) . catch ( deferred . reject ) ;
232
232
return deferred . promise ;
233
233
} ;
@@ -247,7 +247,7 @@ SelfServiceManager.prototype.updateUserDetails = function (uuid, userData, iamTo
247
247
return deferred . promise ;
248
248
} ;
249
249
250
- let _handleRequest = function ( iamToken , method , url , body , queryObject , action , deferred ) {
250
+ let _handleRequest = function ( iamToken , method , url , body , queryObject , action , deferred ) {
251
251
let reqOptions = {
252
252
url : url ,
253
253
method : method ,
@@ -261,7 +261,7 @@ let _handleRequest = function(iamToken, method, url, body, queryObject , action,
261
261
reqOptions . json = body ;
262
262
}
263
263
request ( reqOptions , function ( err , response , responseBody ) {
264
- if ( ! err && response . statusCode >= 200 && response . statusCode < 300 ) {
264
+ if ( ! err && response . statusCode >= 200 && response . statusCode < 300 ) {
265
265
logger . debug ( "request " + action + " success" ) ;
266
266
logger . debug ( "response body: " + JSON . stringify ( responseBody ) ) ;
267
267
deferred . resolve ( responseBody ) ;
@@ -284,26 +284,26 @@ let _handleRequest = function(iamToken, method, url, body, queryObject , action,
284
284
} ) ;
285
285
} ;
286
286
287
- let _getIAMToken = function ( iamToken , iamApiKey , iamTokenUrl ) {
287
+ let _getIAMToken = function ( iamToken , iamApiKey , iamTokenUrl ) {
288
288
if ( iamToken && _ . isString ( iamToken ) ) {
289
289
return Promise . resolve ( iamToken ) ;
290
290
}
291
291
if ( ! iamApiKey ) {
292
292
return Promise . reject ( "You must pass 'iamToken' to self-service-manager APIs or specify 'iamApiKey' in selfServiceManager init options." ) ;
293
293
}
294
294
var reqOptions = {
295
- url : iamTokenUrl ,
295
+ url : iamTokenUrl ,
296
296
method : "POST" ,
297
297
headers : {
298
298
"Content-Type" : "application/x-www-form-urlencoded" ,
299
299
"Accept" : "application/json"
300
300
} ,
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
304
304
}
305
305
} ;
306
- return new Promise ( function ( resolve , reject ) {
306
+ return new Promise ( function ( resolve , reject ) {
307
307
request ( reqOptions , function ( error , response , body ) {
308
308
if ( error ) {
309
309
logger . error ( "Obtained IAM token failure: " + error . message ) ;
0 commit comments