Skip to content
This repository was archived by the owner on Aug 6, 2021. It is now read-only.

Commit 2967f60

Browse files
committed
Started implementing oauth support and refactored user type checking
1 parent a547917 commit 2967f60

File tree

4 files changed

+123
-34
lines changed

4 files changed

+123
-34
lines changed

dist/BunqJSClient.js

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
303343
exports.default = BunqJSClient;

dist/Session.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Session {
1212
this.apiKey = null;
1313
this.encryptionKey = false;
1414
this.allowdIps = [];
15+
this.isOAuthKey = false;
1516
// rsa key storage
1617
this.publicKey = null;
1718
this.publicKeyPem = null;

src/BunqJSClient.ts

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,30 +181,44 @@ export default class BunqJSClient {
181181
// based on account setting we set a expire date
182182
const createdDate = new Date(response.token.created + " UTC");
183183
let sessionTimeout;
184-
if (response.user_info.UserCompany !== undefined) {
185-
sessionTimeout = response.user_info.UserCompany.session_timeout;
184+
185+
// parse the correct user info from response
186+
let userInfoParsed = this.getUserType(response.user_info);
187+
188+
// differentiate between oauth api keys and non-oauth api keys
189+
if (userInfoParsed.isOAuth === false) {
190+
// get the session timeout
191+
sessionTimeout = userInfoParsed.info.session_timeout;
186192
this.logger.debug(
187-
"Received response.user_info.UserCompany.session_timeout from api: " +
188-
response.user_info.UserCompany.session_timeout
193+
"Received userInfoParsed.info.session_timeout from api: " +
194+
userInfoParsed.info.session_timeout
189195
);
190-
} else if (response.user_info.UserPerson !== undefined) {
191-
sessionTimeout = response.user_info.UserPerson.session_timeout;
192196

193-
this.logger.debug(
194-
"Received response.user_info.UserPerson.session_timeout from api: " +
195-
response.user_info.UserPerson.session_timeout
197+
// set isOAuth to false
198+
this.Session.isOAuthKey = false;
199+
// set user info
200+
this.Session.userInfo = response.user_info;
201+
} else {
202+
// parse the granted and request by user objects
203+
const requestedByUserParsed = this.getUserType(
204+
userInfoParsed.info.requested_by_user
196205
);
197-
} else if (response.user_info.UserLight !== undefined) {
198-
sessionTimeout = response.user_info.UserLight.session_timeout;
206+
const grantedByUserParsed = this.getUserType(
207+
userInfoParsed.info.granted_by_user
208+
);
209+
210+
// get the session timeout from request_by_user
211+
sessionTimeout = requestedByUserParsed.info.session_timeout;
199212

200213
this.logger.debug(
201-
"Received response.user_info.UserLight.session_timeout from api: " +
202-
response.user_info.UserLight.session_timeout
203-
);
204-
} else {
205-
throw new Error(
206-
"No supported account type found! (Not one of UserLight, UserPerson or UserCompany)"
214+
"Received requestedByUserParsed.info.session_timeout from api: " +
215+
requestedByUserParsed.info.session_timeout
207216
);
217+
218+
// make sure we set isOAuth to true to handle it more easily
219+
this.Session.isOAuthKey
220+
// set user info for granted by user
221+
this.Session.userInfo = grantedByUserParsed.info;
208222
}
209223

210224
// turn time into MS
@@ -219,7 +233,6 @@ export default class BunqJSClient {
219233
this.Session.sessionId = response.id;
220234
this.Session.sessionToken = response.token.token;
221235
this.Session.sessionTokenId = response.token.id;
222-
this.Session.userInfo = response.user_info;
223236

224237
this.logger.debug("calculated expireDate: " + createdDate);
225238
this.logger.debug("calculated current date: " + new Date());
@@ -413,4 +426,38 @@ export default class BunqJSClient {
413426
// return the users
414427
return this.Session.userInfo;
415428
}
429+
430+
/**
431+
* Receives an object with an unknown user type and returns an object with
432+
* the correct info and a isOAuth boolean
433+
* @param userInfo
434+
* @returns {{info: any; isOAuth: boolean}}
435+
*/
436+
private getUserType(userInfo: any) {
437+
if (userInfo.UserCompany !== undefined) {
438+
return {
439+
info: userInfo.UserCompany,
440+
isOAuth: false
441+
};
442+
} else if (userInfo.UserPerson !== undefined) {
443+
return {
444+
info: userInfo.UserPerson,
445+
isOAuth: false
446+
};
447+
} else if (userInfo.UserLight !== undefined) {
448+
return {
449+
info: userInfo.UserLight,
450+
isOAuth: false
451+
};
452+
} else if (userInfo.UserApiKey !== undefined) {
453+
return {
454+
info: userInfo.UserApiKey,
455+
isOAuth: true
456+
};
457+
}
458+
459+
throw new Error(
460+
"No supported account type found! (Not one of UserLight, UserPerson or UserCompany)"
461+
);
462+
}
416463
}

src/Session.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default class Session {
2424
public apiKey: string | boolean = null;
2525
public encryptionKey: string | boolean = false;
2626
public allowdIps: string[] = [];
27+
public isOAuthKey: boolean = false;
2728

2829
// target enviroment and target envoriment api url
2930
public environment: string;

0 commit comments

Comments
 (0)