@@ -22,7 +22,7 @@ module.exports = class TokenManager {
2222 if ( options . iamApikey ) {
2323 this . iamApikey = options . iamApikey
2424 } else {
25- throw new Error ( ` Missing iamApikey parameter.` )
25+ throw new Error ( ' Missing iamApikey parameter.' )
2626 }
2727 }
2828
@@ -95,6 +95,7 @@ module.exports = class TokenManager {
9595 }
9696 } )
9797 }
98+
9899 /**
99100 * This function returns the Authorization header value including the token
100101 * @returns {Promise }
@@ -104,6 +105,7 @@ module.exports = class TokenManager {
104105 return `Bearer ${ token } `
105106 } )
106107 }
108+
107109 /**
108110 * Triggers the remote IAM API token call, saves the response and resolves the loading promise
109111 * with the access_token
@@ -122,19 +124,20 @@ module.exports = class TokenManager {
122124
123125 return this . tokenLoadingPromise
124126 }
127+
125128 /**
126129 * Request an IAM token using an API key and IAM URL.
127130 *
128131 * @private
129132 * @returns {Promise }
130133 */
131134 requestToken ( ) {
132- let options = {
135+ const options = {
133136 url : this . iamUrl ,
134137 method : 'POST' ,
135138 headers : {
136139 'Content-type' : 'application/x-www-form-urlencoded' ,
137- ' Authorization' : 'Basic Yng6Yng='
140+ Authorization : 'Basic Yng6Yng='
138141 } ,
139142 form : {
140143 grant_type : 'urn:ibm:params:oauth:grant-type:apikey' ,
@@ -143,19 +146,20 @@ module.exports = class TokenManager {
143146 }
144147 return this . sendRequest ( options )
145148 }
149+
146150 /**
147151 * Refresh an IAM token using a refresh token.
148152 *
149153 * @private
150154 * @returns {Promise }
151155 */
152156 refreshToken ( ) {
153- let options = {
157+ const options = {
154158 url : this . iamUrl ,
155159 method : 'POST' ,
156160 headers : {
157161 'Content-type' : 'application/x-www-form-urlencoded' ,
158- ' Authorization' : 'Basic Yng6Yng='
162+ Authorization : 'Basic Yng6Yng='
159163 } ,
160164 form : {
161165 grant_type : 'refresh_token' ,
@@ -164,6 +168,7 @@ module.exports = class TokenManager {
164168 }
165169 return this . sendRequest ( options )
166170 }
171+
167172 /**
168173 * Check if currently stored token is expired.
169174 *
@@ -184,13 +189,14 @@ module.exports = class TokenManager {
184189 if ( ! this . tokenInfo . expires_in || ! this . tokenInfo . expiration ) {
185190 return true
186191 }
187- var fractionOfTtl = 0.8
188- var timeToLive = this . tokenInfo . expires_in
189- var expireTime = this . tokenInfo . expiration
190- var currentTime = Math . floor ( Date . now ( ) / 1000 )
191- var refreshTime = expireTime - ( timeToLive * ( 1.0 - fractionOfTtl ) )
192+ const fractionOfTtl = 0.8
193+ const timeToLive = this . tokenInfo . expires_in
194+ const expireTime = this . tokenInfo . expiration
195+ const currentTime = Math . floor ( Date . now ( ) / 1000 )
196+ const refreshTime = expireTime - ( timeToLive * ( 1.0 - fractionOfTtl ) )
192197 return refreshTime < currentTime
193198 }
199+
194200 /**
195201 * Used as a fail-safe to prevent the condition of a refresh token expiring,
196202 * which could happen after around 30 days. This function will return true
@@ -204,11 +210,12 @@ module.exports = class TokenManager {
204210 if ( ! this . tokenInfo . expiration ) {
205211 return true
206212 }
207- var sevenDays = 7 * 24 * 3600
208- var currentTime = Math . floor ( Date . now ( ) / 1000 )
209- var newTokenTime = this . tokenInfo . expiration + sevenDays
213+ const sevenDays = 7 * 24 * 3600
214+ const currentTime = Math . floor ( Date . now ( ) / 1000 )
215+ const newTokenTime = this . tokenInfo . expiration + sevenDays
210216 return newTokenTime < currentTime
211217 }
218+
212219 /**
213220 * Save the response from the IAM service request to the object's state.
214221 *
@@ -219,6 +226,7 @@ module.exports = class TokenManager {
219226 saveTokenInfo ( tokenResponse ) {
220227 this . tokenInfo = tokenResponse
221228 }
229+
222230 /**
223231 * Creates the request.
224232 * @param options - method, url, form
0 commit comments