diff --git a/.gitignore b/.gitignore index 4d4e37c..dd0d673 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ build/ *.swp src/main.cc +help/ +Testing/ +**/api_key.json \ No newline at end of file diff --git a/README.md b/README.md index 41f443d..2a9132a 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ you can use these endpoint # Installation ## Ubuntu ``` -# apt install clang cmake git libboost-dev libboost-test-dev libcurl4-openssl-dev libssl-dev ninja-build +# apt install clang cmake git libboost-dev libcurl4-openssl-dev libssl-dev ninja-build $ git clone https://github.com/koron0902/CocoaTweet $ cd CocoaTweet $ mkdir build @@ -74,14 +74,14 @@ $ mingw32-make ### 1.Write Key into code write api key into code and create Key object use it. ``` -#include "cocoatweet/oauth/key.h" +#include "cocoatweet/authentication/key.h" auto consumerKey = "your consumer key"; auto consumerSecret = "your consumer secret"; auto accessToken = "your access token"; auto accessTokenSecret = "your access token secret"; -CocoaTweet::OAuth::Key key(consumerKey, consumerSecret, accessToken, accessTokenSecret); +CocoaTweet::Authentication::Key key(consumerKey, consumerSecret, accessToken, accessTokenSecret); ``` @@ -99,9 +99,9 @@ prepare file which written 'api-key' with json format. then you can load api key from json file. ``` -#include "cocoatweet/oauth/key.h" +#include "cocoatweet/authentication/key.h" -CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json"); +CocoaTweet::Authentication::Key key = CocoaTweet::Authentication::Key::fromJsonFile("api_key.json"); ``` ## Generate API object diff --git a/api_key.json b/api_key.json index 37865c0..41b6fa8 100644 --- a/api_key.json +++ b/api_key.json @@ -1,6 +1,6 @@ { - "consumer_key" : "HmXS3q4yrQ2wndiCQzH8YnUQM", - "consumer_secret" : "qZwdGJYGFMpz7CSVGPHmbrh0O3Sk99wa0OLMf8HSvKKtNclrnf", - "access_token" : "1427931990792146949-iuNX8xBgQSXGtxr5cz8m495OtnXKcw", - "access_token_secret" : "A0R7vOOR3pRm4trGLxVbwNiQTkOMAkEh3z5ed3IxJVbJl" + "consumer_key" : "wcSkGjuvoQ7gGjB0rR5XSJiWI", + "consumer_secret" : "Qo9fiQaimj7QwqB8dXOONVI9QL2quJyxh3ZXo2REgvcrpCC3gE", + "access_token" : "1427931990792146949-nmcoAzhLSq0j0vwv8ati9F5oQN3Gb7", + "access_token_secret" : "bzWd71ARfczBMuYLcsHdlXotdNVHY3nX7WMOSrLCuHXHa" } diff --git a/src/cocoatweet/api/api.cc b/src/cocoatweet/api/api.cc index fedaabf..9a5bdcb 100644 --- a/src/cocoatweet/api/api.cc +++ b/src/cocoatweet/api/api.cc @@ -1,18 +1,31 @@ #include +#include + +#include namespace CocoaTweet::API { -API::API(CocoaTweet::OAuth::Key _key) { - oauth_ = std::make_shared(_key); +API::API(CocoaTweet::Authentication::Key _key) { + swapKey(_key); + +} + +void API::swapKey(const CocoaTweet::Authentication::Key _key){ + if(_key.authType() == CocoaTweet::Authentication::Key::OAUTH10A){ + oauth_ = std::make_shared(_key); + }else{ + + } user_ = Users::User(oauth_); status_ = Statuses::Status(oauth_); favorite_ = Favorites::Favorite(oauth_); media_ = Medias::Media(oauth_); directMessage_ = DirectMessages::DirectMessage(oauth_); + oauth1_ = OAuth1::OAuth(oauth_); } -const std::string& API::generateBearerToken() const { - return oauth_->generateBearerToken(); -} +// const std::string& API::generateBearerToken() const { +// return oauth_->generateBearerToken(); +// } Users::User API::user() const { return user_; @@ -33,4 +46,7 @@ Medias::Media API::media() const { DirectMessages::DirectMessage API::directMessage() const { return directMessage_; } +OAuth1::OAuth API::oauth1() const { + return oauth1_; +} } // namespace CocoaTweet::API diff --git a/src/cocoatweet/api/api.h b/src/cocoatweet/api/api.h index 4a41b4f..2a23787 100644 --- a/src/cocoatweet/api/api.h +++ b/src/cocoatweet/api/api.h @@ -6,15 +6,16 @@ #include #include #include -#include +#include +#include namespace CocoaTweet::API { /// @brief Twitter API Entry Point class API { public: /// @brief primary constructor - /// @param[in] _key Twitter API Key typed CocoaTweet::OAuth::Key - API(CocoaTweet::OAuth::Key _key); + /// @param[in] _key Twitter API Key typed CocoaTweet::Authentication::Key + API(CocoaTweet::Authentication::Key _key); Users::User user() const; @@ -29,7 +30,11 @@ public: Medias::Media media() const; DirectMessages::DirectMessage directMessage() const; + + OAuth1::OAuth oauth1() const; + const std::string& generateBearerToken() const; + void swapKey(const CocoaTweet::Authentication::Key _key); private: Users::User user_; @@ -37,7 +42,8 @@ private: Favorites::Favorite favorite_; Medias::Media media_; DirectMessages::DirectMessage directMessage_; - std::shared_ptr oauth_; + OAuth1::OAuth oauth1_; + std::shared_ptr oauth_; }; } // namespace CocoaTweet::API diff --git a/src/cocoatweet/api/directMessage/directMessage.cc b/src/cocoatweet/api/directMessage/directMessage.cc index 4788418..18c7c7a 100644 --- a/src/cocoatweet/api/directMessage/directMessage.cc +++ b/src/cocoatweet/api/directMessage/directMessage.cc @@ -2,7 +2,7 @@ #include namespace CocoaTweet::API::DirectMessages { -DirectMessage::DirectMessage(std::shared_ptr _oauth) { +DirectMessage::DirectMessage(std::shared_ptr _oauth) { oauth_ = _oauth; } diff --git a/src/cocoatweet/api/directMessage/directMessage.h b/src/cocoatweet/api/directMessage/directMessage.h index d796111..d4b2044 100644 --- a/src/cocoatweet/api/directMessage/directMessage.h +++ b/src/cocoatweet/api/directMessage/directMessage.h @@ -2,7 +2,7 @@ #define COCOATWEET_API_DIRECTMESSAGE_DIRECTMESSAGE_H_ #include "cocoatweet/api/interface/groupInterface.h" -#include "cocoatweet/oauth/oauth.h" +#include "cocoatweet/authentication/authenticator.h" #include #include #include @@ -15,8 +15,8 @@ public: DirectMessage() = default; /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object - DirectMessage(std::shared_ptr _oauth); + /// @param[in] std::shared_ptr : pointer to OAuth object + DirectMessage(std::shared_ptr _oauth); void messageCreate(const std::string& _recipient, const std::string& _message); diff --git a/src/cocoatweet/api/directMessage/new.cc b/src/cocoatweet/api/directMessage/new.cc index 7cbbbec..04cecde 100644 --- a/src/cocoatweet/api/directMessage/new.cc +++ b/src/cocoatweet/api/directMessage/new.cc @@ -23,7 +23,7 @@ void New::message(const std::string& _message) { json_["event"]["message_create"]["message_data"]["text"] = _message; } -void New::process(std::weak_ptr _oauth) { +void New::process(std::weak_ptr _oauth) { bodyParam_.insert_or_assign("data", json_.dump()); HttpPost::process(_oauth, [](const std::string& _rcv) {}); } diff --git a/src/cocoatweet/api/directMessage/new.h b/src/cocoatweet/api/directMessage/new.h index e717e9a..b620330 100644 --- a/src/cocoatweet/api/directMessage/new.h +++ b/src/cocoatweet/api/directMessage/new.h @@ -17,9 +17,9 @@ public: void message(const std::string& _message); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[in] std::weak_ptr _oauth : pointer to oauth object /// @param[out] CocoaTweet::API::Model::Tweet : request result - void process(std::weak_ptr _oauth); + void process(std::weak_ptr _oauth); private: std::string status_; diff --git a/src/cocoatweet/api/favorite/create.cc b/src/cocoatweet/api/favorite/create.cc index 74a4358..cf27dcc 100644 --- a/src/cocoatweet/api/favorite/create.cc +++ b/src/cocoatweet/api/favorite/create.cc @@ -11,7 +11,7 @@ void Create::id(const std::string& _id) { bodyParam_.insert_or_assign("id", _id); } -CocoaTweet::API::Model::Tweet Create::process(std::weak_ptr _oauth) { +CocoaTweet::API::Model::Tweet Create::process(std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { tweet = CocoaTweet::API::Model::Tweet(_rcv); diff --git a/src/cocoatweet/api/favorite/create.h b/src/cocoatweet/api/favorite/create.h index 58e6c88..d8bf406 100644 --- a/src/cocoatweet/api/favorite/create.h +++ b/src/cocoatweet/api/favorite/create.h @@ -9,7 +9,7 @@ class Create : public CocoaTweet::API::Interface::HttpPost { public: Create(); void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); private: }; diff --git a/src/cocoatweet/api/favorite/destroy.cc b/src/cocoatweet/api/favorite/destroy.cc index ef88c9a..26143cc 100644 --- a/src/cocoatweet/api/favorite/destroy.cc +++ b/src/cocoatweet/api/favorite/destroy.cc @@ -12,7 +12,7 @@ void Destroy::id(const std::string& _id) { } CocoaTweet::API::Model::Tweet Destroy::process( - std::weak_ptr _oauth) { + std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { tweet = CocoaTweet::API::Model::Tweet(_rcv); diff --git a/src/cocoatweet/api/favorite/destroy.h b/src/cocoatweet/api/favorite/destroy.h index 5515375..7e669bf 100644 --- a/src/cocoatweet/api/favorite/destroy.h +++ b/src/cocoatweet/api/favorite/destroy.h @@ -9,7 +9,7 @@ class Destroy : public CocoaTweet::API::Interface::HttpPost { public: Destroy(); void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); private: }; diff --git a/src/cocoatweet/api/favorite/favorite.cc b/src/cocoatweet/api/favorite/favorite.cc index 37da144..9b9aceb 100644 --- a/src/cocoatweet/api/favorite/favorite.cc +++ b/src/cocoatweet/api/favorite/favorite.cc @@ -3,7 +3,7 @@ #include "cocoatweet/api/favorite/destroy.h" namespace CocoaTweet::API::Favorites { -Favorite::Favorite(std::shared_ptr _oauth) { +Favorite::Favorite(std::shared_ptr _oauth) { oauth_ = _oauth; } diff --git a/src/cocoatweet/api/favorite/favorite.h b/src/cocoatweet/api/favorite/favorite.h index 92355c2..6b32d29 100644 --- a/src/cocoatweet/api/favorite/favorite.h +++ b/src/cocoatweet/api/favorite/favorite.h @@ -2,14 +2,14 @@ #define COCOATWEET_API_FAVORITE_FAVORITE_H_ #include "cocoatweet/api/interface/groupInterface.h" -#include "cocoatweet/oauth/oauth.h" +#include "cocoatweet/authentication/authenticator.h" #include namespace CocoaTweet::API::Favorites { class Favorite : public groupInterface { public: Favorite() = default; - Favorite(std::shared_ptr _oauth); + Favorite(std::shared_ptr _oauth); CocoaTweet::API::Model::Tweet create(const std::string& _id) const; CocoaTweet::API::Model::Tweet destroy(const std::string& _id) const; }; diff --git a/src/cocoatweet/api/interface/groupInterface.h b/src/cocoatweet/api/interface/groupInterface.h index 64866fa..609c223 100644 --- a/src/cocoatweet/api/interface/groupInterface.h +++ b/src/cocoatweet/api/interface/groupInterface.h @@ -2,12 +2,12 @@ #define COCOATWEET_API_INTERFACE_GROUPINTERFACE_H_ #include -#include "cocoatweet/oauth/oauth.h" +#include "cocoatweet/authentication/authenticator.h" namespace CocoaTweet::API { class groupInterface { protected: - std::weak_ptr oauth_; + std::weak_ptr oauth_; }; } // namespace CocoaTweet::API diff --git a/src/cocoatweet/api/interface/httpBase.h b/src/cocoatweet/api/interface/httpBase.h index e6314a3..1be4bf8 100644 --- a/src/cocoatweet/api/interface/httpBase.h +++ b/src/cocoatweet/api/interface/httpBase.h @@ -2,17 +2,18 @@ #define COCOATWEET_API_INTERFACE_HTTPBASE_H_ #include -#include "cocoatweet/oauth/oauth.h" +#include +#include "cocoatweet/authentication/authenticator.h" namespace CocoaTweet::API::Interface { class HttpBase { public: protected: - std::weak_ptr oauth_; + std::weak_ptr oauth_; std::map bodyParam_; std::string url_; std::string contentType_; - virtual void process(std::weak_ptr _oauth, + virtual void process(std::weak_ptr _oauth, std::function _callback) = 0; static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) { int realsize = _size * _nmemb; diff --git a/src/cocoatweet/api/interface/httpGet.cc b/src/cocoatweet/api/interface/httpGet.cc index af5b43e..bdab573 100644 --- a/src/cocoatweet/api/interface/httpGet.cc +++ b/src/cocoatweet/api/interface/httpGet.cc @@ -21,7 +21,7 @@ extern "C" { #endif namespace CocoaTweet::API::Interface { -void HttpGet::process(std::weak_ptr _oauth, +void HttpGet::process(std::weak_ptr _oauth, std::function _callback) { auto url = url_; diff --git a/src/cocoatweet/api/interface/httpGet.h b/src/cocoatweet/api/interface/httpGet.h index bf98421..68f3d47 100644 --- a/src/cocoatweet/api/interface/httpGet.h +++ b/src/cocoatweet/api/interface/httpGet.h @@ -2,7 +2,7 @@ #define COCOATWEET_API_INTERFACE_HTTPGET_H_ #include -#include "cocoatweet/oauth/oauth.h" +#include "cocoatweet/authentication/authenticator.h" #include namespace CocoaTweet::API::Interface { @@ -11,11 +11,11 @@ class HttpGet : public virtual HttpBase { public: protected: /// @brief Send HTTP/POST using OAuth object - /// @param[in] std::weak_ptr _oauth : pointer to OAuth object to + /// @param[in] std::weak_ptr _oauth : pointer to OAuth object to /// authenticate /// @param[in] std::function _callback : /// callback method for processing to response - void process(std::weak_ptr _oauth, + void process(std::weak_ptr _oauth, std::function _callback); }; } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/interface/httpPost.cc b/src/cocoatweet/api/interface/httpPost.cc index 84f20dc..3d2a36e 100644 --- a/src/cocoatweet/api/interface/httpPost.cc +++ b/src/cocoatweet/api/interface/httpPost.cc @@ -21,22 +21,22 @@ extern "C" { #endif namespace CocoaTweet::API::Interface { -void HttpPost::process(std::weak_ptr _oauth, +void HttpPost::process(std::weak_ptr _oauth, std::function _callback) { // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 auto oauth = _oauth.lock(); - auto oauthParam = oauth->oauthParam(); - auto sigingParam = oauthParam; - if (contentType_ == "application/x-www-form-urlencoded") { - for (const auto [k, v] : bodyParam_) { - sigingParam.insert_or_assign(k, v); - } - } + // auto oauthParam = oauth->oauthParam(); + // auto sigingParam = oauthParam; + // if (contentType_ == "application/x-www-form-urlencoded") { + // for (const auto [k, v] : bodyParam_) { + // sigingParam.insert_or_assign(k, v); + // } + // } - auto signature = oauth->signature(sigingParam, "POST", url_); + // auto signature = oauth->signature(sigingParam, "POST", url_); // 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録 - oauthParam.merge(signature); + // oauthParam.merge(signature); // リクエストボディの構築 std::string requestBody = ""; @@ -60,13 +60,21 @@ void HttpPost::process(std::weak_ptr _oauth, } // ヘッダの構築 - std::string oauthHeader = "authorization: OAuth "; - { - std::vector tmp; - for (const auto& [key, value] : oauthParam) { - tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value)); - } - oauthHeader += CocoaTweet::Util::join(tmp, ","); + // std::string oauthHeader = "authorization: OAuth "; + // { + // std::vector tmp; + // for (const auto& [key, value] : oauthParam) { + // tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value)); + // } + // oauthHeader += CocoaTweet::Util::join(tmp, ","); + // } + + + auto oauthHeader = std::string(); + if (contentType_ == "application/x-www-form-urlencoded") { + oauthHeader = oauth->calculateAuthHeader(bodyParam_, "POST", url_); + } else { + oauthHeader = oauth->calculateAuthHeader({}, "POST", url_); } // do post diff --git a/src/cocoatweet/api/interface/httpPost.h b/src/cocoatweet/api/interface/httpPost.h index 441fe58..d307984 100644 --- a/src/cocoatweet/api/interface/httpPost.h +++ b/src/cocoatweet/api/interface/httpPost.h @@ -2,7 +2,7 @@ #define COCOATWEET_API_INTERFACE_HTTPPOST_H_ #include -#include "cocoatweet/oauth/oauth.h" +#include "cocoatweet/authentication/authenticator.h" #include namespace CocoaTweet::API::Interface { @@ -11,11 +11,11 @@ class HttpPost : public HttpBase { public: protected: /// @brief Send HTTP/POST using OAuth object - /// @param[in] std::weak_ptr _oauth : pointer to OAuth object to + /// @param[in] std::weak_ptr _oauth : pointer to OAuth object to /// authenticate /// @param[in] std::function _callback : /// callback method for processing to response - void process(std::weak_ptr _oauth, + void process(std::weak_ptr _oauth, std::function _callback); }; } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/media/media.cc b/src/cocoatweet/api/media/media.cc index 6414edf..f41d097 100644 --- a/src/cocoatweet/api/media/media.cc +++ b/src/cocoatweet/api/media/media.cc @@ -1,7 +1,7 @@ #include "cocoatweet/api/media/media.h" namespace CocoaTweet::API::Medias { -Media::Media(std::shared_ptr _oauth) { +Media::Media(std::shared_ptr _oauth) { oauth_ = _oauth; } diff --git a/src/cocoatweet/api/media/media.h b/src/cocoatweet/api/media/media.h index d435c78..9df9927 100644 --- a/src/cocoatweet/api/media/media.h +++ b/src/cocoatweet/api/media/media.h @@ -2,7 +2,7 @@ #define COCOATWEET_API_MEDIA_MEDIA_H_ #include "cocoatweet/api/interface/groupInterface.h" -#include "cocoatweet/oauth/oauth.h" +#include "cocoatweet/authentication/authenticator.h" #include #include #include @@ -16,8 +16,8 @@ public: Media() = default; /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object - Media(std::shared_ptr _oauth); + /// @param[in] std::shared_ptr : pointer to OAuth object + Media(std::shared_ptr _oauth); CocoaTweet::API::Model::MediaStore upload(const std::string& _file) const; diff --git a/src/cocoatweet/api/media/upload.cc b/src/cocoatweet/api/media/upload.cc index 16c3019..5348cb0 100644 --- a/src/cocoatweet/api/media/upload.cc +++ b/src/cocoatweet/api/media/upload.cc @@ -19,7 +19,7 @@ void Upload::media(const std::string& _media) { void Upload::mediaId(const std::string& _mediaId) {} CocoaTweet::API::Model::MediaStore Upload::process( - std::weak_ptr _oauth) { + std::weak_ptr _oauth) { auto extension = std::filesystem::path(media_).extension().string(); if (mimeType.count(extension) == 0) { throw new CocoaTweet::Exception::UnsupportedMediaTypeException( diff --git a/src/cocoatweet/api/media/upload.h b/src/cocoatweet/api/media/upload.h index a021855..5355207 100644 --- a/src/cocoatweet/api/media/upload.h +++ b/src/cocoatweet/api/media/upload.h @@ -26,11 +26,11 @@ public: void mediaId(const std::string& _mediaId); /// @brief upload media - /// @param[in] std::weak_ptr _oauth : pointer to OAuth object for + /// @param[in] std::weak_ptr _oauth : pointer to OAuth object for /// authenticate /// @param[out] CocoaTweet::API::Model::MediaStore : media upload result. use id() for post /// tweet. - CocoaTweet::API::Model::MediaStore process(std::weak_ptr _oauth); + CocoaTweet::API::Model::MediaStore process(std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Medias diff --git a/src/cocoatweet/api/model/oauthToken.cc b/src/cocoatweet/api/model/oauthToken.cc new file mode 100644 index 0000000..7933473 --- /dev/null +++ b/src/cocoatweet/api/model/oauthToken.cc @@ -0,0 +1,19 @@ +#include "oauthToken.h" + +namespace CocoaTweet::API::Model{ + + OAuthToken::OAuthToken(): oauthToken_(""), oauthTokenSecret_(""){} + OAuthToken::OAuthToken(const std::string _oauthToken, const std::string& _oauthTokenSecret): oauthToken_(_oauthToken), oauthTokenSecret_(_oauthTokenSecret) {} + const std::string& OAuthToken::oauthToken() const { + return oauthToken_; + } + const std::string& OAuthToken::oauthTokenSecret() const{ + return oauthTokenSecret_; + } + void OAuthToken::oauthToken(const std::string& _oauthToken) { + oauthToken_ = _oauthToken; + } + void OAuthToken::oauthTokenSecret(const std::string& _oauthTokenSecret){ + oauthTokenSecret_ = _oauthTokenSecret; + } +} \ No newline at end of file diff --git a/src/cocoatweet/api/model/oauthToken.h b/src/cocoatweet/api/model/oauthToken.h new file mode 100644 index 0000000..38a2d2f --- /dev/null +++ b/src/cocoatweet/api/model/oauthToken.h @@ -0,0 +1,20 @@ +#ifndef COCOATWEET_API_MODEL_OAUTHTOKEN_H_ +#define COCOATWEET_API_MODEL_OAUTHTOKEN_H_ +#include + +namespace CocoaTweet::API::Model{ + class OAuthToken{ + std::string oauthToken_; + std::string oauthTokenSecret_; + + public: + OAuthToken(); + OAuthToken(const std::string _oauthToken, const std::string& _oauthTokenSecret); + const std::string& oauthToken() const; + const std::string& oauthTokenSecret() const; + void oauthToken(const std::string& _oauthToken); + void oauthTokenSecret(const std::string& _oauthTokenSecret); + }; +} + +#endif \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/accessToken.cc b/src/cocoatweet/api/oauth1/accessToken.cc new file mode 100644 index 0000000..82b85d0 --- /dev/null +++ b/src/cocoatweet/api/oauth1/accessToken.cc @@ -0,0 +1,33 @@ +#include +#include +#include + +namespace CocoaTweet::API::OAuth1{ + AccessToken::AccessToken(){ + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/oauth/access_token"; + } + + void AccessToken::oauthVerifier(const std::string& _verifier){ + bodyParam_.insert_or_assign("oauth_verifier", _verifier); + } + + void AccessToken::oauthToken(const CocoaTweet::API::Model::OAuthToken _token){ + oauthToken_ = _token; + } + + const CocoaTweet::API::Model::OAuthToken AccessToken::process(std::weak_ptr _oauth){ + CocoaTweet::API::Model::OAuthToken oauthToken; + HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv){ + auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); + if(mp.count("oauth_token")){ + oauthToken.oauthToken(mp.at("oauth_token")); + } + if(mp.count("oauth_token_secret")){ + oauthToken.oauthTokenSecret(mp.at("oauth_token_secret")); + } + std::cout << _rcv << std::endl; + }); + return oauthToken; + } +} \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/accessToken.h b/src/cocoatweet/api/oauth1/accessToken.h new file mode 100644 index 0000000..3c0ef09 --- /dev/null +++ b/src/cocoatweet/api/oauth1/accessToken.h @@ -0,0 +1,20 @@ +#ifndef COCOATWEET_API_OAUTH1_ACCESSTOKEN_H +#define COCOATWEET_API_OAUTH1_ACCESSTOKEN_H + +#include +#include +namespace CocoaTweet::API::OAuth1{ + class AccessToken: public CocoaTweet::API::Interface::HttpPost { +private: + CocoaTweet::API::Model::OAuthToken oauthToken_; +public: + AccessToken(); + + void oauthVerifier(const std::string& _verifier); + void oauthToken(const CocoaTweet::API::Model::OAuthToken _token); + + const CocoaTweet::API::Model::OAuthToken process(std::weak_ptr _oauth) ; + }; +} + +#endif \ No newline at end of file diff --git a/src/cocoatweet/oauth/authorize.cc b/src/cocoatweet/api/oauth1/authenticate.cc similarity index 100% rename from src/cocoatweet/oauth/authorize.cc rename to src/cocoatweet/api/oauth1/authenticate.cc diff --git a/src/cocoatweet/oauth/oauth2/invalidateToken.cc b/src/cocoatweet/api/oauth1/authenticate.h similarity index 100% rename from src/cocoatweet/oauth/oauth2/invalidateToken.cc rename to src/cocoatweet/api/oauth1/authenticate.h diff --git a/src/cocoatweet/api/oauth1/authorize.cc b/src/cocoatweet/api/oauth1/authorize.cc new file mode 100644 index 0000000..0cd7093 --- /dev/null +++ b/src/cocoatweet/api/oauth1/authorize.cc @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +namespace CocoaTweet::API::OAuth1{ + Authorize::Authorize(){ + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/oauth/authorize"; + } + + void Authorize::oauthToken(const std::string& _oauthToken){ + bodyParam_.insert_or_assign("oauth_token", _oauthToken); + } + + void Authorize::forceLogin(const bool _forceLogin){ + bodyParam_.insert_or_assign("force_login", std::to_string(static_cast(_forceLogin))); + } + + void Authorize::screenName(const std::string& _screenName){ + bodyParam_.insert_or_assign("screen_name", _screenName); + } + + const std::string Authorize::process(std::weak_ptr __unused__) { + std::vector tmp; + std::string query = ""; + for (const auto& [key, value] : bodyParam_) { + tmp.push_back(key + "=" + value); + query = CocoaTweet::Util::join(tmp, "&"); + } + return url_ + "?" + query;; + } +} \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/authorize.h b/src/cocoatweet/api/oauth1/authorize.h new file mode 100644 index 0000000..c79116d --- /dev/null +++ b/src/cocoatweet/api/oauth1/authorize.h @@ -0,0 +1,20 @@ +#ifndef COCOATWEET_API_OAUTH1_AUTHORIZE_H +#define COCOATWEET_API_OAUTH1_AUTHORIZE_H + +#include +namespace CocoaTweet::API::OAuth1{ + class Authorize: public CocoaTweet::API::Interface::HttpPost { +public: + Authorize(); + + void oauthToken(const std::string& _oauthToken); + + void forceLogin(const bool _forceLogin); + + void screenName(const std::string& _screenName); + + const std::string process(std::weak_ptr __unused__) ; + }; +} + +#endif \ No newline at end of file diff --git a/src/cocoatweet/oauth/oauth2/invalidateToken.h b/src/cocoatweet/api/oauth1/invalidateToken.cc similarity index 100% rename from src/cocoatweet/oauth/oauth2/invalidateToken.h rename to src/cocoatweet/api/oauth1/invalidateToken.cc diff --git a/src/cocoatweet/oauth/oauth2/token.cc b/src/cocoatweet/api/oauth1/invalidateToken.h similarity index 100% rename from src/cocoatweet/oauth/oauth2/token.cc rename to src/cocoatweet/api/oauth1/invalidateToken.h diff --git a/src/cocoatweet/api/oauth1/oauth.cc b/src/cocoatweet/api/oauth1/oauth.cc new file mode 100644 index 0000000..71e7636 --- /dev/null +++ b/src/cocoatweet/api/oauth1/oauth.cc @@ -0,0 +1,35 @@ +#include +#include + +namespace CocoaTweet::API::OAuth1 { + OAuth::OAuth(std::shared_ptr _oauth){ + oauth_ = _oauth; + } + + CocoaTweet::API::Model::OAuthToken OAuth::requestToken(const std::string& _oauthCallback) const{ + auto key = oauth_.lock()->key(); + key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); + auto oauth = std::make_shared(key); + + CocoaTweet::API::OAuth1::RequestToken requestToken; + requestToken.oauthCallback(_oauthCallback); + return requestToken.process(oauth); + } + + const std::string OAuth::authorize(const CocoaTweet::API::Model::OAuthToken _oauthToken) const{ + CocoaTweet::API::OAuth1::Authorize authorize; + authorize.oauthToken(_oauthToken.oauthToken()); + return authorize.process(oauth_); + } + + const CocoaTweet::API::Model::OAuthToken OAuth::accessToken(const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const{ + auto key = oauth_.lock()->key(); + key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); + key.accessToken(_oauthToken.oauthToken()); + key.accessTokenSecret(_oauthToken.oauthTokenSecret()); + auto oauth = std::make_shared(key); + CocoaTweet::API::OAuth1::AccessToken accessToken; + accessToken.oauthVerifier(_verifier); + return accessToken.process(oauth); + } +} // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/oauth1/oauth.h b/src/cocoatweet/api/oauth1/oauth.h new file mode 100644 index 0000000..1cb2439 --- /dev/null +++ b/src/cocoatweet/api/oauth1/oauth.h @@ -0,0 +1,30 @@ +#ifndef COCOATWEET_API_OAUTH1_OAUTH_H_ +#define COCOATWEET_API_OAUTH1_OAUTH_H_ + +#include "cocoatweet/api/interface/groupInterface.h" +#include +#include +#include +#include +#include +#include + +namespace CocoaTweet::API::OAuth1 { +/// @brief class for using users/show endpoint +class OAuth : public groupInterface { +public: + /// @brief primary constructor + OAuth() = default; + + /// @brief constructor which finally should to be called. + /// @param[in] std::shared_ptr : pointer to OAuth object + OAuth(std::shared_ptr _oauth); + + CocoaTweet::API::Model::OAuthToken requestToken(const std::string& _oauthCallback) const; + const std::string authorize(const CocoaTweet::API::Model::OAuthToken _oauthToken) const; + const CocoaTweet::API::Model::OAuthToken accessToken(const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const; + +}; +} // namespace CocoaTweet::API::Statuses + +#endif diff --git a/src/cocoatweet/api/oauth1/requestToken.cc b/src/cocoatweet/api/oauth1/requestToken.cc new file mode 100644 index 0000000..95c6128 --- /dev/null +++ b/src/cocoatweet/api/oauth1/requestToken.cc @@ -0,0 +1,29 @@ +#include +#include +#include + +namespace CocoaTweet::API::OAuth1{ + RequestToken::RequestToken(){ + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/oauth/request_token"; + } + + void RequestToken::oauthCallback(const std::string& _oauthCallback){ + bodyParam_.insert_or_assign("oauth_callback", _oauthCallback); + } + + CocoaTweet::API::Model::OAuthToken RequestToken::process(std::weak_ptr _oauth){ + CocoaTweet::API::Model::OAuthToken oauthToken; + HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv){ + auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); + if(mp.count("oauth_token")){ + oauthToken.oauthToken(mp.at("oauth_token")); + } + if(mp.count("oauth_token_secret")){ + oauthToken.oauthTokenSecret(mp.at("oauth_token_secret")); + } + std::cout << _rcv << std::endl; + }); + return oauthToken; + } +} \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/requestToken.h b/src/cocoatweet/api/oauth1/requestToken.h new file mode 100644 index 0000000..32e4d6e --- /dev/null +++ b/src/cocoatweet/api/oauth1/requestToken.h @@ -0,0 +1,17 @@ +#ifndef COCOATWEET_API_OAUTH1_REQUESTTOKEN_H +#define COCOATWEET_API_OAUTH1_REQUESTTOKEN_H + +#include +#include + +namespace CocoaTweet::API::OAuth1{ + class RequestToken: public CocoaTweet::API::Interface::HttpPost { + public: + RequestToken(); + void oauthCallback(const std::string& _oauthCallback); + + CocoaTweet::API::Model::OAuthToken process(std::weak_ptr _oauth); + }; +} + +#endif \ No newline at end of file diff --git a/src/cocoatweet/oauth/oauth2/token.h b/src/cocoatweet/api/oauth2/invalidateToken.cc similarity index 100% rename from src/cocoatweet/oauth/oauth2/token.h rename to src/cocoatweet/api/oauth2/invalidateToken.cc diff --git a/src/cocoatweet/api/oauth2/invalidateToken.h b/src/cocoatweet/api/oauth2/invalidateToken.h new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/api/oauth2/oauth2.cc b/src/cocoatweet/api/oauth2/oauth2.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/api/oauth2/oauth2.h b/src/cocoatweet/api/oauth2/oauth2.h new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/api/oauth2/token.cc b/src/cocoatweet/api/oauth2/token.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/api/oauth2/token.h b/src/cocoatweet/api/oauth2/token.h new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/api/status/destroy.cc b/src/cocoatweet/api/status/destroy.cc index caa0860..974b965 100644 --- a/src/cocoatweet/api/status/destroy.cc +++ b/src/cocoatweet/api/status/destroy.cc @@ -9,7 +9,7 @@ void Destroy::id(const std::string _id) { } CocoaTweet::API::Model::Tweet Destroy::process( - std::weak_ptr _oauth) { + std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { tweet = CocoaTweet::API::Model::Tweet::parse(_rcv); diff --git a/src/cocoatweet/api/status/destroy.h b/src/cocoatweet/api/status/destroy.h index 6e2a036..449b956 100644 --- a/src/cocoatweet/api/status/destroy.h +++ b/src/cocoatweet/api/status/destroy.h @@ -18,9 +18,9 @@ public: void id(const std::string _id); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[in] std::weak_ptr _oauth : pointer to oauth object /// @param[out] CocoaTweet::API::Model::Tweet : request result - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/retweet.cc b/src/cocoatweet/api/status/retweet.cc index 32d34b5..28b91d3 100644 --- a/src/cocoatweet/api/status/retweet.cc +++ b/src/cocoatweet/api/status/retweet.cc @@ -9,7 +9,7 @@ void Retweet::id(const std::string& _id) { } CocoaTweet::API::Model::Tweet Retweet::process( - std::weak_ptr _oauth) { + std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { tweet = CocoaTweet::API::Model::Tweet(_rcv); diff --git a/src/cocoatweet/api/status/retweet.h b/src/cocoatweet/api/status/retweet.h index add7a97..392812f 100644 --- a/src/cocoatweet/api/status/retweet.h +++ b/src/cocoatweet/api/status/retweet.h @@ -11,7 +11,7 @@ public: void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/status.cc b/src/cocoatweet/api/status/status.cc index 7e050aa..9f50d85 100644 --- a/src/cocoatweet/api/status/status.cc +++ b/src/cocoatweet/api/status/status.cc @@ -6,7 +6,7 @@ #include "cocoatweet/api/status/userTimeline.h" namespace CocoaTweet::API::Statuses { -Status::Status(std::shared_ptr _oauth) { +Status::Status(std::shared_ptr _oauth) { oauth_ = _oauth; } diff --git a/src/cocoatweet/api/status/status.h b/src/cocoatweet/api/status/status.h index 74e4bb7..e2b8d06 100644 --- a/src/cocoatweet/api/status/status.h +++ b/src/cocoatweet/api/status/status.h @@ -2,7 +2,7 @@ #define COCOATWEET_API_STATUS_STATUS_H_ #include "cocoatweet/api/interface/groupInterface.h" -#include "cocoatweet/oauth/oauth.h" +#include "cocoatweet/authentication/authenticator.h" #include #include #include @@ -28,8 +28,8 @@ public: Status() = default; /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object - Status(std::shared_ptr _oauth); + /// @param[in] std::shared_ptr : pointer to OAuth object + Status(std::shared_ptr _oauth); /// @brief send request to statuses/update with specified status /// @details this function throws CocoaTweet::Exception::* if something happen diff --git a/src/cocoatweet/api/status/unretweet.cc b/src/cocoatweet/api/status/unretweet.cc index 3d59158..3674591 100644 --- a/src/cocoatweet/api/status/unretweet.cc +++ b/src/cocoatweet/api/status/unretweet.cc @@ -9,7 +9,7 @@ void Unretweet::id(const std::string& _id) { } CocoaTweet::API::Model::Tweet Unretweet::process( - std::weak_ptr _oauth) { + std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { tweet = CocoaTweet::API::Model::Tweet(_rcv); diff --git a/src/cocoatweet/api/status/unretweet.h b/src/cocoatweet/api/status/unretweet.h index 4f2421f..b095929 100644 --- a/src/cocoatweet/api/status/unretweet.h +++ b/src/cocoatweet/api/status/unretweet.h @@ -11,7 +11,7 @@ public: void id(const std::string& _id); - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); }; } // namespace CocoaTweet::API::Statuses diff --git a/src/cocoatweet/api/status/update.cc b/src/cocoatweet/api/status/update.cc index 866ad8b..a37fbc2 100644 --- a/src/cocoatweet/api/status/update.cc +++ b/src/cocoatweet/api/status/update.cc @@ -53,7 +53,7 @@ void Update::failDMCommands(bool _fail) { bodyParam_.insert_or_assign("fail_dmcommands", std::to_string(_fail)); } -CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr _oauth) { +CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr _oauth) { CocoaTweet::API::Model::Tweet tweet; HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { tweet = CocoaTweet::API::Model::Tweet::parse(_rcv); diff --git a/src/cocoatweet/api/status/update.h b/src/cocoatweet/api/status/update.h index 1f3ff38..e0f3699 100644 --- a/src/cocoatweet/api/status/update.h +++ b/src/cocoatweet/api/status/update.h @@ -40,9 +40,9 @@ public: void failDMCommands(bool _fail); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[in] std::weak_ptr _oauth : pointer to oauth object /// @param[out] CocoaTweet::API::Model::Tweet : request result - CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); + CocoaTweet::API::Model::Tweet process(std::weak_ptr _oauth); private: std::string status_; diff --git a/src/cocoatweet/api/status/userTimeline.cc b/src/cocoatweet/api/status/userTimeline.cc index 7da4a3f..59c4930 100644 --- a/src/cocoatweet/api/status/userTimeline.cc +++ b/src/cocoatweet/api/status/userTimeline.cc @@ -13,7 +13,7 @@ void UserTimeline::screenName(const std::string& _screenName) { } std::vector UserTimeline::process( - std::weak_ptr _oauth) { + std::weak_ptr _oauth) { std::vector tweet; HttpGet::process(_oauth, [&tweet](const std::string& _rcv) { auto json = nlohmann::json::parse(_rcv); diff --git a/src/cocoatweet/api/status/userTimeline.h b/src/cocoatweet/api/status/userTimeline.h index 2da1b17..8c8f432 100644 --- a/src/cocoatweet/api/status/userTimeline.h +++ b/src/cocoatweet/api/status/userTimeline.h @@ -19,10 +19,10 @@ public: void screenName(const std::string& _screenName); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[in] std::weak_ptr _oauth : pointer to oauth object /// @param[out] std::vector : request result std::vector process( - std::weak_ptr _oauth); + std::weak_ptr _oauth); private: std::string status_; diff --git a/src/cocoatweet/api/user/show.cc b/src/cocoatweet/api/user/show.cc index eb53776..968b3ed 100644 --- a/src/cocoatweet/api/user/show.cc +++ b/src/cocoatweet/api/user/show.cc @@ -23,7 +23,7 @@ void Show::id(const std::string& _id) { } CocoaTweet::API::Model::User Show::process( - std::weak_ptr _oauth) { + std::weak_ptr _oauth) { CocoaTweet::API::Model::User user; HttpGet::process(_oauth, [&user](const std::string& _rcv) { user = CocoaTweet::API::Model::User::parse(_rcv); diff --git a/src/cocoatweet/api/user/show.h b/src/cocoatweet/api/user/show.h index f8848ee..dcde18e 100644 --- a/src/cocoatweet/api/user/show.h +++ b/src/cocoatweet/api/user/show.h @@ -23,10 +23,10 @@ public: void screenName(const std::string& _screenName); /// @brief process request for endpoint - /// @param[in] std::weak_ptr _oauth : pointer to oauth object + /// @param[in] std::weak_ptr _oauth : pointer to oauth object /// @param[out] CocoaTweet::API::Model::User : request result CocoaTweet::API::Model::User process( - std::weak_ptr _oauth); + std::weak_ptr _oauth); private: std::string status_; diff --git a/src/cocoatweet/api/user/user.cc b/src/cocoatweet/api/user/user.cc index 298b3b1..0a06b07 100644 --- a/src/cocoatweet/api/user/user.cc +++ b/src/cocoatweet/api/user/user.cc @@ -2,7 +2,7 @@ #include namespace CocoaTweet::API::Users{ - User::User(std::shared_ptr _oauth) { + User::User(std::shared_ptr _oauth) { oauth_ = _oauth; } diff --git a/src/cocoatweet/api/user/user.h b/src/cocoatweet/api/user/user.h index f6bfea6..c4b904f 100644 --- a/src/cocoatweet/api/user/user.h +++ b/src/cocoatweet/api/user/user.h @@ -2,7 +2,7 @@ #define COCOATWEET_API_USER_USER_H_ #include "cocoatweet/api/interface/groupInterface.h" -#include "cocoatweet/oauth/oauth.h" +#include "cocoatweet/authentication/authenticator.h" #include #include #include @@ -15,8 +15,8 @@ public: User() = default; /// @brief constructor which finally should to be called. - /// @param[in] std::shared_ptr : pointer to OAuth object - User(std::shared_ptr _oauth); + /// @param[in] std::shared_ptr : pointer to OAuth object + User(std::shared_ptr _oauth); CocoaTweet::API::Model::User show(const std::string& _screenName) const; diff --git a/src/cocoatweet/oauth/oauth.cc b/src/cocoatweet/authentication/authenticate.cc similarity index 61% rename from src/cocoatweet/oauth/oauth.cc rename to src/cocoatweet/authentication/authenticate.cc index 1b8b7ae..d748458 100644 --- a/src/cocoatweet/oauth/oauth.cc +++ b/src/cocoatweet/authentication/authenticate.cc @@ -1,4 +1,4 @@ -#include "oauth.h" +#include "authenticate.h" #include "cocoatweet/util/util.h" #include #include @@ -22,10 +22,15 @@ extern "C" { #include #endif -namespace CocoaTweet::OAuth { -OAuth1::OAuth1() : authType_(AuthType::OAuth) {} +namespace CocoaTweet::Authentication { +OAuth1::OAuth1() { + method_ = AuthenticationMethod::OAUTH10A; +} -OAuth1::OAuth1(const Key _key) : key_(_key), authType_(AuthType::OAuth) {} +OAuth1::OAuth1(const Key _key){ + key_ = _key; + method_ = AuthenticationMethod::OAUTH10A; +} std::map OAuth1::signature( const std::map& _param, const std::string& _method, @@ -49,10 +54,6 @@ std::map OAuth1::signature( const std::string OAuth1::calculateAuthHeader(std::map _bodyParam, const std::string& _method, const std::string& _url) { - if (authType_ == AuthType::Bearer) { - return "Authorization: Bearer " + key_.bearerToken(); - } - auto authParam = oauthParam(); auto sigingParam = authParam; if (!_bodyParam.empty()) { @@ -77,66 +78,67 @@ const std::string OAuth1::calculateAuthHeader(std::map return oauthHeader; } -const std::string& OAuth1::generateBearerToken() { - auto signature = key_.consumerKey() + ":" + key_.consumerSecret(); - auto k64Signature = base64(signature); - auto authHeader = std::string("Authorization: Basic ") + k64Signature; - auto contentType = - std::string("Content-Type: application/x-www-form-urlencoded;charset=UTF-8"); - auto url = std::string("https://api.twitter.com/oauth2/token"); - auto requestBody = std::string("grant_type=client_credentials"); - // do post - CURL* curl; - CURLcode res; - std::string rcv; - long responseCode; - curl = curl_easy_init(); - if (curl) { - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody.c_str()); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.length()); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv); -#ifndef NDEBUG - std::cout << "requestBody : " << requestBody << std::endl; - curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); -#endif - // Headerを保持するcurl_slist*を初期化 - struct curl_slist* headers = NULL; - // Authorizationをヘッダに追加 - headers = curl_slist_append(headers, authHeader.c_str()); - headers = curl_slist_append(headers, contentType.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - res = curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode); - curl_easy_cleanup(curl); - } - if (res != CURLE_OK) { - throw std::runtime_error(std::string("INTERNAL ERROR : curl(") + std::to_string(res) + ")"); - exit(1); - } +// const std::string& OAuth1::generateBearerToken() { +// auto signature = key_.consumerKey() + ":" + key_.consumerSecret(); +// auto k64Signature = base64(signature); +// auto authHeader = std::string("Authorization: Basic ") + k64Signature; +// auto contentType = +// std::string("Content-Type: application/x-www-form-urlencoded;charset=UTF-8"); +// auto url = std::string("https://api.twitter.com/oauth2/token"); +// auto requestBody = std::string("grant_type=client_credentials"); - auto j = nlohmann::json::parse(rcv); - if ((responseCode / 100) == 4) { - auto error = j["errors"][0]["code"]; - auto message = j["errors"][0]["message"]; - if (j.count("error") != 0) { - // この形式はエラーコードを持たないのでエラー種別が特定できない - throw new CocoaTweet::Exception::Exception(j["error"]); - } - if (error.get() == 44) { - throw CocoaTweet::Exception::InvalidParameterException( - message.get().c_str()); - } - } +// // do post +// CURL* curl; +// CURLcode res; +// std::string rcv; +// long responseCode; +// curl = curl_easy_init(); +// if (curl) { +// curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); +// curl_easy_setopt(curl, CURLOPT_POST, 1); +// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody.c_str()); +// curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.length()); +// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); +// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_); +// curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv); +// #ifndef NDEBUG +// std::cout << "requestBody : " << requestBody << std::endl; +// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); +// #endif +// // Headerを保持するcurl_slist*を初期化 +// struct curl_slist* headers = NULL; +// // Authorizationをヘッダに追加 +// headers = curl_slist_append(headers, authHeader.c_str()); +// headers = curl_slist_append(headers, contentType.c_str()); +// curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); +// res = curl_easy_perform(curl); +// curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode); +// curl_easy_cleanup(curl); +// } +// if (res != CURLE_OK) { +// throw std::runtime_error(std::string("INTERNAL ERROR : curl(") + std::to_string(res) + ")"); +// exit(1); +// } - key_.bearerToken(j["access_token"]); - authType_ = AuthType::Bearer; - return key_.bearerToken(); -} +// auto j = nlohmann::json::parse(rcv); +// if ((responseCode / 100) == 4) { +// auto error = j["errors"][0]["code"]; +// auto message = j["errors"][0]["message"]; +// if (j.count("error") != 0) { +// // この形式はエラーコードを持たないのでエラー種別が特定できない +// throw new CocoaTweet::Exception::Exception(j["error"]); +// } +// if (error.get() == 44) { +// throw CocoaTweet::Exception::InvalidParameterException( +// message.get().c_str()); +// } +// } + +// key_.bearerToken(j["access_token"]); +// authType_ = AuthType::Bearer; +// return key_.bearerToken(); +// } const std::string OAuth1::nonce() const { std::random_device engine; @@ -156,16 +158,13 @@ const std::string OAuth1::timestamp() const { } const std::string OAuth1::method() const { - return SIGNATURE_METHOD_; + return "HMAC-SHA1"; } const std::string OAuth1::version() const { - return OAUTH_VERSION_; + return "1.0"; } -const Key OAuth1::key() const { - return key_; -} std::map OAuth1::oauthParam() const { auto tmp = std::map{{"oauth_nonce", nonce()}, @@ -235,4 +234,4 @@ std::string OAuth1::hmacSha1(std::string _key, std::string _data) { return static_cast(k64); } -} // namespace CocoaTweet::OAuth +} // namespace CocoaTweet::Authentication diff --git a/src/cocoatweet/oauth/oauth.h b/src/cocoatweet/authentication/authenticate.h similarity index 64% rename from src/cocoatweet/oauth/oauth.h rename to src/cocoatweet/authentication/authenticate.h index ccd2cfe..9bbfaac 100644 --- a/src/cocoatweet/oauth/oauth.h +++ b/src/cocoatweet/authentication/authenticate.h @@ -5,19 +5,19 @@ #include #include #include "key.h" +#include -namespace CocoaTweet::OAuth { -class OAuth1 { +namespace CocoaTweet::Authentication { +class OAuth1: public AuthenticatorBase { public: - enum AuthType { OAuth, Bearer }; - OAuth1(); OAuth1(const Key _key); std::map signature(const std::map& _param, const std::string& _method, const std::string& _url); - const std::string& generateBearerToken(); + // const std::string& generateBearerToken(); + const std::string calculateAuthHeader(std::map _bodyParam, const std::string& _method, const std::string& _url); @@ -25,22 +25,11 @@ public: const std::string timestamp() const; const std::string method() const; const std::string version() const; - const Key key() const; std::map oauthParam() const; std::string hmacSha1(std::string _key, std::string _data); const std::string base64(const std::string& _raw); -private: - AuthType authType_; - Key key_; - const std::string SIGNATURE_METHOD_ = "HMAC-SHA1"; - const std::string OAUTH_VERSION_ = "1.0"; - static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) { - int realsize = _size * _nmemb; - _stream->append(_ptr, realsize); - return realsize; - } }; -} // namespace CocoaTweet::OAuth +} // namespace CocoaTweet::Authentication #endif diff --git a/src/cocoatweet/authentication/authenticator.cc b/src/cocoatweet/authentication/authenticator.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/authentication/authenticator.h b/src/cocoatweet/authentication/authenticator.h new file mode 100644 index 0000000..91b00dc --- /dev/null +++ b/src/cocoatweet/authentication/authenticator.h @@ -0,0 +1,33 @@ +#ifndef COCOATWEET_AUTHENTICATION_AUTHENTICATORBASE_H +#define COCOATWEET_AUTHENTICATION_AUTHENTICATORBASE_H + +#include +namespace CocoaTweet::Authentication{ +class AuthenticatorBase{ +public: + enum class AuthenticationMethod{ + OAUTH10A, + OAUTH2, + PLAIN, + NONE + }; + + virtual const std::string calculateAuthHeader(std::map _bodyParam, + const std::string& _method, const std::string& _url) = 0; + + const Key key() const{ + return key_; + } + +protected: + AuthenticationMethod method_; + Key key_; + static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) { + int realsize = _size * _nmemb; + _stream->append(_ptr, realsize); + return realsize; + } +}; +} + +#endif diff --git a/src/cocoatweet/authentication/bearer.cc b/src/cocoatweet/authentication/bearer.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/authentication/bearer.h b/src/cocoatweet/authentication/bearer.h new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/oauth/key.cc b/src/cocoatweet/authentication/key.cc similarity index 81% rename from src/cocoatweet/oauth/key.cc rename to src/cocoatweet/authentication/key.cc index 6230935..318c1a1 100644 --- a/src/cocoatweet/oauth/key.cc +++ b/src/cocoatweet/authentication/key.cc @@ -1,16 +1,19 @@ -#include "cocoatweet/oauth/key.h" +#include "cocoatweet/authentication/key.h" #include "nlohmann/json.hpp" #include #include #include -namespace CocoaTweet::OAuth { +namespace CocoaTweet::Authentication { Key Key::fromJsonFile(const std::string _jsonFile) { std::ifstream ifs(_jsonFile); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); auto j = nlohmann::json::parse(str); Key key; + + key.authType(Key::AUTH_TYPE::OAUTH10A); + if (j.contains("consumer_key")) { key.consumerKey(j["consumer_key"].get()); } @@ -29,4 +32,4 @@ Key Key::fromJsonFile(const std::string _jsonFile) { return key; } -} // namespace CocoaTweet::OAuth +} // namespace CocoaTweet::Authentication diff --git a/src/cocoatweet/oauth/key.h b/src/cocoatweet/authentication/key.h similarity index 80% rename from src/cocoatweet/oauth/key.h rename to src/cocoatweet/authentication/key.h index 8a39755..ef9e9ea 100644 --- a/src/cocoatweet/oauth/key.h +++ b/src/cocoatweet/authentication/key.h @@ -4,24 +4,32 @@ #include #include -namespace CocoaTweet::OAuth { +namespace CocoaTweet::Authentication { class Key { +public: + enum AUTH_TYPE{ + OAUTH10A, + OAUTH2, + PLAIN + }; + +private: std::string consumerKey_; std::string consumerSecret_; std::string accessToken_; std::string accessTokenSecret_; std::string bearerToken_; - + AUTH_TYPE authType_; public: Key() : consumerKey_(""), consumerSecret_(""), accessToken_(""), accessTokenSecret_("") {} Key(const std::string& _consumerKey, const std::string& _consumerSecret, - const std::string& _accessToken, const std::string& _accessTokenSecret) + const std::string& _accessToken, const std::string& _accessTokenSecret, const AUTH_TYPE _authType = AUTH_TYPE::OAUTH10A) : consumerKey_(_consumerKey), consumerSecret_(_consumerSecret), accessToken_(_accessToken), - accessTokenSecret_(_accessTokenSecret) {} - Key(const std::string& _consumerKey, const std::string& _consumerSecret) - : consumerKey_(_consumerKey), consumerSecret_(_consumerSecret) {} + accessTokenSecret_(_accessTokenSecret), authType_(_authType) {} + Key(const std::string& _consumerKey, const std::string& _consumerSecret, const AUTH_TYPE _authType = AUTH_TYPE::OAUTH2) + : consumerKey_(_consumerKey), consumerSecret_(_consumerSecret), authType_(_authType){} void consumerKey(const std::string& _consumerKey) { consumerKey_ = _consumerKey; @@ -59,6 +67,14 @@ public: return bearerToken_; } + const AUTH_TYPE authType() const { + return authType_; + } + + void authType(AUTH_TYPE _authType) { + authType_ = _authType; + } + std::map noSecret() const { return std::map{{"oauth_consumer_key", consumerKey_}, {"oauth_token", accessToken_}}; @@ -70,6 +86,6 @@ public: static Key fromJsonFile(const std::string _jsonFile); }; -} // namespace CocoaTweet::OAuth +} // namespace CocoaTweet::Authentication #endif diff --git a/src/cocoatweet/authentication/plain.cc b/src/cocoatweet/authentication/plain.cc new file mode 100644 index 0000000..e69de29 diff --git a/src/cocoatweet/authentication/plain.h b/src/cocoatweet/authentication/plain.h new file mode 100644 index 0000000..7fe3c49 --- /dev/null +++ b/src/cocoatweet/authentication/plain.h @@ -0,0 +1,11 @@ +#ifndef COCOATWEET_AUTHENTICATION_PLAIN_H +#define COCOATWEET_AUTHENTICATION_PLAIN_H + +#include + +namespace CocoaTweet::Authentication{ + class Plain: public AuthenticatorBase { + }; +} + +#endif diff --git a/src/cocoatweet/oauth/authorize.h b/src/cocoatweet/oauth/authorize.h index e69de29..db5033a 100644 --- a/src/cocoatweet/oauth/authorize.h +++ b/src/cocoatweet/oauth/authorize.h @@ -0,0 +1,22 @@ +#ifndef COCOATWEET_OAUTH_AUTHORIZE_H_ +#define COCOATWEET_OAUTH_AUTHORIZE_H_ + +#include "cocoatweet/api/interface/groupInterface.h" +#include +#include + +namespace CocoaTweet::Authentication{ +class Auth: public groupInterface { +public: + enum AuthType { OAuth, Bearer }; + +Auth(); +Auth(Key _key); +authorize(const bool app_only); + +private: +Key key_; +} +} + +#endif \ No newline at end of file diff --git a/src/cocoatweet/util/util.cc b/src/cocoatweet/util/util.cc index cb1ebfc..771e53d 100644 --- a/src/cocoatweet/util/util.cc +++ b/src/cocoatweet/util/util.cc @@ -1,6 +1,9 @@ #include "cocoatweet/util/util.h" #include #include +#include + +#include namespace CocoaTweet::Util { std::string urlEncode(const std::string& _str) { @@ -33,4 +36,34 @@ std::string join(const std::vector _vec, const std::string& _delim) return str; } + +std::unordered_map parse(const std::string str, const char _delim, const char _conn){ + int first = 0; + int last = str.find_first_of(_delim); + std::vector result; + + while (first < str.size()) { + result.push_back(str.substr(first, last - first)); + first = last + 1; + last = str.find_first_of(_delim, first); + if (last == std::string::npos) last = str.size(); + } + + auto mp = std::unordered_map(); + for(auto elm: result){ + int pos = elm.find_first_of(_conn); + + std::cout << pos << std::endl; + if(pos == std::string::npos){ + mp.insert_or_assign(elm, ""); + }else{ + auto key = elm.substr(0, pos); + auto val = elm.substr(pos + 1); + mp.insert_or_assign(key, val); + } + } + + return mp; +} + } // namespace CocoaTweet::Util diff --git a/src/cocoatweet/util/util.h b/src/cocoatweet/util/util.h index a35e2f0..64d172f 100644 --- a/src/cocoatweet/util/util.h +++ b/src/cocoatweet/util/util.h @@ -4,10 +4,12 @@ #include #include #include +#include namespace CocoaTweet::Util { std::string urlEncode(const std::string& _str); std::string join(const std::vector _vec, const std::string& _delim); +std::unordered_map parse(const std::string str, const char _delim, const char _conn); } // namespace CocoaTweet::Util #endif diff --git a/test/oauth/key.cc b/test/oauth/key.cc index f03b2b8..de2c8ba 100644 --- a/test/oauth/key.cc +++ b/test/oauth/key.cc @@ -2,11 +2,11 @@ #include -#include "cocoatweet/oauth/key.h" +#include "cocoatweet/authentication/key.h" BOOST_AUTO_TEST_SUITE(oauth_key) BOOST_AUTO_TEST_CASE(test01) { - CocoaTweet::OAuth::Key key; + CocoaTweet::Authentication::Key key; BOOST_TEST(key.consumerKey() == ""); BOOST_TEST(key.consumerSecret() == ""); @@ -15,7 +15,7 @@ BOOST_AUTO_TEST_CASE(test01) { } BOOST_AUTO_TEST_CASE(test02) { - CocoaTweet::OAuth::Key key("consumerKey", "consumerSecret", "accessToken", + CocoaTweet::Authentication::Key key("consumerKey", "consumerSecret", "accessToken", "accessTokenSecret"); BOOST_TEST(key.consumerKey() == "consumerKey");