feat: promisify session.clearAuthCache() (#17259)
* feat: promisify session.clearAuthCache() * remove unused callback runner helpers
This commit is contained in:
parent
58a9a81895
commit
9ea6c01e02
6 changed files with 80 additions and 25 deletions
|
@ -210,17 +210,6 @@ const char kPersistPrefix[] = "persist:";
|
|||
// Referenced session objects.
|
||||
std::map<uint32_t, v8::Global<v8::Object>> g_sessions;
|
||||
|
||||
// Runs the callback in UI thread.
|
||||
void RunCallbackInUI(const base::Callback<void()>& callback) {
|
||||
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, callback);
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) {
|
||||
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
|
||||
base::BindOnce(callback, result...));
|
||||
}
|
||||
|
||||
void ResolveOrRejectPromiseInUI(atom::util::Promise promise, int net_error) {
|
||||
if (net_error != net::OK) {
|
||||
std::string err_msg = net::ErrorToString(net_error);
|
||||
|
@ -313,7 +302,7 @@ void ClearHostResolverCacheInIO(
|
|||
void ClearAuthCacheInIO(
|
||||
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
||||
const ClearAuthCacheOptions& options,
|
||||
const base::Closure& callback) {
|
||||
util::Promise promise) {
|
||||
auto* request_context = context_getter->GetURLRequestContext();
|
||||
auto* network_session =
|
||||
request_context->http_transaction_factory()->GetSession();
|
||||
|
@ -333,8 +322,9 @@ void ClearAuthCacheInIO(
|
|||
}
|
||||
network_session->CloseAllConnections();
|
||||
}
|
||||
if (!callback.is_null())
|
||||
RunCallbackInUI(callback);
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::UI},
|
||||
base::BindOnce(util::Promise::ResolveEmptyPromise, std::move(promise)));
|
||||
}
|
||||
|
||||
void AllowNTLMCredentialsForDomainsInIO(
|
||||
|
@ -625,20 +615,23 @@ v8::Local<v8::Promise> Session::ClearHostResolverCache(mate::Arguments* args) {
|
|||
return handle;
|
||||
}
|
||||
|
||||
void Session::ClearAuthCache(mate::Arguments* args) {
|
||||
v8::Local<v8::Promise> Session::ClearAuthCache(mate::Arguments* args) {
|
||||
v8::Isolate* isolate = args->isolate();
|
||||
util::Promise promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
ClearAuthCacheOptions options;
|
||||
if (!args->GetNext(&options)) {
|
||||
args->ThrowError("Must specify options object");
|
||||
return;
|
||||
promise.RejectWithErrorMessage("Must specify options object");
|
||||
return handle;
|
||||
}
|
||||
base::Closure callback;
|
||||
args->GetNext(&callback);
|
||||
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(&ClearAuthCacheInIO,
|
||||
WrapRefCounted(browser_context_->GetRequestContext()),
|
||||
options, callback));
|
||||
options, std::move(promise)));
|
||||
return handle;
|
||||
}
|
||||
|
||||
void Session::AllowNTLMCredentialsForDomains(const std::string& domains) {
|
||||
|
|
|
@ -78,7 +78,7 @@ class Session : public mate::TrackableObject<Session>,
|
|||
void SetPermissionCheckHandler(v8::Local<v8::Value> val,
|
||||
mate::Arguments* args);
|
||||
v8::Local<v8::Promise> ClearHostResolverCache(mate::Arguments* args);
|
||||
void ClearAuthCache(mate::Arguments* args);
|
||||
v8::Local<v8::Promise> ClearAuthCache(mate::Arguments* args);
|
||||
void AllowNTLMCredentialsForDomains(const std::string& domains);
|
||||
void SetUserAgent(const std::string& user_agent, mate::Arguments* args);
|
||||
std::string GetUserAgent();
|
||||
|
|
|
@ -14,7 +14,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
|
|||
- [inAppPurchase.purchaseProduct(productID, quantity, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#purchaseProduct)
|
||||
- [inAppPurchase.getProducts(productIDs, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#getProducts)
|
||||
- [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData)
|
||||
- [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache)
|
||||
- [contents.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#executeJavaScript)
|
||||
- [contents.print([options], [callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#print)
|
||||
- [webFrame.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-frame.md#executeJavaScript)
|
||||
|
@ -46,6 +45,7 @@ When a majority of affected functions are migrated, this flag will be enabled by
|
|||
- [ses.setProxy(config, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#setProxy)
|
||||
- [ses.resolveProxy(url, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#resolveProxy)
|
||||
- [ses.getCacheSize(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getCacheSize)
|
||||
- [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache)
|
||||
- [ses.clearCache(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#clearCache)
|
||||
- [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
|
||||
- [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)
|
||||
|
|
|
@ -531,13 +531,21 @@ event. The [DownloadItem](download-item.md) will not have any `WebContents` asso
|
|||
the initial state will be `interrupted`. The download will start only when the
|
||||
`resume` API is called on the [DownloadItem](download-item.md).
|
||||
|
||||
#### `ses.clearAuthCache(options[, callback])`
|
||||
#### `ses.clearAuthCache(options, callback)`
|
||||
|
||||
* `options` ([RemovePassword](structures/remove-password.md) | [RemoveClientCertificate](structures/remove-client-certificate.md))
|
||||
* `callback` Function (optional) - Called when operation is done.
|
||||
* `callback` Function - Called when operation is done.
|
||||
|
||||
Clears the session’s HTTP authentication cache.
|
||||
|
||||
**[Deprecated Soon](promisification.md)**
|
||||
|
||||
#### `ses.clearAuthCache(options)`
|
||||
|
||||
* `options` ([RemovePassword](structures/remove-password.md) | [RemoveClientCertificate](structures/remove-client-certificate.md))
|
||||
|
||||
Returns `Promise<void>` - resolves when the session’s HTTP authentication cache has been cleared.
|
||||
|
||||
#### `ses.setPreloads(preloads)`
|
||||
|
||||
* `preloads` String[] - An array of absolute path to preload scripts
|
||||
|
|
|
@ -29,6 +29,7 @@ Session.prototype.resolveProxy = deprecate.promisify(Session.prototype.resolvePr
|
|||
Session.prototype.setProxy = deprecate.promisify(Session.prototype.setProxy)
|
||||
Session.prototype.getCacheSize = deprecate.promisify(Session.prototype.getCacheSize)
|
||||
Session.prototype.clearCache = deprecate.promisify(Session.prototype.clearCache)
|
||||
Session.prototype.clearAuthCache = deprecate.promisify(Session.prototype.clearAuthCache)
|
||||
|
||||
Cookies.prototype.flushStore = deprecate.promisify(Cookies.prototype.flushStore)
|
||||
Cookies.prototype.get = deprecate.promisify(Cookies.prototype.get)
|
||||
|
|
|
@ -965,8 +965,61 @@ describe('session module', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('ses.clearAuthCache(options[, callback])', () => {
|
||||
describe('ses.clearAuthCache(options)', () => {
|
||||
it('can clear http auth info from cache', (done) => {
|
||||
const ses = session.fromPartition('auth-cache')
|
||||
const server = http.createServer((req, res) => {
|
||||
const credentials = auth(req)
|
||||
if (!credentials || credentials.name !== 'test' || credentials.pass !== 'test') {
|
||||
res.statusCode = 401
|
||||
res.setHeader('WWW-Authenticate', 'Basic realm="Restricted"')
|
||||
res.end()
|
||||
} else {
|
||||
res.end('authenticated')
|
||||
}
|
||||
})
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
const port = server.address().port
|
||||
function issueLoginRequest (attempt = 1) {
|
||||
if (attempt > 2) {
|
||||
server.close()
|
||||
return done()
|
||||
}
|
||||
const request = net.request({
|
||||
url: `http://127.0.0.1:${port}`,
|
||||
session: ses
|
||||
})
|
||||
request.on('login', (info, callback) => {
|
||||
attempt += 1
|
||||
assert.strictEqual(info.scheme, 'basic')
|
||||
assert.strictEqual(info.realm, 'Restricted')
|
||||
callback('test', 'test')
|
||||
})
|
||||
request.on('response', (response) => {
|
||||
let data = ''
|
||||
response.pause()
|
||||
response.on('data', (chunk) => {
|
||||
data += chunk
|
||||
})
|
||||
response.on('end', () => {
|
||||
assert.strictEqual(data, 'authenticated')
|
||||
ses.clearAuthCache({ type: 'password' }).then(() => {
|
||||
issueLoginRequest(attempt)
|
||||
})
|
||||
})
|
||||
response.on('error', (error) => { done(error) })
|
||||
response.resume()
|
||||
})
|
||||
// Internal api to bypass cache for testing.
|
||||
request.urlRequest._setLoadFlags(1 << 1)
|
||||
request.end()
|
||||
}
|
||||
issueLoginRequest()
|
||||
})
|
||||
})
|
||||
|
||||
// TODO(codebytere): remove when promisification complete
|
||||
it('can clear http auth info from cache (callback)', (done) => {
|
||||
const ses = session.fromPartition('auth-cache')
|
||||
const server = http.createServer((req, res) => {
|
||||
const credentials = auth(req)
|
||||
|
|
Loading…
Reference in a new issue