Netrc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_NETRC_H
  36. #define D_NETRC_H
  37. #include "common.h"
  38. #include <string>
  39. #include <vector>
  40. #include <memory>
  41. namespace aria2 {
  42. class Authenticatable {
  43. public:
  44. virtual ~Authenticatable() = default;
  45. virtual bool match(const std::string& hostname) const = 0;
  46. };
  47. class Authenticator : public Authenticatable {
  48. private:
  49. std::string machine_;
  50. std::string login_;
  51. std::string password_;
  52. std::string account_;
  53. public:
  54. Authenticator();
  55. Authenticator(std::string machine, std::string login, std::string password,
  56. std::string account);
  57. virtual ~Authenticator();
  58. virtual bool match(const std::string& hostname) const CXX11_OVERRIDE;
  59. const std::string& getMachine() const { return machine_; }
  60. void setMachine(std::string machine);
  61. const std::string& getLogin() const { return login_; }
  62. void setLogin(std::string login);
  63. const std::string& getPassword() const { return password_; }
  64. void setPassword(std::string password);
  65. const std::string& getAccount() const { return account_; }
  66. void setAccount(std::string account);
  67. };
  68. class DefaultAuthenticator : public Authenticator {
  69. public:
  70. DefaultAuthenticator();
  71. DefaultAuthenticator(std::string login, std::string password,
  72. std::string account);
  73. virtual ~DefaultAuthenticator();
  74. virtual bool match(const std::string& hostname) const CXX11_OVERRIDE;
  75. };
  76. class Netrc {
  77. private:
  78. std::vector<std::unique_ptr<Authenticator>> authenticators_;
  79. void storeAuthenticator(std::unique_ptr<Authenticator> authenticator);
  80. public:
  81. Netrc();
  82. ~Netrc();
  83. void parse(const std::string& path);
  84. const Authenticator* findAuthenticator(const std::string& hostname) const;
  85. const std::vector<std::unique_ptr<Authenticator>>& getAuthenticators() const;
  86. void addAuthenticator(std::unique_ptr<Authenticator> authenticator);
  87. };
  88. } // namespace aria2
  89. #endif // D_NETRC_H