fix: add match_field=from|to to restore recipient-primary OTP matching
the clean lib matched senders by From only; the original imap_tool.py matched primarily by TO (the per-user alias the code was sent to) with a HEADER FROM forwarded fallback. added match_field="from"|"to" to retrieve_otp: "from" (default) is byte-identical to current behavior, "to" searches TO primary and accepts a forwarded From match, restoring the alias flow. server query + client-side predicate both honor it. bump to v0.1.2. Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
parent
6ac8957583
commit
a4abe354eb
@ -11,16 +11,16 @@ This reads codes from email; it does not generate them (that is `pyotp`'s job).
|
|||||||
`requirements.txt`:
|
`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.2
|
||||||
# OAuth token providers (Microsoft / Google) need the extra:
|
# 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.2
|
||||||
```
|
```
|
||||||
|
|
||||||
Direct:
|
Direct:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install "aiomail @ 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.2"
|
||||||
pip install "aiomail[oauth] @ 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.2"
|
||||||
```
|
```
|
||||||
|
|
||||||
Requires `aioimaplib` and `beautifulsoup4` (pulled transitively). The `oauth`
|
Requires `aioimaplib` and `beautifulsoup4` (pulled transitively). The `oauth`
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "aiomail"
|
name = "aiomail"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
description = "async IMAP one-time-code retrieval with password/OAuth2 auth and dynamic matching"
|
description = "async IMAP one-time-code retrieval with password/OAuth2 auth and dynamic matching"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|||||||
@ -29,4 +29,4 @@ __all__ = [
|
|||||||
"DEFAULT_FOLDERS",
|
"DEFAULT_FOLDERS",
|
||||||
]
|
]
|
||||||
|
|
||||||
__version__ = "0.1.1"
|
__version__ = "0.1.2"
|
||||||
|
|||||||
@ -20,16 +20,18 @@ log = logging.getLogger(__name__)
|
|||||||
DEFAULT_FOLDERS: Sequence[str] = ("INBOX", "Junk", "Spam", "Archive", "All Mail")
|
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
|
"""build a narrowing IMAP query from plain-string specs only
|
||||||
|
|
||||||
only plain strings translate to server-side FROM/SUBJECT filters; regex and
|
only plain strings translate to server-side filters; regex and callable specs
|
||||||
callable specs fall back to ALL and are filtered client-side, so dynamic
|
fall back to ALL and are filtered client-side, so dynamic matching always works
|
||||||
matching always works even when the server cannot express it.
|
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] = []
|
parts: List[str] = []
|
||||||
if isinstance(sender, 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):
|
if isinstance(subject, str):
|
||||||
parts.append(f'SUBJECT "{subject}"')
|
parts.append(f'SUBJECT "{subject}"')
|
||||||
return f"({' '.join(parts)})" if parts else "ALL"
|
return f"({' '.join(parts)})" if parts else "ALL"
|
||||||
@ -52,6 +54,7 @@ async def retrieve_otp(
|
|||||||
*,
|
*,
|
||||||
sender: MatchSpec = None,
|
sender: MatchSpec = None,
|
||||||
subject: MatchSpec = None,
|
subject: MatchSpec = None,
|
||||||
|
match_field: str = "from",
|
||||||
folders: Optional[Iterable[str]] = None,
|
folders: Optional[Iterable[str]] = None,
|
||||||
patterns: Sequence[Union[str, Pattern]] = DEFAULT_PATTERNS,
|
patterns: Sequence[Union[str, Pattern]] = DEFAULT_PATTERNS,
|
||||||
lengths: Iterable[int] = DEFAULT_LENGTHS,
|
lengths: Iterable[int] = DEFAULT_LENGTHS,
|
||||||
@ -64,14 +67,18 @@ async def retrieve_otp(
|
|||||||
) -> Optional[str]:
|
) -> Optional[str]:
|
||||||
"""return the newest OTP matching the filters, or None
|
"""return the newest OTP matching the filters, or None
|
||||||
|
|
||||||
sender/subject accept a substring, a compiled regex, or a callable. folders,
|
sender/subject accept a substring, a compiled regex, or a callable. `match_field`
|
||||||
patterns, code lengths, max age and retry behavior are all tunable. set
|
selects which header the `sender` spec is matched against: "from" (default)
|
||||||
`max_age=None` to disable the freshness check.
|
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)
|
folders = list(folders) if folders is not None else list(DEFAULT_FOLDERS)
|
||||||
sender_ok = as_predicate(sender)
|
sender_ok = as_predicate(sender)
|
||||||
subject_ok = as_predicate(subject)
|
subject_ok = as_predicate(subject)
|
||||||
query = _server_query(sender, subject)
|
query = _server_query(sender, subject, match_field)
|
||||||
|
|
||||||
for attempt in range(retries + 1):
|
for attempt in range(retries + 1):
|
||||||
for folder in folders:
|
for folder in folders:
|
||||||
@ -91,7 +98,12 @@ async def retrieve_otp(
|
|||||||
|
|
||||||
from_hdr = message.get("From", "")
|
from_hdr = message.get("From", "")
|
||||||
subj_hdr = message.get("Subject", "")
|
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
|
continue
|
||||||
|
|
||||||
code = extract_code(message, patterns=patterns, lengths=lengths)
|
code = extract_code(message, patterns=patterns, lengths=lengths)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user