access tokenが取得できるように

This commit is contained in:
keita
2022-04-23 04:55:43 +09:00
parent d43e04506b
commit 560fe707a6
75 changed files with 591 additions and 193 deletions
+3
View File
@@ -1,3 +1,6 @@
build/ build/
*.swp *.swp
src/main.cc src/main.cc
help/
Testing/
**/api_key.json
+5 -5
View File
@@ -26,7 +26,7 @@ you can use these endpoint
# Installation # Installation
## Ubuntu ## 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 $ git clone https://github.com/koron0902/CocoaTweet
$ cd CocoaTweet $ cd CocoaTweet
$ mkdir build $ mkdir build
@@ -74,14 +74,14 @@ $ mingw32-make
### 1.Write Key into code ### 1.Write Key into code
write api key into code and create Key object use it. 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 consumerKey = "your consumer key";
auto consumerSecret = "your consumer secret"; auto consumerSecret = "your consumer secret";
auto accessToken = "your access token"; auto accessToken = "your access token";
auto accessTokenSecret = "your access token secret"; 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. 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 ## Generate API object
+4 -4
View File
@@ -1,6 +1,6 @@
{ {
"consumer_key" : "HmXS3q4yrQ2wndiCQzH8YnUQM", "consumer_key" : "wcSkGjuvoQ7gGjB0rR5XSJiWI",
"consumer_secret" : "qZwdGJYGFMpz7CSVGPHmbrh0O3Sk99wa0OLMf8HSvKKtNclrnf", "consumer_secret" : "Qo9fiQaimj7QwqB8dXOONVI9QL2quJyxh3ZXo2REgvcrpCC3gE",
"access_token" : "1427931990792146949-iuNX8xBgQSXGtxr5cz8m495OtnXKcw", "access_token" : "1427931990792146949-nmcoAzhLSq0j0vwv8ati9F5oQN3Gb7",
"access_token_secret" : "A0R7vOOR3pRm4trGLxVbwNiQTkOMAkEh3z5ed3IxJVbJl" "access_token_secret" : "bzWd71ARfczBMuYLcsHdlXotdNVHY3nX7WMOSrLCuHXHa"
} }
+21 -5
View File
@@ -1,18 +1,31 @@
#include <cocoatweet/api/api.h> #include <cocoatweet/api/api.h>
#include <cocoatweet/authentication/authenticate.h>
#include <iostream>
namespace CocoaTweet::API { namespace CocoaTweet::API {
API::API(CocoaTweet::OAuth::Key _key) { API::API(CocoaTweet::Authentication::Key _key) {
oauth_ = std::make_shared<CocoaTweet::OAuth::OAuth1>(_key); swapKey(_key);
}
void API::swapKey(const CocoaTweet::Authentication::Key _key){
if(_key.authType() == CocoaTweet::Authentication::Key::OAUTH10A){
oauth_ = std::make_shared<CocoaTweet::Authentication::OAuth1>(_key);
}else{
}
user_ = Users::User(oauth_); user_ = Users::User(oauth_);
status_ = Statuses::Status(oauth_); status_ = Statuses::Status(oauth_);
favorite_ = Favorites::Favorite(oauth_); favorite_ = Favorites::Favorite(oauth_);
media_ = Medias::Media(oauth_); media_ = Medias::Media(oauth_);
directMessage_ = DirectMessages::DirectMessage(oauth_); directMessage_ = DirectMessages::DirectMessage(oauth_);
oauth1_ = OAuth1::OAuth(oauth_);
} }
const std::string& API::generateBearerToken() const { // const std::string& API::generateBearerToken() const {
return oauth_->generateBearerToken(); // return oauth_->generateBearerToken();
} // }
Users::User API::user() const { Users::User API::user() const {
return user_; return user_;
@@ -33,4 +46,7 @@ Medias::Media API::media() const {
DirectMessages::DirectMessage API::directMessage() const { DirectMessages::DirectMessage API::directMessage() const {
return directMessage_; return directMessage_;
} }
OAuth1::OAuth API::oauth1() const {
return oauth1_;
}
} // namespace CocoaTweet::API } // namespace CocoaTweet::API
+10 -4
View File
@@ -6,15 +6,16 @@
#include <cocoatweet/api/favorite/favorite.h> #include <cocoatweet/api/favorite/favorite.h>
#include <cocoatweet/api/media/media.h> #include <cocoatweet/api/media/media.h>
#include <cocoatweet/api/directMessage/directMessage.h> #include <cocoatweet/api/directMessage/directMessage.h>
#include <cocoatweet/oauth/oauth.h> #include <cocoatweet/authentication/authenticator.h>
#include <cocoatweet/api/oauth1/oauth.h>
namespace CocoaTweet::API { namespace CocoaTweet::API {
/// @brief Twitter API Entry Point /// @brief Twitter API Entry Point
class API { class API {
public: public:
/// @brief primary constructor /// @brief primary constructor
/// @param[in] _key Twitter API Key typed CocoaTweet::OAuth::Key /// @param[in] _key Twitter API Key typed CocoaTweet::Authentication::Key
API(CocoaTweet::OAuth::Key _key); API(CocoaTweet::Authentication::Key _key);
Users::User user() const; Users::User user() const;
@@ -29,7 +30,11 @@ public:
Medias::Media media() const; Medias::Media media() const;
DirectMessages::DirectMessage directMessage() const; DirectMessages::DirectMessage directMessage() const;
OAuth1::OAuth oauth1() const;
const std::string& generateBearerToken() const; const std::string& generateBearerToken() const;
void swapKey(const CocoaTweet::Authentication::Key _key);
private: private:
Users::User user_; Users::User user_;
@@ -37,7 +42,8 @@ private:
Favorites::Favorite favorite_; Favorites::Favorite favorite_;
Medias::Media media_; Medias::Media media_;
DirectMessages::DirectMessage directMessage_; DirectMessages::DirectMessage directMessage_;
std::shared_ptr<CocoaTweet::OAuth::OAuth1> oauth_; OAuth1::OAuth oauth1_;
std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> oauth_;
}; };
} // namespace CocoaTweet::API } // namespace CocoaTweet::API
@@ -2,7 +2,7 @@
#include <cocoatweet/api/directMessage/new.h> #include <cocoatweet/api/directMessage/new.h>
namespace CocoaTweet::API::DirectMessages { namespace CocoaTweet::API::DirectMessages {
DirectMessage::DirectMessage(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { DirectMessage::DirectMessage(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
oauth_ = _oauth; oauth_ = _oauth;
} }
@@ -2,7 +2,7 @@
#define COCOATWEET_API_DIRECTMESSAGE_DIRECTMESSAGE_H_ #define COCOATWEET_API_DIRECTMESSAGE_DIRECTMESSAGE_H_
#include "cocoatweet/api/interface/groupInterface.h" #include "cocoatweet/api/interface/groupInterface.h"
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/authentication/authenticator.h"
#include <cocoatweet/api/model/tweet.h> #include <cocoatweet/api/model/tweet.h>
#include <vector> #include <vector>
#include <utility> #include <utility>
@@ -15,8 +15,8 @@ public:
DirectMessage() = default; DirectMessage() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::OAuth::OAuth1> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object
DirectMessage(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth); DirectMessage(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
void messageCreate(const std::string& _recipient, const std::string& _message); void messageCreate(const std::string& _recipient, const std::string& _message);
+1 -1
View File
@@ -23,7 +23,7 @@ void New::message(const std::string& _message) {
json_["event"]["message_create"]["message_data"]["text"] = _message; json_["event"]["message_create"]["message_data"]["text"] = _message;
} }
void New::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { void New::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
bodyParam_.insert_or_assign("data", json_.dump()); bodyParam_.insert_or_assign("data", json_.dump());
HttpPost::process(_oauth, [](const std::string& _rcv) {}); HttpPost::process(_oauth, [](const std::string& _rcv) {});
} }
+2 -2
View File
@@ -17,9 +17,9 @@ public:
void message(const std::string& _message); void message(const std::string& _message);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object
/// @param[out] CocoaTweet::API::Model::Tweet : request result /// @param[out] CocoaTweet::API::Model::Tweet : request result
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
std::string status_; std::string status_;
+1 -1
View File
@@ -11,7 +11,7 @@ void Create::id(const std::string& _id) {
bodyParam_.insert_or_assign("id", _id); bodyParam_.insert_or_assign("id", _id);
} }
CocoaTweet::API::Model::Tweet Create::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { CocoaTweet::API::Model::Tweet Create::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { HttpPost::process(_oauth, [&tweet](const std::string& _rcv) {
tweet = CocoaTweet::API::Model::Tweet(_rcv); tweet = CocoaTweet::API::Model::Tweet(_rcv);
+1 -1
View File
@@ -9,7 +9,7 @@ class Create : public CocoaTweet::API::Interface::HttpPost {
public: public:
Create(); Create();
void id(const std::string& _id); void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
}; };
+1 -1
View File
@@ -12,7 +12,7 @@ void Destroy::id(const std::string& _id) {
} }
CocoaTweet::API::Model::Tweet Destroy::process( CocoaTweet::API::Model::Tweet Destroy::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { HttpPost::process(_oauth, [&tweet](const std::string& _rcv) {
tweet = CocoaTweet::API::Model::Tweet(_rcv); tweet = CocoaTweet::API::Model::Tweet(_rcv);
+1 -1
View File
@@ -9,7 +9,7 @@ class Destroy : public CocoaTweet::API::Interface::HttpPost {
public: public:
Destroy(); Destroy();
void id(const std::string& _id); void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
}; };
+1 -1
View File
@@ -3,7 +3,7 @@
#include "cocoatweet/api/favorite/destroy.h" #include "cocoatweet/api/favorite/destroy.h"
namespace CocoaTweet::API::Favorites { namespace CocoaTweet::API::Favorites {
Favorite::Favorite(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { Favorite::Favorite(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
oauth_ = _oauth; oauth_ = _oauth;
} }
+2 -2
View File
@@ -2,14 +2,14 @@
#define COCOATWEET_API_FAVORITE_FAVORITE_H_ #define COCOATWEET_API_FAVORITE_FAVORITE_H_
#include "cocoatweet/api/interface/groupInterface.h" #include "cocoatweet/api/interface/groupInterface.h"
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/authentication/authenticator.h"
#include <cocoatweet/api/model/tweet.h> #include <cocoatweet/api/model/tweet.h>
namespace CocoaTweet::API::Favorites { namespace CocoaTweet::API::Favorites {
class Favorite : public groupInterface { class Favorite : public groupInterface {
public: public:
Favorite() = default; Favorite() = default;
Favorite(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth); Favorite(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
CocoaTweet::API::Model::Tweet create(const std::string& _id) const; CocoaTweet::API::Model::Tweet create(const std::string& _id) const;
CocoaTweet::API::Model::Tweet destroy(const std::string& _id) const; CocoaTweet::API::Model::Tweet destroy(const std::string& _id) const;
}; };
@@ -2,12 +2,12 @@
#define COCOATWEET_API_INTERFACE_GROUPINTERFACE_H_ #define COCOATWEET_API_INTERFACE_GROUPINTERFACE_H_
#include <memory> #include <memory>
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/authentication/authenticator.h"
namespace CocoaTweet::API { namespace CocoaTweet::API {
class groupInterface { class groupInterface {
protected: protected:
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_; std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> oauth_;
}; };
} // namespace CocoaTweet::API } // namespace CocoaTweet::API
+4 -3
View File
@@ -2,17 +2,18 @@
#define COCOATWEET_API_INTERFACE_HTTPBASE_H_ #define COCOATWEET_API_INTERFACE_HTTPBASE_H_
#include <functional> #include <functional>
#include "cocoatweet/oauth/oauth.h" #include <memory>
#include "cocoatweet/authentication/authenticator.h"
namespace CocoaTweet::API::Interface { namespace CocoaTweet::API::Interface {
class HttpBase { class HttpBase {
public: public:
protected: protected:
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_; std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> oauth_;
std::map<std::string, std::string> bodyParam_; std::map<std::string, std::string> bodyParam_;
std::string url_; std::string url_;
std::string contentType_; std::string contentType_;
virtual void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, virtual void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth,
std::function<void(const std::string&)> _callback) = 0; std::function<void(const std::string&)> _callback) = 0;
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) { static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) {
int realsize = _size * _nmemb; int realsize = _size * _nmemb;
+1 -1
View File
@@ -21,7 +21,7 @@ extern "C" {
#endif #endif
namespace CocoaTweet::API::Interface { namespace CocoaTweet::API::Interface {
void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void HttpGet::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth,
std::function<void(const std::string&)> _callback) { std::function<void(const std::string&)> _callback) {
auto url = url_; auto url = url_;
+3 -3
View File
@@ -2,7 +2,7 @@
#define COCOATWEET_API_INTERFACE_HTTPGET_H_ #define COCOATWEET_API_INTERFACE_HTTPGET_H_
#include <functional> #include <functional>
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/authentication/authenticator.h"
#include <cocoatweet/api/interface/httpBase.h> #include <cocoatweet/api/interface/httpBase.h>
namespace CocoaTweet::API::Interface { namespace CocoaTweet::API::Interface {
@@ -11,11 +11,11 @@ class HttpGet : public virtual HttpBase {
public: public:
protected: protected:
/// @brief Send HTTP/POST using OAuth object /// @brief Send HTTP/POST using OAuth object
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to OAuth object to /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to OAuth object to
/// authenticate /// authenticate
/// @param[in] std::function<void(const unsigned int, const std::string&)> _callback : /// @param[in] std::function<void(const unsigned int, const std::string&)> _callback :
/// callback method for processing to response /// callback method for processing to response
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth,
std::function<void(const std::string&)> _callback); std::function<void(const std::string&)> _callback);
}; };
} // namespace CocoaTweet::API::Interface } // namespace CocoaTweet::API::Interface
+25 -17
View File
@@ -21,22 +21,22 @@ extern "C" {
#endif #endif
namespace CocoaTweet::API::Interface { namespace CocoaTweet::API::Interface {
void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void HttpPost::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth,
std::function<void(const std::string&)> _callback) { std::function<void(const std::string&)> _callback) {
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
auto oauth = _oauth.lock(); auto oauth = _oauth.lock();
auto oauthParam = oauth->oauthParam(); // auto oauthParam = oauth->oauthParam();
auto sigingParam = oauthParam; // auto sigingParam = oauthParam;
if (contentType_ == "application/x-www-form-urlencoded") { // if (contentType_ == "application/x-www-form-urlencoded") {
for (const auto [k, v] : bodyParam_) { // for (const auto [k, v] : bodyParam_) {
sigingParam.insert_or_assign(k, v); // sigingParam.insert_or_assign(k, v);
} // }
} // }
auto signature = oauth->signature(sigingParam, "POST", url_); // auto signature = oauth->signature(sigingParam, "POST", url_);
// 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録 // 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
oauthParam.merge(signature); // oauthParam.merge(signature);
// リクエストボディの構築 // リクエストボディの構築
std::string requestBody = ""; std::string requestBody = "";
@@ -60,13 +60,21 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
} }
// ヘッダの構築 // ヘッダの構築
std::string oauthHeader = "authorization: OAuth "; // std::string oauthHeader = "authorization: OAuth ";
{ // {
std::vector<std::string> tmp; // std::vector<std::string> tmp;
for (const auto& [key, value] : oauthParam) { // for (const auto& [key, value] : oauthParam) {
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value)); // tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
} // }
oauthHeader += CocoaTweet::Util::join(tmp, ","); // 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 // do post
+3 -3
View File
@@ -2,7 +2,7 @@
#define COCOATWEET_API_INTERFACE_HTTPPOST_H_ #define COCOATWEET_API_INTERFACE_HTTPPOST_H_
#include <functional> #include <functional>
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/authentication/authenticator.h"
#include <cocoatweet/api/interface/httpBase.h> #include <cocoatweet/api/interface/httpBase.h>
namespace CocoaTweet::API::Interface { namespace CocoaTweet::API::Interface {
@@ -11,11 +11,11 @@ class HttpPost : public HttpBase {
public: public:
protected: protected:
/// @brief Send HTTP/POST using OAuth object /// @brief Send HTTP/POST using OAuth object
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to OAuth object to /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to OAuth object to
/// authenticate /// authenticate
/// @param[in] std::function<void(const unsigned int, const std::string&)> _callback : /// @param[in] std::function<void(const unsigned int, const std::string&)> _callback :
/// callback method for processing to response /// callback method for processing to response
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth,
std::function<void(const std::string&)> _callback); std::function<void(const std::string&)> _callback);
}; };
} // namespace CocoaTweet::API::Interface } // namespace CocoaTweet::API::Interface
+1 -1
View File
@@ -1,7 +1,7 @@
#include "cocoatweet/api/media/media.h" #include "cocoatweet/api/media/media.h"
namespace CocoaTweet::API::Medias { namespace CocoaTweet::API::Medias {
Media::Media(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { Media::Media(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
oauth_ = _oauth; oauth_ = _oauth;
} }
+3 -3
View File
@@ -2,7 +2,7 @@
#define COCOATWEET_API_MEDIA_MEDIA_H_ #define COCOATWEET_API_MEDIA_MEDIA_H_
#include "cocoatweet/api/interface/groupInterface.h" #include "cocoatweet/api/interface/groupInterface.h"
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/authentication/authenticator.h"
#include <cocoatweet/api/model/mediaStore.h> #include <cocoatweet/api/model/mediaStore.h>
#include <cocoatweet/api/media/upload.h> #include <cocoatweet/api/media/upload.h>
#include <vector> #include <vector>
@@ -16,8 +16,8 @@ public:
Media() = default; Media() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::OAuth::OAuth1> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object
Media(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth); Media(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
CocoaTweet::API::Model::MediaStore upload(const std::string& _file) const; CocoaTweet::API::Model::MediaStore upload(const std::string& _file) const;
+1 -1
View File
@@ -19,7 +19,7 @@ void Upload::media(const std::string& _media) {
void Upload::mediaId(const std::string& _mediaId) {} void Upload::mediaId(const std::string& _mediaId) {}
CocoaTweet::API::Model::MediaStore Upload::process( CocoaTweet::API::Model::MediaStore Upload::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
auto extension = std::filesystem::path(media_).extension().string<char>(); auto extension = std::filesystem::path(media_).extension().string<char>();
if (mimeType.count(extension) == 0) { if (mimeType.count(extension) == 0) {
throw new CocoaTweet::Exception::UnsupportedMediaTypeException( throw new CocoaTweet::Exception::UnsupportedMediaTypeException(
+2 -2
View File
@@ -26,11 +26,11 @@ public:
void mediaId(const std::string& _mediaId); void mediaId(const std::string& _mediaId);
/// @brief upload media /// @brief upload media
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to OAuth object for /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to OAuth object for
/// authenticate /// authenticate
/// @param[out] CocoaTweet::API::Model::MediaStore : media upload result. use id() for post /// @param[out] CocoaTweet::API::Model::MediaStore : media upload result. use id() for post
/// tweet. /// tweet.
CocoaTweet::API::Model::MediaStore process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::MediaStore process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} // namespace CocoaTweet::API::Medias } // namespace CocoaTweet::API::Medias
+19
View File
@@ -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;
}
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef COCOATWEET_API_MODEL_OAUTHTOKEN_H_
#define COCOATWEET_API_MODEL_OAUTHTOKEN_H_
#include<string>
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
+33
View File
@@ -0,0 +1,33 @@
#include <cocoatweet/api/oauth1/accessToken.h>
#include <cocoatweet/util/util.h>
#include <iostream>
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<CocoaTweet::Authentication::AuthenticatorBase> _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;
}
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef COCOATWEET_API_OAUTH1_ACCESSTOKEN_H
#define COCOATWEET_API_OAUTH1_ACCESSTOKEN_H
#include <cocoatweet/api/interface/httpPost.h>
#include <cocoatweet/api/model/oauthToken.h>
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<CocoaTweet::Authentication::AuthenticatorBase> _oauth) ;
};
}
#endif
+33
View File
@@ -0,0 +1,33 @@
#include <string>
#include <cocoatweet/api/oauth1/authorize.h>
#include <iostream>
#include <cocoatweet/util/util.h>
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<int>(_forceLogin)));
}
void Authorize::screenName(const std::string& _screenName){
bodyParam_.insert_or_assign("screen_name", _screenName);
}
const std::string Authorize::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> __unused__) {
std::vector<std::string> tmp;
std::string query = "";
for (const auto& [key, value] : bodyParam_) {
tmp.push_back(key + "=" + value);
query = CocoaTweet::Util::join(tmp, "&");
}
return url_ + "?" + query;;
}
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef COCOATWEET_API_OAUTH1_AUTHORIZE_H
#define COCOATWEET_API_OAUTH1_AUTHORIZE_H
#include <cocoatweet/api/interface/httpPost.h>
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<CocoaTweet::Authentication::AuthenticatorBase> __unused__) ;
};
}
#endif
+35
View File
@@ -0,0 +1,35 @@
#include <cocoatweet/api/oauth1/oauth.h>
#include <cocoatweet/authentication/authenticate.h>
namespace CocoaTweet::API::OAuth1 {
OAuth::OAuth(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _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<CocoaTweet::Authentication::OAuth1>(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<CocoaTweet::Authentication::OAuth1>(key);
CocoaTweet::API::OAuth1::AccessToken accessToken;
accessToken.oauthVerifier(_verifier);
return accessToken.process(oauth);
}
} // namespace CocoaTweet::API::Statuses
+30
View File
@@ -0,0 +1,30 @@
#ifndef COCOATWEET_API_OAUTH1_OAUTH_H_
#define COCOATWEET_API_OAUTH1_OAUTH_H_
#include "cocoatweet/api/interface/groupInterface.h"
#include <cocoatweet/api/oauth1/accessToken.h>
#include <cocoatweet/api/oauth1/requestToken.h>
#include <cocoatweet/api/oauth1/authorize.h>
#include <cocoatweet/api/model/oauthToken.h>
#include <vector>
#include <utility>
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<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object
OAuth(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _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
+29
View File
@@ -0,0 +1,29 @@
#include <cocoatweet/api/oauth1/requestToken.h>
#include <cocoatweet/util/util.h>
#include <iostream>
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<CocoaTweet::Authentication::AuthenticatorBase> _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;
}
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef COCOATWEET_API_OAUTH1_REQUESTTOKEN_H
#define COCOATWEET_API_OAUTH1_REQUESTTOKEN_H
#include <cocoatweet/api/interface/httpPost.h>
#include <cocoatweet/api/model/oauthToken.h>
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<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
};
}
#endif
View File
View File
View File
View File
+1 -1
View File
@@ -9,7 +9,7 @@ void Destroy::id(const std::string _id) {
} }
CocoaTweet::API::Model::Tweet Destroy::process( CocoaTweet::API::Model::Tweet Destroy::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { HttpPost::process(_oauth, [&tweet](const std::string& _rcv) {
tweet = CocoaTweet::API::Model::Tweet::parse(_rcv); tweet = CocoaTweet::API::Model::Tweet::parse(_rcv);
+2 -2
View File
@@ -18,9 +18,9 @@ public:
void id(const std::string _id); void id(const std::string _id);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object
/// @param[out] CocoaTweet::API::Model::Tweet : request result /// @param[out] CocoaTweet::API::Model::Tweet : request result
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+1 -1
View File
@@ -9,7 +9,7 @@ void Retweet::id(const std::string& _id) {
} }
CocoaTweet::API::Model::Tweet Retweet::process( CocoaTweet::API::Model::Tweet Retweet::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { HttpPost::process(_oauth, [&tweet](const std::string& _rcv) {
tweet = CocoaTweet::API::Model::Tweet(_rcv); tweet = CocoaTweet::API::Model::Tweet(_rcv);
+1 -1
View File
@@ -11,7 +11,7 @@ public:
void id(const std::string& _id); void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+1 -1
View File
@@ -6,7 +6,7 @@
#include "cocoatweet/api/status/userTimeline.h" #include "cocoatweet/api/status/userTimeline.h"
namespace CocoaTweet::API::Statuses { namespace CocoaTweet::API::Statuses {
Status::Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { Status::Status(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
oauth_ = _oauth; oauth_ = _oauth;
} }
+3 -3
View File
@@ -2,7 +2,7 @@
#define COCOATWEET_API_STATUS_STATUS_H_ #define COCOATWEET_API_STATUS_STATUS_H_
#include "cocoatweet/api/interface/groupInterface.h" #include "cocoatweet/api/interface/groupInterface.h"
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/authentication/authenticator.h"
#include <cocoatweet/api/model/tweet.h> #include <cocoatweet/api/model/tweet.h>
#include <vector> #include <vector>
#include <utility> #include <utility>
@@ -28,8 +28,8 @@ public:
Status() = default; Status() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::OAuth::OAuth1> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object
Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth); Status(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
/// @brief send request to statuses/update with specified status /// @brief send request to statuses/update with specified status
/// @details this function throws CocoaTweet::Exception::* if something happen /// @details this function throws CocoaTweet::Exception::* if something happen
+1 -1
View File
@@ -9,7 +9,7 @@ void Unretweet::id(const std::string& _id) {
} }
CocoaTweet::API::Model::Tweet Unretweet::process( CocoaTweet::API::Model::Tweet Unretweet::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { HttpPost::process(_oauth, [&tweet](const std::string& _rcv) {
tweet = CocoaTweet::API::Model::Tweet(_rcv); tweet = CocoaTweet::API::Model::Tweet(_rcv);
+1 -1
View File
@@ -11,7 +11,7 @@ public:
void id(const std::string& _id); void id(const std::string& _id);
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
}; };
} // namespace CocoaTweet::API::Statuses } // namespace CocoaTweet::API::Statuses
+1 -1
View File
@@ -53,7 +53,7 @@ void Update::failDMCommands(bool _fail) {
bodyParam_.insert_or_assign("fail_dmcommands", std::to_string(_fail)); bodyParam_.insert_or_assign("fail_dmcommands", std::to_string(_fail));
} }
CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { CocoaTweet::API::Model::Tweet Update::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::Tweet tweet; CocoaTweet::API::Model::Tweet tweet;
HttpPost::process(_oauth, [&tweet](const std::string& _rcv) { HttpPost::process(_oauth, [&tweet](const std::string& _rcv) {
tweet = CocoaTweet::API::Model::Tweet::parse(_rcv); tweet = CocoaTweet::API::Model::Tweet::parse(_rcv);
+2 -2
View File
@@ -40,9 +40,9 @@ public:
void failDMCommands(bool _fail); void failDMCommands(bool _fail);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object
/// @param[out] CocoaTweet::API::Model::Tweet : request result /// @param[out] CocoaTweet::API::Model::Tweet : request result
CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); CocoaTweet::API::Model::Tweet process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
std::string status_; std::string status_;
+1 -1
View File
@@ -13,7 +13,7 @@ void UserTimeline::screenName(const std::string& _screenName) {
} }
std::vector<CocoaTweet::API::Model::Tweet> UserTimeline::process( std::vector<CocoaTweet::API::Model::Tweet> UserTimeline::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
std::vector<CocoaTweet::API::Model::Tweet> tweet; std::vector<CocoaTweet::API::Model::Tweet> tweet;
HttpGet::process(_oauth, [&tweet](const std::string& _rcv) { HttpGet::process(_oauth, [&tweet](const std::string& _rcv) {
auto json = nlohmann::json::parse(_rcv); auto json = nlohmann::json::parse(_rcv);
+2 -2
View File
@@ -19,10 +19,10 @@ public:
void screenName(const std::string& _screenName); void screenName(const std::string& _screenName);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object
/// @param[out] std::vector<CocoaTweet::API::Model::Tweet> : request result /// @param[out] std::vector<CocoaTweet::API::Model::Tweet> : request result
std::vector<CocoaTweet::API::Model::Tweet> process( std::vector<CocoaTweet::API::Model::Tweet> process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
std::string status_; std::string status_;
+1 -1
View File
@@ -23,7 +23,7 @@ void Show::id(const std::string& _id) {
} }
CocoaTweet::API::Model::User Show::process( CocoaTweet::API::Model::User Show::process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
CocoaTweet::API::Model::User user; CocoaTweet::API::Model::User user;
HttpGet::process(_oauth, [&user](const std::string& _rcv) { HttpGet::process(_oauth, [&user](const std::string& _rcv) {
user = CocoaTweet::API::Model::User::parse(_rcv); user = CocoaTweet::API::Model::User::parse(_rcv);
+2 -2
View File
@@ -23,10 +23,10 @@ public:
void screenName(const std::string& _screenName); void screenName(const std::string& _screenName);
/// @brief process request for endpoint /// @brief process request for endpoint
/// @param[in] std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth : pointer to oauth object /// @param[in] std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth : pointer to oauth object
/// @param[out] CocoaTweet::API::Model::User : request result /// @param[out] CocoaTweet::API::Model::User : request result
CocoaTweet::API::Model::User process( CocoaTweet::API::Model::User process(
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth); std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private: private:
std::string status_; std::string status_;
+1 -1
View File
@@ -2,7 +2,7 @@
#include <cocoatweet/api/user/show.h> #include <cocoatweet/api/user/show.h>
namespace CocoaTweet::API::Users{ namespace CocoaTweet::API::Users{
User::User(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) { User::User(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
oauth_ = _oauth; oauth_ = _oauth;
} }
+3 -3
View File
@@ -2,7 +2,7 @@
#define COCOATWEET_API_USER_USER_H_ #define COCOATWEET_API_USER_USER_H_
#include "cocoatweet/api/interface/groupInterface.h" #include "cocoatweet/api/interface/groupInterface.h"
#include "cocoatweet/oauth/oauth.h" #include "cocoatweet/authentication/authenticator.h"
#include <cocoatweet/api/model/user.h> #include <cocoatweet/api/model/user.h>
#include <vector> #include <vector>
#include <utility> #include <utility>
@@ -15,8 +15,8 @@ public:
User() = default; User() = default;
/// @brief constructor which finally should to be called. /// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::OAuth::OAuth1> : pointer to OAuth object /// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object
User(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth); User(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
CocoaTweet::API::Model::User show(const std::string& _screenName) const; CocoaTweet::API::Model::User show(const std::string& _screenName) const;
@@ -1,4 +1,4 @@
#include "oauth.h" #include "authenticate.h"
#include "cocoatweet/util/util.h" #include "cocoatweet/util/util.h"
#include <random> #include <random>
#include <ctime> #include <ctime>
@@ -22,10 +22,15 @@ extern "C" {
#include <iostream> #include <iostream>
#endif #endif
namespace CocoaTweet::OAuth { namespace CocoaTweet::Authentication {
OAuth1::OAuth1() : authType_(AuthType::OAuth) {} 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<std::string, std::string> OAuth1::signature( std::map<std::string, std::string> OAuth1::signature(
const std::map<std::string, std::string>& _param, const std::string& _method, const std::map<std::string, std::string>& _param, const std::string& _method,
@@ -49,10 +54,6 @@ std::map<std::string, std::string> OAuth1::signature(
const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _method,
const std::string& _url) { const std::string& _url) {
if (authType_ == AuthType::Bearer) {
return "Authorization: Bearer " + key_.bearerToken();
}
auto authParam = oauthParam(); auto authParam = oauthParam();
auto sigingParam = authParam; auto sigingParam = authParam;
if (!_bodyParam.empty()) { if (!_bodyParam.empty()) {
@@ -77,66 +78,67 @@ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string>
return oauthHeader; 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 // const std::string& OAuth1::generateBearerToken() {
CURL* curl; // auto signature = key_.consumerKey() + ":" + key_.consumerSecret();
CURLcode res; // auto k64Signature = base64(signature);
std::string rcv; // auto authHeader = std::string("Authorization: Basic ") + k64Signature;
long responseCode; // auto contentType =
curl = curl_easy_init(); // std::string("Content-Type: application/x-www-form-urlencoded;charset=UTF-8");
if (curl) { // auto url = std::string("https://api.twitter.com/oauth2/token");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // auto requestBody = std::string("grant_type=client_credentials");
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);
}
auto j = nlohmann::json::parse(rcv); // // do post
if ((responseCode / 100) == 4) { // CURL* curl;
auto error = j["errors"][0]["code"]; // CURLcode res;
auto message = j["errors"][0]["message"]; // std::string rcv;
if (j.count("error") != 0) { // long responseCode;
// この形式はエラーコードを持たないのでエラー種別が特定できない // curl = curl_easy_init();
throw new CocoaTweet::Exception::Exception(j["error"]); // if (curl) {
} // curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
if (error.get<int>() == 44) { // curl_easy_setopt(curl, CURLOPT_POST, 1);
throw CocoaTweet::Exception::InvalidParameterException( // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody.c_str());
message.get<std::string>().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"]); // auto j = nlohmann::json::parse(rcv);
authType_ = AuthType::Bearer; // if ((responseCode / 100) == 4) {
return key_.bearerToken(); // 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<int>() == 44) {
// throw CocoaTweet::Exception::InvalidParameterException(
// message.get<std::string>().c_str());
// }
// }
// key_.bearerToken(j["access_token"]);
// authType_ = AuthType::Bearer;
// return key_.bearerToken();
// }
const std::string OAuth1::nonce() const { const std::string OAuth1::nonce() const {
std::random_device engine; std::random_device engine;
@@ -156,16 +158,13 @@ const std::string OAuth1::timestamp() const {
} }
const std::string OAuth1::method() const { const std::string OAuth1::method() const {
return SIGNATURE_METHOD_; return "HMAC-SHA1";
} }
const std::string OAuth1::version() const { const std::string OAuth1::version() const {
return OAUTH_VERSION_; return "1.0";
} }
const Key OAuth1::key() const {
return key_;
}
std::map<std::string, std::string> OAuth1::oauthParam() const { std::map<std::string, std::string> OAuth1::oauthParam() const {
auto tmp = std::map<std::string, std::string>{{"oauth_nonce", nonce()}, auto tmp = std::map<std::string, std::string>{{"oauth_nonce", nonce()},
@@ -235,4 +234,4 @@ std::string OAuth1::hmacSha1(std::string _key, std::string _data) {
return static_cast<std::string>(k64); return static_cast<std::string>(k64);
} }
} // namespace CocoaTweet::OAuth } // namespace CocoaTweet::Authentication
@@ -5,19 +5,19 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include "key.h" #include "key.h"
#include <cocoatweet/authentication/authenticator.h>
namespace CocoaTweet::OAuth { namespace CocoaTweet::Authentication {
class OAuth1 { class OAuth1: public AuthenticatorBase {
public: public:
enum AuthType { OAuth, Bearer };
OAuth1(); OAuth1();
OAuth1(const Key _key); OAuth1(const Key _key);
std::map<std::string, std::string> signature(const std::map<std::string, std::string>& _param, std::map<std::string, std::string> signature(const std::map<std::string, std::string>& _param,
const std::string& _method, const std::string& _method,
const std::string& _url); const std::string& _url);
const std::string& generateBearerToken(); // const std::string& generateBearerToken();
const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _url); const std::string& _method, const std::string& _url);
@@ -25,22 +25,11 @@ public:
const std::string timestamp() const; const std::string timestamp() const;
const std::string method() const; const std::string method() const;
const std::string version() const; const std::string version() const;
const Key key() const;
std::map<std::string, std::string> oauthParam() const; std::map<std::string, std::string> oauthParam() const;
std::string hmacSha1(std::string _key, std::string _data); std::string hmacSha1(std::string _key, std::string _data);
const std::string base64(const std::string& _raw); 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 #endif
@@ -0,0 +1,33 @@
#ifndef COCOATWEET_AUTHENTICATION_AUTHENTICATORBASE_H
#define COCOATWEET_AUTHENTICATION_AUTHENTICATORBASE_H
#include <cocoatweet/authentication/key.h>
namespace CocoaTweet::Authentication{
class AuthenticatorBase{
public:
enum class AuthenticationMethod{
OAUTH10A,
OAUTH2,
PLAIN,
NONE
};
virtual const std::string calculateAuthHeader(std::map<std::string, std::string> _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
@@ -1,16 +1,19 @@
#include "cocoatweet/oauth/key.h" #include "cocoatweet/authentication/key.h"
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <typeinfo> #include <typeinfo>
namespace CocoaTweet::OAuth { namespace CocoaTweet::Authentication {
Key Key::fromJsonFile(const std::string _jsonFile) { Key Key::fromJsonFile(const std::string _jsonFile) {
std::ifstream ifs(_jsonFile); std::ifstream ifs(_jsonFile);
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
auto j = nlohmann::json::parse(str); auto j = nlohmann::json::parse(str);
Key key; Key key;
key.authType(Key::AUTH_TYPE::OAUTH10A);
if (j.contains("consumer_key")) { if (j.contains("consumer_key")) {
key.consumerKey(j["consumer_key"].get<std::string>()); key.consumerKey(j["consumer_key"].get<std::string>());
} }
@@ -29,4 +32,4 @@ Key Key::fromJsonFile(const std::string _jsonFile) {
return key; return key;
} }
} // namespace CocoaTweet::OAuth } // namespace CocoaTweet::Authentication
@@ -4,24 +4,32 @@
#include <string> #include <string>
#include <map> #include <map>
namespace CocoaTweet::OAuth { namespace CocoaTweet::Authentication {
class Key { class Key {
public:
enum AUTH_TYPE{
OAUTH10A,
OAUTH2,
PLAIN
};
private:
std::string consumerKey_; std::string consumerKey_;
std::string consumerSecret_; std::string consumerSecret_;
std::string accessToken_; std::string accessToken_;
std::string accessTokenSecret_; std::string accessTokenSecret_;
std::string bearerToken_; std::string bearerToken_;
AUTH_TYPE authType_;
public: public:
Key() : consumerKey_(""), consumerSecret_(""), accessToken_(""), accessTokenSecret_("") {} Key() : consumerKey_(""), consumerSecret_(""), accessToken_(""), accessTokenSecret_("") {}
Key(const std::string& _consumerKey, const std::string& _consumerSecret, 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), : consumerKey_(_consumerKey),
consumerSecret_(_consumerSecret), consumerSecret_(_consumerSecret),
accessToken_(_accessToken), accessToken_(_accessToken),
accessTokenSecret_(_accessTokenSecret) {} accessTokenSecret_(_accessTokenSecret), authType_(_authType) {}
Key(const std::string& _consumerKey, const std::string& _consumerSecret) Key(const std::string& _consumerKey, const std::string& _consumerSecret, const AUTH_TYPE _authType = AUTH_TYPE::OAUTH2)
: consumerKey_(_consumerKey), consumerSecret_(_consumerSecret) {} : consumerKey_(_consumerKey), consumerSecret_(_consumerSecret), authType_(_authType){}
void consumerKey(const std::string& _consumerKey) { void consumerKey(const std::string& _consumerKey) {
consumerKey_ = _consumerKey; consumerKey_ = _consumerKey;
@@ -59,6 +67,14 @@ public:
return bearerToken_; return bearerToken_;
} }
const AUTH_TYPE authType() const {
return authType_;
}
void authType(AUTH_TYPE _authType) {
authType_ = _authType;
}
std::map<std::string, std::string> noSecret() const { std::map<std::string, std::string> noSecret() const {
return std::map<std::string, std::string>{{"oauth_consumer_key", consumerKey_}, return std::map<std::string, std::string>{{"oauth_consumer_key", consumerKey_},
{"oauth_token", accessToken_}}; {"oauth_token", accessToken_}};
@@ -70,6 +86,6 @@ public:
static Key fromJsonFile(const std::string _jsonFile); static Key fromJsonFile(const std::string _jsonFile);
}; };
} // namespace CocoaTweet::OAuth } // namespace CocoaTweet::Authentication
#endif #endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef COCOATWEET_AUTHENTICATION_PLAIN_H
#define COCOATWEET_AUTHENTICATION_PLAIN_H
#include <cocoatweet/authentication/authenticator.h>
namespace CocoaTweet::Authentication{
class Plain: public AuthenticatorBase {
};
}
#endif
+22
View File
@@ -0,0 +1,22 @@
#ifndef COCOATWEET_OAUTH_AUTHORIZE_H_
#define COCOATWEET_OAUTH_AUTHORIZE_H_
#include "cocoatweet/api/interface/groupInterface.h"
#include <vector>
#include <utility>
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
+33
View File
@@ -1,6 +1,9 @@
#include "cocoatweet/util/util.h" #include "cocoatweet/util/util.h"
#include <cctype> #include <cctype>
#include <iomanip> #include <iomanip>
#include <vector>
#include <iostream>
namespace CocoaTweet::Util { namespace CocoaTweet::Util {
std::string urlEncode(const std::string& _str) { std::string urlEncode(const std::string& _str) {
@@ -33,4 +36,34 @@ std::string join(const std::vector<std::string> _vec, const std::string& _delim)
return str; return str;
} }
std::unordered_map<std::string, std::string> parse(const std::string str, const char _delim, const char _conn){
int first = 0;
int last = str.find_first_of(_delim);
std::vector<std::string> 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<std::string, std::string>();
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 } // namespace CocoaTweet::Util
+2
View File
@@ -4,10 +4,12 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <unordered_map>
namespace CocoaTweet::Util { namespace CocoaTweet::Util {
std::string urlEncode(const std::string& _str); std::string urlEncode(const std::string& _str);
std::string join(const std::vector<std::string> _vec, const std::string& _delim); std::string join(const std::vector<std::string> _vec, const std::string& _delim);
std::unordered_map<std::string, std::string> parse(const std::string str, const char _delim, const char _conn);
} // namespace CocoaTweet::Util } // namespace CocoaTweet::Util
#endif #endif
+3 -3
View File
@@ -2,11 +2,11 @@
#include <boost/test/included/unit_test.hpp> #include <boost/test/included/unit_test.hpp>
#include "cocoatweet/oauth/key.h" #include "cocoatweet/authentication/key.h"
BOOST_AUTO_TEST_SUITE(oauth_key) BOOST_AUTO_TEST_SUITE(oauth_key)
BOOST_AUTO_TEST_CASE(test01) { BOOST_AUTO_TEST_CASE(test01) {
CocoaTweet::OAuth::Key key; CocoaTweet::Authentication::Key key;
BOOST_TEST(key.consumerKey() == ""); BOOST_TEST(key.consumerKey() == "");
BOOST_TEST(key.consumerSecret() == ""); BOOST_TEST(key.consumerSecret() == "");
@@ -15,7 +15,7 @@ BOOST_AUTO_TEST_CASE(test01) {
} }
BOOST_AUTO_TEST_CASE(test02) { BOOST_AUTO_TEST_CASE(test02) {
CocoaTweet::OAuth::Key key("consumerKey", "consumerSecret", "accessToken", CocoaTweet::Authentication::Key key("consumerKey", "consumerSecret", "accessToken",
"accessTokenSecret"); "accessTokenSecret");
BOOST_TEST(key.consumerKey() == "consumerKey"); BOOST_TEST(key.consumerKey() == "consumerKey");