@@ -153,23 +153,31 @@ class BunqJSClient {
153153 // based on account setting we set a expire date
154154 const createdDate = new Date ( response . token . created + " UTC" ) ;
155155 let sessionTimeout ;
156- if ( response . user_info . UserCompany !== undefined ) {
157- sessionTimeout = response . user_info . UserCompany . session_timeout ;
158- this . logger . debug ( "Received response.user_info.UserCompany.session_timeout from api: " +
159- response . user_info . UserCompany . session_timeout ) ;
160- }
161- else if ( response . user_info . UserPerson !== undefined ) {
162- sessionTimeout = response . user_info . UserPerson . session_timeout ;
163- this . logger . debug ( "Received response.user_info.UserPerson.session_timeout from api: " +
164- response . user_info . UserPerson . session_timeout ) ;
165- }
166- else if ( response . user_info . UserLight !== undefined ) {
167- sessionTimeout = response . user_info . UserLight . session_timeout ;
168- this . logger . debug ( "Received response.user_info.UserLight.session_timeout from api: " +
169- response . user_info . UserLight . session_timeout ) ;
156+ // parse the correct user info from response
157+ let userInfoParsed = this . getUserType ( response . user_info ) ;
158+ // differentiate between oauth api keys and non-oauth api keys
159+ if ( userInfoParsed . isOAuth === false ) {
160+ // get the session timeout
161+ sessionTimeout = userInfoParsed . info . session_timeout ;
162+ this . logger . debug ( "Received userInfoParsed.info.session_timeout from api: " +
163+ userInfoParsed . info . session_timeout ) ;
164+ // set isOAuth to false
165+ this . Session . isOAuthKey = false ;
166+ // set user info
167+ this . Session . userInfo = response . user_info ;
170168 }
171169 else {
172- throw new Error ( "No supported account type found! (Not one of UserLight, UserPerson or UserCompany)" ) ;
170+ // parse the granted and request by user objects
171+ const requestedByUserParsed = this . getUserType ( userInfoParsed . info . requested_by_user ) ;
172+ const grantedByUserParsed = this . getUserType ( userInfoParsed . info . granted_by_user ) ;
173+ // get the session timeout from request_by_user
174+ sessionTimeout = requestedByUserParsed . info . session_timeout ;
175+ this . logger . debug ( "Received requestedByUserParsed.info.session_timeout from api: " +
176+ requestedByUserParsed . info . session_timeout ) ;
177+ // make sure we set isOAuth to true to handle it more easily
178+ this . Session . isOAuthKey ;
179+ // set user info for granted by user
180+ this . Session . userInfo = grantedByUserParsed . info ;
173181 }
174182 // turn time into MS
175183 sessionTimeout = sessionTimeout * 1000 ;
@@ -181,7 +189,6 @@ class BunqJSClient {
181189 this . Session . sessionId = response . id ;
182190 this . Session . sessionToken = response . token . token ;
183191 this . Session . sessionTokenId = response . token . id ;
184- this . Session . userInfo = response . user_info ;
185192 this . logger . debug ( "calculated expireDate: " + createdDate ) ;
186193 this . logger . debug ( "calculated current date: " + new Date ( ) ) ;
187194 // update storage
@@ -299,5 +306,38 @@ class BunqJSClient {
299306 // return the users
300307 return this . Session . userInfo ;
301308 }
309+ /**
310+ * Receives an object with an unknown user type and returns an object with
311+ * the correct info and a isOAuth boolean
312+ * @param userInfo
313+ * @returns {{info: any; isOAuth: boolean} }
314+ */
315+ getUserType ( userInfo ) {
316+ if ( userInfo . UserCompany !== undefined ) {
317+ return {
318+ info : userInfo . UserCompany ,
319+ isOAuth : false
320+ } ;
321+ }
322+ else if ( userInfo . UserPerson !== undefined ) {
323+ return {
324+ info : userInfo . UserPerson ,
325+ isOAuth : false
326+ } ;
327+ }
328+ else if ( userInfo . UserLight !== undefined ) {
329+ return {
330+ info : userInfo . UserLight ,
331+ isOAuth : false
332+ } ;
333+ }
334+ else if ( userInfo . UserApiKey !== undefined ) {
335+ return {
336+ info : userInfo . UserApiKey ,
337+ isOAuth : true
338+ } ;
339+ }
340+ throw new Error ( "No supported account type found! (Not one of UserLight, UserPerson or UserCompany)" ) ;
341+ }
302342}
303343exports . default = BunqJSClient ;
0 commit comments