Skip to content

Commit ef5a483

Browse files
committed
Added an additional demo for mutli step auth and url assignment.
1 parent 59708be commit ef5a483

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

docs/demo-4.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
from wsrepl import Plugin
2+
import requests
3+
4+
# Set to None if no proxy required
5+
plugin_proxy = {
6+
"http": "http://127.0.0.1:8080",
7+
"https": "http://127.0.0.1:8080"
8+
}
9+
10+
class MultiStepAuthDemo(Plugin):
11+
url = None # Required to pass dynamic wss url to MessageHandler.py
12+
13+
def init(self):
14+
# Step one: Get a JWT
15+
response = self.send_jwt_request()
16+
jwt_token = self.extract_jwt_from_response(response)
17+
if jwt_token:
18+
# Step 2: Get a access/bearer token
19+
response_jwt = self.request_access_token(jwt_token)
20+
access_token = self.extract_access_token_from_response(response_jwt)
21+
if access_token:
22+
# Step 3: Get the dynamic wss link
23+
wss_start = self.get_wss_endpoint(access_token)
24+
self.url = self.extract_url_from_response(wss_start)
25+
26+
def send_jwt_request(self):
27+
url = "https://example.com/users/auth"
28+
headers = {
29+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0",
30+
"Accept": "application/json",
31+
"Accept-Language": "en-US,en;q=0.5",
32+
"Accept-Encoding": "gzip, deflate",
33+
"Content-Type": "application/x-www-form-urlencoded",
34+
"Origin": "https://example-origin.com",
35+
"Referer": "https://example-referer.com/",
36+
"Sec-Fetch-Dest": "empty",
37+
"Sec-Fetch-Mode": "cors",
38+
"Sec-Fetch-Site": "cross-site"
39+
}
40+
41+
data = {
42+
"clientId": "your-client-id",
43+
"secret": "your-client-secret",
44+
"identity": "user-identity",
45+
"aud": "public",
46+
"isAnonymous": "true",
47+
"sid": "session-id",
48+
"page": "contactus",
49+
"lang": "en_US",
50+
"role": "VISITOR"
51+
}
52+
53+
response = requests.post(url, headers=headers, data=data, proxies=plugin_proxy, verify=False)
54+
return response
55+
56+
def extract_jwt_from_response(self, response):
57+
try:
58+
json_data = response.json()
59+
jwt_token = json_data.get("token")
60+
return jwt_token
61+
except ValueError:
62+
return None
63+
64+
def request_access_token(self, jwt_token):
65+
url = "https://example.com/api/token/jwtgrant"
66+
headers = {
67+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0",
68+
"Accept": "*/*",
69+
"Accept-Language": "en-US,en;q=0.5",
70+
"Accept-Encoding": "gzip, deflate",
71+
"Content-Type": "application/json",
72+
"Origin": "https://example-origin.com",
73+
"Referer": "https://example-referer.com/",
74+
"Sec-Fetch-Dest": "empty",
75+
"Sec-Fetch-Mode": "cors",
76+
"Sec-Fetch-Site": "cross-site"
77+
}
78+
79+
data = {
80+
"assertion": jwt_token,
81+
"botInfo": {
82+
"chatBot": "example-bot",
83+
"botId": "task-bot-id"
84+
}
85+
}
86+
87+
response = requests.post(url, headers=headers, json=data, proxies=plugin_proxy, verify=False)
88+
return response
89+
90+
def extract_access_token_from_response(self, response):
91+
try:
92+
json_data = response.json()
93+
access_token = json_data["authorization"]["accessToken"]
94+
return access_token
95+
except (ValueError, KeyError):
96+
return None
97+
98+
def get_wss_endpoint(self, access_token):
99+
url = "https://example.com/api/chat/start"
100+
headers = {
101+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0",
102+
"Accept": "application/json",
103+
"Accept-Language": "en-US,en;q=0.5",
104+
"Accept-Encoding": "gzip, deflate",
105+
"Content-Type": "application/json",
106+
"Authorization": f"Bearer {access_token}",
107+
"Origin": "https://example-origin.com",
108+
"Referer": "https://example-referer.com/",
109+
"Sec-Fetch-Dest": "empty",
110+
"Sec-Fetch-Mode": "cors",
111+
"Sec-Fetch-Site": "cross-site"
112+
}
113+
114+
data = {
115+
"botInfo": {
116+
"chatBot": "example-bot",
117+
"botId": "task-bot-id"
118+
}
119+
}
120+
121+
response = requests.post(url, headers=headers, json=data, proxies=plugin_proxy, verify=False)
122+
return response
123+
124+
def extract_url_from_response(self, response):
125+
try:
126+
json_data = response.json()
127+
url = json_data["endpoint"]
128+
return url
129+
except (ValueError, KeyError):
130+
return None

0 commit comments

Comments
 (0)