Skip to content

Decode JWT Token (Boost) #248

@hanusek

Description

@hanusek

What's your question?

How to decode JWT token (HS256) with Boost traits?

Additional Context

Hello.
I have a token generated by https://jwt.io but I have a problem - Exception: Invalid input: not within alphabet.
How to fix it?
Encoded:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Header:

{
  "alg": "HS256",
  "typ": "JWT"
}

Payload:

{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022
}

VERIFY SIGNATURE:

HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload), 
your-256-bit-secret
)

Code:

#include <boost/json/src.hpp>
#include <jwt-cpp/traits/boost-json/traits.h>

using traits = jwt::traits::boost_json;
using claim = jwt::basic_claim<traits>;

void jwt_token(const std::string& token)
{
    const auto decoded = jwt::decode<traits>(token);
    std::cout << "decoded token: " << decoded.get_algorithm() << std::endl;
}

Error log:

token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
Exception: Invalid input: not within alphabet

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions