Skip to content

Commit

Permalink
feat: promisify session.clearAuthCache()
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Mar 8, 2019
1 parent 17dab8f commit dffb858
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
22 changes: 13 additions & 9 deletions atom/browser/api/atom_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,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();
Expand All @@ -311,8 +311,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(
Expand Down Expand Up @@ -575,20 +576,23 @@ void Session::ClearHostResolverCache(mate::Arguments* args) {
callback));
}

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) {
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Session : public mate::TrackableObject<Session>,
void SetPermissionCheckHandler(v8::Local<v8::Value> val,
mate::Arguments* args);
void 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();
Expand Down
2 changes: 1 addition & 1 deletion docs/api/promisification.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
- [ses.resolveProxy(url, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#resolveProxy)
- [ses.clearHostResolverCache([callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearHostResolverCache)
- [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)
Expand Down Expand Up @@ -48,6 +47,7 @@ When a majority of affected functions are migrated, this flag will be enabled by
- [netLog.stopLogging([callback])](https://github.com/electron/electron/blob/master/docs/api/net-log.md#stopLogging)
- [protocol.isProtocolHandled(scheme, callback)](https://github.com/electron/electron/blob/master/docs/api/protocol.md#isProtocolHandled)
- [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
- [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache)
- [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)
- [webviewTag.printToPDF(options, callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#printToPDF)
- [win.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/browser-window.md#capturePage)
12 changes: 10 additions & 2 deletions docs/api/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,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
Expand Down
2 changes: 2 additions & 0 deletions lib/browser/api/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Session.prototype._init = function () {
app.emit('session-created', this)
}

Session.prototype.clearAuthCache = deprecate.promisify(Session.prototype.clearAuthCache)

Cookies.prototype.flushStore = deprecate.promisify(Cookies.prototype.flushStore)
Cookies.prototype.get = deprecate.promisify(Cookies.prototype.get)
Cookies.prototype.remove = deprecate.promisify(Cookies.prototype.remove)
Expand Down
55 changes: 54 additions & 1 deletion spec/api-session-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,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)
Expand Down

0 comments on commit dffb858

Please sign in to comment.