From 32a43b544598b24cc6ebe60f322bdbd5fe7ba1fd Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sun, 31 Dec 2023 21:09:33 +0530 Subject: [PATCH] feat: verify authorization token against mcaptcha --- src/mcaptcha_api/__init__.py | 6 ++++++ src/mcaptcha_api/client.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/mcaptcha_api/__init__.py create mode 100644 src/mcaptcha_api/client.py diff --git a/src/mcaptcha_api/__init__.py b/src/mcaptcha_api/__init__.py new file mode 100644 index 0000000..a8b3c22 --- /dev/null +++ b/src/mcaptcha_api/__init__.py @@ -0,0 +1,6 @@ +# SPDX-FileCopyrightText: 2023 Aravinth Manivannan +# +# SPDX-License-Identifier: Apache-2.0 +# SPDX-License-Identifier: MIT + +from .client import MCaptcha diff --git a/src/mcaptcha_api/client.py b/src/mcaptcha_api/client.py new file mode 100644 index 0000000..840c6b4 --- /dev/null +++ b/src/mcaptcha_api/client.py @@ -0,0 +1,30 @@ +# SPDX-FileCopyrightText: 2023 Aravinth Manivannan +# +# SPDX-License-Identifier: Apache-2.0 +# SPDX-License-Identifier: MIT + +from urllib.parse import urlparse, urlunparse +from requests import Session + + +class MCaptcha: + def __init__(self, instance_url: str, sitekey: str, secret: str): + self.client = Session() + self.instance_url = self.__clean_url(instance_url) + self.sitekey = sitekey + self.secret = secret + self.verify_url = self.__set_path(self.instance_url, "/api/v1/pow/siteverify") + + def __clean_url(self, url): + parsed = urlparse(url) + return urlunparse((parsed.scheme, parsed.netloc, "", "", "", "")) + + def __set_path(self, url: str, path: str) -> str: + parsed = urlparse(url) + return urlunparse((parsed.scheme, parsed.netloc, path, "", "", "")) + + def verify(self, token: str) -> bool: + payload = {"token": token, "key": self.sitekey, "secret": self.secret} + resp = self.client.post(self.verify_url, json=payload) + assert resp.status_code == 200 + return resp.json()["valid"]