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

Add support to login using TOTP Code #34

Merged
merged 1 commit into from
Jun 18, 2021
Merged
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
25 changes: 22 additions & 3 deletions avanza/avanza.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@


class Avanza:
def __init__(self, credentials):
def __init__(self, credentials: dict):
"""
Args:
credentials: Login credentials.
Using TOTP secret:
{
'username': 'MY_USERNAME',
'password': 'MY_PASSWORD',
'totpSecret': 'MY_TOTP_SECRET'
}
Using TOTP code:
{
'username': 'MY_USERNAME',
'password': 'MY_PASSWORD',
'totpCode': 'MY_TOTP_CODE'
}
"""
self._authenticationTimeout = MAX_INACTIVE_MINUTES
self._session = requests.Session()

Expand Down Expand Up @@ -66,8 +82,11 @@ def __authenticate(self, credentials):
return self.__validate_2fa(credentials)

def __validate_2fa(self, credentials):
totp = pyotp.TOTP(credentials['totpSecret'], digest=hashlib.sha1)
totp_code = totp.now()
if 'totpSecret' in credentials:
totp = pyotp.TOTP(credentials['totpSecret'], digest=hashlib.sha1)
totp_code = totp.now()
elif 'totpCode' in credentials:
totp_code = credentials['totpCode']

if totp_code is None:
raise ValueError('Failed to get totp code')
Expand Down