Rewrite AuthConfig objects using std::unique_ptr

pull/103/head
Tatsuhiro Tsujikawa 2013-06-26 21:45:29 +09:00
parent a4cf50914d
commit d485c8e767
18 changed files with 342 additions and 345 deletions

View File

@ -34,6 +34,7 @@
/* copyright --> */
#include "AbstractAuthResolver.h"
#include "AuthConfig.h"
#include "a2functional.h"
namespace aria2 {
@ -41,16 +42,29 @@ AbstractAuthResolver::AbstractAuthResolver() {}
AbstractAuthResolver::~AbstractAuthResolver() {}
void AbstractAuthResolver::setUserDefinedAuthConfig
(const std::shared_ptr<AuthConfig>& authConfig)
void AbstractAuthResolver::setUserDefinedCred
(std::string user, std::string password)
{
userDefinedAuthConfig_ = authConfig;
userDefinedUser_ = std::move(user);
userDefinedPassword_ = std::move(password);
}
void AbstractAuthResolver::setDefaultAuthConfig
(const std::shared_ptr<AuthConfig>& authConfig)
std::unique_ptr<AuthConfig>
AbstractAuthResolver::getUserDefinedAuthConfig() const
{
defaultAuthConfig_ = authConfig;
return AuthConfig::create(userDefinedUser_, userDefinedPassword_);
}
void AbstractAuthResolver::setDefaultCred
(std::string user, std::string password)
{
defaultUser_ = std::move(user);
defaultPassword_ = std::move(password);
}
std::unique_ptr<AuthConfig> AbstractAuthResolver::getDefaultAuthConfig() const
{
return AuthConfig::create(defaultUser_, defaultPassword_);
}
} // namespace aria2

View File

@ -40,29 +40,24 @@
namespace aria2 {
class AbstractAuthResolver : public AuthResolver {
private:
std::shared_ptr<AuthConfig> userDefinedAuthConfig_;
std::shared_ptr<AuthConfig> defaultAuthConfig_;
public:
AbstractAuthResolver();
virtual ~AbstractAuthResolver();
void setUserDefinedAuthConfig(const std::shared_ptr<AuthConfig>& authConfig);
void setUserDefinedCred(std::string user, std::string password);
const std::shared_ptr<AuthConfig>& getUserDefinedAuthConfig() const
{
return userDefinedAuthConfig_;
}
std::unique_ptr<AuthConfig> getUserDefinedAuthConfig() const;
void setDefaultAuthConfig(const std::shared_ptr<AuthConfig>& authConfig);
void setDefaultCred(std::string user, std::string password);
const std::shared_ptr<AuthConfig>& getDefaultAuthConfig() const
{
return defaultAuthConfig_;
}
std::unique_ptr<AuthConfig> getDefaultAuthConfig() const;
private:
std::string userDefinedUser_;
std::string userDefinedPassword_;
std::string defaultUser_;
std::string defaultPassword_;
};
} // namespace aria2

View File

@ -42,9 +42,9 @@ namespace aria2 {
AuthConfig::AuthConfig() {}
AuthConfig::AuthConfig(const std::string& user, const std::string& password)
: user_(user),
password_(password)
AuthConfig::AuthConfig(std::string user, std::string password)
: user_(std::move(user)),
password_(std::move(password))
{}
AuthConfig::~AuthConfig() {}
@ -57,6 +57,16 @@ std::string AuthConfig::getAuthText() const
return s;
}
std::unique_ptr<AuthConfig> AuthConfig::create
(std::string user, std::string password)
{
if(user.empty()) {
return std::unique_ptr<AuthConfig>();
} else {
return make_unique<AuthConfig>(std::move(user), std::move(password));
}
}
std::ostream& operator<<(std::ostream& o,
const std::shared_ptr<AuthConfig>& authConfig)
{

View File

@ -50,7 +50,7 @@ private:
std::string password_;
public:
AuthConfig();
AuthConfig(const std::string& user, const std::string& password);
AuthConfig(std::string user, std::string password);
~AuthConfig();
// Don't allow copying
@ -68,6 +68,9 @@ public:
{
return password_;
}
static std::unique_ptr<AuthConfig> create
(std::string user, std::string password);
};
std::ostream& operator<<(std::ostream& o,

View File

@ -56,33 +56,32 @@ AuthConfigFactory::AuthConfigFactory() {}
AuthConfigFactory::~AuthConfigFactory() {}
std::shared_ptr<AuthConfig>
std::unique_ptr<AuthConfig>
AuthConfigFactory::createAuthConfig
(const std::shared_ptr<Request>& request, const Option* op)
{
if(request->getProtocol() == "http" || request->getProtocol() == "https") {
if(op->getAsBool(PREF_HTTP_AUTH_CHALLENGE)) {
if(!request->getUsername().empty()) {
std::shared_ptr<BasicCred> bc(new BasicCred(request->getUsername(),
request->getPassword(),
request->getHost(),
request->getPort(),
request->getDir(), true));
updateBasicCred(bc);
return createAuthConfig(request->getUsername(), request->getPassword());
updateBasicCred(make_unique<BasicCred>(request->getUsername(),
request->getPassword(),
request->getHost(),
request->getPort(),
request->getDir(), true));
return AuthConfig::create(request->getUsername(),
request->getPassword());
}
BasicCredSet::iterator i =
findBasicCred(request->getHost(), request->getPort(),
request->getDir());
if(i == basicCreds_.end()) {
return std::shared_ptr<AuthConfig>();
auto i = findBasicCred(request->getHost(), request->getPort(),
request->getDir());
if(i == std::end(basicCreds_)) {
return std::unique_ptr<AuthConfig>();
} else {
return createAuthConfig((*i)->user_, (*i)->password_);
return AuthConfig::create((*i)->user_, (*i)->password_);
}
} else {
if(!request->getUsername().empty()) {
return createAuthConfig(request->getUsername(), request->getPassword());
return AuthConfig::create(request->getUsername(),
request->getPassword());
} else {
return
createHttpAuthResolver(op)->resolveAuthConfig(request->getHost());
@ -91,93 +90,80 @@ AuthConfigFactory::createAuthConfig
} else if(request->getProtocol() == "ftp") {
if(!request->getUsername().empty()) {
if(request->hasPassword()) {
return createAuthConfig(request->getUsername(), request->getPassword());
return AuthConfig::create(request->getUsername(),
request->getPassword());
} else {
if(!op->getAsBool(PREF_NO_NETRC)) {
// First, check we have password corresponding to host and
// username
NetrcAuthResolver authResolver;
authResolver.setNetrc(netrc_);
authResolver.setNetrc(netrc_.get());
std::shared_ptr<AuthConfig> ac =
authResolver.resolveAuthConfig(request->getHost());
auto ac = authResolver.resolveAuthConfig(request->getHost());
if(ac && ac->getUser() == request->getUsername()) {
return ac;
}
}
// We don't have password for host and username. Return
// password specified by --ftp-passwd
return
createAuthConfig(request->getUsername(), op->get(PREF_FTP_PASSWD));
return AuthConfig::create(request->getUsername(),
op->get(PREF_FTP_PASSWD));
}
} else {
return
createFtpAuthResolver(op)->resolveAuthConfig(request->getHost());
}
} else {
return std::shared_ptr<AuthConfig>();
return std::unique_ptr<AuthConfig>();
}
}
std::shared_ptr<AuthConfig>
AuthConfigFactory::createAuthConfig(const std::string& user, const std::string& password) const
{
std::shared_ptr<AuthConfig> ac;
if(!user.empty()) {
ac.reset(new AuthConfig(user, password));
}
return ac;
}
std::shared_ptr<AuthResolver> AuthConfigFactory::createHttpAuthResolver
std::unique_ptr<AuthResolver> AuthConfigFactory::createHttpAuthResolver
(const Option* op) const
{
AbstractAuthResolver* resolver;
std::unique_ptr<AbstractAuthResolver> resolver;
if(op->getAsBool(PREF_NO_NETRC)) {
resolver = new DefaultAuthResolver();
resolver.reset(new DefaultAuthResolver());
} else {
NetrcAuthResolver* authResolver(new NetrcAuthResolver());
authResolver->setNetrc(netrc_);
auto authResolver = make_unique<NetrcAuthResolver>();
authResolver->setNetrc(netrc_.get());
authResolver->ignoreDefault();
resolver = authResolver;
resolver = std::move(authResolver);
}
resolver->setUserDefinedAuthConfig
(createAuthConfig(op->get(PREF_HTTP_USER), op->get(PREF_HTTP_PASSWD)));
return std::shared_ptr<AuthResolver>(resolver);
resolver->setUserDefinedCred(op->get(PREF_HTTP_USER),
op->get(PREF_HTTP_PASSWD));
return std::move(resolver);
}
std::shared_ptr<AuthResolver> AuthConfigFactory::createFtpAuthResolver
std::unique_ptr<AuthResolver> AuthConfigFactory::createFtpAuthResolver
(const Option* op) const
{
AbstractAuthResolver* resolver;
std::unique_ptr<AbstractAuthResolver> resolver;
if(op->getAsBool(PREF_NO_NETRC)) {
resolver = new DefaultAuthResolver();
resolver.reset(new DefaultAuthResolver());
} else {
NetrcAuthResolver* authResolver(new NetrcAuthResolver());
authResolver->setNetrc(netrc_);
resolver = authResolver;
auto authResolver = make_unique<NetrcAuthResolver>();
authResolver->setNetrc(netrc_.get());
resolver = std::move(authResolver);
}
resolver->setUserDefinedAuthConfig
(createAuthConfig(op->get(PREF_FTP_USER), op->get(PREF_FTP_PASSWD)));
std::shared_ptr<AuthConfig> defaultAuthConfig
(new AuthConfig(AUTH_DEFAULT_USER, AUTH_DEFAULT_PASSWD));
resolver->setDefaultAuthConfig(defaultAuthConfig);
return std::shared_ptr<AuthResolver>(resolver);
resolver->setUserDefinedCred(op->get(PREF_FTP_USER),
op->get(PREF_FTP_PASSWD));
resolver->setDefaultCred(AUTH_DEFAULT_USER, AUTH_DEFAULT_PASSWD);
return std::move(resolver);
}
void AuthConfigFactory::setNetrc(const std::shared_ptr<Netrc>& netrc)
void AuthConfigFactory::setNetrc(std::unique_ptr<Netrc> netrc)
{
netrc_ = netrc;
netrc_ = std::move(netrc);
}
void AuthConfigFactory::updateBasicCred
(const std::shared_ptr<BasicCred>& basicCred)
void AuthConfigFactory::updateBasicCred(std::unique_ptr<BasicCred> basicCred)
{
BasicCredSet::iterator i = basicCreds_.lower_bound(basicCred);
if(i != basicCreds_.end() && *(*i) == *basicCred) {
*(*i) = *basicCred;
auto i = basicCreds_.lower_bound(basicCred);
if(i != std::end(basicCreds_) && *i == basicCred) {
*(*i) = std::move(*basicCred);
} else {
basicCreds_.insert(i, basicCred);
basicCreds_.insert(i, std::move(basicCred));
}
}
@ -187,17 +173,15 @@ bool AuthConfigFactory::activateBasicCred
const std::string& path,
const Option* op)
{
BasicCredSet::iterator i = findBasicCred(host, port, path);
if(i == basicCreds_.end()) {
std::shared_ptr<AuthConfig> authConfig =
createHttpAuthResolver(op)->resolveAuthConfig(host);
auto i = findBasicCred(host, port, path);
if(i == std::end(basicCreds_)) {
auto authConfig = createHttpAuthResolver(op)->resolveAuthConfig(host);
if(!authConfig) {
return false;
} else {
std::shared_ptr<BasicCred> bc
(new BasicCred(authConfig->getUser(), authConfig->getPassword(),
host, port, path, true));
basicCreds_.insert(bc);
basicCreds_.insert(make_unique<BasicCred>(authConfig->getUser(),
authConfig->getPassword(),
host, port, path, true));
return true;
}
} else {
@ -206,55 +190,59 @@ bool AuthConfigFactory::activateBasicCred
}
}
AuthConfigFactory::BasicCred::BasicCred
(const std::string& user, const std::string& password,
const std::string& host, uint16_t port, const std::string& path,
bool activated):
user_(user), password_(password),
host_(host), port_(port), path_(path), activated_(activated)
BasicCred::BasicCred
(std::string user, std::string password,
std::string host, uint16_t port, std::string path,
bool activated)
: user_(std::move(user)),
password_(std::move(password)),
host_(std::move(host)),
port_(port),
path_(std::move(path)),
activated_(activated)
{
if(path_.empty() || path_[path_.size()-1] != '/') {
path_ += "/";
}
}
void AuthConfigFactory::BasicCred::activate()
void BasicCred::activate()
{
activated_ = true;
}
bool AuthConfigFactory::BasicCred::isActivated() const
bool BasicCred::isActivated() const
{
return activated_;
}
bool AuthConfigFactory::BasicCred::operator==(const BasicCred& cred) const
bool BasicCred::operator==(const BasicCred& cred) const
{
return host_ == cred.host_ && port_ == cred.port_ && path_ == cred.path_;
}
bool AuthConfigFactory::BasicCred::operator<(const BasicCred& cred) const
bool BasicCred::operator<(const BasicCred& cred) const
{
return host_ < cred.host_ ||
(!(cred.host_ < host_) && (port_ < cred.port_ ||
(!(cred.port_ < port_) && path_ > cred.path_)));
}
AuthConfigFactory::BasicCredSet::iterator
AuthConfigFactory::findBasicCred
AuthConfigFactory::BasicCredSet::iterator AuthConfigFactory::findBasicCred
(const std::string& host,
uint16_t port,
const std::string& path)
{
std::shared_ptr<BasicCred> bc(new BasicCred("", "", host, port, path));
BasicCredSet::iterator i = basicCreds_.lower_bound(bc);
for(; i != basicCreds_.end() && (*i)->host_ == host && (*i)->port_ == port;
++i) {
auto bc = make_unique<BasicCred>("", "", host, port, path);
auto i = basicCreds_.lower_bound(bc);
for(; i != std::end(basicCreds_) &&
(*i)->host_ == host &&
(*i)->port_ == port; ++i) {
if(util::startsWith(bc->path_, (*i)->path_)) {
return i;
}
}
return basicCreds_.end();
return std::end(basicCreds_);
}
} // namespace aria2

View File

@ -52,42 +52,39 @@ class AuthConfig;
class Request;
class AuthResolver;
class AuthConfigFactory {
private:
std::shared_ptr<Netrc> netrc_;
std::shared_ptr<AuthConfig> createAuthConfig(const std::string& user,
const std::string& password) const;
std::shared_ptr<AuthResolver> createHttpAuthResolver(const Option* op) const;
std::shared_ptr<AuthResolver> createFtpAuthResolver(const Option* op) const;
class BasicCred {
public:
class BasicCred {
public:
std::string user_;
std::string password_;
std::string host_;
uint16_t port_;
std::string path_;
bool activated_;
std::string user_;
std::string password_;
std::string host_;
uint16_t port_;
std::string path_;
bool activated_;
BasicCred(const std::string& user, const std::string& password,
const std::string& host, uint16_t port, const std::string& path,
bool activated = false);
BasicCred(std::string user, std::string password,
std::string host, uint16_t port, std::string path,
bool activated = false);
void activate();
void activate();
bool isActivated() const;
bool isActivated() const;
bool operator==(const BasicCred& cred) const;
bool operator==(const BasicCred& cred) const;
bool operator<(const BasicCred& cred) const;
};
bool operator<(const BasicCred& cred) const;
};
typedef std::set<std::shared_ptr<BasicCred>,
DerefLess<std::shared_ptr<BasicCred> > > BasicCredSet;
class AuthConfigFactory {
public:
typedef std::set<std::unique_ptr<BasicCred>,
DerefLess<std::unique_ptr<BasicCred>>> BasicCredSet;
private:
std::unique_ptr<Netrc> netrc_;
std::unique_ptr<AuthResolver> createHttpAuthResolver(const Option* op) const;
std::unique_ptr<AuthResolver> createFtpAuthResolver(const Option* op) const;
BasicCredSet basicCreds_;
public:
AuthConfigFactory();
@ -98,10 +95,10 @@ public:
// are used in this method: PREF_HTTP_USER, PREF_HTTP_PASSWD,
// PREF_FTP_USER, PREF_FTP_PASSWD, PREF_NO_NETRC and
// PREF_HTTP_AUTH_CHALLENGE.
std::shared_ptr<AuthConfig> createAuthConfig
std::unique_ptr<AuthConfig> createAuthConfig
(const std::shared_ptr<Request>& request, const Option* op);
void setNetrc(const std::shared_ptr<Netrc>& netrc);
void setNetrc(std::unique_ptr<Netrc> netrc);
// Find a BasicCred using findBasicCred() and activate it then
// return true. If matching BasicCred is not found, AuthConfig
@ -127,7 +124,9 @@ public:
// If the same BasicCred is already added, then it is replaced with
// given basicCred. Otherwise, insert given basicCred to
// basicCreds_.
void updateBasicCred(const std::shared_ptr<BasicCred>& basicCred);
//
// Made public for unit test.
void updateBasicCred(std::unique_ptr<BasicCred> basicCred);
};
} // namespace aria2

View File

@ -48,7 +48,8 @@ class AuthResolver {
public:
virtual ~AuthResolver() {}
virtual std::shared_ptr<AuthConfig> resolveAuthConfig(const std::string& hostname) = 0;
virtual std::unique_ptr<AuthConfig> resolveAuthConfig
(const std::string& hostname) = 0;
};
} // namespace aria2

View File

@ -37,13 +37,14 @@
namespace aria2 {
std::shared_ptr<AuthConfig> DefaultAuthResolver::resolveAuthConfig
std::unique_ptr<AuthConfig> DefaultAuthResolver::resolveAuthConfig
(const std::string& hostname)
{
if(!getUserDefinedAuthConfig()) {
return getDefaultAuthConfig();
auto authConfig = getUserDefinedAuthConfig();
if(authConfig) {
return authConfig;
} else {
return getUserDefinedAuthConfig();
return getDefaultAuthConfig();
}
}

View File

@ -41,7 +41,7 @@ namespace aria2 {
class DefaultAuthResolver : public AbstractAuthResolver {
public:
virtual std::shared_ptr<AuthConfig> resolveAuthConfig
virtual std::unique_ptr<AuthConfig> resolveAuthConfig
(const std::string& hostname);
};

View File

@ -200,9 +200,9 @@ int MultiUrlRequestInfo::prepare()
A2_LOG_NOTICE(fmt(MSG_INCORRECT_NETRC_PERMISSION,
option_->get(PREF_NETRC_PATH).c_str()));
} else {
std::shared_ptr<Netrc> netrc(new Netrc());
auto netrc = make_unique<Netrc>();
netrc->parse(option_->get(PREF_NETRC_PATH));
authConfigFactory->setNetrc(netrc);
authConfigFactory->setNetrc(std::move(netrc));
}
}
e_->setAuthConfigFactory(std::move(authConfigFactory));

View File

@ -50,14 +50,14 @@ namespace aria2 {
Authenticator::Authenticator() {}
Authenticator::Authenticator
(const std::string& machine,
const std::string& login,
const std::string& password,
const std::string& account)
: machine_(machine),
login_(login),
password_(password),
account_(account)
(std::string machine,
std::string login,
std::string password,
std::string account)
: machine_(std::move(machine)),
login_(std::move(login)),
password_(std::move(password)),
account_(std::move(account))
{}
Authenticator::~Authenticator() {}
@ -67,33 +67,34 @@ bool Authenticator::match(const std::string& hostname) const
return util::noProxyDomainMatch(hostname, machine_);
}
void Authenticator::setMachine(const std::string& machine)
void Authenticator::setMachine(std::string machine)
{
machine_ = machine;
machine_ = std::move(machine);
}
void Authenticator::setLogin(const std::string& login)
void Authenticator::setLogin(std::string login)
{
login_ = login;
login_ = std::move(login);
}
void Authenticator::setPassword(const std::string& password)
void Authenticator::setPassword(std::string password)
{
password_ = password;
password_ = std::move(password);
}
void Authenticator::setAccount(const std::string& account)
void Authenticator::setAccount(std::string account)
{
account_ = account;
account_ = std::move(account);
}
DefaultAuthenticator::DefaultAuthenticator() {}
DefaultAuthenticator::DefaultAuthenticator
(const std::string& login,
const std::string& password,
const std::string& account)
: Authenticator(A2STR::NIL, login, password, account)
(std::string login,
std::string password,
std::string account)
: Authenticator("", std::move(login), std::move(password),
std::move(account))
{}
DefaultAuthenticator::~DefaultAuthenticator() {}
@ -107,9 +108,9 @@ Netrc::Netrc() {}
Netrc::~Netrc() {}
void Netrc::addAuthenticator(const std::shared_ptr<Authenticator>& authenticator)
void Netrc::addAuthenticator(std::unique_ptr<Authenticator> authenticator)
{
authenticators_.push_back(authenticator);
authenticators_.push_back(std::move(authenticator));
}
namespace {
@ -146,7 +147,7 @@ void Netrc::parse(const std::string& path)
SET_ACCOUNT,
SET_MACDEF
};
std::shared_ptr<Authenticator> authenticator;
std::unique_ptr<Authenticator> authenticator;
STATE state = GET_TOKEN;
while(1) {
std::string line = fp.getLine();
@ -169,11 +170,11 @@ void Netrc::parse(const std::string& path)
eoi = tokens.end(); iter != eoi; ++iter) {
if(state == GET_TOKEN) {
if(util::streq((*iter).first, (*iter).second, "machine")) {
storeAuthenticator(authenticator);
storeAuthenticator(std::move(authenticator));
authenticator.reset(new Authenticator());
state = SET_MACHINE;
} else if(util::streq((*iter).first, (*iter).second, "default")) {
storeAuthenticator(authenticator);
storeAuthenticator(std::move(authenticator));
authenticator.reset(new DefaultAuthenticator());
} else {
if(!authenticator) {
@ -194,13 +195,13 @@ void Netrc::parse(const std::string& path)
}
} else {
if(state == SET_MACHINE) {
authenticator->setMachine((*iter).first, (*iter).second);
authenticator->setMachine({(*iter).first, (*iter).second});
} else if(state == SET_LOGIN) {
authenticator->setLogin((*iter).first, (*iter).second);
authenticator->setLogin({(*iter).first, (*iter).second});
} else if(state == SET_PASSWORD) {
authenticator->setPassword((*iter).first, (*iter).second);
authenticator->setPassword({(*iter).first, (*iter).second});
} else if(state == SET_ACCOUNT) {
authenticator->setAccount((*iter).first, (*iter).second);
authenticator->setAccount({(*iter).first, (*iter).second});
} else if(state == SET_MACDEF) {
skipMacdef(fp);
}
@ -212,13 +213,13 @@ void Netrc::parse(const std::string& path)
throw DL_ABORT_EX
("Netrc:parse error. EOF reached where a token expected.");
}
storeAuthenticator(authenticator);
storeAuthenticator(std::move(authenticator));
}
void Netrc::storeAuthenticator(const std::shared_ptr<Authenticator>& authenticator)
void Netrc::storeAuthenticator(std::unique_ptr<Authenticator> authenticator)
{
if(authenticator) {
authenticators_.push_back(authenticator);
authenticators_.push_back(std::move(authenticator));
}
}
@ -229,24 +230,31 @@ private:
public:
AuthHostMatch(const std::string& hostname):hostname(hostname) {}
bool operator()(const std::shared_ptr<Authenticator>& authenticator)
bool operator()(const std::unique_ptr<Authenticator>& authenticator)
{
return authenticator->match(hostname);
}
};
} // namespace
std::shared_ptr<Authenticator>
const Authenticator*
Netrc::findAuthenticator(const std::string& hostname) const
{
std::shared_ptr<Authenticator> res;
std::vector<std::shared_ptr<Authenticator> >::const_iterator itr =
std::find_if(authenticators_.begin(), authenticators_.end(),
AuthHostMatch(hostname));
if(itr != authenticators_.end()) {
res = *itr;
std::unique_ptr<Authenticator> res;
auto itr = std::find_if(std::begin(authenticators_),
std::end(authenticators_),
AuthHostMatch(hostname));
if(itr == std::end(authenticators_)) {
return nullptr;
} else {
return (*itr).get();
}
return res;
}
const std::vector<std::unique_ptr<Authenticator>>&
Netrc::getAuthenticators() const
{
return authenticators_;
}
} // namespace aria2

View File

@ -59,10 +59,10 @@ private:
public:
Authenticator();
Authenticator(const std::string& machine,
const std::string& login,
const std::string& password,
const std::string& account);
Authenticator(std::string machine,
std::string login,
std::string password,
std::string account);
virtual ~Authenticator();
@ -73,61 +73,37 @@ public:
return machine_;
}
void setMachine(const std::string& machine);
template<typename InputIterator>
void setMachine(InputIterator first, InputIterator last)
{
machine_.assign(first, last);
}
void setMachine(std::string machine);
const std::string& getLogin() const
{
return login_;
}
void setLogin(const std::string& login);
template<typename InputIterator>
void setLogin(InputIterator first, InputIterator last)
{
login_.assign(first, last);
}
void setLogin(std::string login);
const std::string& getPassword() const
{
return password_;
}
void setPassword(const std::string& password);
template<typename InputIterator>
void setPassword(InputIterator first, InputIterator last)
{
password_.assign(first, last);
}
void setPassword(std::string password);
const std::string& getAccount() const
{
return account_;
}
void setAccount(const std::string& account);
template<typename InputIterator>
void setAccount(InputIterator first, InputIterator last)
{
account_.assign(first, last);
}
void setAccount(std::string account);
};
class DefaultAuthenticator : public Authenticator {
public:
DefaultAuthenticator();
DefaultAuthenticator(const std::string& login,
const std::string& password,
const std::string& account);
DefaultAuthenticator(std::string login,
std::string password,
std::string account);
virtual ~DefaultAuthenticator();
@ -136,9 +112,9 @@ public:
class Netrc {
private:
std::vector<std::shared_ptr<Authenticator> > authenticators_;
std::vector<std::unique_ptr<Authenticator>> authenticators_;
void storeAuthenticator(const std::shared_ptr<Authenticator>& authenticator);
void storeAuthenticator(std::unique_ptr<Authenticator> authenticator);
public:
Netrc();
@ -146,15 +122,11 @@ public:
void parse(const std::string& path);
std::shared_ptr<Authenticator> findAuthenticator
(const std::string& hostname) const;
const Authenticator* findAuthenticator(const std::string& hostname) const;
const std::vector<std::shared_ptr<Authenticator> >& getAuthenticators() const
{
return authenticators_;
}
const std::vector<std::unique_ptr<Authenticator>>& getAuthenticators() const;
void addAuthenticator(const std::shared_ptr<Authenticator>& authenticator);
void addAuthenticator(std::unique_ptr<Authenticator> authenticator);
};
} // namespace aria2

View File

@ -35,42 +35,44 @@
#include "NetrcAuthResolver.h"
#include "AuthConfig.h"
#include "Netrc.h"
#include "a2functional.h"
namespace aria2 {
NetrcAuthResolver::NetrcAuthResolver():ignoreDefault_(false) {}
NetrcAuthResolver::NetrcAuthResolver()
: netrc_(nullptr), ignoreDefault_(false) {}
std::shared_ptr<AuthConfig>
std::unique_ptr<AuthConfig>
NetrcAuthResolver::resolveAuthConfig(const std::string& hostname)
{
if(!getUserDefinedAuthConfig()) {
return findNetrcAuthenticator(hostname);
auto authConfig = getUserDefinedAuthConfig();
if(authConfig) {
return authConfig;
} else {
return getUserDefinedAuthConfig();
return findNetrcAuthenticator(hostname);
}
}
std::shared_ptr<AuthConfig>
std::unique_ptr<AuthConfig>
NetrcAuthResolver::findNetrcAuthenticator(const std::string& hostname) const
{
if(!netrc_) {
return getDefaultAuthConfig();
} else {
std::shared_ptr<Authenticator> auth = netrc_->findAuthenticator(hostname);
auto auth = netrc_->findAuthenticator(hostname);
if(!auth) {
return getDefaultAuthConfig();
} else {
if(ignoreDefault_ && auth->getMachine().empty()) {
return getDefaultAuthConfig();
} else {
return std::shared_ptr<AuthConfig>
(new AuthConfig(auth->getLogin(), auth->getPassword()));
return make_unique<AuthConfig>(auth->getLogin(), auth->getPassword());
}
}
}
}
void NetrcAuthResolver::setNetrc(const std::shared_ptr<Netrc>& netrc)
void NetrcAuthResolver::setNetrc(Netrc* netrc)
{
netrc_ = netrc;
}

View File

@ -43,24 +43,19 @@ class Netrc;
class NetrcAuthResolver : public AbstractAuthResolver {
private:
std::shared_ptr<Netrc> netrc_;
Netrc* netrc_;
bool ignoreDefault_;
std::shared_ptr<AuthConfig> findNetrcAuthenticator
std::unique_ptr<AuthConfig> findNetrcAuthenticator
(const std::string& hostname) const;
public:
NetrcAuthResolver();
virtual std::shared_ptr<AuthConfig> resolveAuthConfig
virtual std::unique_ptr<AuthConfig> resolveAuthConfig
(const std::string& hostname);
void setNetrc(const std::shared_ptr<Netrc>& netrc);
const std::shared_ptr<Netrc>& getNetrc() const
{
return netrc_;
}
void setNetrc(Netrc* netrc);
// Ignores default token of netrc
void ignoreDefault();

View File

@ -44,15 +44,15 @@ void AuthConfigFactoryTest::testCreateAuthConfig_http()
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
// with Netrc
std::shared_ptr<Netrc> netrc(new Netrc());
netrc->addAuthenticator
(std::shared_ptr<Authenticator>(new Authenticator("localhost",
"localhostuser",
"localhostpass",
"localhostacct")));
netrc->addAuthenticator
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
factory.setNetrc(netrc);
auto netrc = make_unique<Netrc>();
netrc->addAuthenticator(make_unique<Authenticator>("localhost",
"localhostuser",
"localhostpass",
"localhostacct"));
netrc->addAuthenticator(make_unique<DefaultAuthenticator>("default",
"defaultpassword",
"defaultaccount"));
factory.setNetrc(std::move(netrc));
// not activated
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
@ -100,15 +100,15 @@ void AuthConfigFactoryTest::testCreateAuthConfig_httpNoChallenge()
CPPUNIT_ASSERT(!factory.createAuthConfig(req, &option));
// with Netrc
std::shared_ptr<Netrc> netrc(new Netrc());
netrc->addAuthenticator
(std::shared_ptr<Authenticator>(new Authenticator("localhost",
"localhostuser",
"localhostpass",
"localhostacct")));
netrc->addAuthenticator
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
factory.setNetrc(netrc);
auto netrc = make_unique<Netrc>();
netrc->addAuthenticator(make_unique<Authenticator>("localhost",
"localhostuser",
"localhostpass",
"localhostacct"));
netrc->addAuthenticator(make_unique<DefaultAuthenticator>("default",
"defaultpassword",
"defaultaccount"));
factory.setNetrc(std::move(netrc));
// not activated
CPPUNIT_ASSERT_EQUAL(std::string("localhostuser:localhostpass"),
@ -147,10 +147,11 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
factory.createAuthConfig(req, &option)->getAuthText());
// with Netrc
std::shared_ptr<Netrc> netrc(new Netrc());
netrc->addAuthenticator
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
factory.setNetrc(netrc);
auto netrc = make_unique<Netrc>();
netrc->addAuthenticator(make_unique<DefaultAuthenticator>("default",
"defaultpassword",
"defaultaccount"));
factory.setNetrc(std::move(netrc));
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpassword"),
factory.createAuthConfig(req, &option)->getAuthText());
@ -179,12 +180,11 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
// Recreate netrc with entry for user aria2user
netrc.reset(new Netrc());
netrc->addAuthenticator
(std::shared_ptr<Authenticator>(new Authenticator("localhost",
"aria2user",
"netrcpass",
"netrcacct")));
factory.setNetrc(netrc);
netrc->addAuthenticator(make_unique<Authenticator>("localhost",
"aria2user",
"netrcpass",
"netrcacct"));
factory.setNetrc(std::move(netrc));
// This time, we can find same username "aria2user" in netrc, so the
// password "netrcpass" is used, instead of "userDefinedPassword"
CPPUNIT_ASSERT_EQUAL(std::string("aria2user:netrcpass"),
@ -196,17 +196,14 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
}
namespace {
std::shared_ptr<AuthConfigFactory::BasicCred>
std::unique_ptr<BasicCred>
createBasicCred(const std::string& user,
const std::string& password,
const std::string& host, uint16_t port,
const std::string& path,
bool activated = false)
{
std::shared_ptr<AuthConfigFactory::BasicCred> bc
(new AuthConfigFactory::BasicCred(user, password, host, port, path,
activated));
return bc;
return make_unique<BasicCred>(user, password, host, port, path, activated);
}
} // namespace

View File

@ -12,9 +12,7 @@ class DefaultAuthResolverTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testResolveAuthConfig_with_userDefined);
CPPUNIT_TEST_SUITE_END();
private:
//NetrcHandle _netrc;
//std::shared_ptr<Option> _option;
std::shared_ptr<DefaultAuthResolver> resolver_;
std::unique_ptr<DefaultAuthResolver> resolver_;
public:
void setUp()
{
@ -22,8 +20,7 @@ public:
//_option = new Option();
resolver_.reset(new DefaultAuthResolver());
//_factory->setOption(_option.get());
resolver_->setDefaultAuthConfig
(std::shared_ptr<AuthConfig>(new AuthConfig("foo", "bar")));
resolver_->setDefaultCred("foo", "bar");
}
void testResolveAuthConfig_without_userDefined();
@ -35,16 +32,16 @@ CPPUNIT_TEST_SUITE_REGISTRATION( DefaultAuthResolverTest );
void DefaultAuthResolverTest::testResolveAuthConfig_without_userDefined()
{
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
auto authConfig = resolver_->resolveAuthConfig("localhost");
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
}
void DefaultAuthResolverTest::testResolveAuthConfig_with_userDefined()
{
resolver_->setUserDefinedAuthConfig
(std::shared_ptr<AuthConfig>(new AuthConfig("myname", "mypasswd")));
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
resolver_->setUserDefinedCred("myname", "mypasswd");
auto authConfig = resolver_->resolveAuthConfig("localhost");
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
authConfig->getAuthText());
}
} // namespace aria2

View File

@ -1,8 +1,11 @@
#include "NetrcAuthResolver.h"
#include <cppunit/extensions/HelperMacros.h>
#include "prefs.h"
#include "Netrc.h"
#include "AuthConfig.h"
#include <cppunit/extensions/HelperMacros.h>
#include "a2functional.h"
namespace aria2 {
@ -14,23 +17,24 @@ class NetrcAuthResolverTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testResolveAuthConfig_ignoreDefault);
CPPUNIT_TEST_SUITE_END();
private:
std::shared_ptr<Netrc> netrc_;
//std::shared_ptr<Option> _option;
std::shared_ptr<NetrcAuthResolver> resolver_;
std::unique_ptr<Netrc> netrc_;
std::unique_ptr<NetrcAuthResolver> resolver_;
public:
void setUp()
{
netrc_.reset(new Netrc());
netrc_->addAuthenticator
(std::shared_ptr<Authenticator>(new Authenticator("localhost", "name", "passwd", "account")));
netrc_->addAuthenticator
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpasswd", "defaultaccount")));
netrc_->addAuthenticator(make_unique<Authenticator>("localhost",
"name",
"passwd",
"account"));
netrc_->addAuthenticator(make_unique<DefaultAuthenticator>
("default",
"defaultpasswd",
"defaultaccount"));
//_option = new Option();
resolver_.reset(new NetrcAuthResolver());
resolver_->setNetrc(netrc_);
resolver_->setDefaultAuthConfig
(std::shared_ptr<AuthConfig>(new AuthConfig("foo", "bar")));
resolver_->setNetrc(netrc_.get());
resolver_->setDefaultCred("foo", "bar");
}
void testResolveAuthConfig_without_userDefined();
@ -43,13 +47,14 @@ CPPUNIT_TEST_SUITE_REGISTRATION( NetrcAuthResolverTest );
void NetrcAuthResolverTest::testResolveAuthConfig_without_userDefined()
{
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
auto authConfig = resolver_->resolveAuthConfig("localhost");
CPPUNIT_ASSERT_EQUAL(std::string("name:passwd"), authConfig->getAuthText());
authConfig = resolver_->resolveAuthConfig("mymachine");
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"), authConfig->getAuthText());
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"),
authConfig->getAuthText());
resolver_->setNetrc(std::shared_ptr<Netrc>());
resolver_->setNetrc(nullptr);
authConfig = resolver_->resolveAuthConfig("localhost");
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
@ -57,28 +62,29 @@ void NetrcAuthResolverTest::testResolveAuthConfig_without_userDefined()
void NetrcAuthResolverTest::testResolveAuthConfig_with_userDefined()
{
resolver_->setUserDefinedAuthConfig
(std::shared_ptr<AuthConfig>(new AuthConfig("myname", "mypasswd")));
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("localhost");
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
resolver_->setUserDefinedCred("myname", "mypasswd");
auto authConfig = resolver_->resolveAuthConfig("localhost");
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
authConfig->getAuthText());
authConfig = resolver_->resolveAuthConfig("mymachine");
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
authConfig->getAuthText());
resolver_->setNetrc(std::shared_ptr<Netrc>());
resolver_->setNetrc(nullptr);
authConfig = resolver_->resolveAuthConfig("mymachine");
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"), authConfig->getAuthText());
CPPUNIT_ASSERT_EQUAL(std::string("myname:mypasswd"),
authConfig->getAuthText());
}
void NetrcAuthResolverTest::testResolveAuthConfig_ignoreDefault()
{
resolver_->ignoreDefault();
std::shared_ptr<AuthConfig> authConfig = resolver_->resolveAuthConfig("mirror");
auto authConfig = resolver_->resolveAuthConfig("mirror");
CPPUNIT_ASSERT_EQUAL(std::string("foo:bar"), authConfig->getAuthText());
resolver_->useDefault();
std::shared_ptr<AuthConfig> defAuthConfig =
resolver_->resolveAuthConfig("mirror");
auto defAuthConfig = resolver_->resolveAuthConfig("mirror");
CPPUNIT_ASSERT_EQUAL(std::string("default:defaultpasswd"),
defAuthConfig->getAuthText());
}

View File

@ -4,6 +4,7 @@
#include <cppunit/extensions/HelperMacros.h>
#include "Exception.h"
#include "a2functional.h"
namespace aria2 {
@ -35,34 +36,41 @@ CPPUNIT_TEST_SUITE_REGISTRATION( NetrcTest );
void NetrcTest::testFindAuthenticator()
{
Netrc netrc;
netrc.addAuthenticator
(std::shared_ptr<Authenticator>(new Authenticator("host1", "tujikawa", "tujikawapasswd", "tujikawaaccount")));
netrc.addAuthenticator
(std::shared_ptr<Authenticator>(new Authenticator("host2", "aria2", "aria2password", "aria2account")));
netrc.addAuthenticator
(std::shared_ptr<Authenticator>(new Authenticator(".my.domain", "dmname", "dmpass", "dmaccount")));
netrc.addAuthenticator
(std::shared_ptr<Authenticator>(new DefaultAuthenticator("default", "defaultpassword", "defaultaccount")));
netrc.addAuthenticator(make_unique<Authenticator>("host1",
"tujikawa",
"tujikawapasswd",
"tujikawaaccount"));
netrc.addAuthenticator(make_unique<Authenticator>("host2",
"aria2",
"aria2password",
"aria2account"));
netrc.addAuthenticator(make_unique<Authenticator>(".my.domain",
"dmname",
"dmpass",
"dmaccount"));
netrc.addAuthenticator(make_unique<DefaultAuthenticator>("default",
"defaultpassword",
"defaultaccount"));
std::shared_ptr<Authenticator> aria2auth = netrc.findAuthenticator("host2");
auto aria2auth = netrc.findAuthenticator("host2");
CPPUNIT_ASSERT(aria2auth);
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
std::shared_ptr<Authenticator> defaultauth = netrc.findAuthenticator("host3");
auto defaultauth = netrc.findAuthenticator("host3");
CPPUNIT_ASSERT(defaultauth);
CPPUNIT_ASSERT_EQUAL(std::string("default"), defaultauth->getLogin());
CPPUNIT_ASSERT_EQUAL(std::string("defaultpassword"), defaultauth->getPassword());
CPPUNIT_ASSERT_EQUAL(std::string("defaultaccount"), defaultauth->getAccount());
CPPUNIT_ASSERT_EQUAL(std::string("defaultpassword"),
defaultauth->getPassword());
CPPUNIT_ASSERT_EQUAL(std::string("defaultaccount"),
defaultauth->getAccount());
std::shared_ptr<Authenticator> domainMatchAuth =
netrc.findAuthenticator("host3.my.domain");
auto domainMatchAuth = netrc.findAuthenticator("host3.my.domain");
CPPUNIT_ASSERT(domainMatchAuth);
CPPUNIT_ASSERT_EQUAL(std::string("dmname"), domainMatchAuth->getLogin());
std::shared_ptr<Authenticator> domainMatchAuth2 =
netrc.findAuthenticator("my.domain");
auto domainMatchAuth2 = netrc.findAuthenticator("my.domain");
CPPUNIT_ASSERT(domainMatchAuth2);
CPPUNIT_ASSERT_EQUAL(std::string("default"), domainMatchAuth2->getLogin());
}
@ -71,24 +79,25 @@ void NetrcTest::testParse()
{
Netrc netrc;
netrc.parse(A2_TEST_DIR"/sample.netrc");
std::vector<std::shared_ptr<Authenticator> >::const_iterator itr =
netrc.getAuthenticators().begin();
auto itr = std::begin(netrc.getAuthenticators());
std::shared_ptr<Authenticator> tujikawaauth = *itr;
const auto& tujikawaauth = *itr;
CPPUNIT_ASSERT(tujikawaauth);
CPPUNIT_ASSERT_EQUAL(std::string("host1"), tujikawaauth->getMachine());
CPPUNIT_ASSERT_EQUAL(std::string("tujikawa"), tujikawaauth->getLogin());
CPPUNIT_ASSERT_EQUAL(std::string("tujikawapassword"), tujikawaauth->getPassword());
CPPUNIT_ASSERT_EQUAL(std::string("tujikawaaccount"), tujikawaauth->getAccount());
CPPUNIT_ASSERT_EQUAL(std::string("tujikawapassword"),
tujikawaauth->getPassword());
CPPUNIT_ASSERT_EQUAL(std::string("tujikawaaccount"),
tujikawaauth->getAccount());
++itr;
std::shared_ptr<Authenticator> aria2auth = *itr;
const auto& aria2auth = *itr;
CPPUNIT_ASSERT(aria2auth);
CPPUNIT_ASSERT_EQUAL(std::string("host2"), aria2auth->getMachine());
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), aria2auth->getLogin());
CPPUNIT_ASSERT_EQUAL(std::string("aria2password"), aria2auth->getPassword());
CPPUNIT_ASSERT_EQUAL(std::string("aria2account"), aria2auth->getAccount());
++itr;
std::shared_ptr<Authenticator> defaultauth = *itr;
const auto& defaultauth = *itr;
CPPUNIT_ASSERT(defaultauth);
CPPUNIT_ASSERT_EQUAL(std::string("anonymous"), defaultauth->getLogin());
CPPUNIT_ASSERT_EQUAL(std::string("ARIA2@USER"), defaultauth->getPassword());