Skip to content
Merged
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
23 changes: 16 additions & 7 deletions src/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@
headers={"Referer": host + "/login/"},
)
alphabet = string.ascii_uppercase + string.digits
code_verifier = "".join(secrets.choice(alphabet) for i in range(43 + secrets.randbelow(86)))
code_verifier = "".join(
secrets.choice(alphabet) for i in range(43 + secrets.randbelow(86))
)
code_verifier_base64 = base64.urlsafe_b64encode(code_verifier.encode("utf-8"))
code_challenge = hashlib.sha256(code_verifier_base64).digest()
code_challenge_base64 = base64.urlsafe_b64encode(code_challenge).decode("utf-8").replace("=", "")
code_challenge_base64 = (
base64.urlsafe_b64encode(code_challenge).decode("utf-8").replace("=", "")
)
state = "".join(secrets.choice(alphabet) for i in range(15))

data = {
Expand All @@ -58,14 +62,19 @@
"scope": "openid profile",
"allow": "Authorize",
}
auth = s.post(host + "/o/authorize/", allow_redirects=False, data=data, headers={"Referer": host + "/o/authorize/"})
if auth.status_code != 302: # noqa: PLR2004
print(f"/o/authorize/ returned status code {auth.status_code} - no token today") # noqa: T201
auth = s.post(
host + "/o/authorize/",
allow_redirects=False,
data=data,
headers={"Referer": host + "/o/authorize/"},
)
if auth.status_code != 302:
print(f"/o/authorize/ returned status code {auth.status_code} - no token today")
sys.exit(1)
url = auth.headers["Location"]
result = urlparse(url)
qs = parse_qs(result.query)
assert state == qs["state"][0] # noqa: S101
assert state == qs["state"][0]
authcode = qs["code"][0]
token = s.post(
host + "/o/token/",
Expand All @@ -77,4 +86,4 @@
"code_verifier": code_verifier_base64,
},
)
print(token.json()) # noqa: T201
print(token.json())
2 changes: 1 addition & 1 deletion src/requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ django-debug-toolbar==5.1.0
factory_boy==3.3.3
coverage==7.8.0
pre-commit==4.2.0
hypothesis==6.130.6
hypothesis==6.131.3
tblib==3.1.0
unittest-xml-reporting==3.2.0
pipdeptree==2.26.0