This source file includes following definitions.
- CanUseDefaultCredentials
- CanDelegate
- EnsureSystemSecurityManager
- Create
#include "net/http/url_security_manager.h"
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_comptr.h"
#include "net/http/http_auth_filter.h"
#include "url/gurl.h"
namespace net {
class URLSecurityManagerWin : public URLSecurityManager {
public:
explicit URLSecurityManagerWin(const HttpAuthFilter* whitelist_delegate);
virtual bool CanUseDefaultCredentials(const GURL& auth_origin) const;
virtual bool CanDelegate(const GURL& auth_origin) const;
private:
bool EnsureSystemSecurityManager();
base::win::ScopedComPtr<IInternetSecurityManager> security_manager_;
scoped_ptr<const HttpAuthFilter> whitelist_delegate_;
DISALLOW_COPY_AND_ASSIGN(URLSecurityManagerWin);
};
URLSecurityManagerWin::URLSecurityManagerWin(
const HttpAuthFilter* whitelist_delegate)
: whitelist_delegate_(whitelist_delegate) {
}
bool URLSecurityManagerWin::CanUseDefaultCredentials(
const GURL& auth_origin) const {
if (!const_cast<URLSecurityManagerWin*>(this)->EnsureSystemSecurityManager())
return false;
std::wstring url_w = base::ASCIIToWide(auth_origin.spec());
DWORD policy = 0;
HRESULT hr;
hr = security_manager_->ProcessUrlAction(url_w.c_str(),
URLACTION_CREDENTIALS_USE,
reinterpret_cast<BYTE*>(&policy),
sizeof(policy), NULL, 0,
PUAF_NOUI, 0);
if (FAILED(hr)) {
LOG(ERROR) << "IInternetSecurityManager::ProcessUrlAction failed: " << hr;
return false;
}
switch (policy) {
case URLPOLICY_CREDENTIALS_SILENT_LOGON_OK:
return true;
case URLPOLICY_CREDENTIALS_CONDITIONAL_PROMPT: {
DWORD zone = 0;
hr = security_manager_->MapUrlToZone(url_w.c_str(), &zone, 0);
if (FAILED(hr)) {
LOG(ERROR) << "IInternetSecurityManager::MapUrlToZone failed: " << hr;
return false;
}
return zone <= URLZONE_INTRANET;
}
case URLPOLICY_CREDENTIALS_MUST_PROMPT_USER:
return false;
case URLPOLICY_CREDENTIALS_ANONYMOUS_ONLY:
return false;
default:
NOTREACHED();
return false;
}
}
bool URLSecurityManagerWin::CanDelegate(const GURL& auth_origin) const {
if (whitelist_delegate_.get())
return whitelist_delegate_->IsValid(auth_origin, HttpAuth::AUTH_SERVER);
return false;
}
bool URLSecurityManagerWin::EnsureSystemSecurityManager() {
if (!security_manager_) {
HRESULT hr = CoInternetCreateSecurityManager(NULL,
security_manager_.Receive(),
NULL);
if (FAILED(hr) || !security_manager_) {
LOG(ERROR) << "Unable to create the Windows Security Manager instance";
return false;
}
}
return true;
}
URLSecurityManager* URLSecurityManager::Create(
const HttpAuthFilter* whitelist_default,
const HttpAuthFilter* whitelist_delegate) {
if (whitelist_default)
return new URLSecurityManagerWhitelist(whitelist_default,
whitelist_delegate);
return new URLSecurityManagerWin(whitelist_delegate);
}
}