diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-26 17:12:52 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2016-03-26 17:20:27 +0900 |
| commit | d64089632e82a2548752c7a6baaa68365a795a74 (patch) | |
| tree | 57fc7b85a8c1290975d58fa4761b04a7695bf7e3 /src/AuthConfigFactory.cc | |
| parent | c81b1321122e8509b55c94c0cbca0d8d6d319f0c (diff) | |
| parent | a7cd943a06b1a5ff4515b57060b84037f8d01f9f (diff) | |
Merge branch 'master' into random-webseeding
Diffstat (limited to 'src/AuthConfigFactory.cc')
| -rw-r--r-- | src/AuthConfigFactory.cc | 157 |
1 files changed, 78 insertions, 79 deletions
diff --git a/src/AuthConfigFactory.cc b/src/AuthConfigFactory.cc index 2f3c0573..43ba7285 100644 --- a/src/AuthConfigFactory.cc +++ b/src/AuthConfigFactory.cc @@ -57,51 +57,54 @@ AuthConfigFactory::AuthConfigFactory() {} AuthConfigFactory::~AuthConfigFactory() {} std::unique_ptr<AuthConfig> -AuthConfigFactory::createAuthConfig -(const std::shared_ptr<Request>& request, const Option* op) +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()) { - updateBasicCred(make_unique<BasicCred>(request->getUsername(), - request->getPassword(), - request->getHost(), - request->getPort(), - request->getDir(), true)); + if (request->getProtocol() == "http" || request->getProtocol() == "https") { + if (op->getAsBool(PREF_HTTP_AUTH_CHALLENGE)) { + if (!request->getUsername().empty()) { + updateBasicCred(make_unique<BasicCred>( + request->getUsername(), request->getPassword(), request->getHost(), + request->getPort(), request->getDir(), true)); return AuthConfig::create(request->getUsername(), request->getPassword()); } auto i = findBasicCred(request->getHost(), request->getPort(), request->getDir()); - if(i == std::end(basicCreds_)) { + if (i == std::end(basicCreds_)) { return nullptr; - } else { + } + else { return AuthConfig::create((*i)->user_, (*i)->password_); } - } else { - if(!request->getUsername().empty()) { + } + else { + if (!request->getUsername().empty()) { return AuthConfig::create(request->getUsername(), request->getPassword()); - } else { - return - createHttpAuthResolver(op)->resolveAuthConfig(request->getHost()); + } + else { + return createHttpAuthResolver(op) + ->resolveAuthConfig(request->getHost()); } } - } else if(request->getProtocol() == "ftp" || - request->getProtocol() == "sftp") { - if(!request->getUsername().empty()) { - if(request->hasPassword()) { + } + else if (request->getProtocol() == "ftp" || + request->getProtocol() == "sftp") { + if (!request->getUsername().empty()) { + if (request->hasPassword()) { return AuthConfig::create(request->getUsername(), request->getPassword()); - } else { - if(!op->getAsBool(PREF_NO_NETRC)) { + } + else { + if (!op->getAsBool(PREF_NO_NETRC)) { // First, check we have password corresponding to host and // username NetrcAuthResolver authResolver; authResolver.setNetrc(netrc_.get()); auto ac = authResolver.resolveAuthConfig(request->getHost()); - if(ac && ac->getUser() == request->getUsername()) { + if (ac && ac->getUser() == request->getUsername()) { return ac; } } @@ -110,22 +113,24 @@ AuthConfigFactory::createAuthConfig return AuthConfig::create(request->getUsername(), op->get(PREF_FTP_PASSWD)); } - } else { - return - createFtpAuthResolver(op)->resolveAuthConfig(request->getHost()); } - } else { + else { + return createFtpAuthResolver(op)->resolveAuthConfig(request->getHost()); + } + } + else { return nullptr; } } -std::unique_ptr<AuthResolver> AuthConfigFactory::createHttpAuthResolver -(const Option* op) const +std::unique_ptr<AuthResolver> +AuthConfigFactory::createHttpAuthResolver(const Option* op) const { std::unique_ptr<AbstractAuthResolver> resolver; - if(op->getAsBool(PREF_NO_NETRC)) { + if (op->getAsBool(PREF_NO_NETRC)) { resolver = make_unique<DefaultAuthResolver>(); - } else { + } + else { auto authResolver = make_unique<NetrcAuthResolver>(); authResolver->setNetrc(netrc_.get()); authResolver->ignoreDefault(); @@ -136,13 +141,14 @@ std::unique_ptr<AuthResolver> AuthConfigFactory::createHttpAuthResolver return std::move(resolver); } -std::unique_ptr<AuthResolver> AuthConfigFactory::createFtpAuthResolver -(const Option* op) const +std::unique_ptr<AuthResolver> +AuthConfigFactory::createFtpAuthResolver(const Option* op) const { std::unique_ptr<AbstractAuthResolver> resolver; - if(op->getAsBool(PREF_NO_NETRC)) { + if (op->getAsBool(PREF_NO_NETRC)) { resolver = make_unique<DefaultAuthResolver>(); - } else { + } + else { auto authResolver = make_unique<NetrcAuthResolver>(); authResolver->setNetrc(netrc_.get()); resolver = std::move(authResolver); @@ -161,61 +167,55 @@ void AuthConfigFactory::setNetrc(std::unique_ptr<Netrc> netrc) void AuthConfigFactory::updateBasicCred(std::unique_ptr<BasicCred> basicCred) { auto i = basicCreds_.lower_bound(basicCred); - if(i != std::end(basicCreds_) && *i == basicCred) { + if (i != std::end(basicCreds_) && *i == basicCred) { *(*i) = std::move(*basicCred); - } else { + } + else { basicCreds_.insert(i, std::move(basicCred)); } } -bool AuthConfigFactory::activateBasicCred -(const std::string& host, - uint16_t port, - const std::string& path, - const Option* op) +bool AuthConfigFactory::activateBasicCred(const std::string& host, + uint16_t port, + const std::string& path, + const Option* op) { auto i = findBasicCred(host, port, path); - if(i == std::end(basicCreds_)) { + if (i == std::end(basicCreds_)) { auto authConfig = createHttpAuthResolver(op)->resolveAuthConfig(host); - if(!authConfig) { + if (!authConfig) { return false; - } else { + } + else { basicCreds_.insert(make_unique<BasicCred>(authConfig->getUser(), - authConfig->getPassword(), - host, port, path, true)); + authConfig->getPassword(), host, + port, path, true)); return true; } - } else { + } + else { (*i)->activate(); return true; } } -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) +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] != '/') { + if (path_.empty() || path_[path_.size() - 1] != '/') { path_ += "/"; } } -void BasicCred::activate() -{ - activated_ = true; -} +void BasicCred::activate() { activated_ = true; } -bool BasicCred::isActivated() const -{ - return activated_; -} +bool BasicCred::isActivated() const { return activated_; } bool BasicCred::operator==(const BasicCred& cred) const { @@ -224,22 +224,21 @@ bool 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_))); + return host_ < cred.host_ || (!(cred.host_ < host_) && + (port_ < cred.port_ || (!(cred.port_ < port_) && + path_ > cred.path_))); } -AuthConfigFactory::BasicCredSet::iterator AuthConfigFactory::findBasicCred -(const std::string& host, - uint16_t port, - const std::string& path) +AuthConfigFactory::BasicCredSet::iterator +AuthConfigFactory::findBasicCred(const std::string& host, uint16_t port, + const std::string& path) { 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_)) { + for (; + i != std::end(basicCreds_) && (*i)->host_ == host && (*i)->port_ == port; + ++i) { + if (util::startsWith(bc->path_, (*i)->path_)) { return i; } } |
