-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdemo-4.py
130 lines (115 loc) · 4.56 KB
/
demo-4.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from wsrepl import Plugin
import requests
# Set to None if no proxy required
plugin_proxy = {
"http": "http://127.0.0.1:8080",
"https": "http://127.0.0.1:8080"
}
class MultiStepAuthDemo(Plugin):
url = None # Required to pass dynamic wss url to MessageHandler.py
def init(self):
# Step one: Get a JWT
response = self.send_jwt_request()
jwt_token = self.extract_jwt_from_response(response)
if jwt_token:
# Step 2: Get a access/bearer token
response_jwt = self.request_access_token(jwt_token)
access_token = self.extract_access_token_from_response(response_jwt)
if access_token:
# Step 3: Get the dynamic wss link
wss_start = self.get_wss_endpoint(access_token)
self.url = self.extract_url_from_response(wss_start)
def send_jwt_request(self):
url = "https://example.com/users/auth"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0",
"Accept": "application/json",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/x-www-form-urlencoded",
"Origin": "https://example-origin.com",
"Referer": "https://example-referer.com/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site"
}
data = {
"clientId": "your-client-id",
"secret": "your-client-secret",
"identity": "user-identity",
"aud": "public",
"isAnonymous": "true",
"sid": "session-id",
"page": "contactus",
"lang": "en_US",
"role": "VISITOR"
}
response = requests.post(url, headers=headers, data=data, proxies=plugin_proxy, verify=False)
return response
def extract_jwt_from_response(self, response):
try:
json_data = response.json()
jwt_token = json_data.get("token")
return jwt_token
except ValueError:
return None
def request_access_token(self, jwt_token):
url = "https://example.com/api/token/jwtgrant"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/json",
"Origin": "https://example-origin.com",
"Referer": "https://example-referer.com/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site"
}
data = {
"assertion": jwt_token,
"botInfo": {
"chatBot": "example-bot",
"botId": "task-bot-id"
}
}
response = requests.post(url, headers=headers, json=data, proxies=plugin_proxy, verify=False)
return response
def extract_access_token_from_response(self, response):
try:
json_data = response.json()
access_token = json_data["authorization"]["accessToken"]
return access_token
except (ValueError, KeyError):
return None
def get_wss_endpoint(self, access_token):
url = "https://example.com/api/chat/start"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0",
"Accept": "application/json",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}",
"Origin": "https://example-origin.com",
"Referer": "https://example-referer.com/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site"
}
data = {
"botInfo": {
"chatBot": "example-bot",
"botId": "task-bot-id"
}
}
response = requests.post(url, headers=headers, json=data, proxies=plugin_proxy, verify=False)
return response
def extract_url_from_response(self, response):
try:
json_data = response.json()
url = json_data["endpoint"]
return url
except (ValueError, KeyError):
return None