Reactive-Extensions / RxJS-DOM

HTML DOM Bindings for the Reactive Extensions for JavaScript
http://reactivex.io
Other
437 stars 99 forks source link

PhantomJS Bug and RxDom.DOM.ajax() Basic Authorization #97

Closed skoppe closed 9 years ago

skoppe commented 9 years ago

This is all due to some bug in PhantomJS.

What happens is that PhantomJS strips the Authorization Header.

To fix, change you call to ajax() from:

RxDom.DOM.ajax({url:"http://example.com",user:"noob",password:"123456",headers:{"Authorization":api.auth}});

to:

RxDom.DOM.ajax({url:"http://example.com",headers:{"Authorization":"Basic "+btoa("noob:123456")}});

Watchout, btoa is not supported on <= IE 9. Use some other base64 encoding script in case you need IE9 support.

If RxJS-DOM could detect Phantom-JS environment it might auto-apply this workaround.

mattpodwysocki commented 9 years ago

@skoppe unfortunately, I don't think I can detect if I'm in a broken environment for basic auth, although I'd love to know. Closing for now unless someone else has an answer.

skoppe commented 9 years ago

Yeah, there seems to be only hacks to detect PhantomJS. Although a combo would provide good detection rate, it feels brittle.

Correct me I am wrong, but I suspect RxDOM puts the user and password options in the uri (http://noob:123456@example.com), and lets the browser create the Authorization header. In that case RxDOM could always create the Authorization header itself.

mattpodwysocki commented 9 years ago

@skoppe we do nothing more than setting the user name and password as specified in the xhr.open method

skoppe commented 9 years ago

Of course, that is the way to set it. Sorry, not enough JS programming lately :)

Still, the same solution applies.

xhr.open(method,uri,async);
xhr.setRequestHeader("Authorization","Basic "+btoa(user+":"+password));
xhr.send();

Although like I said before, btoa isn't supported on <= IE 9. I don't know if that is a problem. In that case you would need to pull in a base64 encoder.

mattpodwysocki commented 9 years ago

@skoppe luckily btoa is easily polyfilled if necessary such as what we did here: https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/tests/helpers/btoa.js