Skip to content

Commit 3f24790

Browse files
authored
Merge pull request #5 from tokopedia:f_fixing_test
feat(sdk): Adding public key verification on check status
2 parents 24e6e5e + 9e084b4 commit 3f24790

9 files changed

+248
-152
lines changed

README.md

+13-17
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ config = (
5353
client_id="YOUR_CLIENT_ID", # required
5454
client_secret="YOUR_CLIENT_SECRET", # required
5555
private_key="YOUR_PRIVATE_KEY", # required
56+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
5657
)
5758
# Below is optional parameter
5859
.with_timeout(10)
59-
# Public key is optional, used only for callback signature verification
60-
.with_public_key("SAT_PUBLIC_KEY")
6160
.with_is_debug(True)
6261
)
6362

@@ -75,11 +74,10 @@ config = (
7574
client_id="YOUR_CLIENT_ID", # required
7675
client_secret="YOUR_CLIENT_SECRET", # required
7776
private_key="YOUR_PRIVATE_KEY", # required
77+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
7878
)
7979
# Below is optional parameter
8080
.with_timeout(10)
81-
# Public key is optional, used only for callback signature verification
82-
.with_public_key("SAT_PUBLIC_KEY")
8381
# Override SAT Base URL
8482
.with_sat_base_url("https://b2b.tokopedia.com/api")
8583
)
@@ -113,11 +111,10 @@ config = (
113111
client_id="YOUR_CLIENT_ID", # required
114112
client_secret="YOUR_CLIENT_SECRET", # required
115113
private_key="YOUR_PRIVATE_KEY", # required
114+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
116115
)
117116
# Below is optional parameter
118117
.with_timeout(10)
119-
# Public key is optional, used only for callback signature verification
120-
.with_public_key("SAT_PUBLIC_KEY")
121118
# Override SAT Base URL
122119
.with_sat_base_url("https://b2b.tokopedia.com/api")
123120
)
@@ -155,11 +152,10 @@ config = (
155152
client_id="YOUR_CLIENT_ID", # required
156153
client_secret="YOUR_CLIENT_SECRET", # required
157154
private_key="YOUR_PRIVATE_KEY", # required
155+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
158156
)
159157
# Below is optional parameter
160158
.with_timeout(10)
161-
# Public key is optional, used only for callback signature verification
162-
.with_public_key("SAT_PUBLIC_KEY")
163159
# Override SAT Base URL
164160
.with_sat_base_url("https://b2b.tokopedia.com/api")
165161
)
@@ -201,11 +197,10 @@ config = (
201197
client_id="YOUR_CLIENT_ID", # required
202198
client_secret="YOUR_CLIENT_SECRET", # required
203199
private_key="YOUR_PRIVATE_KEY", # required
200+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
204201
)
205202
# Below is optional parameter
206203
.with_timeout(10)
207-
# Public key is optional, used only for callback signature verification
208-
.with_public_key("SAT_PUBLIC_KEY")
209204
# Override SAT Base URL
210205
.with_sat_base_url("https://b2b.tokopedia.com/api")
211206
)
@@ -254,11 +249,10 @@ config = (
254249
client_id="YOUR_CLIENT_ID", # required
255250
client_secret="YOUR_CLIENT_SECRET", # required
256251
private_key="YOUR_PRIVATE_KEY", # required
252+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
257253
)
258254
# Below is optional parameter
259255
.with_timeout(10)
260-
# Public key is optional, used only for callback signature verification
261-
.with_public_key("SAT_PUBLIC_KEY")
262256
# Override SAT Base URL
263257
.with_sat_base_url("https://b2b.tokopedia.com/api")
264258
)
@@ -275,11 +269,10 @@ config = (
275269
client_id="YOUR_CLIENT_ID", # required
276270
client_secret="YOUR_CLIENT_SECRET", # required
277271
private_key="YOUR_PRIVATE_KEY", # required
272+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
278273
)
279274
# Below is optional parameter
280275
.with_timeout(10)
281-
# Public key is optional, used only for callback signature verification
282-
.with_public_key("SAT_PUBLIC_KEY")
283276
# Override SAT Base URL
284277
.with_sat_base_url("https://b2b.tokopedia.com/api")
285278
)
@@ -303,7 +296,8 @@ config = SATClientConfig(
303296
client_id="YOUR_CLIENT_ID", # required
304297
client_secret="YOUR_CLIENT_SECRET", # required
305298
private_key="YOUR_PRIVATE_KEY", # required
306-
).with_public_key("SAT_PUBLIC_KEY")
299+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
300+
)
307301

308302
sat_client = SATClient(config)
309303

@@ -354,7 +348,8 @@ config = SATClientConfig(
354348
client_id="YOUR_CLIENT_ID", # required
355349
client_secret="YOUR_CLIENT_SECRET", # required
356350
private_key="YOUR_PRIVATE", # required
357-
).with_public_key("SAT_PUBLIC_KEY")
351+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
352+
)
358353

359354
sat_client = SATClient(config)
360355

@@ -387,7 +382,8 @@ config = SATClientConfig(
387382
client_id="YOUR_CLIENT_ID", # required
388383
client_secret="YOUR_CLIENT_SECRET", # required
389384
private_key="YOUR_PRIVATE", # required
390-
).with_public_key("SAT_PUBLIC_KEY")
385+
sat_public_key="SAT_PUBLIC_KEY", # required !IMPORTANT: This is SAT public key, NOT your public key
386+
)
391387

392388
try:
393389
sat_client = SATClient(config)

py_sat/client.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class SATClientConfig:
3434
client_id: str
3535
client_secret: str
3636
private_key: str
37+
sat_public_key: str
3738

3839
# Optional
39-
public_key: Optional[str]
4040
padding_type: SignatureType
4141
is_debug: bool
4242
sat_base_url: str
@@ -49,6 +49,7 @@ def __init__(
4949
client_id: str,
5050
client_secret: str,
5151
private_key: str,
52+
sat_public_key: str,
5253
):
5354
if not client_id or not isinstance(client_id, str):
5455
raise InvalidInputException("Client ID are required and must be a string")
@@ -61,9 +62,13 @@ def __init__(
6162
if not private_key or not isinstance(private_key, str):
6263
raise InvalidInputException("Private key is required and must be a string")
6364

65+
if not sat_public_key or not isinstance(sat_public_key, str):
66+
raise InvalidInputException("Public key is required and must be a string")
67+
6468
self.client_id = client_id
6569
self.client_secret = client_secret
6670
self.private_key = private_key
71+
self.sat_public_key = sat_public_key
6772

6873
self._set_default_value()
6974

@@ -73,7 +78,6 @@ def _set_default_value(self):
7378
logger.setLevel(logging.DEBUG)
7479
self.logger = logger
7580

76-
self.public_key = None
7781
self.padding_type = SignatureType.PSS
7882
self.is_debug = False
7983
self.sat_base_url = PLAYGROUND_SAT_BASE_URL
@@ -84,10 +88,6 @@ def with_logger(self, logger: logging.Logger):
8488
self.logger = logger
8589
return self
8690

87-
def with_public_key(self, public_key: str):
88-
self.public_key = public_key
89-
return self
90-
9191
def with_padding_type(self, padding_type: SignatureType):
9292
self.padding_type = padding_type
9393
return self
@@ -122,7 +122,7 @@ class SATClient:
122122
def __init__(self, config: SATClientConfig):
123123
self._config = config
124124
self.signature = Signature(
125-
config.private_key, config.public_key, config.padding_type
125+
config.private_key, config.sat_public_key, config.padding_type
126126
)
127127
self._logger = config.logger
128128
self._http_client = HTTPClient(
@@ -235,7 +235,18 @@ def check_status(self, request_id: str) -> Union[OrderDetail, ErrorResponse]:
235235
response = self._http_client.send_request(http_req)
236236
response.raise_for_status()
237237

238+
signature = response.headers.get("signature")
239+
if not signature:
240+
raise UnauthenticatedException(
241+
"Signature is not present in the header, please check the request"
242+
)
243+
244+
valid = self.signature.verify(response.text, signature)
245+
if not valid:
246+
raise UnauthenticatedException("Signature is not valid")
247+
238248
json_response = response.json()
249+
239250
data = parse_json_api_response(json_response)
240251

241252
return OrderDetail.from_dict(data).with_raw_response(response)

py_sat/signature/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class Signature:
2424
def __init__(
2525
self,
2626
private_key_str: Optional[str],
27-
public_key_str: Optional[str],
27+
sat_public_key_str: Optional[str],
2828
padding_type: SignatureType,
2929
):
3030
if not padding_type:
3131
raise InvalidInputException("Padding type is required")
3232

3333
self._private_key = self._parse_rsa_private_key_from_pem_str(private_key_str)
34-
self._public_key = self._parse_public_key(public_key_str)
34+
self._public_key = self._parse_public_key(sat_public_key_str)
3535
self._algorithm = self.__decide_padding_algorithm(padding_type)
3636

3737
def verify(self, msg: str, signature: str) -> bool:

0 commit comments

Comments
 (0)