Repository navigation
Expand file tree
/
Copy pathcredentials.py
More file actions
254 lines (208 loc) · 7.71 KB
/
Copy pathcredentials.py
File metadata and controls
254 lines (208 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""
Credential storage abstraction for BotMyPC.
Stores Bot Token and Master User ID securely:
1. Primary: Windows Credential Manager via `keyring`.
2. Secondary (Fallback): AES-CBC/HMAC-SHA256 via `cryptography.fernet.Fernet`,
where the Fernet encryption key is protected using Windows DPAPI
(Data Protection API via win32crypt.CryptProtectData). The key is encrypted
using the Windows user's logon credentials and cannot be decrypted outside
the specific Windows account and machine.
"""
from __future__ import annotations
import json
import logging
from pathlib import Path
from typing import Optional, Dict
import ctypes
from ctypes import wintypes
import keyring
from cryptography.fernet import Fernet
from config import (
APP_NAME,
FALLBACK_CREDENTIALS_FILE,
FALLBACK_KEY_FILE,
)
from i18n import t
logger = logging.getLogger(APP_NAME)
SERVICE_NAME: str = APP_NAME
KEY_BOT_TOKEN: str = "telegram_bot_token"
KEY_MASTER_USER_ID: str = "telegram_master_user_id"
# =====================================================================
# DPAPI Utilities (win32crypt with ctypes fallback)
# =====================================================================
def _dpapi_protect(data: bytes) -> bytes:
"""Encrypts bytes using Windows DPAPI for the current logged-in user."""
try:
import win32crypt
return win32crypt.CryptProtectData(data, "BotMyPC DPAPI Key", None, None, None, 0)
except ImportError:
pass
# ctypes fallback for CryptProtectData
class DATA_BLOB(ctypes.Structure):
_fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_byte))]
crypt32 = ctypes.windll.crypt32
in_blob = DATA_BLOB(len(data), ctypes.cast(ctypes.create_string_buffer(data), ctypes.POINTER(ctypes.c_byte)))
out_blob = DATA_BLOB()
if not crypt32.CryptProtectData(
ctypes.byref(in_blob),
"BotMyPC DPAPI Key",
None,
None,
None,
0,
ctypes.byref(out_blob)
):
raise ctypes.WinError()
try:
buffer = (ctypes.c_byte * out_blob.cbData)()
ctypes.memmove(buffer, out_blob.pbData, out_blob.cbData)
return bytes(buffer)
finally:
ctypes.windll.kernel32.LocalFree(out_blob.pbData)
def _dpapi_unprotect(data: bytes) -> bytes:
"""Decrypts bytes using Windows DPAPI for the current logged-in user."""
try:
import win32crypt
_, unprotected = win32crypt.CryptUnprotectData(data, None, None, None, 0)
return unprotected
except ImportError:
pass
# ctypes fallback for CryptUnprotectData
class DATA_BLOB(ctypes.Structure):
_fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_byte))]
crypt32 = ctypes.windll.crypt32
in_blob = DATA_BLOB(len(data), ctypes.cast(ctypes.create_string_buffer(data), ctypes.POINTER(ctypes.c_byte)))
out_blob = DATA_BLOB()
if not crypt32.CryptUnprotectData(
ctypes.byref(in_blob),
None,
None,
None,
None,
0,
ctypes.byref(out_blob)
):
raise ctypes.WinError()
try:
buffer = (ctypes.c_byte * out_blob.cbData)()
ctypes.memmove(buffer, out_blob.pbData, out_blob.cbData)
return bytes(buffer)
finally:
ctypes.windll.kernel32.LocalFree(out_blob.pbData)
# =====================================================================
# Fallback Storage via DPAPI + Fernet
# =====================================================================
def _get_or_create_fernet_cipher() -> Fernet:
"""
Retrieves or creates a Fernet key whose bytes are protected on disk via DPAPI.
The raw Fernet key is never stored on disk in plaintext.
"""
if FALLBACK_KEY_FILE.exists():
encrypted_key = FALLBACK_KEY_FILE.read_bytes()
raw_key = _dpapi_unprotect(encrypted_key)
return Fernet(raw_key)
# Generate brand new Fernet key and protect with DPAPI
raw_key = Fernet.generate_key()
encrypted_key = _dpapi_protect(raw_key)
FALLBACK_KEY_FILE.parent.mkdir(parents=True, exist_ok=True)
FALLBACK_KEY_FILE.write_bytes(encrypted_key)
return Fernet(raw_key)
def _read_fallback_store() -> Dict[str, str]:
"""Reads and decrypts the fallback credentials JSON file."""
if not FALLBACK_CREDENTIALS_FILE.exists():
return {}
try:
cipher = _get_or_create_fernet_cipher()
encrypted_data = FALLBACK_CREDENTIALS_FILE.read_bytes()
decrypted_json = cipher.decrypt(encrypted_data).decode("utf-8")
return json.loads(decrypted_json)
except Exception as e:
logger.warning(t("log_fallback_read_error", e=e))
return {}
def _write_fallback_store(data: Dict[str, str]) -> None:
"""Encrypts and writes the fallback credentials JSON file."""
cipher = _get_or_create_fernet_cipher()
payload = json.dumps(data).encode("utf-8")
encrypted_payload = cipher.encrypt(payload)
FALLBACK_CREDENTIALS_FILE.parent.mkdir(parents=True, exist_ok=True)
FALLBACK_CREDENTIALS_FILE.write_bytes(encrypted_payload)
# =====================================================================
# Public Credential Management API
# =====================================================================
def save_credential(key: str, value: str) -> bool:
"""
Saves a credential. Attempts Windows Credential Manager (keyring) first;
falls back to DPAPI-protected encrypted file if keyring fails.
"""
cleaned_value = str(value).strip()
saved_keyring = False
try:
keyring.set_password(SERVICE_NAME, key, cleaned_value)
saved_keyring = True
except Exception as e:
logger.warning(t("log_keyring_save_error", key=key, e=e))
# Also keep fallback in sync or as primary if keyring failed
try:
store = _read_fallback_store()
store[key] = cleaned_value
_write_fallback_store(store)
return True
except Exception as e:
logger.error(t("log_dpapi_save_error", key=key, e=e))
return saved_keyring
def load_credential(key: str) -> Optional[str]:
"""
Loads a credential. Checks keyring first, falls back to DPAPI encrypted file.
Returns None if not found.
"""
# 1. Try keyring
try:
val = keyring.get_password(SERVICE_NAME, key)
if val is not None and val.strip():
return val.strip()
except Exception as e:
logger.debug(t("log_keyring_load_debug", key=key, e=e))
# 2. Try DPAPI fallback
try:
store = _read_fallback_store()
val = store.get(key)
if val is not None and val.strip():
return val.strip()
except Exception as e:
logger.debug(t("log_fallback_load_debug", key=key, e=e))
return None
def delete_credential(key: str) -> bool:
"""
Deletes a credential from both keyring and DPAPI fallback.
"""
success = False
try:
keyring.delete_password(SERVICE_NAME, key)
success = True
except Exception:
pass
try:
store = _read_fallback_store()
if key in store:
del store[key]
_write_fallback_store(store)
success = True
except Exception:
pass
return success
def credentials_available() -> bool:
"""Returns True if both bot token and master user ID are set."""
token = load_credential(KEY_BOT_TOKEN)
master_id = load_credential(KEY_MASTER_USER_ID)
return bool(token and master_id)
def mask_token(token: Optional[str]) -> str:
"""
Returns a masked preview of a Telegram bot token (e.g. '123456...Wxyz').
Never returns or exposes the full token.
"""
if not token:
return "[NON CONFIGURATO]"
clean = token.strip()
if len(clean) < 12:
return "******"
return f"{clean[:6]}...{clean[-4:]}"