Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

Commit 9cf20e4

Browse files
authored
style: fixes stricter ruff linter issues (#110)
1 parent 178e96b commit 9cf20e4

File tree

15 files changed

+451
-441
lines changed

15 files changed

+451
-441
lines changed

.devcontainer/devcontainer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
"tamasfe.even-better-toml",
1515
"-ms-python.autopep8",
1616
"-dbaeumer.vscode-eslint"
17-
]
17+
],
18+
"settings": {
19+
"linter.linters": {
20+
"pylint": {
21+
"enabled": false
22+
}
23+
}
24+
}
1825
}
1926
},
20-
"postCreateCommand": "pip install pytest python-dotenv build ruff"
27+
"postCreateCommand": "pip install --upgrade pip && pip install -e .[dev]"
2128
}

.github/workflows/on-pull-request.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ jobs:
3838
with:
3939
python-version: "3.8"
4040
- name: Install dependencies
41-
run: |
42-
python -m pip install --upgrade pip
43-
pip install pytest python-dotenv
41+
run: pip install -e .[dev]
4442
- name: Test
45-
run: python setup.py pytest
43+
run: pytest

passageidentity/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from .passage import Passage
1+
"""Initializes the Passage identity package."""
2+
23
from .errors import PassageError
4+
from .passage import Passage
35

46
__all__ = [
57
"Passage",

passageidentity/errors.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
"""
2-
Error class for Passage errors
3-
"""
1+
"""Defines custom error classes for handling Passage-related errors."""
2+
3+
from __future__ import annotations
44

55

66
class PassageError(Exception):
7+
"""Error class for handling Passage errors."""
8+
79
def __init__(
810
self,
9-
message,
10-
status_code: int = None,
11-
status_text: str = None,
12-
body: dict = None,
13-
):
11+
message: str,
12+
status_code: int | None = None,
13+
status_text: str | None = None,
14+
body: dict | None = None,
15+
) -> None:
16+
"""Initialize the error with a message, status code, status text, and optional body."""
1417
self.message = message
1518
self.status_code = status_code
1619
self.status_text = status_text

passageidentity/helper.py

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,58 @@
1+
"""Provides helper functions for interacting with the Passage Identity API."""
2+
13
import re
4+
from http import HTTPStatus
5+
6+
from requests.sessions import Request
27

38
from passageidentity import requests
49
from passageidentity.errors import PassageError
510

6-
TOKEN_TYPE = "Bearer"
11+
BEARER_PATTERN = r"Bearer ([^\s,]+)"
712
BASE_URL = "https://api.passage.id/v1/apps/"
813

9-
"""
10-
Helper function to extract the JWT from an Authorization header.
11-
"""
1214

15+
def extract_token(auth_header: str) -> str:
16+
"""Extract the JWT from an Authorization header."""
17+
expression = re.escape(BEARER_PATTERN)
18+
match = re.search(expression, auth_header)
1319

14-
def extractToken(authHeader):
15-
expression = re.escape(TOKEN_TYPE) + r" ([^\s,]+)"
16-
match = re.search(expression, authHeader)
17-
try:
20+
if match:
1821
return match.group(1)
19-
except (AttributeError, IndexError):
20-
raise PassageError("No Passage authorization header.")
2122

23+
msg = "No Passage authorization header."
24+
raise PassageError(msg)
2225

23-
"""
24-
Helper funtion to get the auth token from a request.
25-
Checks the Authorization header first, then the psg_auth_token cookie
26-
"""
2726

27+
def get_auth_token_from_request(request: Request, auth_strategy: int) -> str:
28+
"""Get the auth token from a request.
2829
29-
def getAuthTokenFromRequest(request, auth_strategy):
30-
if auth_strategy == 2:
31-
authHeader = request.headers["Authorization"]
32-
expression = re.escape(TOKEN_TYPE) + r" ([^\s,]+)"
33-
match = re.search(expression, authHeader)
34-
try:
30+
Checks the Authorization header first, then the psg_auth_token cookie.
31+
"""
32+
if auth_strategy == 2: # noqa: PLR2004
33+
auth_header = request.headers["Authorization"]
34+
expression = re.escape(BEARER_PATTERN)
35+
match = re.search(expression, auth_header)
36+
37+
if match:
3538
return match.group(1)
36-
except (AttributeError, IndexError):
37-
raise PassageError("No Passage authorization header.")
38-
else:
39-
try:
40-
cookies = request.COOKIES
41-
if "psg_auth_token" not in cookies.keys():
42-
raise PassageError("No Passage authentication token.")
43-
return cookies["psg_auth_token"]
44-
except Exception:
45-
try:
46-
cookies = request.cookies
47-
if "psg_auth_token" not in cookies.keys():
48-
raise PassageError("No Passage authentication token.")
49-
return cookies["psg_auth_token"]
50-
except Exception:
51-
raise PassageError("No passage authentication token")
52-
53-
54-
"""
55-
Helper function to fetch the public key for the given app id from Passage
56-
"""
57-
58-
59-
def fetchApp(app_id):
39+
40+
msg = "No Passage authorization header."
41+
raise PassageError(msg)
42+
43+
if "psg_auth_token" not in request.cookies:
44+
msg = "No Passage authentication token."
45+
raise PassageError(msg)
46+
47+
return request.cookies["psg_auth_token"]
48+
49+
50+
def fetch_app(app_id: str) -> dict:
51+
"""Fetch the public key for the given app id from Passage."""
6052
# unauthenticated request to get the public key
6153
r = requests.get(BASE_URL + app_id)
6254

63-
# check response code
64-
if r.status_code != 200:
55+
if r.status_code != HTTPStatus.OK:
6556
raise PassageError("Could not fetch app information for app id " + app_id)
6657

6758
return r.json()["app"]

passageidentity/models/update_magic_link_auth_method.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ def from_dict(cls, obj: Dict) -> Self:
9292
"ttl_display_unit": obj.get("ttl_display_unit"),
9393
}
9494
)
95-
return _obj
95+
return _obj

passageidentity/models/update_otp_auth_method.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ def from_dict(cls, obj: Dict) -> Self:
9292
"ttl_display_unit": obj.get("ttl_display_unit"),
9393
}
9494
)
95-
return _obj
95+
return _obj

passageidentity/models/update_passkey_auth_method.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ def from_dict(cls, obj: Dict) -> Self:
8181
_obj = cls.model_validate(
8282
{"enabled": obj.get("enabled") if obj.get("enabled") is not None else True}
8383
)
84-
return _obj
84+
return _obj

0 commit comments

Comments
 (0)