basic認証用のプロバイダとベアラトークンを取得するためのエンドポイントアクセスを有効に

This commit is contained in:
keita
2022-04-23 18:00:41 +09:00
parent 560fe707a6
commit d99ba765aa
17 changed files with 300 additions and 67 deletions
+8 -2
View File
@@ -1,5 +1,7 @@
#include <cocoatweet/api/api.h>
#include <cocoatweet/authentication/authenticate.h>
#include <cocoatweet/authentication/bearer.h>
#include <cocoatweet/authentication/plain.h>
#include <iostream>
@@ -12,8 +14,8 @@ API::API(CocoaTweet::Authentication::Key _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{
}else if(_key.authType() == CocoaTweet::Authentication::Key::OAUTH2){
oauth_ = std::make_shared<CocoaTweet::Authentication::Bearer>(_key);
}
user_ = Users::User(oauth_);
status_ = Statuses::Status(oauth_);
@@ -21,6 +23,7 @@ void API::swapKey(const CocoaTweet::Authentication::Key _key){
media_ = Medias::Media(oauth_);
directMessage_ = DirectMessages::DirectMessage(oauth_);
oauth1_ = OAuth1::OAuth(oauth_);
oauth2_ = OAuth2::OAuth2(oauth_);
}
// const std::string& API::generateBearerToken() const {
@@ -49,4 +52,7 @@ DirectMessages::DirectMessage API::directMessage() const {
OAuth1::OAuth API::oauth1() const {
return oauth1_;
}
OAuth2::OAuth2 API::oauth2() const {
return oauth2_;
}
} // namespace CocoaTweet::API
+3
View File
@@ -8,6 +8,7 @@
#include <cocoatweet/api/directMessage/directMessage.h>
#include <cocoatweet/authentication/authenticator.h>
#include <cocoatweet/api/oauth1/oauth.h>
#include <cocoatweet/api/oauth2/oauth2.h>
namespace CocoaTweet::API {
/// @brief Twitter API Entry Point
@@ -32,6 +33,7 @@ public:
DirectMessages::DirectMessage directMessage() const;
OAuth1::OAuth oauth1() const;
OAuth2::OAuth2 oauth2() const;
const std::string& generateBearerToken() const;
void swapKey(const CocoaTweet::Authentication::Key _key);
@@ -43,6 +45,7 @@ private:
Medias::Media media_;
DirectMessages::DirectMessage directMessage_;
OAuth1::OAuth oauth1_;
OAuth2::OAuth2 oauth2_;
std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> oauth_;
};
} // namespace CocoaTweet::API
+6
View File
@@ -6,6 +6,8 @@
#include <cocoatweet/exception/tweetTooLongException.h>
#include <cocoatweet/exception/rateLimitException.h>
#include <cocoatweet/exception/tokenInvalidException.h>
#include <cocoatweet/exception/missingRequiredParamException.h>
#include <cocoatweet/exception/credentialNotAllowedException.h>
#include "nlohmann/json.hpp"
#include <iterator>
#include <memory>
@@ -128,6 +130,10 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBas
throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str());
} else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
}else if(error.get<int>() == 170){
throw CocoaTweet::Exception::MissingRequiredParamException(message.get<std::string>().c_str());
}else if(error.get<int>() == 220){
throw CocoaTweet::Exception::CredentialNotAllowedException(message.get<std::string>().c_str());
} else {
}
}
+6
View File
@@ -6,6 +6,8 @@
#include <cocoatweet/exception/tweetTooLongException.h>
#include <cocoatweet/exception/rateLimitException.h>
#include <cocoatweet/exception/tokenInvalidException.h>
#include <cocoatweet/exception/missingRequiredParamException.h>
#include <cocoatweet/exception/credentialNotAllowedException.h>
#include "nlohmann/json.hpp"
#include <iterator>
#include <memory>
@@ -146,6 +148,10 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBa
throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str());
} else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
}else if(error.get<int>() == 170){
throw CocoaTweet::Exception::MissingRequiredParamException(message.get<std::string>().c_str());
}else if(error.get<int>() == 220){
throw CocoaTweet::Exception::CredentialNotAllowedException(message.get<std::string>().c_str());
}
}
+16
View File
@@ -0,0 +1,16 @@
#include <cocoatweet/api/oauth2/oauth2.h>
#include <cocoatweet/authentication/basic.h>
namespace CocoaTweet::API::OAuth2 {
OAuth2::OAuth2(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth){
oauth_ = _oauth;
}
const std::string OAuth2::token() const{
auto key = oauth_.lock()->key();
auto oauth = std::make_shared<CocoaTweet::Authentication::Basic>(key);
CocoaTweet::API::OAuth2::Token token;
return token.process(oauth);
}
} // namespace CocoaTweet::API::Statuses
+26
View File
@@ -0,0 +1,26 @@
#ifndef COCOATWEET_API_OAUTH2_OAUTH2_H_
#define COCOATWEET_API_OAUTH2_OAUTH2_H_
#include "cocoatweet/api/interface/groupInterface.h"
#include <cocoatweet/api/oauth2/token.h>
#include <cocoatweet/api/model/oauthToken.h>
#include <vector>
#include <utility>
namespace CocoaTweet::API::OAuth2 {
/// @brief class for using users/show endpoint
class OAuth2 : public groupInterface {
public:
/// @brief primary constructor
OAuth2() = default;
/// @brief constructor which finally should to be called.
/// @param[in] std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> : pointer to OAuth object
OAuth2(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
const std::string token() const;
};
} // namespace CocoaTweet::API::Statuses
#endif
+25
View File
@@ -0,0 +1,25 @@
#include <cocoatweet/api/oauth2/token.h>
#include "nlohmann/json.hpp"
namespace CocoaTweet::API::OAuth2{
Token::Token(){
contentType_ = "application/x-www-form-urlencoded";
url_ = "https://api.twitter.com/oauth2/token";
bodyParam_.insert_or_assign("grant_type", "client_credentials");
}
const std::string Token::process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
auto basic = std::make_shared<CocoaTweet::Authentication::Basic>(_oauth.lock()->key());
std::string bearer = "";
HttpPost::process(basic, [&bearer](const std::string& _rcv) {
auto j = nlohmann::json::parse(_rcv);
bearer = j["access_token"];
});
return bearer;
}
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef COCOATWEET_API_OAUTH2_TOKEN_H
#define COCOATWEET_API_OAUTH2_TOKEN_H
#include <cocoatweet/api/interface/httpPost.h>
#include <cocoatweet/authentication/basic.h>
namespace CocoaTweet::API::OAuth2{
class Token: public CocoaTweet::API::Interface::HttpPost{
public:
Token();
const std::string process(std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
};
}
#endif