Skip to content

Commit 5c588fc

Browse files
authored
Allow moving decoded_jwt (#225)
1 parent e0f89e0 commit 5c588fc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

include/jwt-cpp/jwt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,7 @@ namespace jwt {
25302530
class decoded_jwt : public header<json_traits>, public payload<json_traits> {
25312531
protected:
25322532
/// Unmodifed token, as passed to constructor
2533-
const typename json_traits::string_type token;
2533+
typename json_traits::string_type token;
25342534
/// Header part decoded from base64
25352535
typename json_traits::string_type header;
25362536
/// Unmodified header part in base64

tests/TokenTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,29 @@ TEST(TokenTest, ThrowInvalidKeyLength) {
774774
ASSERT_NO_THROW(jwt::algorithm::es384(ecdsa384_pub_key, ecdsa384_priv_key));
775775
ASSERT_NO_THROW(jwt::algorithm::es512(ecdsa521_pub_key, ecdsa521_priv_key));
776776
}
777+
778+
TEST(TokenTest, MoveDecodedToken) {
779+
std::string token0 = "eyJhbGciOiJub25lIiwidHlwIjoiSldTIn0.eyJpc3MiOiJhdXRoMCJ9.";
780+
std::string token1 =
781+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
782+
auto decoded_token0 = jwt::decode(token0);
783+
auto decoded_token1 = jwt::decode(token1);
784+
decoded_token0 = std::move(decoded_token1);
785+
ASSERT_EQ(token1, decoded_token0.get_token());
786+
787+
ASSERT_TRUE(decoded_token0.has_algorithm());
788+
ASSERT_TRUE(decoded_token0.has_type());
789+
ASSERT_FALSE(decoded_token0.has_content_type());
790+
ASSERT_FALSE(decoded_token0.has_key_id());
791+
ASSERT_TRUE(decoded_token0.has_issuer());
792+
ASSERT_FALSE(decoded_token0.has_subject());
793+
ASSERT_FALSE(decoded_token0.has_audience());
794+
ASSERT_FALSE(decoded_token0.has_expires_at());
795+
ASSERT_FALSE(decoded_token0.has_not_before());
796+
ASSERT_FALSE(decoded_token0.has_issued_at());
797+
ASSERT_FALSE(decoded_token0.has_id());
798+
799+
ASSERT_EQ("HS256", decoded_token0.get_algorithm());
800+
ASSERT_EQ("JWS", decoded_token0.get_type());
801+
ASSERT_EQ("auth0", decoded_token0.get_issuer());
802+
}

0 commit comments

Comments
 (0)