diff --git a/program/.gitignore b/program/.gitignore index e4bd8d46d7dd12733adaa6dd8fe2899166463931..c94585c0641fac34093700ff08518570f533a265 100644 --- a/program/.gitignore +++ b/program/.gitignore @@ -2,7 +2,5 @@ *.opensdf *.obj *.pdb -token.data -data -temp -cloud +*.sdf +*.token diff --git a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj index fb9203755cb3b12cdf36ff2c27cbeb4e97f8f066..f0408368d7b4aba61ecbd6ec6f261f306b35d5c7 100644 --- a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj +++ b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj @@ -61,8 +61,8 @@ <Link> <SubSystem>Console</SubSystem> <GenerateDebugInformation>true</GenerateDebugInformation> - <AdditionalDependencies>Crypt32.Lib;c:\KodoDependencies\AdditionalLibs\openSSL_Dbg\libeay32.lib;c:\KodoDependencies\AdditionalLibs\openSSL_Dbg\ssleay32.lib;%(AdditionalDependencies)</AdditionalDependencies> - <AdditionalLibraryDirectories>c:\KodoDependencies\AdditionalLibs\;c:\KodoDependencies\DependencyLIBS\</AdditionalLibraryDirectories> + <AdditionalDependencies>Crypt32.Lib;libeay32.lib;ssleay32.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalLibraryDirectories>c:\KodoDependencies\AdditionalLibs\;c:\KodoDependencies\DependencyLIBS\;c:\KodoDependencies\AdditionalLibs\openSSL_Dbg\</AdditionalLibraryDirectories> <ShowProgress>LinkVerbose</ShowProgress> </Link> </ItemDefinitionGroup> @@ -100,6 +100,8 @@ <ClCompile Include="..\..\Source\cloud\ExampleCloudAdapter.cpp" /> <ClCompile Include="..\..\Source\cloud\FTPAdapter.cpp" /> <ClCompile Include="..\..\Source\cloud\LocalCloudAdapter.cpp" /> + <ClCompile Include="..\..\Source\cloud\OneDriveAdapter.cpp" /> + <ClCompile Include="..\..\Source\cloud\RestAdapter.cpp" /> <ClCompile Include="..\..\Source\dataAccess\CloudFileList.cpp" /> <ClCompile Include="..\..\Source\dataAccess\CloudFileSystem.cpp" /> <ClCompile Include="..\..\Source\dataAccess\ComputeOnClient.cpp" /> @@ -136,6 +138,8 @@ <ClInclude Include="..\..\Source\cloud\ExampleCloudAdapter.h" /> <ClInclude Include="..\..\Source\cloud\FTPAdapter.h" /> <ClInclude Include="..\..\Source\cloud\LocalCloudAdapter.h" /> + <ClInclude Include="..\..\Source\cloud\OneDriveAdapter.h" /> + <ClInclude Include="..\..\Source\cloud\RestAdapter.h" /> <ClInclude Include="..\..\Source\dataAccess\CloudFileList.h" /> <ClInclude Include="..\..\Source\dataAccess\CloudFileSystem.h" /> <ClInclude Include="..\..\Source\dataAccess\ComputeOnClient.h" /> diff --git a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters index 8deec8fefe4d177adabf27bd2c8d384a1b3f662f..3bc54169970c6ad25a079abf00eecaf49db7f91c 100644 --- a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters +++ b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters @@ -156,6 +156,12 @@ <ClCompile Include="..\..\Source\cache\FIFOCache.cpp"> <Filter>Source Files\cache</Filter> </ClCompile> + <ClCompile Include="..\..\Source\cloud\OneDriveAdapter.cpp"> + <Filter>Source Files\cloud\adapter</Filter> + </ClCompile> + <ClCompile Include="..\..\Source\cloud\RestAdapter.cpp"> + <Filter>Source Files\cloud\adapter</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\..\Source\app\UI.h"> @@ -269,5 +275,11 @@ <ClInclude Include="..\..\Source\cache\CacheFile.h"> <Filter>Header Files\cache</Filter> </ClInclude> + <ClInclude Include="..\..\Source\cloud\OneDriveAdapter.h"> + <Filter>Header Files\cloud\adapter</Filter> + </ClInclude> + <ClInclude Include="..\..\Source\cloud\RestAdapter.h"> + <Filter>Header Files\cloud\adapter</Filter> + </ClInclude> </ItemGroup> </Project> \ No newline at end of file diff --git a/program/Source/app/Application.cpp b/program/Source/app/Application.cpp index cadc08e018a8f2bba0c4149e822a41fa1d4209f6..874c0a9dc1ad2b746ecb094eeeb8da41c2e4c4b2 100644 --- a/program/Source/app/Application.cpp +++ b/program/Source/app/Application.cpp @@ -8,6 +8,10 @@ #include "../cache/FIFOCache.h" #include "../cache/LRUCache.h" #include "../cache/LFUCache.h" +#include "../cloud/OneDriveAdapter.h" + +#define WITH_MEASURE + using namespace std; @@ -37,9 +41,10 @@ int main(int argc, char **argv) { Application application; - application.cm.addCloud(make_shared<LocalCloudAdapter>()); + //application.cm.addCloud(make_shared<LocalCloudAdapter>()); application.cm.addCloud(make_shared<DropboxAdapter>()); - application.cm.addCloud(make_shared<FTPAdapter>()); + application.cm.addCloud(make_shared<OneDriveAdapter>()); + //application.cm.addCloud(make_shared<FTPAdapter>()); string dataFolder = "C:\\Users\\krisz\\raspberrycloud\\skeleton\\Program\\data\\"; string fragFolder = "C:\\Users\\krisz\\raspberrycloud\\skeleton\\Program\\temp\\"; @@ -70,31 +75,30 @@ int main(int argc, char **argv) { application.cfs.getDecoder()->setFolders(dataFolder,fragFolder); application.cfs.setFolders(fragFolder); - bool waitAnswer = true; - Cache *cacheToSetup; - while (waitAnswer){ - cout << "Please select caching method (1: no, 2: FIFO, 3: LRU, 4: LFU)" << endl; - int answer = 0; - cin >> answer; + Cache *cacheToSetup = nullptr; + string strAnswer; + int answer = 0; + + while (cacheToSetup == nullptr){ + std::cout << "Please select caching method (1: no, 2: FIFO, 3: LRU, 4: LFU)" << endl; + getline(cin, strAnswer); + + answer = atoi(strAnswer.c_str()); int cacheSize = 3; switch (answer){ case 1: cacheToSetup = new NoCache(); - waitAnswer = false; break; case 2: cacheToSetup = new FIFOCache(cacheSize); - waitAnswer = false; break; case 3: cacheToSetup = new LRUCache(cacheSize); - waitAnswer = false; break; case 4: cacheToSetup = new LFUCache(cacheSize); - waitAnswer = false; break; default: break; @@ -157,6 +161,10 @@ int main(int argc, char **argv) { cout << "Please give a new order!" << endl; cin >> command; +#ifdef WITH_MEASURE + clock_t start = 0; +#endif + if (command == "help"){ cout << "To stop the application enter \"exit\"." << endl; cout << "To add a new file enter \"addFile\"." << endl; @@ -164,7 +172,7 @@ int main(int argc, char **argv) { cout << "To get a file enter \"getFile\"." << endl; cout << "To get the avaliable files enter \"getFileList\"." << endl; cout << "To delete a file enter \"deleteFile\"." << endl; - cout << "To authorize a cloud adapter enter \"authCA\"." << endl; + cout << "To authorize a cloud adapter enter \"auth\"." << endl; cout << "To start or stop the server enter \"server\"." << endl; } else if (command == "addFile"){ @@ -174,6 +182,9 @@ int main(int argc, char **argv) { cin >> localid; cout << "Adding file. Please enter destination file id" << endl; cin >> destid; +#ifdef WITH_MEASURE + start = clock(); +#endif cout << application.ui.addFile(localid, destid) << endl; } else if (command == "createDirectory"){ @@ -186,6 +197,9 @@ int main(int argc, char **argv) { cout << "Getting file. Please enter file id" << endl; string fileid; cin >> fileid; +#ifdef WITH_MEASURE + start = clock(); +#endif FileDescriptor fileDesc = FileDescriptor(fileid); ReturnableFile returned = application.ui.getFile(fileDesc); if (returned.isValid()) { @@ -213,7 +227,7 @@ int main(int argc, char **argv) { FileDescriptor fileDesc = FileDescriptor(fileid); cout << application.ui.deleteFile(fileDesc) << endl; } - else if (command == "authCA") + else if (command == "auth") { cout << "Enter cloud adapter ID (eg. \"dropboxAdapter\")" << endl; string cloudAdapterId; @@ -238,6 +252,12 @@ int main(int argc, char **argv) { else { cout << "Invalid command." << endl; } + +#ifdef WITH_MEASURE + auto stop = clock(); + if (start != 0) + cout << "Completed in: " << double(stop - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl; +#endif } return 0; diff --git a/program/Source/cloud/CloudAdapter.h b/program/Source/cloud/CloudAdapter.h index c37715002300606400ccafbf7e3567aab891f775..54ae9c46864c2fbf554a99d024686d4175b053dd 100644 --- a/program/Source/cloud/CloudAdapter.h +++ b/program/Source/cloud/CloudAdapter.h @@ -19,6 +19,7 @@ class CloudAdapter { protected: std::string fragFolder; std::string cloudFolder; + public: std::string cloudID; @@ -31,7 +32,7 @@ public: * CloudApater constructor * @param _cloudID the ID of the cloud that is implemented */ - CloudAdapter(std::string _cloudID); + explicit CloudAdapter(std::string _cloudID); virtual ~CloudAdapter() {} diff --git a/program/Source/cloud/DropboxAdapter.cpp b/program/Source/cloud/DropboxAdapter.cpp index ceb3f74ae45766227664dd90ce4096f30478e1ba..cc881f203d2022ac88d7aafc7bdefbf0a00ab3d8 100644 --- a/program/Source/cloud/DropboxAdapter.cpp +++ b/program/Source/cloud/DropboxAdapter.cpp @@ -20,25 +20,7 @@ using namespace Poco; using namespace boost::filesystem; using namespace std; -DropboxAdapter::DropboxAdapter(string _cloudID) : CloudAdapter(_cloudID) { LOG_ENTER_EXIT; } - -bool DropboxAdapter::signHeader(HTTPRequest& request) -{ - ifstream tokenFile("token.data", ifstream::in); - if (!tokenFile.good()) - return false; - - string token; - getline(tokenFile, token); - - if (token == "") - return false; - - OAuth20Credentials cred(token); - cred.authenticate(request); - - return true; -} +DropboxAdapter::DropboxAdapter(string _cloudID) : RestAdapter(_cloudID) { LOG_ENTER_EXIT; } bool DropboxAdapter::upload(CloudFile cloudFile) { LOG_ENTER_EXIT; @@ -89,7 +71,7 @@ bool DropboxAdapter::upload(CloudFile cloudFile) { // delete local fragmant in.close(); - remove(localFile.c_str()); + //remove(localFile.c_str()); return true; } diff --git a/program/Source/cloud/DropboxAdapter.h b/program/Source/cloud/DropboxAdapter.h index 93b3bbb112a222d67eb308267b2ea4b6776ff5c3..5ad3b2d0ace925818bbb9d18babe0d03a7549c6d 100644 --- a/program/Source/cloud/DropboxAdapter.h +++ b/program/Source/cloud/DropboxAdapter.h @@ -3,7 +3,7 @@ #include <string> #include "../fileModel/CloudFile.h" -#include "../cloud/CloudAdapter.h" +#include "../cloud/RestAdapter.h" #include "../fileModel/Fragment.h" #include <Poco/Net/HTTPRequest.h> #include <Poco/Net/Context.h> @@ -11,7 +11,7 @@ /** * DropboxAdapter class */ -class DropboxAdapter : public CloudAdapter { +class DropboxAdapter : public RestAdapter { const std::string contentHost = "content.dropboxapi.com"; const std::string apiHost = "api.dropboxapi.com"; @@ -26,15 +26,11 @@ class DropboxAdapter : public CloudAdapter { const std::string appSecret = "t0tktamnuj2y6bd"; const std::string authCodeType = "authorization_code"; - bool signHeader(Poco::Net::HTTPRequest& request); - static Poco::Net::Context::Ptr getContext() { return new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "rootcert.pem"); } public: - std::string cloudID; - explicit DropboxAdapter(std::string _cloudID = "dropboxAdapter"); /** diff --git a/program/Source/cloud/ExampleCloudAdapter.h b/program/Source/cloud/ExampleCloudAdapter.h index dc8e27fa382d320a860a6842a93d2942b2a8adb3..1d1cf4a550097b6d8510432b10ac3656b446b19c 100644 --- a/program/Source/cloud/ExampleCloudAdapter.h +++ b/program/Source/cloud/ExampleCloudAdapter.h @@ -14,8 +14,6 @@ */ class ExampleCloudAdapter : CloudAdapter { public: - std::string cloudID; - ExampleCloudAdapter(std::string _cloudID); /** diff --git a/program/Source/cloud/FTPAdapter.h b/program/Source/cloud/FTPAdapter.h index 19bd51ef864b87202dd4171ac1abb66453e67cdc..cb1d9abb3dd73f422d2183a217da47ef03c837eb 100644 --- a/program/Source/cloud/FTPAdapter.h +++ b/program/Source/cloud/FTPAdapter.h @@ -16,8 +16,6 @@ class FTPAdapter : public CloudAdapter { static const std::string ROOT; public: - std::string cloudID; - explicit FTPAdapter(std::string _cloudID = "FTPAdapter"); /** diff --git a/program/Source/cloud/OneDriveAdapter.cpp b/program/Source/cloud/OneDriveAdapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b157ecc24f036b7d13726eb2f8301eace6a1ae78 --- /dev/null +++ b/program/Source/cloud/OneDriveAdapter.cpp @@ -0,0 +1,254 @@ +#include "OneDriveAdapter.h" +#include "../logger/Logger.h" +#include <fstream> +#include <Poco/Net/FTPClientSession.h> +#include <Poco/Net/HTTPSClientSession.h> +#include <Poco/Net/HTTPResponse.h> +#include <Poco/Net/HTTPRequest.h> +#include <Poco/Net/OAuth20Credentials.h> +#include <Poco/JSON/Parser.h> +#include <Poco/JSON/Object.h> +#include <Poco/Net/Context.h> +#include <Poco/Dynamic/Var.h> +#include <Poco/Exception.h> +#include <Poco/StreamCopier.h> +#include <iostream> +#include <memory> +#include <boost/filesystem.hpp> +#include <Poco/Net/SSLManager.h> +#include <Poco/Net/HTTPSStreamFactory.h> +#include <Poco/URIStreamOpener.h> + +using namespace Poco::Net; +using namespace Poco; +using namespace boost::filesystem; +using namespace std; + +OneDriveAdapter::OneDriveAdapter(string _cloudID) : RestAdapter(_cloudID) +{ + LOG_ENTER_EXIT; + HTTPSStreamFactory::registerFactory(); +} + +bool OneDriveAdapter::upload(CloudFile cloudFile) { + LOG_ENTER_EXIT; + + auto context = getContext(); + + // Get file + auto localFile(fragFolder + cloudFile.getCloudFileID()); + + if (!exists(localFile)) + return false; // If file can't be opened + + // Prepare for upload + auto requestPath(appRoot + cloudFile.getCloudFileID() + content); + HTTPSClientSession session(apiHost, HTTPSClientSession::HTTPS_PORT, context); + HTTPRequest request(HTTPRequest::HTTP_PUT, requestPath, HTTPMessage::HTTP_1_1); + HTTPResponse response; + signHeader(request); + request.setContentLength(file_size(localFile)); + + // Do upload + try + { + ifstream in(localFile, ios::binary); + auto& rs = session.sendRequest(request); + + StreamCopier::copyStream(in, rs); + + auto& responseStream = session.receiveResponse(response); + if (response.getStatus() != HTTPResponse::HTTP_CREATED && + response.getStatus() != HTTPResponse::HTTP_OK) + { + StreamCopier::copyStream(responseStream, cerr); + return false; + } + } + catch (Exception& e) + { + cerr << e.message() << endl; + return false; + } + + // delete local fragmant + //remove(localFile.c_str()); + + return true; +} + +shared_ptr<Fragment> OneDriveAdapter::download(CloudFile cloudFile) { + LOG_ENTER_EXIT; + + auto context = getContext(); + + const auto requestPath(appRoot + cloudFile.getCloudFileID() + content); + HTTPSClientSession session(apiHost, HTTPSClientSession::HTTPS_PORT, context); + HTTPRequest request(HTTPRequest::HTTP_GET, requestPath, HTTPMessage::HTTP_1_1); + HTTPResponse response; + signHeader(request); + + URI downloadUri; + + try + { + session.sendRequest(request); + session.receiveResponse(response); + + if (response.getStatus() != HTTPResponse::HTTP_FOUND) + return nullptr; + + downloadUri = URI(response.get("Location")); + } + catch (Exception& e) + { + cerr << e.message() << endl; + return nullptr; + } + + path localFilePath = fragFolder + cloudFile.getCloudFileID(); + + if (!exists(localFilePath.parent_path())) + { + create_directories(localFilePath.parent_path()); + } + + ofstream file(localFilePath.string(), ios::binary); + + SSLManager::instance().initializeClient(nullptr, nullptr, context); + + std::auto_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(downloadUri)); + StreamCopier::copyStream(*pStr.get(), file); + + return make_shared<Fragment>(cloudFile); +} + +bool OneDriveAdapter::deleteFile(CloudFile cloudFile) { + LOG_ENTER_EXIT; + + auto context = getContext(); + + const auto requestPath(appRoot + cloudFile.getCloudFileID()); + HTTPSClientSession session(apiHost, HTTPSClientSession::HTTPS_PORT, context); + HTTPRequest request(HTTPRequest::HTTP_DELETE, requestPath, HTTPMessage::HTTP_1_1); + HTTPResponse response; + + signHeader(request); + try + { + session.sendRequest(request); + } + catch (Exception& e) + { + cerr << e.message() << endl; + return false; + } + session.receiveResponse(response); + + return response.getStatus() == HTTPResponse::HTTP_NO_CONTENT; +} + +bool OneDriveAdapter::auth() { + LOG_ENTER_EXIT; + + auto context = getContext(); + + HTTPSClientSession session(tokenApi.getHost(), HTTPSClientSession::HTTPS_PORT, context); + HTTPRequest request(HTTPRequest::HTTP_POST, tokenApi.getPath(), HTTPMessage::HTTP_1_1); + HTTPResponse response; + + string authCode; + + cout << "Please visit the following URL and \"Allow\" the app!" << endl; + cout << "https://login.live.com/oauth20_authorize.srf?client_id=" << appKey << "&scope=onedrive.appfolder&response_type=code&redirect_uri=http://users.hszk.bme.hu/~sm1206/raspberrycloud/" << endl; + cout << "the auth code: "; + cin >> authCode; + + auto body( + "code=" + authCode + + "&grant_type=" + authCodeType + + "&client_id=" + appKey + + "&client_secret=" + appSecret + + "&redirect_uri=http://users.hszk.bme.hu/~sm1206/raspberrycloud/"); + request.setContentLength(body.length()); + request.setContentType("application/x-www-form-urlencoded"); + + Dynamic::Var responseVar; + + try + { + session.sendRequest(request) << body; + auto& rs = session.receiveResponse(response); + + if (response.getStatus() != HTTPResponse::HTTP_OK) + { + StreamCopier::copyStream(rs, cout); + return false; + } + + JSON::Parser parser; + responseVar = (parser.parse(rs)); + } + catch (Exception& e) + { + cerr << e.message() << endl; + return false; + } + + auto authResponse = responseVar.extract<JSON::Object::Ptr>(); + + ofstream(cloudID + ".token", ios::out) << (authResponse->getValue<string>("access_token")); + + return true; +} + +long OneDriveAdapter::getSize(CloudFile cloudFile) { + LOG_ENTER_EXIT; + + auto context = getContext(); + + string requestPath(appRoot + cloudFile.getCloudFileID()); + + HTTPSClientSession session(apiHost, HTTPSClientSession::HTTPS_PORT, context); + HTTPRequest request(HTTPRequest::HTTP_GET, requestPath, HTTPMessage::HTTP_1_1); + HTTPResponse response; + + Dynamic::Var responseVar; + + if (!signHeader(request)) + { + return -1; + } + + try + { + session.sendRequest(request); + auto& rs = session.receiveResponse(response); + + if (response.getStatus() != HTTPResponse::HTTP_OK) + { + //StreamCopier::copyStream(rs, cout); + return -1; + } + + JSON::Parser parser; + responseVar = parser.parse(rs); + } + catch (Exception& e) + { + cerr << e.message() << endl; + return -1; + } + + auto authResponse = responseVar.extract<JSON::Object::Ptr>(); + + if (authResponse->has("deleted") && authResponse->getValue<bool>("deleted")) + { + return -1; + } + return authResponse->getValue<long long int>("size"); +} + +bool OneDriveAdapter::checkExists(CloudFile cloudFile){ + return getSize(cloudFile) >= 0; +} \ No newline at end of file diff --git a/program/Source/cloud/OneDriveAdapter.h b/program/Source/cloud/OneDriveAdapter.h new file mode 100644 index 0000000000000000000000000000000000000000..f91f02908f69e30229df4d4b4d1a7e3652555a28 --- /dev/null +++ b/program/Source/cloud/OneDriveAdapter.h @@ -0,0 +1,75 @@ +#ifndef _ONEDRIVEADAPTER_H +#define _ONEDRIVEADAPTER_H + +#include <string> +#include "../fileModel/CloudFile.h" +#include "../cloud/RestAdapter.h" +#include "../fileModel/Fragment.h" +#include <Poco/URI.h> +#include <Poco/Net/Context.h> + +/** +* OneDriveAdapter class +*/ +class OneDriveAdapter : public RestAdapter { + const std::string apiHost = "api.onedrive.com"; + + const std::string content = ":/content"; + const std::string appRoot = "/v1.0/drive/special/approot:/"; + + const Poco::URI tokenApi = Poco::URI("https://login.live.com/oauth20_token.srf"); + + const std::string appKey = "0000000040179EA1"; + const std::string appSecret = "gBy-QbN0a6WOKK2KBmYLlDHNRurCerW2"; + const std::string authCodeType = "authorization_code"; + + static Poco::Net::Context::Ptr getContext() + { + return new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, "", "", "rootcert.pem"); + } +public: + explicit OneDriveAdapter(std::string _cloudID = "oneDriveAdapter"); + + /** + * Uploads a file to the cloud + * @param cloudFile file to upload + * @return successful + */ + bool upload(CloudFile cloudFile) override; + + /** + * Downloads a file from the cloud + * @param cloudFile file to download + * @return a downloaded file-fragment + */ + std::shared_ptr<Fragment> download(CloudFile cloudFile) override; + + /** + * Deletes a file from the cloud + * @param cloudFile file to download + * @return successful + */ + bool deleteFile(CloudFile cloudFile) override; + + /** + * Established authentication + * @return successful + */ + bool auth() override; + + /** + * Measures a files size on the cloud + * @param cloudFile file to measure + * @return size of file + */ + long getSize(CloudFile cloudFile) override; + + /** + * Checks if the file exists. + * @param cloudFile descriptor of the file to check + * @return true if the file exists, false otherwise + */ + bool checkExists(CloudFile cloudFile) override; +}; + +#endif //_ONEDRIVEADAPTER_H \ No newline at end of file diff --git a/program/Source/cloud/RestAdapter.cpp b/program/Source/cloud/RestAdapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fcce8ef8f0e9b655f008b9e54d5333ff50f8f65 --- /dev/null +++ b/program/Source/cloud/RestAdapter.cpp @@ -0,0 +1,24 @@ +#include "RestAdapter.h" +#include <Poco/Net/HTTPRequest.h> +#include <Poco/Net/OAuth20Credentials.h> +#include <fstream> + +using namespace std; + +bool RestAdapter::signHeader(Poco::Net::HTTPRequest& request) const +{ + ifstream tokenFile(cloudID + ".token", ifstream::in); + if (!tokenFile.good()) + return false; + + string token; + getline(tokenFile, token); + + if (token == "") + return false; + + Poco::Net::OAuth20Credentials cred(token); + cred.authenticate(request); + + return true; +} \ No newline at end of file diff --git a/program/Source/cloud/RestAdapter.h b/program/Source/cloud/RestAdapter.h new file mode 100644 index 0000000000000000000000000000000000000000..96a72073823cfd07876a42c80ad036af97bf468d --- /dev/null +++ b/program/Source/cloud/RestAdapter.h @@ -0,0 +1,19 @@ +#ifndef _RESTADAPTER_H +#define _RESTADAPTER_H + +#include "CloudAdapter.h" + +namespace Poco{namespace Net{ + class HTTPRequest; +}} + +class RestAdapter : public CloudAdapter +{ +protected: + bool signHeader(Poco::Net::HTTPRequest& request) const; + +public: + explicit RestAdapter(std::string _cloudID) : CloudAdapter(_cloudID) {}; +}; + +#endif