Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to log in when using one time passwords. #13

Merged
merged 3 commits into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/DeGiro.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DeGiroSettupType, AccountConfigType, AccountDataType, CashFoundType, Se
export declare class DeGiro implements DeGiroClassInterface {
private readonly username;
private readonly pwd;
private readonly oneTimePassword;
private jsessionId;
private accountConfig;
private accountData;
Expand Down
2 changes: 1 addition & 1 deletion dist/DeGiro.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions dist/DeGiro.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/DeGiro.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/api/login.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions dist/api/login.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/api/login.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/types/DeGiroSettupType.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export declare type DeGiroSettupType = {
username?: string;
pwd?: string;
oneTimePassword?: string;
jsessionId?: string;
};
//# sourceMappingURL=DeGiroSettupType.d.ts.map
2 changes: 1 addition & 1 deletion dist/types/DeGiroSettupType.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/types/LoginRequestBodyType.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export declare type LoginRequestBodyType = {
isRedirectToMobile: boolean;
password: string;
username: string;
oneTimePassword: string | undefined;
queryParams: {
reason: string;
};
Expand Down
2 changes: 1 addition & 1 deletion dist/types/LoginRequestBodyType.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/types/LoginRequestParamsType.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare type LoginRequestParamsType = {
username: string;
pwd: string;
oneTimePassword: string | undefined;
};
//# sourceMappingURL=LoginRequestParamsType.d.ts.map
2 changes: 1 addition & 1 deletion dist/types/LoginRequestParamsType.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/DeGiro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,27 @@ export class DeGiro implements DeGiroClassInterface {

private readonly username: string
private readonly pwd: string
private readonly oneTimePassword: string | undefined
private jsessionId: string | undefined
private accountConfig: AccountConfigType | undefined
private accountData: AccountDataType | undefined

/* Constructor and generator function */

constructor(params: DeGiroSettupType = {}) {
let { username, pwd, jsessionId } = params
let { username, pwd, oneTimePassword, jsessionId } = params

username = username || process.env['DEGIRO_USER']
pwd = pwd || process.env['DEGIRO_PWD']
oneTimePassword = oneTimePassword || process.env['DEGIRO_OTP']
jsessionId = jsessionId || process.env['DEGIRO_JSESSIONID']

if (!username) throw new Error('DeGiro api needs an username to access')
if (!pwd) throw new Error('DeGiro api needs an password to access')

this.username = username
this.pwd = pwd
this.oneTimePassword = oneTimePassword

this.jsessionId = jsessionId
}
Expand All @@ -101,7 +104,11 @@ export class DeGiro implements DeGiroClassInterface {
login(): Promise<AccountDataType> {
if (this.jsessionId) return this.loginWithJSESSIONID(this.jsessionId)
return new Promise((resolve, reject) => {
loginRequest({ username: this.username, pwd: this.pwd })
loginRequest({
username: this.username,
pwd: this.pwd,
oneTimePassword: this.oneTimePassword
})
.then((loginResponse: LoginResponseType) => {
if (!loginResponse.sessionId) reject('Login response have not a sessionId field')
else return this.getAccountConfig(loginResponse.sessionId)
Expand Down Expand Up @@ -349,4 +356,4 @@ export class DeGiro implements DeGiroClassInterface {
return getConfigDictionaryRequest(<AccountDataType>this.accountData, <AccountConfigType>this.accountConfig)
}

}
}
10 changes: 8 additions & 2 deletions src/api/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function loginRequest(params: LoginRequestParamsType): Promise<LoginRespo
isRedirectToMobile: false,
password: params.pwd,
username: params.username.toLowerCase().trim(),
oneTimePassword: params.oneTimePassword,
queryParams: {
reason: 'session_expired',
},
Expand Down Expand Up @@ -44,12 +45,17 @@ export function loginRequest(params: LoginRequestParamsType): Promise<LoginRespo
debug(`Making request to ${BASE_API_URL + LOGIN_URL_PATH} with options:`)
debug(JSON.stringify(requestOptions, null, 2))
fetch(BASE_API_URL + LOGIN_URL_PATH, requestOptions)
.then((res) => {
if (!payload.oneTimePassword) return res
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be a break point because res is not a promise that is going to be result so the next then step maybe is not going to be triggered never??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be, but I'll test it out.

Hello Daniel!!

First pull the last changes of master branch.
Your changes break one login test
Provide a new custom test for your new awesome feature please to ensure this lib always works
After that I will merge your pull request for sure!

Thank you

Unfortunately tests do not work out of the box. What I still had to do to get them working:

  • npm i -g mocha ts-node
  • I had to install something else but unfortunately I forgot what it was.

Returning from within a .then() should always allow the chain to continue.

Once I got the tests working, unfortunately my account got blocked. Reason being that a one time password can be used only once, and the tests fire numerous times with the same OTP. To make this work, the tests should only log in once, and then reuse that session. For tests like the logout, it should then prompt for a new OTP.

For now, once I get my account reactivated, I will be suspending my own testing, and simply keep using my own fork, as that one does work for me.

debug('Sending OTP')
return fetch(BASE_API_URL + LOGIN_URL_PATH + "/totp", requestOptions);
})
.then(res => res.json())
.then((res) => {
if (!res.sessionId) return reject(res.statusText)
debug('Login response: ', JSON.stringify(res, null, 2))
resolve(res)
})
.catch(reject)
.catch(reject);
})
}
}
1 change: 1 addition & 0 deletions src/types/DeGiroSettupType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type DeGiroSettupType = {
username?: string,
pwd?: string,
oneTimePassword?: string,
jsessionId?: string,
}
3 changes: 2 additions & 1 deletion src/types/LoginRequestBodyType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export type LoginRequestBodyType = {
isRedirectToMobile: boolean,
password: string,
username: string,
oneTimePassword: string | undefined,
queryParams: {
reason: string,
},
}
}
3 changes: 2 additions & 1 deletion src/types/LoginRequestParamsType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type LoginRequestParamsType = {
username: string,
pwd: string,
}
oneTimePassword: string | undefined
}