Skip to content

Commit

Permalink
feat:support 2fa
Browse files Browse the repository at this point in the history
pump 1.0.11
  • Loading branch information
zbjdonald committed Dec 1, 2022
1 parent df0aed2 commit f84471e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ with SynologyDrive(NAS_USER, NAS_PASS, NAS_IP, NAS_PORT) as synd:
# use http instead of https. https: default is True.
with SynologyDrive(NAS_USER, NAS_PASS, NAS_IP, https=False) as synd:
pass
# Enable 2fa.
with SynologyDrive(NAS_USER, NAS_PASS, otp_code='XXXXXX') as synd:
pass
# use domain name or name + path access drive
# Enabled in Application Portal | Application | Drive | General | Enable customized alias
drive_path_demo = 'your_nas_domain/drive'
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "synology_drive_api"
version = "1.0.10"
version = "1.0.11"
description = "synology drive api python wrapper"
authors = ["zbjdonald <[email protected]>"]
license = "MIT"
Expand Down
10 changes: 8 additions & 2 deletions synology_drive_api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class SynologySession:
"""
_username: str
_password: str
_otp_code: str
# api base url
_base_url: str
# sid token
Expand All @@ -139,7 +140,7 @@ class SynologySession:
_session_expire: bool = True
# dsm version, used for login api version
dsm_version: str = '6'
max_retry: int = 3
max_retry: int = 2

def __init__(self,
username: str,
Expand All @@ -149,12 +150,14 @@ def __init__(self,
nas_domain: Optional[str] = None,
https: Optional[bool] = True,
dsm_version: str = '6',
max_retry: int = 3) -> None:
max_retry: int = 2,
otp_code: Optional[str] = None) -> None:
assert dsm_version in ('6', '7'), "dsm_version should be either '6' or '7'."

nas_address = concat_nas_address(ip_address, port, nas_domain, https)
self._username = username
self._password = password
self._otp_code = otp_code
self.dsm_version = dsm_version
self._base_url = f"{nas_address}/webapi/"
self.req_session.cookies.set_policy(BlockAll())
Expand Down Expand Up @@ -243,6 +246,9 @@ def login(self, application: str):
login_api_version = '2' if self.dsm_version == '6' else '3'
params = {'api': 'SYNO.API.Auth', 'version': login_api_version, 'method': 'login', 'account': self._username,
'passwd': self._password, 'session': application, 'format': 'cookie'}
if self._otp_code is not None:
params['otp_code'] = self._otp_code

if not self._session_expire:
if self._sid is not None:
self._session_expire = False
Expand Down
6 changes: 4 additions & 2 deletions synology_drive_api/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ def __init__(self,
https: bool = True,
enable_label_cache: bool = True,
dsm_version: str = '6',
max_retry: int = 10) -> None:
self.session = SynologySession(username, password, ip_address, port, nas_domain, https, dsm_version, max_retry)
max_retry: int = 2,
otp_code: Optional[str] = None) -> None:
self.session = SynologySession(username, password, ip_address, port, nas_domain, https, dsm_version, max_retry,
otp_code)
self.enable_label_cache = enable_label_cache

def __enter__(self):
Expand Down

0 comments on commit f84471e

Please sign in to comment.