-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.py
64 lines (50 loc) · 1.36 KB
/
request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
import datetime
import json
import urllib
publicKey = RSA.import_key(open("rsa_public_key.pem", "rb").read())
def get_pub_cert():
url = 'http://127.0.0.1:8000/public_cert'
r = requests.get(url)
return r.json()
def rsa_encrypt(plaintext):
cert = RSA.import_key(get_pub_cert())
cipher = PKCS1_OAEP.new(cert)
encrypt_text = cipher.encrypt(bytes(plaintext.encode("utf8")))
return encrypt_text.hex()
def generate_login_credit():
_id = '65a0bd12e0ffb1863b9a48ca'
password = '20230616'
stamp = datetime.datetime.utcnow()
payload = {
"password": password,
"time": stamp.timestamp(),
}
credit = rsa_encrypt(json.dumps(payload))
return _id, credit
def request():
url = 'http://127.0.0.1:8000/auth'
_id, credit = generate_login_credit()
payload = {
"userid": _id,
"credit": credit,
}
r = requests.post(url, data=payload)
return r.json()
token = request()
print(token)
header = {
'Authorization': 'Bearer ' + token
}
def valid():
url = 'http://127.0.0.1:8000/valid'
r = requests.post(url, headers=header)
return r.json()
print(valid())
def is_admin():
url = 'http://127.0.0.1:8000/need_admin'
r = requests.post(url, headers=header)
return r.json()
print(is_admin())