feat: API client with method to verify authorization token

This commit is contained in:
Aravinth Manivannan 2024-01-03 01:06:29 +05:30
parent 8ffc6cd45f
commit 567bd494e6
Signed by: realaravinth
GPG key ID: F8F50389936984FF
5 changed files with 1502 additions and 0 deletions

21
Makefile Normal file
View file

@ -0,0 +1,21 @@
env:
./scripts/download-cli.sh download
. venv/bin/activate && pip install -r requirements.txt
. venv/bin/activate && pip install build
test.env:
$(eval SECRET = $(shell . ./venv/bin/activate && python ./scripts/create_captcha.py get_secret $(INSTANCE_URL) $(USERNAME) $(PASSWORD)))
$(eval SITEKEY = $(shell . ./venv/bin/activate && python ./scripts/create_captcha.py create_captcha $(INSTANCE_URL) $(USERNAME) $(PASSWORD)))
$(eval WIDGET_URL = $(shell . ./venv/bin/activate && python ./scripts/create_captcha.py widget_url $(INSTANCE_URL) $(SITEKEY)))
$(eval TOKEN = $(shell ./scripts/download-cli.sh gen_pow $(WIDGET_URL)))
@-rm .env.local
echo "SECRET=$(SECRET)" > .env.local
echo "SITEKEY=$(SITEKEY)" >> .env.local
echo "TOKEN=$(TOKEN)" >> .env.local
test:
pnpm test
lint:
black src/ scripts/

1384
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

46
package.json Normal file
View file

@ -0,0 +1,46 @@
{
"name": "mcaptcha-api-js",
"version": "1.0.0",
"author": "Aravinth Manivannan <realaravinth@batsense.net>",
"description": "Library to interact with mCaptcha API",
"license": "(MIT OR Apache-2.0)",
"keywords": ["mCaptcha", "CAPTCHA", "proof of work"],
"homepage": "https://mcaptcha.org",
"main": "src/index.js",
"files": ["src"],
"bugs": {
"url": "https://git.batsense.net/mCaptcha/mcaptcha-api-js/issues",
"email": "realaravinth@batsense.net"
},
"funding": [
{
"type": "individual",
"url": "http://mcaptcha.org/donate"
},
{
"type": "liberapay",
"url": "https://liberapay.com/mcaptcha"
},
{
"type": "individual",
"url": "http://batsense.net/donate"
},
{
"type": "liberapay",
"url": "https://liberapay.com/realaravinth"
}
],
"repository": {
"type": "git",
"url": "https://git.batsense.net/mCaptcha/mcaptcha-api-js"
},
"scripts": {
"test": "mocha src/index.test.js"
},
"devDependencies": {
"dotenv": "^16.3.1",
"mocha": "^10.2.0",
"request": "^2.88.2"
}
}

7
requirements.txt Normal file
View file

@ -0,0 +1,7 @@
certifi==2023.11.17
charset-normalizer==3.3.2
idna==3.6
iniconfig==2.0.0
pluggy==1.3.0
requests==2.31.0
urllib3==2.1.0

44
src/index.js Normal file
View file

@ -0,0 +1,44 @@
/**
* mCaptcha API client
*/
class MCaptcha {
/**
* @constructor
* @param {string} sitekey - the unique identifier of captcha configuration
* @param {string} secret - account secret, can be obtained from mCaptcha dashboard
* @param {URL} instanceUrl - the URL of the mCaptcha instance you are using
*/
constructor(sitekey, secret, instanceUrl) {
this.sitekey = sitekey;
this.secret = secret;
this.verify_url = new URL(instanceUrl);
this.verify_url.pathname = "/api/v1/pow/siteverify";
}
/**
* Verify authorization token presented by user
* @param {string} token
*/
async verify(token) {
let payload = {
token: token,
secret: this.secret,
key: this.sitekey,
};
let res = await fetch(this.verify_url, {
method: "POST",
body: JSON.stringify(payload),
headers: { "Content-Type": "application/json; charset=UTF-8" },
});
if (res.status != 200) {
let error = await res.json();
error = `status code: ${res.status}: ${error.error}`;
throw new Error(error);
}
res = await res.json();
console.log(res);
return res.valid;
}
}
module.exports = MCaptcha;