Skip to content

Commit

Permalink
[INTERNET MODULE][JWT] Implementation + test
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Onufrak committed Oct 29, 2024
1 parent 4a929d0 commit 3443138
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 729 deletions.
69 changes: 31 additions & 38 deletions include/faker-cxx/internet.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include <array>
#include <ctime>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <map>
#include <ctime>

#include "faker-cxx/export.h"
#include "faker-cxx/types/locale.h"
Expand Down Expand Up @@ -353,8 +353,6 @@ FAKER_CXX_EXPORT std::string_view domainSuffix();
*/
FAKER_CXX_EXPORT std::string anonymousUsername(unsigned maxLength);



/**
* @brief Generates a JSON Web Token (JWT).
*
Expand All @@ -374,24 +372,22 @@ FAKER_CXX_EXPORT std::string anonymousUsername(unsigned maxLength);
* std::string token = faker::internet::getJWTToken(header, payload); // "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
* @endcode
*/
FAKER_CXX_EXPORT std::string getJWTToken(std::optional<std::map<std::string, std::string>> header = std::nullopt,
std::optional<std::map<std::string, std::string>> payload = std::nullopt,
std::optional<std::string> refDate = std::nullopt);


/**
* @brief Returns the algorithm used for signing the JWT.
*
* This function provides the algorithm that is used to sign the JSON Web Token (JWT).
*
* @returns A string view representing the JWT signing algorithm.
*
* @code
* std::string_view algorithm = faker::internet::getJWTAlgorithm(); // "HS256"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view getJWTAlgorithm();
FAKER_CXX_EXPORT std::string getJWTToken(std::optional<std::map<std::string, std::string>> header = std::nullopt,
std::optional<std::map<std::string, std::string>> payload = std::nullopt,
std::optional<std::string> refDate = std::nullopt);

/**
* @brief Returns the algorithm used for signing the JWT.
*
* This function provides the algorithm that is used to sign the JSON Web Token (JWT).
*
* @returns A string view representing the JWT signing algorithm.
*
* @code
* std::string_view algorithm = faker::internet::getJWTAlgorithm(); // "HS256"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view getJWTAlgorithm();

/**
* @brief Encodes a given string to Base64 URL format.
Expand All @@ -410,23 +406,20 @@ FAKER_CXX_EXPORT std::string_view getJWTAlgorithm();
*/
FAKER_CXX_EXPORT std::string toBase64UrlEncode(std::string& input);


/**
* @brief Converts a map of key-value pairs to a JSON string.
*
* This function takes a map where both keys and values are strings and converts it into a JSON formatted string.
*
* @param data The map containing key-value pairs to be converted to JSON.
*
* @returns A JSON formatted string representing the input map.
*
* @code
* std::map<std::string, std::string> data = {{"name", "John"}, {"age", "30"}};
* std::string json = faker::internet::toJSON(data); // json is now "{\"name\":\"John\",\"age\":\"30\"}"
* @endcode
*/
/**
* @brief Converts a map of key-value pairs to a JSON string.
*
* This function takes a map where both keys and values are strings and converts it into a JSON formatted string.
*
* @param data The map containing key-value pairs to be converted to JSON.
*
* @returns A JSON formatted string representing the input map.
*
* @code
* std::map<std::string, std::string> data = {{"name", "John"}, {"age", "30"}};
* std::string json = faker::internet::toJSON(data); // json is now "{\"name\":\"John\",\"age\":\"30\"}"
* @endcode
*/
FAKER_CXX_EXPORT std::string toJSON(std::map<std::string, std::string>& data);

}


42 changes: 21 additions & 21 deletions src/modules/internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@

#include <algorithm>
#include <array>
#include <cmath>
#include <initializer_list>
#include <map>
#include <optional>
#include <regex>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <regex>
#include <cmath>


#include "common/algo_helper.h"
#include "common/format_helper.h"
#include "common/string_helper.h"
#include "faker-cxx/company.h"
#include "faker-cxx/faker.h"
#include "faker-cxx/helper.h"
#include "faker-cxx/number.h"
#include "faker-cxx/person.h"
#include "faker-cxx/types/hex.h"
#include "faker-cxx/types/locale.h"
#include "faker-cxx/word.h"
#include "faker-cxx/faker.h"
#include "faker-cxx/company.h"
#include "internet_data.h"
#include "modules/string_data.h"

Expand Down Expand Up @@ -354,7 +353,7 @@ std::string anonymousUsername(unsigned maxLength)
}

std::string toBase64UrlEncode(std::string& input)
{
{
const std::string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string encodedInput;

Expand All @@ -372,8 +371,10 @@ std::string toBase64UrlEncode(std::string& input)
}
}

if (validBits > -6) encodedInput.push_back(base64Chars[((value << 8) >> validBits) & 0x3F]);
while (encodedInput.size() % 4) encodedInput.push_back('=');
if (validBits > -6)
encodedInput.push_back(base64Chars[((value << 8) >> validBits) & 0x3F]);
while (encodedInput.size() % 4)
encodedInput.push_back('=');

std::replace(encodedInput.begin(), encodedInput.end(), '+', '-');
std::replace(encodedInput.begin(), encodedInput.end(), '/', '_');
Expand All @@ -387,7 +388,8 @@ std::string toJSON(std::map<std::string, std::string>& data)
std::string json = "{";
for (auto it = data.begin(); it != data.end(); ++it)
{
if (it != data.begin()) json += ",";
if (it != data.begin())
json += ",";
json += "\"" + it->first + "\":\"" + it->second + "\"";
}
json += "}";
Expand All @@ -400,8 +402,7 @@ std::string_view getJWTAlgorithm()
}

std::string getJWTToken(std::optional<std::map<std::string, std::string>> header,
std::optional<std::map<std::string, std::string>> payload,
std::optional<std::string> refDate)
std::optional<std::map<std::string, std::string>> payload, std::optional<std::string> refDate)
{
std::string refDateValue = refDate.value_or(faker::date::anytime());

Expand All @@ -412,18 +413,17 @@ std::string getJWTToken(std::optional<std::map<std::string, std::string>> header

std::string algorithm(getJWTAlgorithm());

if (!header) header = {{"alg", algorithm}, {"typ", "JWT"}};
if (!header)
header = {{"alg", algorithm}, {"typ", "JWT"}};
if (!payload)
{
payload = {
{"iat", std::to_string(std::round(std::stoll(iatDefault)))},
{"exp", std::to_string(std::round(std::stoll(expDefault)))},
{"nbf", std::to_string(std::round(std::stoll(nbfDefault)))},
{"iss", faker::company::companyName()},
{"sub", faker::string::uuid()},
{"aud", faker::string::uuid()},
{"jti", faker::string::uuid()}
};
payload = {{"iat", std::to_string(std::round(std::stoll(iatDefault)))},
{"exp", std::to_string(std::round(std::stoll(expDefault)))},
{"nbf", std::to_string(std::round(std::stoll(nbfDefault)))},
{"iss", faker::company::companyName()},
{"sub", faker::string::uuid()},
{"aud", faker::string::uuid()},
{"jti", faker::string::uuid()}};
}

std::string headerToJSON = toJSON(header.value());
Expand Down
Loading

0 comments on commit 3443138

Please sign in to comment.