Skip to content

Commit

Permalink
Merge pull request #478 from opensanctions/add-authn
Browse files Browse the repository at this point in the history
feat: add dummy authentication
  • Loading branch information
SimonThordal authored Jul 4, 2024
2 parents dd61610 + 75c5699 commit fafc4a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
21 changes: 18 additions & 3 deletions yente/data/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from followthemoney.types import registry
from prefixdate.precision import Precision
from contextlib import asynccontextmanager
from typing import AsyncGenerator, Dict, List, Iterable, Optional, Set
from typing import AsyncGenerator, Dict, List, Iterable, Optional, Set, Generator
from rigour.text.scripts import is_modern_alphabet
from rigour.text.distance import levenshtein
from fingerprints import remove_types, clean_name_light
from nomenklatura.util import fingerprint_name, names_word_list
from yente.settings import HTTP_PROXY, VERSION
from yente.settings import HTTP_PROXY, VERSION, AUTH_TOKEN


@lru_cache(maxsize=5000)
Expand Down Expand Up @@ -128,12 +128,27 @@ def get_url_local_path(url: str) -> Optional[Path]:
return None


class Authenticator(httpx.Auth):
def auth_flow(
self, request: httpx.Request
) -> Generator[httpx.Request, httpx.Response, None]:
response = yield request
if response.status_code == 401 and AUTH_TOKEN:
request.headers["Authentication"] = f"Token {AUTH_TOKEN}"
yield request


@asynccontextmanager
async def httpx_session() -> AsyncGenerator[httpx.AsyncClient, None]:
transport = httpx.AsyncHTTPTransport(retries=3)
proxy = HTTP_PROXY if HTTP_PROXY != "" else None
headers = {"User-Agent": f"Yente/{VERSION}"}
async with httpx.AsyncClient(
transport=transport, http2=True, timeout=None, proxy=proxy, headers=headers
transport=transport,
http2=True,
timeout=None,
proxy=proxy,
headers=headers,
auth=Authenticator(),
) as client:
yield client
3 changes: 3 additions & 0 deletions yente/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,6 @@ def random_cron() -> str:

# Used to pad out first_seen, last_seen on static collections
RUN_TIME = datetime.utcnow().isoformat()[:19]

# Authentication settings
AUTH_TOKEN = env_get("YENTE_AUTH_TOKEN")

0 comments on commit fafc4a7

Please sign in to comment.