Skip to content

Commit 005ccae

Browse files
committed
oauth: Support Python 3.6 in tests
RHEL8 ships a patched 3.6.8 as its base Python version, and I accidentally let some newer Python-isms creep into oauth_server.py during development. Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Jelte Fennema-Nio <postgres@jeltef.nl> Tested-by: Renan Alves Fonseca <renanfonseca@gmail.com> Tested-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/16098.1745079444%40sss.pgh.pa.us
1 parent bb78e42 commit 005ccae

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/test/modules/oauth_validator/t/oauth_server.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import time
1515
import urllib.parse
1616
from collections import defaultdict
17+
from typing import Dict
1718

1819

1920
class OAuthHandler(http.server.BaseHTTPRequestHandler):
@@ -23,7 +24,7 @@ class OAuthHandler(http.server.BaseHTTPRequestHandler):
2324
documentation for BaseHTTPRequestHandler.
2425
"""
2526

26-
JsonObject = dict[str, object] # TypeAlias is not available until 3.10
27+
JsonObject = Dict[str, object] # TypeAlias is not available until 3.10
2728

2829
def _check_issuer(self):
2930
"""
@@ -35,14 +36,16 @@ def _check_issuer(self):
3536
)
3637
self._parameterized = self.path.startswith("/param/")
3738

39+
# Strip off the magic path segment. (The more readable
40+
# str.removeprefix()/removesuffix() aren't available until Py3.9.)
3841
if self._alt_issuer:
3942
# The /alternate issuer uses IETF-style .well-known URIs.
4043
if self.path.startswith("/.well-known/"):
41-
self.path = self.path.removesuffix("/alternate")
44+
self.path = self.path[: -len("/alternate")]
4245
else:
43-
self.path = self.path.removeprefix("/alternate")
46+
self.path = self.path[len("/alternate") :]
4447
elif self._parameterized:
45-
self.path = self.path.removeprefix("/param")
48+
self.path = self.path[len("/param") :]
4649

4750
def _check_authn(self):
4851
"""
@@ -58,8 +61,10 @@ def _check_authn(self):
5861
if method != "Basic":
5962
raise RuntimeError(f"client used {method} auth; expected Basic")
6063

61-
username = urllib.parse.quote_plus(self.client_id)
62-
password = urllib.parse.quote_plus(secret)
64+
# TODO: Remove "~" from the safe list after Py3.6 support is removed.
65+
# 3.7 does this by default.
66+
username = urllib.parse.quote_plus(self.client_id, safe="~")
67+
password = urllib.parse.quote_plus(secret, safe="~")
6368
expected_creds = f"{username}:{password}"
6469

6570
if creds.encode() != base64.b64encode(expected_creds.encode()):
@@ -83,7 +88,7 @@ def do_GET(self):
8388

8489
self._send_json(resp)
8590

86-
def _parse_params(self) -> dict[str, str]:
91+
def _parse_params(self) -> Dict[str, str]:
8792
"""
8893
Parses apart the form-urlencoded request body and returns the resulting
8994
dict. For use by do_POST().
@@ -316,11 +321,14 @@ def authorization(self) -> JsonObject:
316321
return resp
317322

318323
def token(self) -> JsonObject:
319-
if err := self._get_param("error_code", None):
324+
err = self._get_param("error_code", None)
325+
if err:
320326
self._response_code = self._get_param("error_status", 400)
321327

322328
resp = {"error": err}
323-
if desc := self._get_param("error_desc", ""):
329+
330+
desc = self._get_param("error_desc", "")
331+
if desc:
324332
resp["error_description"] = desc
325333

326334
return resp

0 commit comments

Comments
 (0)