From 35349643af379ecaacd4b636644974fb4333e5b1 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Wed, 30 Nov 2016 22:05:15 +0530 Subject: [PATCH] session: api to clear auth cache --- atom/browser/api/atom_api_session.cc | 84 +++++++++++++++++++++++++++- atom/browser/api/atom_api_session.h | 1 + 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 8cb9d3395e5c..ab0c99ade5e5 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -62,6 +62,15 @@ struct ClearStorageDataOptions { uint32_t quota_types = StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL; }; +struct ClearAuthCacheOptions { + std::string type; + GURL origin; + std::string realm; + base::string16 username; + base::string16 password; + net::HttpAuth::Scheme auth_scheme; +}; + uint32_t GetStorageMask(const std::vector& storage_types) { uint32_t storage_mask = 0; for (const auto& it : storage_types) { @@ -100,6 +109,18 @@ uint32_t GetQuotaMask(const std::vector& quota_types) { return quota_mask; } +net::HttpAuth::Scheme GetAuthSchemeFromString(const std::string& scheme) { + if (scheme == "basic") + return net::HttpAuth::AUTH_SCHEME_BASIC; + if (scheme == "digest") + return net::HttpAuth::AUTH_SCHEME_DIGEST; + if (scheme == "ntlm") + return net::HttpAuth::AUTH_SCHEME_NTLM; + if (scheme == "negotiate") + return net::HttpAuth::AUTH_SCHEME_NEGOTIATE; + return net::HttpAuth::AUTH_SCHEME_MAX; +} + void SetUserAgentInIO(scoped_refptr getter, const std::string& accept_lang, const std::string& user_agent) { @@ -131,7 +152,27 @@ struct Converter { } }; -template<> +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + ClearAuthCacheOptions* out) { + mate::Dictionary options; + if (!ConvertFromV8(isolate, val, &options)) + return false; + options.Get("type", &out->type); + options.Get("origin", &out->origin); + options.Get("realm", &out->realm); + options.Get("username", &out->username); + options.Get("password", &out->password); + std::string scheme; + if (options.Get("scheme", &scheme)) + out->auth_scheme = GetAuthSchemeFromString(scheme); + return true; + } +}; + +template <> struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Local val, @@ -314,6 +355,33 @@ void ClearHostResolverCacheInIO( } } +void ClearAuthCacheInIO( + const scoped_refptr& context_getter, + const ClearAuthCacheOptions& options, + const base::Closure& callback) { + auto request_context = context_getter->GetURLRequestContext(); + auto network_session = + request_context->http_transaction_factory()->GetSession(); + if (network_session) { + if (options.type == "password") { + auto auth_cache = network_session->http_auth_cache(); + if (!options.origin.is_empty()) { + auth_cache->Remove( + options.origin, options.realm, options.auth_scheme, + net::AuthCredentials(options.username, options.password)); + } else { + auth_cache->Clear(); + } + } else if (options.type == "clientCertificate") { + auto client_auth_cache = network_session->ssl_client_auth_cache(); + client_auth_cache->Remove(net::HostPortPair::FromURL(options.origin)); + } + network_session->CloseAllConnections(); + } + if (!callback.is_null()) + RunCallbackInUI(callback); +} + void AllowNTLMCredentialsForDomainsInIO( const scoped_refptr& context_getter, const std::string& domains) { @@ -501,6 +569,19 @@ void Session::ClearHostResolverCache(mate::Arguments* args) { callback)); } +void Session::ClearAuthCache(mate::Arguments* args) { + ClearAuthCacheOptions options; + args->GetNext(&options); + base::Closure callback; + args->GetNext(&callback); + + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + base::Bind(&ClearAuthCacheInIO, + make_scoped_refptr(browser_context_->GetRequestContext()), + options, callback)); +} + void Session::AllowNTLMCredentialsForDomains(const std::string& domains) { BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&AllowNTLMCredentialsForDomainsInIO, @@ -649,6 +730,7 @@ void Session::BuildPrototype(v8::Isolate* isolate, .SetMethod("setPermissionRequestHandler", &Session::SetPermissionRequestHandler) .SetMethod("clearHostResolverCache", &Session::ClearHostResolverCache) + .SetMethod("clearAuthCache", &Session::ClearAuthCache) .SetMethod("allowNTLMCredentialsForDomains", &Session::AllowNTLMCredentialsForDomains) .SetMethod("setUserAgent", &Session::SetUserAgent) diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 6024e135b1cb..72f186e4fee4 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -74,6 +74,7 @@ class Session: public mate::TrackableObject, void SetPermissionRequestHandler(v8::Local val, mate::Arguments* args); void ClearHostResolverCache(mate::Arguments* args); + void ClearAuthCache(mate::Arguments* args); void AllowNTLMCredentialsForDomains(const std::string& domains); void SetUserAgent(const std::string& user_agent, mate::Arguments* args); std::string GetUserAgent();