diff --git a/cpr/auth.cpp b/cpr/auth.cpp index b3576f5c1..841c1f5a9 100644 --- a/cpr/auth.cpp +++ b/cpr/auth.cpp @@ -1,7 +1,29 @@ #include "cpr/auth.h" #include "cpr/util.h" +#include + namespace cpr { + +Authentication::Authentication(std::string_view username, std::string_view password, AuthMode auth_mode) : auth_mode_{auth_mode} { + auth_string_.reserve(username.size() + 1 + password.size()); + auth_string_ += username; + auth_string_ += ':'; + auth_string_ += password; +} + +Authentication::Authentication(Authentication&& old) noexcept : auth_string_{std::move(old.auth_string_)}, auth_mode_{old.auth_mode_} { + old.auth_string_.resize(old.auth_string_.capacity()); +} + +Authentication& Authentication::operator=(Authentication&& old) noexcept { + auth_mode_ = old.auth_mode_; + util::secureStringClear(auth_string_); + auth_string_ = std::move(old.auth_string_); + old.auth_string_.resize(old.auth_string_.capacity()); + return *this; +} + Authentication::~Authentication() noexcept { util::secureStringClear(auth_string_); } diff --git a/include/cpr/auth.h b/include/cpr/auth.h index e78396944..55ea0fb84 100644 --- a/include/cpr/auth.h +++ b/include/cpr/auth.h @@ -2,8 +2,7 @@ #define CPR_AUTH_H #include - -#include +#include namespace cpr { @@ -11,12 +10,12 @@ enum class AuthMode { BASIC, DIGEST, NTLM }; class Authentication { public: - Authentication(std::string username, std::string password, AuthMode auth_mode) : auth_string_{std::move(username) + ":" + std::move(password)}, auth_mode_{std::move(auth_mode)} {} + Authentication(std::string_view username, std::string_view password, AuthMode auth_mode); Authentication(const Authentication& other) = default; - Authentication(Authentication&& old) noexcept = default; + Authentication(Authentication&& old) noexcept; ~Authentication() noexcept; - Authentication& operator=(Authentication&& old) noexcept = default; + Authentication& operator=(Authentication&& old) noexcept; Authentication& operator=(const Authentication& other) = default; const char* GetAuthString() const noexcept;