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
@@ -78,68 +78,6 @@ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string>
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";
@@ -8,6 +8,7 @@ public:
enum class AuthenticationMethod{
OAUTH10A,
OAUTH2,
BASIC,
PLAIN,
NONE
};
+77
View File
@@ -0,0 +1,77 @@
#include "basic.h"
#include "cocoatweet/util/util.h"
#include <random>
#include <ctime>
#include <bitset>
#include <sstream>
#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>
}
#ifndef NDEBUG
#include <iostream>
#endif
namespace CocoaTweet::Authentication {
Basic::Basic() {
method_ = AuthenticationMethod::BASIC;
}
Basic::Basic(const Key _key){
key_ = _key;
method_ = AuthenticationMethod::BASIC;
}
const std::string Basic::calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method,
const std::string& _url) {
auto signature = key_.consumerKey() + ":" + key_.consumerSecret();
auto k64Signature = base64(signature);
auto authHeader = std::string("Authorization: Basic ") + k64Signature;
return authHeader;
}
const std::string Basic::base64(const std::string& _raw) {
auto base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::stringstream ss;
for (auto r : _raw) {
ss << std::bitset<8>(r);
}
if (_raw.length() % 3 == 1) {
ss << "0000";
} else if (_raw.length() % 3 == 2) {
ss << "00";
}
auto bin = ss.str();
std::string base64 = "";
for (auto i = 0; i < bin.length() / 6; i++) {
base64 += base64Table[std::stoi(bin.substr(i * 6, 6), nullptr, 2)];
}
if (base64.length() % 4 == 3) {
base64 += "=";
} else if (base64.length() % 4 == 2) {
base64 += "==";
} else if (base64.length() % 4 == 1) {
base64 += "===";
}
return base64;
}
} // namespace CocoaTweet::Authentication
+25
View File
@@ -0,0 +1,25 @@
#ifndef COCOATWEET_AUTHENTICATION_BASIC_H_
#define COCOATWEET_AUTHENTICATION_BASIC_H_
#include <string>
#include <map>
#include <memory>
#include "key.h"
#include <cocoatweet/authentication/authenticator.h>
namespace CocoaTweet::Authentication {
class Basic: public AuthenticatorBase {
public:
Basic();
Basic(const Key _key);
// 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 base64(const std::string& _raw);
};
} // namespace CocoaTweet::Authentication
#endif
+44
View File
@@ -0,0 +1,44 @@
#include "bearer.h"
#include "cocoatweet/util/util.h"
#include <random>
#include <ctime>
#include <bitset>
#include <sstream>
#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>
}
#ifndef NDEBUG
#include <iostream>
#endif
namespace CocoaTweet::Authentication {
Bearer::Bearer() {
method_ = AuthenticationMethod::OAUTH2;
}
Bearer::Bearer(const Key _key){
key_ = _key;
method_ = AuthenticationMethod::OAUTH2;
}
const std::string Bearer::calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method,
const std::string& _url) {
auto authHeader = std::string("Authorization: Bearer ") + key_.bearerToken();
return authHeader;
}
} // namespace CocoaTweet::Authentication
+22
View File
@@ -0,0 +1,22 @@
#ifndef COCOATWEET_AUTHENTICATION_BEARER_H_
#define COCOATWEET_AUTHENTICATION_BEARER_H_
#include <string>
#include <map>
#include <memory>
#include "key.h"
#include <cocoatweet/authentication/authenticator.h>
namespace CocoaTweet::Authentication {
class Bearer: public AuthenticatorBase {
public:
Bearer();
Bearer(const Key _key);
const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _url);
};
} // namespace CocoaTweet::Authentication
#endif
+2 -3
View File
@@ -9,8 +9,7 @@ class Key {
public:
enum AUTH_TYPE{
OAUTH10A,
OAUTH2,
PLAIN
OAUTH2
};
private:
@@ -70,7 +69,7 @@ public:
const AUTH_TYPE authType() const {
return authType_;
}
void authType(AUTH_TYPE _authType) {
authType_ = _authType;
}