| 1 |
/* |
| 2 |
* Copyright (C) 2021 flubshi |
| 3 |
* https://github.com/flubshi |
| 4 |
* |
| 5 |
* This Program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2, or (at your option) |
| 8 |
* any later version. |
| 9 |
* |
| 10 |
* This Program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with XBMC; see the file COPYING. If not, write to |
| 17 |
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
* http://www.gnu.org/copyleft/gpl.html |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "JWT.h" |
| 23 |
|
| 24 |
#include "Base64.h" |
| 25 |
#include "Utils.h" |
| 26 |
#include "kodi/General.h" |
| 27 |
#include "kodi/tools/StringUtils.h" |
| 28 |
|
| 29 |
#include <chrono> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
JWT::JWT(std::string token) |
| 33 |
{ |
| 34 |
if (token.empty()) |
| 35 |
{ |
| 36 |
return; |
| 37 |
}; |
| 38 |
strToken = token; |
| 39 |
std::vector<std::string> jwt_arr = kodi::tools::StringUtils::Split(strToken, ".", 3); |
| 40 |
if (jwt_arr.size() == 3) |
| 41 |
{ |
| 42 |
kodi::Log(ADDON_LOG_DEBUG, "[jwt parse] middle: %s", jwt_arr.at(1).c_str()); |
| 43 |
std::string jwt_payload = base64_decode(jwt_arr.at(1)); |
| 44 |
kodi::Log(ADDON_LOG_DEBUG, "[jwt parse] payload: %s", jwt_payload.c_str()); |
| 45 |
|
| 46 |
this->parsedToken.Parse(jwt_payload.c_str()); |
| 47 |
|
| 48 |
if (this->parsedToken.HasParseError()) |
| 49 |
{ |
| 50 |
kodi::Log(ADDON_LOG_ERROR, "[jwt parse doc] ERROR: error while parsing json"); |
| 51 |
this->initialized = false; |
| 52 |
return; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// parse iat |
| 57 |
if (!this->parsedToken.HasMember("iat") || !this->parsedToken["iat"].IsInt()) |
| 58 |
{ |
| 59 |
kodi::Log(ADDON_LOG_ERROR, "[jwt parse doc] ERROR: field 'iat' missing"); |
| 60 |
this->initialized = false; |
| 61 |
return; |
| 62 |
} |
| 63 |
this->iat = this->parsedToken["iat"].GetInt(); |
| 64 |
|
| 65 |
// parse exp |
| 66 |
if (!this->parsedToken.HasMember("exp") || !this->parsedToken["exp"].IsInt()) |
| 67 |
{ |
| 68 |
kodi::Log(ADDON_LOG_ERROR, "[jwt parse doc] ERROR: field 'exp' missing"); |
| 69 |
this->initialized = false; |
| 70 |
return; |
| 71 |
} |
| 72 |
this->exp = this->parsedToken["exp"].GetInt(); |
| 73 |
|
| 74 |
this->initialized = true; |
| 75 |
} |
| 76 |
|
| 77 |
bool JWT::isExpired(int offset) const |
| 78 |
{ |
| 79 |
kodi::Log(ADDON_LOG_DEBUG, "[jwt isExpired] exp: %i", exp); |
| 80 |
int currTime = std::chrono::duration_cast<std::chrono::seconds>( |
| 81 |
std::chrono::system_clock::now().time_since_epoch()) |
| 82 |
.count(); |
| 83 |
kodi::Log(ADDON_LOG_DEBUG, "[jwt isExpired] curr: %i", currTime); |
| 84 |
return (exp - offset) < currTime; |
| 85 |
} |