Skip to content

Commit f4e16ce

Browse files
committed
feature: added get api key API and tested successfully
1 parent 678e49e commit f4e16ce

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

src/lighthouseweb3/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import os
44
import io
5-
from .functions import upload as d,deal_status, get_uploads as getUploads, download as _download
5+
from .functions import (
6+
upload as d,
7+
deal_status,
8+
get_uploads as getUploads,
9+
download as _download,
10+
get_api_key as getApiKey
11+
)
612

713

814
class Lighthouse:
@@ -96,6 +102,22 @@ def download(cid: str):
96102
return _download.get_file(cid)
97103
except Exception as e:
98104
raise e
105+
106+
@staticmethod
107+
def getApiKey(publicKey: str, signedMessage: str):
108+
"""
109+
Generates and returns an API key for the given public key and signed message.
110+
111+
:param publicKey: str, The public key associated with the user.
112+
:param signedMessage: str, The message signed by the user's private key.
113+
:return: dict, A dict with generated API key.
114+
"""
115+
116+
117+
try:
118+
return getApiKey.get_api_key(publicKey, signedMessage)
119+
except Exception as e:
120+
raise e
99121

100122
def getTagged(self, tag: str):
101123
"""
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)