Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control HTTP basic authentication using javascript

I'd like to check if a URL is protected by a Http Basic Authentication using javascript. Here is what I have so far :

  function showFailure(){ alert("FAILED ") }
  function showSuccess(){ alert("SUCCESS") }

  var myRequest = new Request({
    url: 'http://localhost/access_protected_HTTP_BASIC', 
    method: 'get', 
    onSuccess: showSuccess,
    onFailure: showFailure
  }).send();

But that actually opens the browser login popup to access the resource. Is there a way not to trigger that popup?

Thanks!

Note : I use mootools in this example but I'd take any javascript example that does the trick :)

like image 322
Nhaolio Avatar asked Mar 24 '26 07:03

Nhaolio


1 Answers

Based on the answer to a similar question, you can manually pass a username and password when sending a request. (And according to the MooTools Docs, the user and password parameters do exactly this.)

Further, the XMLHttpRequest spec says:

If authentication fails, Authorization is not in the list of author request headers, request username is non-null, and request password is non-null, user agents must not prompt the end user for their username and password.

This means you can set a dummy username and password, and the browser won't prompt the user. If you get back a 401 status code, it means authorization is required.

like image 183
casablanca Avatar answered Mar 25 '26 20:03

casablanca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!