parent
36842df31d
commit
ec3b63e691
@ -0,0 +1,79 @@ |
||||
// TeslaNX
|
||||
// Copyright (C) 2021 Nichole Mattera
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#include <curl/curl.h> |
||||
|
||||
#include "AuthResponse.hpp" |
||||
|
||||
namespace TeslaNX::Models { |
||||
AuthResponse::AuthResponse() { |
||||
this->code = ""; |
||||
this->state = ""; |
||||
this->issuer = ""; |
||||
} |
||||
|
||||
void AuthResponse::parseResponse(std::string url) { |
||||
auto queryParamStart = url.find("?"); |
||||
if (queryParamStart == std::string::npos) |
||||
return; |
||||
|
||||
auto queryParams = url.substr(queryParamStart + 1); |
||||
if (queryParams.length() == 0) |
||||
return; |
||||
|
||||
std::string::size_type startPos = 0; |
||||
while (startPos != queryParams.length()) { |
||||
auto foundPos = queryParams.find("&", startPos); |
||||
if (foundPos == std::string::npos) { |
||||
_parseParameter(queryParams); |
||||
startPos = queryParams.length(); |
||||
} else { |
||||
_parseParameter(queryParams.substr(startPos, foundPos)); |
||||
startPos = foundPos + 1; |
||||
} |
||||
} |
||||
} |
||||
|
||||
void AuthResponse::_parseParameter(std::string parameter) { |
||||
auto pos = parameter.find("="); |
||||
if (pos == std::string::npos) |
||||
return; |
||||
|
||||
auto key = parameter.substr(0, pos); |
||||
auto value = parameter.substr(pos + 1); |
||||
|
||||
if (key == "code") { |
||||
this->code = _decodeValue(value); |
||||
} else if (key == "state") { |
||||
this->state = _decodeValue(value); |
||||
} else if (key == "issuer") { |
||||
this->issuer = _decodeValue(value); |
||||
} |
||||
} |
||||
|
||||
std::string AuthResponse::_decodeValue(std::string value) { |
||||
auto curl = curl_easy_init(); |
||||
|
||||
auto decodedValue = curl_easy_unescape(curl, value.c_str(), value.length(), NULL); |
||||
auto result = std::string(decodedValue); |
||||
|
||||
curl_free(decodedValue); |
||||
curl_easy_cleanup(curl); |
||||
|
||||
return result; |
||||
} |
||||
} |
@ -0,0 +1,37 @@ |
||||
// TeslaNX
|
||||
// Copyright (C) 2021 Nichole Mattera
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#include <string> |
||||
|
||||
#pragma once |
||||
|
||||
namespace TeslaNX::Models { |
||||
class AuthResponse { |
||||
public: |
||||
AuthResponse(); |
||||
|
||||
void parseResponse(std::string url); |
||||
|
||||
std::string code; |
||||
std::string state; |
||||
std::string issuer; |
||||
|
||||
private: |
||||
void _parseParameter(std::string parameter); |
||||
std::string _decodeValue(std::string value); |
||||
}; |
||||
} |
@ -0,0 +1,82 @@ |
||||
// TeslaNX
|
||||
// Copyright (C) 2021 Nichole Mattera
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#include <jansson.h> |
||||
|
||||
#include "TokensResponse.hpp" |
||||
|
||||
namespace TeslaNX::Models { |
||||
TokensResponse::TokensResponse() { |
||||
this->accessToken = ""; |
||||
this->refreshToken = ""; |
||||
this->idToken = ""; |
||||
this->expiresIn = 0; |
||||
this->state = ""; |
||||
this->tokenType = ""; |
||||
} |
||||
|
||||
void TokensResponse::parseResponse(std::vector<char> data) { |
||||
std::string jsonData = std::string(data.begin(), data.end()); |
||||
|
||||
json_t * root = json_loads(jsonData.c_str(), 0, NULL); |
||||
if (!root || !json_is_object(root)) |
||||
return; |
||||
|
||||
json_t * accessToken = json_object_get(root, "access_token"); |
||||
if (!accessToken || !json_is_string(accessToken)) { |
||||
json_decref(root); |
||||
return; |
||||
} |
||||
|
||||
json_t * refreshToken = json_object_get(root, "refresh_token"); |
||||
if (!refreshToken || !json_is_string(refreshToken)) { |
||||
json_decref(root); |
||||
return; |
||||
} |
||||
|
||||
json_t * idToken = json_object_get(root, "id_token"); |
||||
if (!idToken || !json_is_string(idToken)) { |
||||
json_decref(root); |
||||
return; |
||||
} |
||||
|
||||
json_t * expiresIn = json_object_get(root, "expires_in"); |
||||
if (!expiresIn || !json_is_integer(expiresIn)) { |
||||
json_decref(root); |
||||
return; |
||||
} |
||||
|
||||
json_t * state = json_object_get(root, "state"); |
||||
if (!state || !json_is_string(state)) { |
||||
json_decref(root); |
||||
return; |
||||
} |
||||
|
||||
json_t * tokenType = json_object_get(root, "token_type"); |
||||
if (!tokenType || !json_is_string(tokenType)) { |
||||
json_decref(root); |
||||
return; |
||||
} |
||||
|
||||
this->accessToken = std::string(json_string_value(accessToken)); |
||||
this->refreshToken = std::string(json_string_value(refreshToken)); |
||||
this->idToken = std::string(json_string_value(idToken)); |
||||
this->expiresIn = json_integer_value(expiresIn); |
||||
this->state = std::string(json_string_value(state)); |
||||
this->tokenType = std::string(json_string_value(tokenType)); |
||||
} |
||||
} |
@ -0,0 +1,38 @@ |
||||
// TeslaNX
|
||||
// Copyright (C) 2021 Nichole Mattera
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#include <string> |
||||
#include <vector> |
||||
|
||||
#pragma once |
||||
|
||||
namespace TeslaNX::Models { |
||||
class TokensResponse { |
||||
public: |
||||
TokensResponse(); |
||||
|
||||
void parseResponse(std::vector<char> data); |
||||
|
||||
std::string accessToken; |
||||
std::string refreshToken; |
||||
std::string idToken; |
||||
long long expiresIn; |
||||
std::string state; |
||||
std::string tokenType; |
||||
|
||||
}; |
||||
} |
Loading…
Reference in new issue