Added github and samsung authcode providers in static lib iot-es demo
authorAndriy Gudz <a.gudz@samsung.com>
Fri, 21 Apr 2017 11:19:48 +0000 (14:19 +0300)
committerAndriy Gudz <a.gudz@samsung.com>
Fri, 21 Apr 2017 11:19:48 +0000 (14:19 +0300)
demo/iot-es/src/authcode/CMakeLists.txt [new file with mode: 0644]
demo/iot-es/src/authcode/authcodeprovider.h [new file with mode: 0644]
demo/iot-es/src/authcode/githubauthcodeprovider.cpp [new file with mode: 0644]
demo/iot-es/src/authcode/samsungauthcodeprovider.cpp [new file with mode: 0644]

diff --git a/demo/iot-es/src/authcode/CMakeLists.txt b/demo/iot-es/src/authcode/CMakeLists.txt
new file mode 100644 (file)
index 0000000..7db90a1
--- /dev/null
@@ -0,0 +1,8 @@
+get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
+project(${ProjectId})
+
+FILE(GLOB SRCS *.cpp)
+
+add_library(${PROJECT_NAME} ${SRCS})
+
+target_link_libraries(${PROJECT_NAME} curl)
diff --git a/demo/iot-es/src/authcode/authcodeprovider.h b/demo/iot-es/src/authcode/authcodeprovider.h
new file mode 100644 (file)
index 0000000..7b703e8
--- /dev/null
@@ -0,0 +1,47 @@
+/**
+ * @brief  TODO
+ * @date   Created 20.04.2017
+ * @author Created 2017 in Samsung Ukraine R&D Center (SURC) under a contract
+ *                between LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
+ *                and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea).
+ *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ * @author Mail to: <A HREF="mailto:a.gudz@samsung.com">Andriy Gudz, a.gudz@samsung.com</A>
+ */
+#ifndef AUTHCODEPROVIDER_H
+#define AUTHCODEPROVIDER_H
+
+#include <string>
+
+class AuthCodeProvider
+{
+public:
+    /**
+     * @brief getAuthCode Returns intermediate auth_code in OAuth authorization process. Ex: "bf9beb5db17ea476fa46".
+     * @param login user login for current provider
+     * @param password user password for current provider
+     * @return auth_code string
+     */
+    virtual std::string getAuthCode(std::string login, std::string password) = 0;
+
+    /**
+     * @brief getName Returns provider name
+     * @return provider name
+     */
+    virtual std::string getName() = 0;
+};
+
+class GithubAuthCodeProvider: public AuthCodeProvider
+{
+public:
+    virtual std::string getAuthCode(std::string login, std::string password);
+    virtual std::string getName();
+};
+
+class SamsungAuthCodeProvider: public AuthCodeProvider
+{
+public:
+    virtual std::string getAuthCode(std::string login, std::string password);
+    virtual std::string getName();
+};
+
+#endif // AUTHCODEPROVIDER_H
diff --git a/demo/iot-es/src/authcode/githubauthcodeprovider.cpp b/demo/iot-es/src/authcode/githubauthcodeprovider.cpp
new file mode 100644 (file)
index 0000000..e74aa1c
--- /dev/null
@@ -0,0 +1,112 @@
+/**
+ * @brief  TODO
+ * @date   Created 20.04.2017
+ * @author Created 2017 in Samsung Ukraine R&D Center (SURC) under a contract
+ *                between LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
+ *                and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea).
+ *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ * @author Mail to: <A HREF="mailto:a.gudz@samsung.com">Andriy Gudz, a.gudz@samsung.com</A>
+ */
+
+#include "authcodeprovider.h"
+
+#include <stdio.h>
+#include <curl/curl.h>
+
+#include <exception>
+#include <stdexcept>
+#include <regex>
+
+static size_t writefunc(char* buf, size_t size, size_t nmemb, std::string &s)
+{
+    for (int c = 0; c < size * nmemb; c++)
+    {
+        s.push_back(buf[c]);
+    }
+    return size * nmemb;
+}
+
+static std::string getLoginAuthToken()
+{
+    CURL *curl = curl_easy_init();
+    if(!curl) throw std::runtime_error("curl init failed");
+    std::string loginPage;
+
+    curl_easy_setopt(curl, CURLOPT_URL, "https://github.com/login");
+
+    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");
+    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt");
+
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &loginPage);
+    CURLcode res = curl_easy_perform(curl);
+    curl_easy_cleanup(curl);
+    if(res != CURLE_OK) throw std::runtime_error("curl return code failed");
+
+    std::smatch matches;
+    std::regex expression ("authenticity_token.*value=\"(.*==)");
+    std::regex_search (loginPage, matches, expression);
+
+    return matches[1];
+}
+
+static void signIn(std::string loginAuthToken, std::string login, std::string password)
+{
+    CURL *curl = curl_easy_init();
+    if(!curl) throw std::runtime_error("curl init failed");
+    std::string sessionResponce;
+
+    curl_easy_setopt(curl, CURLOPT_URL, "https://github.com/session");
+
+    curl_easy_setopt(curl, CURLOPT_POST, 1);
+
+    char fields[1000];
+    sprintf(fields, "commit=Sign+in&utf8=%%E2%%9C%%93&authenticity_token=%s&login=%s&password=%s",
+            curl_escape(loginAuthToken.c_str(), loginAuthToken.size()), login.c_str(), password.c_str());
+
+    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields);
+
+    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");
+    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt");
+
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &sessionResponce);
+
+    CURLcode res = curl_easy_perform(curl);
+    curl_easy_cleanup(curl);
+    if(res != CURLE_OK) throw std::runtime_error("curl return code failed");
+}
+
+std::string GithubAuthCodeProvider::getAuthCode(std::string login, std::string password)
+{
+    std::string loginAuthToken = getLoginAuthToken();
+    if (loginAuthToken.size() > 0)
+        signIn(loginAuthToken, login, password);
+
+    CURL *curl = curl_easy_init();
+    if(!curl) throw std::runtime_error("curl init failed");
+    std::string authorizeRequest;
+
+    curl_easy_setopt(curl, CURLOPT_URL, "https://github.com/login/oauth/authorize?client_id=ea9c18f540323b0213d0");
+
+    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");
+    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt");
+
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &authorizeRequest);
+
+    CURLcode res = curl_easy_perform(curl);
+    curl_easy_cleanup(curl);
+    if(res != CURLE_OK) throw std::runtime_error("curl return code failed");
+
+    std::smatch matches;
+    std::regex expression ("code=([a-z0-9]*)");
+    std::regex_search (authorizeRequest, matches, expression);
+
+    return matches[1];
+}
+
+std::string GithubAuthCodeProvider::getName()
+{
+    return "github";
+}
diff --git a/demo/iot-es/src/authcode/samsungauthcodeprovider.cpp b/demo/iot-es/src/authcode/samsungauthcodeprovider.cpp
new file mode 100644 (file)
index 0000000..86c4145
--- /dev/null
@@ -0,0 +1,21 @@
+/**
+ * @brief  TODO
+ * @date   Created 20.04.2017
+ * @author Created 2017 in Samsung Ukraine R&D Center (SURC) under a contract
+ *                between LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
+ *                and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea).
+ *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ * @author Mail to: <A HREF="mailto:a.gudz@samsung.com">Andriy Gudz, a.gudz@samsung.com</A>
+ */
+
+#include "authcodeprovider.h"
+
+std::string SamsungAuthCodeProvider::getAuthCode(std::string login, std::string password)
+{
+    return "AuthCode_A";
+}
+
+std::string SamsungAuthCodeProvider::getName()
+{
+    return "github";
+}