Skip to content

Commit d95c8c0

Browse files
Merge pull request #21 from unniznd/feature_get_api_key
feature: getApiKey API added and tested successfully
2 parents 4c6ada8 + 392d1ae commit d95c8c0

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ charset-normalizer==3.1.0
44
idna==3.4
55
requests==2.31.0
66
urllib3==2.0.2
7+
web3
8+
eth-accounts

src/lighthouseweb3/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
get_uploads as getUploads,
99
download as _download,
1010
get_file_info as getFileInfo,
11-
get_balance as getBalance
11+
get_balance as getBalance,
12+
get_api_key as getApiKey
1213
)
1314

1415

@@ -125,6 +126,21 @@ def getFileInfo(cid: str):
125126
return getFileInfo.get_file_info(cid)
126127
except Exception as e:
127128
raise e
129+
130+
@staticmethod
131+
def getApiKey(publicKey: str, signedMessage: str):
132+
"""
133+
Generates and returns an API key for the given public key and signed message.
134+
:param publicKey: str, The public key associated with the user.
135+
:param signedMessage: str, The message signed by the user's private key.
136+
:return: dict, A dict with generated API key.
137+
"""
138+
139+
140+
try:
141+
return getApiKey.get_api_key(publicKey, signedMessage)
142+
except Exception as e:
143+
raise e
128144

129145
def getTagged(self, tag: str):
130146
"""
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def get_api_key(publicKey: str, signedMessage: str):
5+
url = f"{Config.lighthouse_api}/api/auth/create_api_key"
6+
7+
data = {
8+
"publicKey": publicKey,
9+
"signedMessage": signedMessage
10+
}
11+
12+
try:
13+
response = req.post(url, data=data)
14+
except Exception as e:
15+
raise Exception("Failed to create api key")
16+
17+
if response.status_code != 200:
18+
return response.json()
19+
20+
apiKey = response.json()
21+
22+
result = {
23+
"data": {
24+
"apiKey" : apiKey
25+
}
26+
}
27+
28+
return result

tests/test_get_api_key.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import unittest
4+
from src.lighthouseweb3 import Lighthouse
5+
from .setup import parse_env
6+
import requests as req
7+
from src.lighthouseweb3.functions.config import Config
8+
from web3 import Web3
9+
from eth_account.messages import encode_defunct
10+
11+
12+
class TestGetApiKey(unittest.TestCase):
13+
14+
def test_get_api_key(self):
15+
"""test getApiKey using valid signed message and public key"""
16+
parse_env()
17+
publicKey = os.environ.get("PUBLIC_KEY")
18+
response = req.get(
19+
f"{Config.lighthouse_api}/api/auth/get_auth_message?publicKey={publicKey}"
20+
)
21+
22+
if(response.status_code != 200):
23+
raise Exception("Failed to get authentication message")
24+
25+
verificationMessage = response.json()
26+
27+
self.assertIn("Please prove you are the owner", verificationMessage, "Owner response should come")
28+
29+
encodedMessage = encode_defunct(text=verificationMessage)
30+
31+
32+
signedMessage = Web3().eth.account.sign_message(
33+
encodedMessage,
34+
private_key=os.environ.get("PRIVATE_KEY")
35+
).signature.hex()
36+
37+
res = Lighthouse.getApiKey(publicKey, f"0x{signedMessage}")
38+
39+
self.assertIsInstance(res, dict, "res is a dict")
40+
self.assertIsInstance(res.get("data"), dict, "data is a dict")
41+
self.assertIsInstance(res.get('data').get('apiKey'), str, "apiKey is a string")
42+
43+
def test_get_api_key_with_invalid_message(self):
44+
"""test getApiKey using signed invalid message and public key"""
45+
parse_env()
46+
publicKey = os.environ.get("PUBLIC_KEY")
47+
encodedMessage = encode_defunct(text='random_message')
48+
49+
50+
signedMessage = Web3().eth.account.sign_message(
51+
encodedMessage,
52+
private_key=os.environ.get("PRIVATE_KEY")
53+
).signature.hex()
54+
55+
res = Lighthouse.getApiKey(publicKey, f"0x{signedMessage}")
56+
self.assertIsInstance(res, dict, "res is a dict")
57+
self.assertIsInstance(res.get("error"), dict, "data is a dict")
58+
59+
def test_get_api_key_with_random_private_key(self):
60+
"""test getApiKey using signed message with invalid private key and public key"""
61+
62+
parse_env()
63+
publicKey = os.environ.get("PUBLIC_KEY")
64+
response = req.get(
65+
f"{Config.lighthouse_api}/api/auth/get_auth_message?publicKey={publicKey}"
66+
)
67+
68+
if(response.status_code != 200):
69+
raise Exception("Failed to get authentication message")
70+
71+
verificationMessage = response.json()
72+
73+
self.assertIn("Please prove you are the owner", verificationMessage, "Owner response should come")
74+
75+
encodedMessage = encode_defunct(text=verificationMessage)
76+
77+
78+
signedMessage = Web3().eth.account.sign_message(
79+
encodedMessage,
80+
private_key='0x8218aa5dbf4dbec243142286b93e26af521b3e91219583595a06a7765abc9c8b'
81+
).signature.hex()
82+
83+
res = Lighthouse.getApiKey(publicKey, f"0x{signedMessage}")
84+
85+
self.assertIsInstance(res, dict, "res is a dict")
86+
self.assertIsInstance(res.get("error"), dict, "data is a dict")

0 commit comments

Comments
 (0)