proton2m365 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- proton2m365/__init__.py +1 -0
- proton2m365/auth.py +255 -0
- proton2m365/cli.py +1342 -0
- proton2m365/content.py +195 -0
- proton2m365/doctor.py +219 -0
- proton2m365/graph_store.py +341 -0
- proton2m365/imap_store.py +554 -0
- proton2m365/lease.py +111 -0
- proton2m365/locks.py +80 -0
- proton2m365/pipeline.py +974 -0
- proton2m365/progress.py +60 -0
- proton2m365/proton_export.py +765 -0
- proton2m365/receipt.py +330 -0
- proton2m365/sample_export.py +122 -0
- proton2m365/state.py +577 -0
- proton2m365/status.py +211 -0
- proton2m365/targets.py +75 -0
- proton2m365-0.1.0.dist-info/METADATA +172 -0
- proton2m365-0.1.0.dist-info/RECORD +22 -0
- proton2m365-0.1.0.dist-info/WHEEL +4 -0
- proton2m365-0.1.0.dist-info/entry_points.txt +2 -0
- proton2m365-0.1.0.dist-info/licenses/LICENSE +21 -0
proton2m365/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""proton2m365: move a Proton Mail export into a Microsoft 365 mailbox."""
|
proton2m365/auth.py
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"""Sign-in to Microsoft 365 or Outlook.com with MSAL: one account, tokens for both Graph and IMAP.
|
|
2
|
+
|
|
3
|
+
proton2m365 ships no sign-in app of its own: each user registers one (docs/setup-own-app.md), a public client
|
|
4
|
+
with an `http://localhost` redirect and the delegated permissions Mail.ReadWrite, MailboxSettings.ReadWrite and
|
|
5
|
+
IMAP.AccessAsUser.All (its token is requested for the Outlook resource), and gives its client ID with
|
|
6
|
+
--client-id, PROTON2M365_CLIENT_ID or client_id in config.toml. Tokens are cached in the OS secret store,
|
|
7
|
+
falling back to an owner-only file.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
import stat
|
|
14
|
+
import sys
|
|
15
|
+
import tomllib
|
|
16
|
+
from collections.abc import Callable
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
GRAPH_SCOPES = [
|
|
22
|
+
"https://graph.microsoft.com/Mail.ReadWrite",
|
|
23
|
+
"https://graph.microsoft.com/MailboxSettings.ReadWrite",
|
|
24
|
+
]
|
|
25
|
+
IMAP_SCOPES = ["https://outlook.office.com/IMAP.AccessAsUser.All"]
|
|
26
|
+
CONFIG_DIR = Path.home() / ".config" / "proton2m365"
|
|
27
|
+
DEFAULT_CACHE = CONFIG_DIR / "token_cache.bin"
|
|
28
|
+
DEFAULT_CONFIG = CONFIG_DIR / "config.toml"
|
|
29
|
+
DEFAULT_TENANT = "common"
|
|
30
|
+
CONSUMER_TENANT = "9188040d-6c67-4c5b-b112-36a304b66dad" # personal Microsoft accounts
|
|
31
|
+
BROWSER_TIMEOUT_SECONDS = 300
|
|
32
|
+
ENV_CLIENT_ID = "PROTON2M365_CLIENT_ID"
|
|
33
|
+
ENV_TENANT = "PROTON2M365_TENANT"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class AuthError(Exception):
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _token_cache(path: Path):
|
|
41
|
+
"""MSAL cache persisted in the Keychain; falls back to an owner-only file if Keychain is unavailable."""
|
|
42
|
+
from msal_extensions import FilePersistence, PersistedTokenCache, build_encrypted_persistence
|
|
43
|
+
|
|
44
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
45
|
+
try:
|
|
46
|
+
persistence = build_encrypted_persistence(str(path))
|
|
47
|
+
except Exception: # noqa: BLE001 - any Keychain failure means: use the 0600 file instead
|
|
48
|
+
persistence = FilePersistence(str(path))
|
|
49
|
+
path.touch(exist_ok=True)
|
|
50
|
+
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR)
|
|
51
|
+
return PersistedTokenCache(persistence)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True)
|
|
55
|
+
class AppConfig:
|
|
56
|
+
client_id: str
|
|
57
|
+
tenant: str
|
|
58
|
+
client_id_source: str # flag, environment or config.toml (printed with sign-in errors)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def load_config(path: Path | None = None) -> dict:
|
|
62
|
+
path = path or DEFAULT_CONFIG
|
|
63
|
+
if not path.is_file():
|
|
64
|
+
return {}
|
|
65
|
+
try:
|
|
66
|
+
return tomllib.loads(path.read_text())
|
|
67
|
+
except (OSError, tomllib.TOMLDecodeError) as exc:
|
|
68
|
+
raise AuthError(f"cannot read {path}: {exc}") from exc
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def resolve_app_config(client_id: str | None, tenant: str | None, *, config: dict | None = None) -> AppConfig:
|
|
72
|
+
"""Client ID and tenant, first found wins: flag, environment, config.toml. There is no built-in app."""
|
|
73
|
+
config = load_config() if config is None else config
|
|
74
|
+
sources = (("flag", client_id), ("environment", os.environ.get(ENV_CLIENT_ID)),
|
|
75
|
+
("config.toml", config.get("client_id")))
|
|
76
|
+
source, cid = next(((s, v) for s, v in sources if v), (None, ""))
|
|
77
|
+
if not cid:
|
|
78
|
+
raise AuthError("P2M-AUTH-NO-APP: proton2m365 needs your own sign-in app (a one-time setup, about 10 "
|
|
79
|
+
"minutes: docs/setup-own-app.md). Then pass --client-id and --tenant, or put client_id and "
|
|
80
|
+
f"tenant in {DEFAULT_CONFIG}")
|
|
81
|
+
tenant = tenant or os.environ.get(ENV_TENANT) or config.get("tenant") or DEFAULT_TENANT
|
|
82
|
+
return AppConfig(cid, tenant, source)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def headless() -> bool:
|
|
86
|
+
"""No browser can open here: an SSH session, or Linux without a display."""
|
|
87
|
+
if os.environ.get("SSH_CONNECTION") or os.environ.get("SSH_TTY"):
|
|
88
|
+
return True
|
|
89
|
+
if sys.platform.startswith("linux"):
|
|
90
|
+
return not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY"))
|
|
91
|
+
return False
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def home_account_id(claims: dict) -> str | None:
|
|
95
|
+
oid, tid = claims.get("oid"), claims.get("tid")
|
|
96
|
+
return f"{oid}.{tid}" if oid and tid else None
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class Auth:
|
|
100
|
+
"""Holds one signed-in account; hands out fresh Graph and IMAP access tokens."""
|
|
101
|
+
|
|
102
|
+
def __init__(
|
|
103
|
+
self,
|
|
104
|
+
config: AppConfig,
|
|
105
|
+
*,
|
|
106
|
+
device_code: bool = False,
|
|
107
|
+
account: str | None = None,
|
|
108
|
+
cache_path: Path = DEFAULT_CACHE,
|
|
109
|
+
app: Any = None,
|
|
110
|
+
say: Callable[[str], None] = print,
|
|
111
|
+
is_headless: Callable[[], bool] = headless,
|
|
112
|
+
):
|
|
113
|
+
if app is None:
|
|
114
|
+
import msal
|
|
115
|
+
|
|
116
|
+
app = msal.PublicClientApplication(
|
|
117
|
+
config.client_id,
|
|
118
|
+
authority=f"https://login.microsoftonline.com/{config.tenant}",
|
|
119
|
+
token_cache=_token_cache(Path(cache_path)),
|
|
120
|
+
)
|
|
121
|
+
self.app = app
|
|
122
|
+
self.config = config
|
|
123
|
+
self.device_code = device_code
|
|
124
|
+
self.wanted = account # --account <address>
|
|
125
|
+
self.say = say
|
|
126
|
+
self.is_headless = is_headless
|
|
127
|
+
self.account: dict | None = None
|
|
128
|
+
|
|
129
|
+
@property
|
|
130
|
+
def username(self) -> str:
|
|
131
|
+
if self.account is None:
|
|
132
|
+
raise AuthError("not signed in")
|
|
133
|
+
return self.account["username"]
|
|
134
|
+
|
|
135
|
+
@property
|
|
136
|
+
def home_account_id(self) -> str:
|
|
137
|
+
if self.account is None:
|
|
138
|
+
raise AuthError("not signed in")
|
|
139
|
+
return self.account["home_account_id"]
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def is_personal(self) -> bool:
|
|
143
|
+
"""Outlook.com / Microsoft 365 Personal or Family: the consumer tenant."""
|
|
144
|
+
return self.home_account_id.endswith("." + CONSUMER_TENANT)
|
|
145
|
+
|
|
146
|
+
def sign_in(self, *, recorded: str | None = None) -> str:
|
|
147
|
+
"""Use the right cached account if its tokens still refresh; otherwise sign in. Returns the username.
|
|
148
|
+
|
|
149
|
+
`recorded` is the home_account_id a state file is bound to: it picks the cached account when there
|
|
150
|
+
are several, and a different account is never used for that state.
|
|
151
|
+
"""
|
|
152
|
+
if self.sign_in_silent(recorded=recorded):
|
|
153
|
+
return self.username
|
|
154
|
+
return self._interactive()
|
|
155
|
+
|
|
156
|
+
def sign_in_silent(self, *, recorded: str | None = None) -> str | None:
|
|
157
|
+
"""Use a cached account whose tokens still refresh; never open a browser or ask for a code."""
|
|
158
|
+
cached = self.app.get_accounts()
|
|
159
|
+
if self.wanted:
|
|
160
|
+
cached = [a for a in cached if a.get("username", "").casefold() == self.wanted.casefold()]
|
|
161
|
+
elif recorded:
|
|
162
|
+
cached = [a for a in cached if a.get("home_account_id") == recorded] or cached
|
|
163
|
+
if len(cached) > 1:
|
|
164
|
+
names = ", ".join(sorted(a.get("username", "?") for a in cached))
|
|
165
|
+
raise AuthError(f"P2M-AUTH-WHICH-ACCOUNT: several accounts are signed in ({names}); choose one with --account <address>")
|
|
166
|
+
for account in cached:
|
|
167
|
+
if self.app.acquire_token_silent(GRAPH_SCOPES, account=account):
|
|
168
|
+
self.account = account
|
|
169
|
+
return self.username
|
|
170
|
+
return None
|
|
171
|
+
|
|
172
|
+
def _interactive(self) -> str:
|
|
173
|
+
self.say("Signing in to Microsoft. proton2m365 needs access to your mail to upload your export and set "
|
|
174
|
+
"categories, using the app you registered; nothing leaves this computer except calls to Microsoft.")
|
|
175
|
+
device = self.device_code
|
|
176
|
+
if not device and self.is_headless():
|
|
177
|
+
self.say("No browser is available here (SSH session or no display), so signing in with a device code.")
|
|
178
|
+
device = True
|
|
179
|
+
if device:
|
|
180
|
+
self.say("Device-code sign-in may ask twice: once for Microsoft Graph, then once for IMAP.")
|
|
181
|
+
first = self._device_flow(GRAPH_SCOPES)
|
|
182
|
+
else:
|
|
183
|
+
self.say("Opening your browser to sign in. Choose the mailbox you are migrating into.")
|
|
184
|
+
try:
|
|
185
|
+
first = self.app.acquire_token_interactive(
|
|
186
|
+
GRAPH_SCOPES, extra_scopes_to_consent=IMAP_SCOPES, prompt="select_account",
|
|
187
|
+
login_hint=self.wanted, timeout=BROWSER_TIMEOUT_SECONDS,
|
|
188
|
+
)
|
|
189
|
+
except RuntimeError as exc: # msal raises BrowserInteractionTimeoutError (a RuntimeError)
|
|
190
|
+
if "Timeout" not in type(exc).__name__:
|
|
191
|
+
raise
|
|
192
|
+
raise AuthError(f"P2M-AUTH-TIMEOUT: no browser completed sign-in within {BROWSER_TIMEOUT_SECONDS // 60} minutes; "
|
|
193
|
+
"run the command again with --device-code") from exc
|
|
194
|
+
self._raise_for(first)
|
|
195
|
+
self.account = self._account_for(first)
|
|
196
|
+
if device and not self.app.acquire_token_silent(IMAP_SCOPES, account=self.account):
|
|
197
|
+
second = self._device_flow(IMAP_SCOPES)
|
|
198
|
+
self._raise_for(second)
|
|
199
|
+
if home_account_id(second.get("id_token_claims") or {}) not in (None, self.home_account_id):
|
|
200
|
+
self.account = None
|
|
201
|
+
raise AuthError("P2M-AUTH-TWO-ACCOUNTS: the two device-code prompts were answered with different accounts; "
|
|
202
|
+
"run the command again and use the same mailbox both times")
|
|
203
|
+
return self.username
|
|
204
|
+
|
|
205
|
+
def _device_flow(self, scopes: list[str]) -> dict:
|
|
206
|
+
flow = self.app.initiate_device_flow(scopes=scopes)
|
|
207
|
+
if "user_code" not in flow:
|
|
208
|
+
raise AuthError(f"device code sign-in failed: {flow.get('error_description', flow)}")
|
|
209
|
+
self.say(flow["message"])
|
|
210
|
+
return self.app.acquire_token_by_device_flow(flow)
|
|
211
|
+
|
|
212
|
+
def _account_for(self, result: dict) -> dict:
|
|
213
|
+
"""The account the user actually chose, by its object and tenant IDs, never just the first cached one."""
|
|
214
|
+
wanted = home_account_id(result.get("id_token_claims") or {})
|
|
215
|
+
accounts = self.app.get_accounts()
|
|
216
|
+
match = [a for a in accounts if a.get("home_account_id") == wanted] if wanted else accounts[:1]
|
|
217
|
+
if not match:
|
|
218
|
+
raise AuthError("sign-in succeeded but the account is not in the token cache; run the command again")
|
|
219
|
+
return match[0]
|
|
220
|
+
|
|
221
|
+
def _raise_for(self, result: dict) -> None:
|
|
222
|
+
if "access_token" in result:
|
|
223
|
+
return
|
|
224
|
+
error, detail = result.get("error", "?"), result.get("error_description", "")
|
|
225
|
+
where = (f"(client ID from {self.config.client_id_source}, authority "
|
|
226
|
+
f"https://login.microsoftonline.com/{self.config.tenant})")
|
|
227
|
+
if "AADSTS50194" in detail or "AADSTS700016" in detail:
|
|
228
|
+
raise AuthError(
|
|
229
|
+
"P2M-AUTH-TENANT: this app is registered for one organization, so sign-in must name it: pass --tenant "
|
|
230
|
+
"with your tenant ID or domain (or set tenant in config.toml); for a personal-accounts app use "
|
|
231
|
+
f"--tenant consumers {where}")
|
|
232
|
+
if "AADSTS65001" in detail or (error in ("consent_required", "interaction_required") and "consent" in detail):
|
|
233
|
+
raise AuthError(
|
|
234
|
+
"P2M-AUTH-CONSENT: an administrator has not granted this app its permissions yet. In the Entra admin "
|
|
235
|
+
"center, open the app, API permissions, and select \"Grant admin consent\" (docs/setup-own-app.md, "
|
|
236
|
+
f"step 3), or have your admin open https://login.microsoftonline.com/{self.config.tenant}/adminconsent"
|
|
237
|
+
f"?client_id={self.config.client_id} {where}")
|
|
238
|
+
if "AADSTS90094" in detail or "AADSTS90095" in detail:
|
|
239
|
+
raise AuthError(f"P2M-AUTH-CONSENT: an administrator must approve this app (user consent is blocked): {detail} {where}")
|
|
240
|
+
raise AuthError(f"P2M-AUTH-FAILED: sign-in failed: {error}: {detail} {where}")
|
|
241
|
+
|
|
242
|
+
def _token(self, scopes: list[str]) -> str:
|
|
243
|
+
if self.account is None:
|
|
244
|
+
raise AuthError("not signed in")
|
|
245
|
+
result = self.app.acquire_token_silent(scopes, account=self.account)
|
|
246
|
+
if not result or "access_token" not in result:
|
|
247
|
+
detail = (result or {}).get("error_description", "no cached token")
|
|
248
|
+
raise AuthError(f"P2M-AUTH-TOKEN: could not get a token for {scopes[0]}: {detail}; run the command again to sign in")
|
|
249
|
+
return result["access_token"]
|
|
250
|
+
|
|
251
|
+
def graph_token(self) -> str:
|
|
252
|
+
return self._token(GRAPH_SCOPES)
|
|
253
|
+
|
|
254
|
+
def imap_token(self) -> str:
|
|
255
|
+
return self._token(IMAP_SCOPES)
|