My Cordova mobile app uses an iframe to load in a website. I want to send and receive contents over the iframe with postMessage()
. However, through my testing my mobile app origin is always localhost:8000
or file://
. Every other example on this site uses a unique domain and origin (e.g. www.example.com), but my origin is obviously not unique.
How can I secure communication between my mobile app and my website if my origin is localhost:8000
/ file://
? If for whatever reason I'm not able to, can I use access tokens to validate any communication like shown below?
mobile.app
var iframe = document.getElementById('iframe');
var data = {
'access_token': 'whatever'
};
iframe.contentWindow.postMessage(data, 'localhost:8000');
website.com
window.addEventListener('message', function(event) {
if (!event.data || !event.data.access_token) {return;}
// ajax request to validate the token here
});
If it helps, the things I'm doing are:
- Detect when the iframe has finished loading. iframe sends back an object.
{'loaded':true}
- Send an object with a boolean from the iframe when the user logs out.
{'logout':true}
- Send an object with a boolean and an html string from the iframe when the user presses a print button.
{'print':true, 'html':htmlString}
- Send encrypted payload to the iframe, then waiting for the iframe to return an object.
{'success':true}