|
9 | 9 | import time |
10 | 10 | import traceback |
11 | 11 | import types |
12 | | - |
13 | | -import seafile |
| 12 | +import urllib.error |
14 | 13 |
|
15 | 14 | def load_as_module(path: str): |
16 | 15 | module = types.ModuleType("dynamic_module") |
@@ -127,7 +126,7 @@ def __run(self, cmd: list, **kwargs): |
127 | 126 | logging.debug(f'run `{ subprocess.list2cmdline(cmd) }`') |
128 | 127 | return subprocess.run(cmd, **kwargs) |
129 | 128 |
|
130 | | - def __run_tfa(self, totp_secret: str): |
| 129 | + def __run_tfa(self): |
131 | 130 | cmd = ['oathtool', '--base32', '--totp', self.totp_secret] |
132 | 131 | mask_cmd = self.__mask_cmd(cmd, ['--totp']) |
133 | 132 | logging.debug(f'run `{ subprocess.list2cmdline(mask_cmd) }`') |
@@ -178,10 +177,27 @@ def __get_rpc_client(self): |
178 | 177 | def __get_cached_token(self): |
179 | 178 | if self.__cached_token is None: |
180 | 179 | seafcli.seafile_datadir = str(self.seafile_data_dir) |
181 | | - tfa = self.__run_tfa(self.totp_secret) |
182 | | - self.__cached_token = seafcli.get_token(self.server_url, self.username, self.password, tfa, seafcli.DEFAULT_CONF_DIR) |
| 180 | + tfa = self.__run_tfa() |
| 181 | + # https://github.com/haiwen/seahub/blob/master/seahub/api2/serializers.py#L137 |
| 182 | + # https://github.com/haiwen/seahub/blob/master/seahub/two_factor/views/login.py#L212 |
| 183 | + # if otp failed, could check log file: `/shared/logs/seahub.log` |
| 184 | + self.__cached_token = self.__debug_seafile_error( |
| 185 | + lambda: seafcli.get_token(self.server_url, self.username, self.password, tfa, seafcli.DEFAULT_CONF_DIR) |
| 186 | + ) |
183 | 187 | return self.__cached_token |
184 | 188 |
|
| 189 | + def __debug_seafile_error(self, fn): |
| 190 | + try: |
| 191 | + return fn() |
| 192 | + except Exception as e: |
| 193 | + if isinstance(e, urllib.error.HTTPError): |
| 194 | + try: |
| 195 | + body = e.read().decode('utf-8') |
| 196 | + except Exception: |
| 197 | + body = None |
| 198 | + logging.error(f'HTTPError: {e.code} {body}') |
| 199 | + raise |
| 200 | + |
185 | 201 | def init(self): |
186 | 202 | logging.info(f'Initializing...') |
187 | 203 | if not self.seafile_ini.exists(): |
|
0 commit comments