Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e349638700 | ||
|
|
a4abe354eb |
@@ -11,16 +11,16 @@ This reads codes from email; it does not generate them (that is `pyotp`'s job).
|
||||
`requirements.txt`:
|
||||
|
||||
```
|
||||
aiomail @ git+ssh://git@git.rethinkstudios.io/rethink-public/aiomail.git@v0.1.1
|
||||
aiomail @ git+ssh://git@git.rethinkstudios.io/rethink-public/aiomail.git@v0.1.3
|
||||
# OAuth token providers (Microsoft / Google) need the extra:
|
||||
aiomail[oauth] @ git+ssh://git@git.rethinkstudios.io/rethink-public/aiomail.git@v0.1.1
|
||||
aiomail[oauth] @ git+ssh://git@git.rethinkstudios.io/rethink-public/aiomail.git@v0.1.3
|
||||
```
|
||||
|
||||
Direct:
|
||||
|
||||
```bash
|
||||
pip install "aiomail @ git+ssh://git@git.rethinkstudios.io/rethink-public/aiomail.git@v0.1.1"
|
||||
pip install "aiomail[oauth] @ git+ssh://git@git.rethinkstudios.io/rethink-public/aiomail.git@v0.1.1"
|
||||
pip install "aiomail @ git+ssh://git@git.rethinkstudios.io/rethink-public/aiomail.git@v0.1.3"
|
||||
pip install "aiomail[oauth] @ git+ssh://git@git.rethinkstudios.io/rethink-public/aiomail.git@v0.1.3"
|
||||
```
|
||||
|
||||
Requires `aioimaplib` and `beautifulsoup4` (pulled transitively). The `oauth`
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "aiomail"
|
||||
version = "0.1.1"
|
||||
version = "0.1.3"
|
||||
description = "async IMAP one-time-code retrieval with password/OAuth2 auth and dynamic matching"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
|
||||
@@ -29,4 +29,4 @@ __all__ = [
|
||||
"DEFAULT_FOLDERS",
|
||||
]
|
||||
|
||||
__version__ = "0.1.1"
|
||||
__version__ = "0.1.3"
|
||||
|
||||
@@ -169,7 +169,11 @@ class IMAPClient:
|
||||
if result != "OK" or not data:
|
||||
return None
|
||||
for item in data:
|
||||
if isinstance(item, (bytes, bytearray)) and len(item) > 20:
|
||||
# aioimaplib stores the literal message payload as the only bytearray in
|
||||
# the response; every other line (including the `<id> FETCH (...` header)
|
||||
# is plain bytes. select by structure, not length — a length heuristic
|
||||
# mismatches the header line for any 2+ digit id or a BODY[]/UID fetch.
|
||||
if isinstance(item, bytearray):
|
||||
return email.message_from_bytes(bytes(item))
|
||||
if isinstance(item, tuple) and len(item) > 1:
|
||||
return email.message_from_bytes(item[1])
|
||||
|
||||
+22
-10
@@ -20,16 +20,18 @@ log = logging.getLogger(__name__)
|
||||
DEFAULT_FOLDERS: Sequence[str] = ("INBOX", "Junk", "Spam", "Archive", "All Mail")
|
||||
|
||||
|
||||
def _server_query(sender: MatchSpec, subject: MatchSpec) -> str:
|
||||
def _server_query(sender: MatchSpec, subject: MatchSpec, match_field: str = "from") -> str:
|
||||
"""build a narrowing IMAP query from plain-string specs only
|
||||
|
||||
only plain strings translate to server-side FROM/SUBJECT filters; regex and
|
||||
callable specs fall back to ALL and are filtered client-side, so dynamic
|
||||
matching always works even when the server cannot express it.
|
||||
only plain strings translate to server-side filters; regex and callable specs
|
||||
fall back to ALL and are filtered client-side, so dynamic matching always works
|
||||
even when the server cannot express it. `match_field` selects which header the
|
||||
`sender` spec searches: "from" filters by the sender address (default), "to"
|
||||
filters by the recipient address (the per-user alias the code was sent to).
|
||||
"""
|
||||
parts: List[str] = []
|
||||
if isinstance(sender, str):
|
||||
parts.append(f'FROM "{sender}"')
|
||||
parts.append(f'TO "{sender}"' if match_field == "to" else f'FROM "{sender}"')
|
||||
if isinstance(subject, str):
|
||||
parts.append(f'SUBJECT "{subject}"')
|
||||
return f"({' '.join(parts)})" if parts else "ALL"
|
||||
@@ -52,6 +54,7 @@ async def retrieve_otp(
|
||||
*,
|
||||
sender: MatchSpec = None,
|
||||
subject: MatchSpec = None,
|
||||
match_field: str = "from",
|
||||
folders: Optional[Iterable[str]] = None,
|
||||
patterns: Sequence[Union[str, Pattern]] = DEFAULT_PATTERNS,
|
||||
lengths: Iterable[int] = DEFAULT_LENGTHS,
|
||||
@@ -64,14 +67,18 @@ async def retrieve_otp(
|
||||
) -> Optional[str]:
|
||||
"""return the newest OTP matching the filters, or None
|
||||
|
||||
sender/subject accept a substring, a compiled regex, or a callable. folders,
|
||||
patterns, code lengths, max age and retry behavior are all tunable. set
|
||||
`max_age=None` to disable the freshness check.
|
||||
sender/subject accept a substring, a compiled regex, or a callable. `match_field`
|
||||
selects which header the `sender` spec is matched against: "from" (default)
|
||||
matches the sender address; "to" matches the recipient address (the per-user
|
||||
alias the code was sent to) and additionally accepts a forwarded match on the
|
||||
From header, so a forwarded code still resolves. folders, patterns, code lengths,
|
||||
max age and retry behavior are all tunable. set `max_age=None` to disable the
|
||||
freshness check.
|
||||
"""
|
||||
folders = list(folders) if folders is not None else list(DEFAULT_FOLDERS)
|
||||
sender_ok = as_predicate(sender)
|
||||
subject_ok = as_predicate(subject)
|
||||
query = _server_query(sender, subject)
|
||||
query = _server_query(sender, subject, match_field)
|
||||
|
||||
for attempt in range(retries + 1):
|
||||
for folder in folders:
|
||||
@@ -91,7 +98,12 @@ async def retrieve_otp(
|
||||
|
||||
from_hdr = message.get("From", "")
|
||||
subj_hdr = message.get("Subject", "")
|
||||
if not sender_ok(from_hdr) or not subject_ok(subj_hdr):
|
||||
if match_field == "to":
|
||||
to_hdr = message.get("To", "")
|
||||
matched = sender_ok(to_hdr) or sender_ok(from_hdr)
|
||||
else:
|
||||
matched = sender_ok(from_hdr)
|
||||
if not matched or not subject_ok(subj_hdr):
|
||||
continue
|
||||
|
||||
code = extract_code(message, patterns=patterns, lengths=lengths)
|
||||
|
||||
Reference in New Issue
Block a user