Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion databusclient/api/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ def __get_vault_access__(
if not refresh_token:
if not os.path.exists(token_file):
raise FileNotFoundError(f"Vault token file not found: {token_file}")
with open(token_file, "r") as f:
with open(token_file, "r", encoding="utf-8-sig") as f:
refresh_token = f.read().strip()
if len(refresh_token) < 80:
print(f"Warning: token from {token_file} is short (<80 chars)")
Expand Down
25 changes: 25 additions & 0 deletions tests/test_download_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,28 @@ def test_403_reports_insufficient_permissions():
dl._download_file(url, localDir=".", vault_token_file="/some/token/file")

assert "permission" in str(exc.value) or "forbidden" in str(exc.value)


@pytest.mark.parametrize("bom", [b"\xef\xbb\xbf", b""], ids=["utf8-bom", "no-bom"])
def test_vault_token_file_is_read_without_bom(tmp_path, monkeypatch, bom):
# Windows PowerShell 5.1 `Set-Content -Encoding UTF8` writes a UTF-8 BOM
token = "t" * 90
token_file = tmp_path / "vault-token.dat"
token_file.write_bytes(bom + token.encode("utf-8") + b"\r\n")
monkeypatch.delenv("REFRESH_TOKEN", raising=False)

post_resp_1 = Mock()
post_resp_1.json.return_value = {"access_token": "ACCESS"}
post_resp_2 = Mock()
post_resp_2.json.return_value = {"access_token": "VAULT"}

with patch("requests.post", side_effect=[post_resp_1, post_resp_2]) as post:
vault_token = dl.__get_vault_access__(
"https://data.dbpedia.io/protected/file.ttl",
str(token_file),
"https://auth.example.org/token",
"vault-token-exchange",
)

assert vault_token == "VAULT"
assert post.call_args_list[0].kwargs["data"]["refresh_token"] == token