PluginProbe
RabbitLoader / 3.0.5
RabbitLoader v3.0.5
4.0.2 4.0.1 4.0 3.2.0 3.1.1 3.1.0 3.0.5 3.0.3 3.0.4 2.17.1 2.17.2 2.17.3 2.17.4 2.17.5 2.17.6 2.17.7 2.18.0 2.18.1 2.18.2 2.18.3 2.18.4 2.18.5 2.18.6 2.18.7 2.18.8 All 129 releases
rabbit-loader / admin / js / connect-proof.js

connect-proof.js in RabbitLoader 3.0.5, at admin/js/connect-proof.js

93 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function () {
2 const config = window.RLConnectProofConfig || {};
3
4 if (!config.admin_ajax || !config.rl_nonce || !config.can_manage_options) {
5 return;
6 }
7
8 const allowedOrigins = Array.isArray(config.allowed_dashboard_origins)
9 ? config.allowed_dashboard_origins
10 : [];
11
12 const isAllowedOrigin = (origin) => allowedOrigins.includes(origin);
13
14 const sendResponse = (source, origin, payload) => {
15 if (!source || !origin) {
16 return;
17 }
18
19 source.postMessage(
20 {
21 type: 'rabbitloader:connect-proof-response',
22 source: 'rabbitloader-wordpress-admin',
23 ...payload,
24 },
25 origin,
26 );
27 };
28
29 window.addEventListener('message', async (event) => {
30 const data = event.data || {};
31 if (data.type !== 'rabbitloader:connect-proof-request') {
32 return;
33 }
34
35 if (!isAllowedOrigin(event.origin)) {
36 return;
37 }
38
39 if (!data.challenge_id || !data.challenge_nonce || !data.site_url) {
40 sendResponse(event.source, event.origin, {
41 result: false,
42 message: 'Missing reconnect challenge data.',
43 });
44 return;
45 }
46
47 try {
48 const body = new URLSearchParams({
49 action: 'rabbitloader_connect_proof',
50 rl_nonce: config.rl_nonce,
51 challenge_id: data.challenge_id,
52 challenge_nonce: data.challenge_nonce,
53 site_url: data.site_url,
54 });
55
56 const response = await fetch(config.admin_ajax, {
57 method: 'POST',
58 credentials: 'same-origin',
59 headers: {
60 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
61 },
62 body: body.toString(),
63 });
64 const json = await response.json();
65
66 if (!response.ok || !json || !json.result || !json.redeem_token) {
67 sendResponse(event.source, event.origin, {
68 result: false,
69 challenge_id: data.challenge_id,
70 message:
71 json && json.data && json.data.message
72 ? json.data.message
73 : 'Reconnect proof failed.',
74 });
75 return;
76 }
77
78 sendResponse(event.source, event.origin, {
79 result: true,
80 challenge_id: data.challenge_id,
81 redeem_token: json.redeem_token,
82 expires_at: json.expires_at || 0,
83 });
84 } catch (_error) {
85 sendResponse(event.source, event.origin, {
86 result: false,
87 challenge_id: data.challenge_id,
88 message: 'Reconnect proof request failed.',
89 });
90 }
91 });
92 })();
93