| 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 |
|