Skip to content

Commit 71c3d36

Browse files
Ensure array_type has front() method + fix array_type on jsoncpp (#361)
* Use operator[] instead of front to support jsoncpp Replace the `front()` with `[0]` as front is not supported by jsoncpp on array types. Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com> * Revert "Use operator[] instead of front to support jsoncpp" This reverts commit dfa7d29. * Implement `front()` in jsoncpp array_type Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com> * Check if array_type has a front() method Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com> * Add `front()` to jsoncons array_type * Remove non-const overloads of front() Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com> * Fix check for front() in array_type Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com> * Use std::decay Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com> * fix example to print array value * expose base class ctors * update as_array traits helper * fixup linter --------- Signed-off-by: Omar Mohamed <mohamed.omar67492@gmail.com> Co-authored-by: Chris Mc <prince.chrismc@gmail.com>
1 parent 59cdb43 commit 71c3d36

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed

example/traits/danielaparker-jsoncons.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main() {
3434
const auto decoded = jwt::decode<traits>(token);
3535

3636
const auto array = traits::as_array(decoded.get_payload_claim("object").to_json()["api"]["array"]);
37-
std::cout << "payload /object/api/array = " << array << std::endl;
37+
std::cout << "payload /object/api/array = " << jsoncons::pretty_print(traits::json(array)) << std::endl;
3838

3939
jwt::verify<traits>()
4040
.allow_algorithm(jwt::algorithm::none{})

include/jwt-cpp/jwt.h

+31-29
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ namespace jwt {
808808
* \brief Load a public key from a string.
809809
*
810810
* The string should contain a pem encoded certificate or public key
811-
*
811+
*
812812
* \deprecated Use the templated version helper::load_private_key_from_string with error::ecdsa_error
813813
*
814814
* \param key String containing the certificate encoded as pem
@@ -872,7 +872,7 @@ namespace jwt {
872872
* The string should contain a pem encoded certificate or public key
873873
*
874874
* \deprecated Use the templated version helper::load_private_key_from_string with error::ecdsa_error
875-
*
875+
*
876876
* \param key String containing the certificate or key encoded as pem
877877
* \param password Password used to decrypt certificate or key (leave empty if not encrypted)
878878
* \throw ecdsa_exception if an error occurred
@@ -887,7 +887,7 @@ namespace jwt {
887887

888888
/**
889889
* \brief Load a private key from a string.
890-
*
890+
*
891891
* \deprecated Use the templated version helper::load_private_key_from_string with error::ecdsa_error
892892
*
893893
* \param key String containing a private key as pem
@@ -1077,7 +1077,7 @@ namespace jwt {
10771077
* \brief Load a private key from a string.
10781078
*
10791079
* \deprecated Use the templated version helper::load_private_key_from_string with error::ecdsa_error
1080-
*
1080+
*
10811081
* \param key String containing a private key as pem
10821082
* \param password Password used to decrypt key (leave empty if not encrypted)
10831083
* \throw ecdsa_exception if an error occurred
@@ -1372,7 +1372,7 @@ namespace jwt {
13721372
struct hmacsha {
13731373
/**
13741374
* Construct new hmac algorithm
1375-
*
1375+
*
13761376
* \param key Key to use for HMAC
13771377
* \param md Pointer to hash function
13781378
* \param name Name of the algorithm
@@ -1381,7 +1381,7 @@ namespace jwt {
13811381
: secret(std::move(key)), md(md), alg_name(std::move(name)) {}
13821382
/**
13831383
* Sign jwt data
1384-
*
1384+
*
13851385
* \param data The data to sign
13861386
* \param ec error_code filled with details on error
13871387
* \return HMAC signature for the given data
@@ -1402,7 +1402,7 @@ namespace jwt {
14021402
}
14031403
/**
14041404
* Check if signature is valid
1405-
*
1405+
*
14061406
* \param data The data to check signature against
14071407
* \param signature Signature provided by the jwt
14081408
* \param ec Filled with details about failure.
@@ -1423,7 +1423,7 @@ namespace jwt {
14231423
}
14241424
/**
14251425
* Returns the algorithm name provided to the constructor
1426-
*
1426+
*
14271427
* \return algorithm's name
14281428
*/
14291429
std::string name() const { return alg_name; }
@@ -1442,7 +1442,7 @@ namespace jwt {
14421442
struct rsa {
14431443
/**
14441444
* Construct new rsa algorithm
1445-
*
1445+
*
14461446
* \param public_key RSA public key in PEM format
14471447
* \param private_key RSA private key or empty string if not available. If empty, signing will always fail.
14481448
* \param public_key_password Password to decrypt public key pem.
@@ -1495,7 +1495,7 @@ namespace jwt {
14951495
}
14961496
/**
14971497
* Check if signature is valid
1498-
*
1498+
*
14991499
* \param data The data to check signature against
15001500
* \param signature Signature provided by the jwt
15011501
* \param ec Filled with details on failure
@@ -2053,13 +2053,13 @@ namespace jwt {
20532053
};
20542054
/**
20552055
* RS256 algorithm.
2056-
*
2056+
*
20572057
* This data structure is used to describe the RSA256 and can be used to verify JWTs
20582058
*/
20592059
struct rs256 : public rsa {
20602060
/**
20612061
* \brief Construct new instance of algorithm
2062-
*
2062+
*
20632063
* \param public_key RSA public key in PEM format
20642064
* \param private_key RSA private key or empty string if not available. If empty, signing will always fail.
20652065
* \param public_key_password Password to decrypt public key pem.
@@ -2459,11 +2459,13 @@ namespace jwt {
24592459
struct is_valid_json_array {
24602460
template<typename T>
24612461
using value_type_t = typename T::value_type;
2462+
using front_base_type = typename std::decay<decltype(std::declval<array_type>().front())>::type;
24622463

24632464
static constexpr auto value = std::is_constructible<value_type, array_type>::value &&
24642465
is_iterable<array_type>::value &&
24652466
is_detected<value_type_t, array_type>::value &&
2466-
std::is_same<typename array_type::value_type, value_type>::value;
2467+
std::is_same<typename array_type::value_type, value_type>::value &&
2468+
std::is_same<front_base_type, value_type>::value;
24672469
};
24682470

24692471
template<typename string_type, typename integer_type>
@@ -3728,9 +3730,9 @@ namespace jwt {
37283730
* Specify a claim to check for using the specified operation.
37293731
* This is helpful for implementating application specific authentication checks
37303732
* such as the one seen in partial-claim-verifier.cpp
3731-
*
3733+
*
37323734
* \snippet{trimleft} partial-claim-verifier.cpp verifier check custom claim
3733-
*
3735+
*
37343736
* \param name Name of the claim to check for
37353737
* \param fn Function to use for verifying the claim
37363738
* \return *this to allow chaining
@@ -3743,9 +3745,9 @@ namespace jwt {
37433745
/**
37443746
* Specify a claim to check for equality (both type & value).
37453747
* See the private-claims.cpp example.
3746-
*
3748+
*
37473749
* \snippet{trimleft} private-claims.cpp verify exact claim
3748-
*
3750+
*
37493751
* \param name Name of the claim to check for
37503752
* \param c Claim to check for
37513753
* \return *this to allow chaining
@@ -3756,13 +3758,13 @@ namespace jwt {
37563758

37573759
/**
37583760
* \brief Add an algorithm available for checking.
3759-
*
3761+
*
37603762
* This is used to handle incomming tokens for predefined algorithms
37613763
* which the authorization server is provided. For example a small system
37623764
* where only a single RSA key-pair is used to sign tokens
3763-
*
3765+
*
37643766
* \snippet{trimleft} example/rsa-verify.cpp allow rsa algorithm
3765-
*
3767+
*
37663768
* \tparam Algorithm any algorithm such as those provided by jwt::algorithm
37673769
* \param alg Algorithm to allow
37683770
* \return *this to allow chaining
@@ -4122,18 +4124,18 @@ namespace jwt {
41224124
* Default clock class using std::chrono::system_clock as a backend.
41234125
*/
41244126
struct default_clock {
4125-
/**
4127+
/**
41264128
* Gets the current system time
4127-
* \return time_point of the host system
4129+
* \return time_point of the host system
41284130
*/
41294131
date now() const { return date::clock::now(); }
41304132
};
41314133

41324134
/**
41334135
* Create a verifier using the default_clock.
4134-
*
4135-
*
4136-
*
4136+
*
4137+
*
4138+
*
41374139
* \param c Clock instance to use
41384140
* \return verifier instance
41394141
*/
@@ -4153,7 +4155,7 @@ namespace jwt {
41534155
/**
41544156
* \brief Decode a token. This can be used to to help access important feild like 'x5c'
41554157
* for verifying tokens. See associated example rsa-verify.cpp for more details.
4156-
*
4158+
*
41574159
* \tparam json_traits JSON implementation traits
41584160
* \tparam Decode is callable, taking a string_type and returns a string_type.
41594161
* It should ensure the padding of the input and then base64url decode and
@@ -4172,7 +4174,7 @@ namespace jwt {
41724174
/**
41734175
* Decode a token. This can be used to to help access important feild like 'x5c'
41744176
* for verifying tokens. See associated example rsa-verify.cpp for more details.
4175-
*
4177+
*
41764178
* \tparam json_traits JSON implementation traits
41774179
* \param token Token to decode
41784180
* \return Decoded token
@@ -4194,10 +4196,10 @@ namespace jwt {
41944196
return jwk<json_traits>(jwk_);
41954197
}
41964198
/**
4197-
* Parse a JSON Web Key Set. This can be used to to help access
4199+
* Parse a JSON Web Key Set. This can be used to to help access
41984200
* important feild like 'x5c' for verifying tokens. See example
41994201
* jwks-verify.cpp for more information.
4200-
*
4202+
*
42014203
* \tparam json_traits JSON implementation traits
42024204
* \param jwks_ string buffer containing the JSON object
42034205
* \return Parsed JSON object containing the data of the JWK SET string

include/jwt-cpp/traits/danielaparker-jsoncons/traits.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ namespace jwt {
6161
return 0;
6262
}
6363
};
64-
using array_type = json::array;
64+
class array_type : public json::array {
65+
public:
66+
using json::array::array;
67+
explicit array_type(const json::array& a) : json::array(a) {}
68+
explicit array_type(json::array&& a) : json::array(a) {}
69+
value_type const& front() const { return this->operator[](0U); }
70+
};
6571
using string_type = std::string; // current limitation of traits implementation
6672
using number_type = double;
6773
using integer_type = int64_t;
@@ -89,7 +95,7 @@ namespace jwt {
8995

9096
static array_type as_array(const json& val) {
9197
if (val.type() != jsoncons::json_type::array_value) throw std::bad_cast();
92-
return val.array_value();
98+
return array_type(val.array_value());
9399
}
94100

95101
static string_type as_string(const json& val) {

include/jwt-cpp/traits/open-source-parsers-jsoncpp/traits.h

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ namespace jwt {
3333
~array_type() = default;
3434
array_type& operator=(const array_type& o) = default;
3535
array_type& operator=(array_type&& o) noexcept = default;
36+
37+
value_type const& front() const { return this->operator[](0U); }
3638
};
3739
using number_type = double;
3840
using integer_type = Json::Value::Int;

0 commit comments

Comments
 (0)