Domain Authorization in Chrome Extension

There is a requirement to make a cross-domain request to a remote service in a chrome extension using ajax.

Everything works correctly when the user is authorized on the service before he uses the extension. But when the unarized user makes the request extension with an error with error code 401 (unaronized).

I need to make a browser similar in appearance . If you are not authorized in any browser window of the web page, you will be prompted to enter your credentials. Because they are the correct contents of the browser web page.

I tried to add some invisible element to the HTML layout extension that lies on the service to make the browser of the built-in domain authorization dialog appear, but the identifier does not seem to be correct.

In short, did you know any strategy to force the built-in browser authorization from the chrome extension or simply through javascript code without redirecting the user to any separate service web page. Or maybe this is not possible and why?

The code that makes the request is simple:

$.ajax({ url: 'service url that requires authorization', dataType: 'json', success: function (info){ //success handler }, error: function (){ //error handler } }); 

WBR, Vitaliy

+3
source share
1 answer

I think you can listen to chrome.webRequest.onAuthRequired ( https://developer.chrome.com/trunk/extensions/webRequest.html#event-onAuthRequired ). In the event listener, you can ask the user to enter credentials, say, by showing a user window that looks like a built-in authorization dialog. onAuthRequired is the only event in chrome.webRequest that supports asynchronous locking, which allows you to asynchronously receive credentials (from the user) and send a response (credentials).

 chrome.webRequest.onAuthRequired.addListener(function(details, callback){ // Prompt the user to enter credentials. Call // callback({authCredentials: {username: xxx, password: xxx}}); // when they are ready. }, {.../*request filter*/}, ['asyncBlocking']); 
+1
source

Source: https://habr.com/ru/post/945284/


All Articles