OAuth2が出来るようになったよ。ブランチの目的からめちゃくちゃズレてるね
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
#include "nlohmann/json.hpp"
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
|
||||
|
||||
namespace CocoaTweet::OAuth {
|
||||
Key Key::fromJsonFile(const std::string _jsonFile) {
|
||||
@@ -9,7 +11,26 @@ Key Key::fromJsonFile(const std::string _jsonFile) {
|
||||
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
||||
|
||||
auto j = nlohmann::json::parse(str);
|
||||
return Key(j["consumer_key"], j["consumer_secret"], j["access_token"],
|
||||
j["access_token_secret"]);
|
||||
Key key;
|
||||
if(j.contains("consumer_key")){
|
||||
key.consumerKey(j["consumer_key"].get<std::string>());
|
||||
}
|
||||
|
||||
|
||||
if(j.contains("consumer_secret")){
|
||||
key.consumerSecret(j["consumer_secret"].get<std::string>());
|
||||
}
|
||||
|
||||
|
||||
if(j.contains("access_token")){
|
||||
key.accessToken(j["access_token"].get<std::string>());
|
||||
}
|
||||
|
||||
|
||||
if(j.contains("access_token_secret")){
|
||||
key.accessTokenSecret(j["access_token_secret"].get<std::string>());
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
} // namespace CocoaTweet::OAuth
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
|
||||
namespace CocoaTweet::OAuth {
|
||||
class Key {
|
||||
const std::string consumerKey_;
|
||||
const std::string consumerSecret_;
|
||||
const std::string accessToken_;
|
||||
const std::string accessTokenSecret_;
|
||||
std::string consumerKey_;
|
||||
std::string consumerSecret_;
|
||||
std::string accessToken_;
|
||||
std::string accessTokenSecret_;
|
||||
std::string bearerToken_;
|
||||
|
||||
public:
|
||||
Key() : consumerKey_(""), consumerSecret_(""), accessToken_(""), accessTokenSecret_("") {}
|
||||
@@ -19,6 +20,27 @@ public:
|
||||
consumerSecret_(_consumerSecret),
|
||||
accessToken_(_accessToken),
|
||||
accessTokenSecret_(_accessTokenSecret) {}
|
||||
Key(const std::string& _consumerKey, const std::string& _consumerSecret): consumerKey_(_consumerKey), consumerSecret_(_consumerSecret){}
|
||||
|
||||
void consumerKey(const std::string& _consumerKey){
|
||||
consumerKey_ = _consumerKey;
|
||||
}
|
||||
|
||||
void consumerSecret(const std::string& _consumerSecret){
|
||||
consumerSecret_ = _consumerSecret;
|
||||
}
|
||||
|
||||
void accessToken(const std::string& _accessToken){
|
||||
accessToken_ = _accessToken;
|
||||
}
|
||||
|
||||
void accessTokenSecret(const std::string& _accessTokenSecret){
|
||||
accessTokenSecret_ = _accessTokenSecret;
|
||||
}
|
||||
|
||||
void bearerToken(const std::string& _bearer){
|
||||
bearerToken_ = _bearer;
|
||||
}
|
||||
const std::string& consumerKey() const {
|
||||
return consumerKey_;
|
||||
}
|
||||
@@ -31,6 +53,11 @@ public:
|
||||
const std::string& accessTokenSecret() const {
|
||||
return accessTokenSecret_;
|
||||
}
|
||||
|
||||
const std::string& bearerToken() const{
|
||||
return bearerToken_;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> noSecret() const {
|
||||
return std::map<std::string, std::string>{{"oauth_consumer_key", consumerKey_},
|
||||
{"oauth_token", accessToken_}};
|
||||
|
||||
@@ -7,17 +7,25 @@
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <cocoatweet/exception/invalidParameterException.h>
|
||||
|
||||
extern "C" {
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/buffer.h>
|
||||
#include <curl/curl.h>
|
||||
}
|
||||
|
||||
namespace CocoaTweet::OAuth {
|
||||
OAuth1::OAuth1() {}
|
||||
#ifndef NDEBUG
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
OAuth1::OAuth1(const Key _key) : key_(_key) {}
|
||||
namespace CocoaTweet::OAuth {
|
||||
OAuth1::OAuth1(): authType_(AuthType::OAuth) {}
|
||||
|
||||
OAuth1::OAuth1(const Key _key) : key_(_key), authType_(AuthType::OAuth) {}
|
||||
|
||||
std::map<std::string, std::string> OAuth1::signature(
|
||||
const std::map<std::string, std::string>& _param, const std::string& _method,
|
||||
@@ -38,6 +46,98 @@ std::map<std::string, std::string> OAuth1::signature(
|
||||
return ret;
|
||||
}
|
||||
|
||||
const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string> _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()) {
|
||||
for (const auto [k, v] : _bodyParam) {
|
||||
sigingParam.insert_or_assign(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
auto sign = signature(sigingParam, _method, _url);
|
||||
|
||||
authParam.merge(sign);
|
||||
// ヘッダの構築
|
||||
std::string oauthHeader = "authorization: OAuth ";
|
||||
{
|
||||
std::vector<std::string> tmp;
|
||||
for (const auto& [key, value] : authParam) {
|
||||
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
|
||||
}
|
||||
oauthHeader += CocoaTweet::Util::join(tmp, ",");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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<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 {
|
||||
std::random_device engine;
|
||||
std::string nonceTable = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
@@ -9,11 +9,22 @@
|
||||
namespace CocoaTweet::OAuth {
|
||||
class OAuth1 {
|
||||
public:
|
||||
|
||||
enum AuthType{
|
||||
OAuth,
|
||||
Bearer
|
||||
};
|
||||
|
||||
OAuth1();
|
||||
OAuth1(const Key _key);
|
||||
std::map<std::string, std::string> signature(const std::map<std::string, std::string>& _param,
|
||||
const std::string& _method,
|
||||
const std::string& _url);
|
||||
|
||||
const std::string& generateBearerToken();
|
||||
const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string& _method, const std::string& _url);
|
||||
|
||||
|
||||
const std::string nonce() const;
|
||||
const std::string timestamp() const;
|
||||
const std::string method() const;
|
||||
@@ -24,9 +35,16 @@ public:
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user