Add session.setCertificateVerifyProc
This commit is contained in:
parent
e432abfb42
commit
c5bfac1969
4 changed files with 51 additions and 102 deletions
|
@ -13,6 +13,7 @@
|
||||||
#include "atom/browser/api/save_page_handler.h"
|
#include "atom/browser/api/save_page_handler.h"
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/browser/atom_browser_main_parts.h"
|
#include "atom/browser/atom_browser_main_parts.h"
|
||||||
|
#include "atom/browser/net/atom_cert_verifier.h"
|
||||||
#include "atom/common/native_mate_converters/callback.h"
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
#include "atom/common/native_mate_converters/gurl_converter.h"
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
||||||
#include "atom/common/native_mate_converters/file_path_converter.h"
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
||||||
|
@ -243,7 +244,6 @@ void SetProxyInIO(net::URLRequestContextGetter* getter,
|
||||||
Session::Session(AtomBrowserContext* browser_context)
|
Session::Session(AtomBrowserContext* browser_context)
|
||||||
: browser_context_(browser_context) {
|
: browser_context_(browser_context) {
|
||||||
AttachAsUserData(browser_context);
|
AttachAsUserData(browser_context);
|
||||||
browser_context->cert_verifier()->SetDelegate(this);
|
|
||||||
|
|
||||||
// Observe DownloadManger to get download notifications.
|
// Observe DownloadManger to get download notifications.
|
||||||
content::BrowserContext::GetDownloadManager(browser_context)->
|
content::BrowserContext::GetDownloadManager(browser_context)->
|
||||||
|
@ -276,7 +276,6 @@ bool Session::IsDestroyed() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::Destroy() {
|
void Session::Destroy() {
|
||||||
browser_context_->cert_verifier()->SetDelegate(nullptr);
|
|
||||||
browser_context_ = nullptr;
|
browser_context_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -358,6 +357,17 @@ void Session::DisableNetworkEmulation() {
|
||||||
base::Passed(&conditions)));
|
base::Passed(&conditions)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Session::SetCertVerifyProc(v8::Local<v8::Value> val, mate::Arguments* args) {
|
||||||
|
AtomCertVerifier::VerifyProc proc;
|
||||||
|
if (val.IsEmpty() ||
|
||||||
|
!(val->IsNull() || mate::ConvertFromV8(args->isolate(), val, &proc))) {
|
||||||
|
args->ThrowError("Must pass null or function");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
browser_context_->cert_verifier()->SetVerifyProc(proc);
|
||||||
|
}
|
||||||
|
|
||||||
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
|
||||||
if (cookies_.IsEmpty()) {
|
if (cookies_.IsEmpty()) {
|
||||||
auto handle = atom::api::Cookies::Create(isolate, browser_context());
|
auto handle = atom::api::Cookies::Create(isolate, browser_context());
|
||||||
|
@ -376,6 +386,7 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
|
||||||
.SetMethod("setDownloadPath", &Session::SetDownloadPath)
|
.SetMethod("setDownloadPath", &Session::SetDownloadPath)
|
||||||
.SetMethod("enableNetworkEmulation", &Session::EnableNetworkEmulation)
|
.SetMethod("enableNetworkEmulation", &Session::EnableNetworkEmulation)
|
||||||
.SetMethod("disableNetworkEmulation", &Session::DisableNetworkEmulation)
|
.SetMethod("disableNetworkEmulation", &Session::DisableNetworkEmulation)
|
||||||
|
.SetMethod("setCertificateVerifyProc", &Session::SetCertVerifyProc)
|
||||||
.SetProperty("cookies", &Session::Cookies);
|
.SetProperty("cookies", &Session::Cookies);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
#include "atom/browser/net/atom_cert_verifier.h"
|
|
||||||
#include "content/public/browser/download_manager.h"
|
#include "content/public/browser/download_manager.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
#include "net/base/completion_callback.h"
|
#include "net/base/completion_callback.h"
|
||||||
|
@ -35,7 +34,6 @@ class AtomBrowserContext;
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class Session: public mate::TrackableObject<Session>,
|
class Session: public mate::TrackableObject<Session>,
|
||||||
public AtomCertVerifier::Delegate,
|
|
||||||
public content::DownloadManager::Observer {
|
public content::DownloadManager::Observer {
|
||||||
public:
|
public:
|
||||||
using ResolveProxyCallback = base::Callback<void(std::string)>;
|
using ResolveProxyCallback = base::Callback<void(std::string)>;
|
||||||
|
@ -74,6 +72,7 @@ class Session: public mate::TrackableObject<Session>,
|
||||||
void SetDownloadPath(const base::FilePath& path);
|
void SetDownloadPath(const base::FilePath& path);
|
||||||
void EnableNetworkEmulation(const mate::Dictionary& options);
|
void EnableNetworkEmulation(const mate::Dictionary& options);
|
||||||
void DisableNetworkEmulation();
|
void DisableNetworkEmulation();
|
||||||
|
void SetCertVerifyProc(v8::Local<v8::Value> proc, mate::Arguments* args);
|
||||||
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
||||||
|
|
||||||
// Cached object for cookies API.
|
// Cached object for cookies API.
|
||||||
|
|
|
@ -15,30 +15,31 @@ using content::BrowserThread;
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
AtomCertVerifier::CertVerifyRequest::~CertVerifyRequest() {
|
namespace {
|
||||||
}
|
|
||||||
|
|
||||||
void AtomCertVerifier::CertVerifyRequest::ContinueWithResult(int result) {
|
void OnResult(
|
||||||
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
net::CertVerifyResult* verify_result,
|
||||||
|
const net::CompletionCallback& callback,
|
||||||
if (handled_)
|
bool result) {
|
||||||
return;
|
|
||||||
|
|
||||||
handled_ = true;
|
|
||||||
BrowserThread::PostTask(
|
BrowserThread::PostTask(
|
||||||
BrowserThread::IO, FROM_HERE,
|
BrowserThread::IO, FROM_HERE,
|
||||||
base::Bind(args_.callback,
|
base::Bind(callback, result ? net::OK : net::ERR_FAILED));
|
||||||
result == net::ERR_IO_PENDING ? result_ : result));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
AtomCertVerifier::AtomCertVerifier()
|
AtomCertVerifier::AtomCertVerifier()
|
||||||
: delegate_(nullptr) {
|
: default_cert_verifier_(net::CertVerifier::CreateDefault()) {
|
||||||
default_cert_verifier_.reset(net::CertVerifier::CreateDefault());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AtomCertVerifier::~AtomCertVerifier() {
|
AtomCertVerifier::~AtomCertVerifier() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AtomCertVerifier::SetVerifyProc(const VerifyProc& proc) {
|
||||||
|
base::AutoLock auto_lock(lock_);
|
||||||
|
verify_proc_ = proc;
|
||||||
|
}
|
||||||
|
|
||||||
int AtomCertVerifier::Verify(
|
int AtomCertVerifier::Verify(
|
||||||
net::X509Certificate* cert,
|
net::X509Certificate* cert,
|
||||||
const std::string& hostname,
|
const std::string& hostname,
|
||||||
|
@ -51,45 +52,26 @@ int AtomCertVerifier::Verify(
|
||||||
const net::BoundNetLog& net_log) {
|
const net::BoundNetLog& net_log) {
|
||||||
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
||||||
|
|
||||||
if (callback.is_null() || !verify_result || hostname.empty() || !delegate_)
|
VerifyProc proc;
|
||||||
return net::ERR_INVALID_ARGUMENT;
|
{
|
||||||
|
base::AutoLock auto_lock(lock_);
|
||||||
VerifyArgs args = { cert, hostname, callback };
|
proc = verify_proc_;
|
||||||
int result = default_cert_verifier_->Verify(
|
|
||||||
cert, hostname, ocsp_response, flags, crl_set, verify_result,
|
|
||||||
base::Bind(&AtomCertVerifier::OnDefaultVerificationResult,
|
|
||||||
base::Unretained(this), args),
|
|
||||||
out_req, net_log);
|
|
||||||
if (result != net::OK && result != net::ERR_IO_PENDING) {
|
|
||||||
// The default verifier fails immediately.
|
|
||||||
VerifyCertificateFromDelegate(args, result);
|
|
||||||
return net::ERR_IO_PENDING;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
if (proc.is_null())
|
||||||
|
return default_cert_verifier_->Verify(
|
||||||
|
cert, hostname, ocsp_response, flags, crl_set, verify_result, callback,
|
||||||
|
out_req, net_log);
|
||||||
|
|
||||||
|
BrowserThread::PostTask(
|
||||||
|
BrowserThread::UI, FROM_HERE,
|
||||||
|
base::Bind(proc, hostname, make_scoped_refptr(cert),
|
||||||
|
base::Bind(OnResult, verify_result, callback)));
|
||||||
|
return net::ERR_IO_PENDING;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AtomCertVerifier::SupportsOCSPStapling() {
|
bool AtomCertVerifier::SupportsOCSPStapling() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AtomCertVerifier::VerifyCertificateFromDelegate(
|
|
||||||
const VerifyArgs& args, int result) {
|
|
||||||
CertVerifyRequest* request = new CertVerifyRequest(this, result, args);
|
|
||||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
|
||||||
base::Bind(&Delegate::RequestCertVerification,
|
|
||||||
base::Unretained(delegate_),
|
|
||||||
make_scoped_refptr(request)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void AtomCertVerifier::OnDefaultVerificationResult(
|
|
||||||
const VerifyArgs& args, int result) {
|
|
||||||
if (result == net::OK) {
|
|
||||||
args.callback.Run(result);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
VerifyCertificateFromDelegate(args, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -8,61 +8,22 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "base/memory/ref_counted.h"
|
#include "base/memory/ref_counted.h"
|
||||||
|
#include "base/synchronization/lock.h"
|
||||||
#include "net/cert/cert_verifier.h"
|
#include "net/cert/cert_verifier.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
class AtomCertVerifier : public net::CertVerifier {
|
class AtomCertVerifier : public net::CertVerifier {
|
||||||
public:
|
public:
|
||||||
struct VerifyArgs {
|
|
||||||
scoped_refptr<net::X509Certificate> cert;
|
|
||||||
const std::string& hostname;
|
|
||||||
net::CompletionCallback callback;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CertVerifyRequest
|
|
||||||
: public base::RefCountedThreadSafe<CertVerifyRequest> {
|
|
||||||
public:
|
|
||||||
CertVerifyRequest(AtomCertVerifier* cert_verifier,
|
|
||||||
int result,
|
|
||||||
const VerifyArgs& args)
|
|
||||||
: cert_verifier_(cert_verifier),
|
|
||||||
result_(result),
|
|
||||||
args_(args),
|
|
||||||
handled_(false) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void ContinueWithResult(int result);
|
|
||||||
|
|
||||||
const VerifyArgs& args() const { return args_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class base::RefCountedThreadSafe<CertVerifyRequest>;
|
|
||||||
~CertVerifyRequest();
|
|
||||||
|
|
||||||
AtomCertVerifier* cert_verifier_;
|
|
||||||
int result_;
|
|
||||||
VerifyArgs args_;
|
|
||||||
bool handled_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(CertVerifyRequest);
|
|
||||||
};
|
|
||||||
|
|
||||||
class Delegate {
|
|
||||||
public:
|
|
||||||
virtual ~Delegate() {}
|
|
||||||
|
|
||||||
// Called on UI thread.
|
|
||||||
virtual void RequestCertVerification(
|
|
||||||
const scoped_refptr<CertVerifyRequest>& request) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
AtomCertVerifier();
|
AtomCertVerifier();
|
||||||
virtual ~AtomCertVerifier();
|
virtual ~AtomCertVerifier();
|
||||||
|
|
||||||
void SetDelegate(Delegate* delegate) {
|
using VerifyProc =
|
||||||
delegate_ = delegate;
|
base::Callback<void(const std::string& hostname,
|
||||||
}
|
scoped_refptr<net::X509Certificate>,
|
||||||
|
const base::Callback<void(bool)>&)>;
|
||||||
|
|
||||||
|
void SetVerifyProc(const VerifyProc& proc);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// net::CertVerifier:
|
// net::CertVerifier:
|
||||||
|
@ -78,12 +39,8 @@ class AtomCertVerifier : public net::CertVerifier {
|
||||||
bool SupportsOCSPStapling() override;
|
bool SupportsOCSPStapling() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class CertVerifyRequest;
|
base::Lock lock_;
|
||||||
|
VerifyProc verify_proc_;
|
||||||
void VerifyCertificateFromDelegate(const VerifyArgs& args, int result);
|
|
||||||
void OnDefaultVerificationResult(const VerifyArgs& args, int result);
|
|
||||||
|
|
||||||
Delegate* delegate_;
|
|
||||||
scoped_ptr<net::CertVerifier> default_cert_verifier_;
|
scoped_ptr<net::CertVerifier> default_cert_verifier_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(AtomCertVerifier);
|
DISALLOW_COPY_AND_ASSIGN(AtomCertVerifier);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue