sptk2 logo
SPTK Home Page
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
sptk::JWT Class Reference

#include <JWT.h>

Public Types

enum class  Algorithm : uint8_t {
  NONE = 0 , HS256 , HS384 , HS512 ,
  RS256 , RS384 , RS512 , ES256 ,
  ES384 , ES512 , INVAL
}
 

Public Member Functions

 JWT ()=default
 
const Variantget (const String &name) const
 
void set (const String &name, const Variant &data) const
 
Algorithm get_alg () const
 
void set_alg (Algorithm _alg, const String &_key)
 
void sign (Buffer &token, const char *str) const
 
void encode (std::ostream &out) const
 
void decode (const char *token, const String &_key="")
 
void exportTo (std::ostream &output, bool pretty) const
 
void write_head (std::ostream &output, bool pretty) const
 
void write_body (std::ostream &output, bool pretty) const
 
void verify (const Buffer &head, const Buffer &sig) const
 
void sign_sha_hmac (Buffer &out, const char *str) const
 
void verify_sha_hmac (const char *head, const char *sig) const
 
void sign_sha_pem (Buffer &out, const char *str) const
 
void verify_sha_pem (const char *head, const char *sig_b64) const
 

Static Public Member Functions

static const char * alg_str (Algorithm _alg)
 
static Algorithm str_alg (const char *alg)
 
static String get_js_string (const xdoc::SNode &node, const String &key, bool *found=nullptr)
 
static long get_js_int (const xdoc::SNode &node, const String &key, bool *found=nullptr)
 
static bool get_js_bool (const xdoc::SNode &node, const String &key, bool *found=nullptr)
 

Public Attributes

Algorithm alg {Algorithm::NONE}
 Signature encryption algorithm.
 
String key
 Signature encryption key.
 
xdoc::Document grants
 Token content.
 

Detailed Description

Java Web Token encoding and decoding

Member Enumeration Documentation

◆ Algorithm

enum class sptk::JWT::Algorithm : uint8_t
strong

JWT algorithm types.

Constructor & Destructor Documentation

◆ JWT()

sptk::JWT::JWT ( )
default

Constructor

Member Function Documentation

◆ alg_str()

static const char * sptk::JWT::alg_str ( Algorithm  _alg)
static

Get signature encryption algorithm name

Parameters
_algSignature encryption algorithm
Returns

◆ decode()

void sptk::JWT::decode ( const char *  token,
const String _key = "" 
)

Decode token

Parameters
tokenInput token data
_keyOptional signature encryption key

◆ encode()

void sptk::JWT::encode ( std::ostream &  out) const

Encode token to stream

Parameters
outOutput stream

◆ exportTo()

void sptk::JWT::exportTo ( std::ostream &  output,
bool  pretty 
) const

Export token to stream

Parameters
outputOutput stream
prettyIf true, produce formatted output

◆ get()

const Variant & sptk::JWT::get ( const String name) const
inline

Get JSON element in JSON object element by name. If element doesn't exist in JSON object yet, it's created as JSON null element. If this element is not JSON object, an exception is thrown.

Parameters
nameName of the element in the object element
Returns
Element for the name, or NULL if not found

◆ get_js_bool()

static bool sptk::JWT::get_js_bool ( const xdoc::SNode &  node,
const String key,
bool *  found = nullptr 
)
static

Find boolean grant value by name

Parameters
nodeParent JSON element
keyGrant name
foundOptional (output) flag, true is found
Returns
grant value

◆ get_js_int()

static long sptk::JWT::get_js_int ( const xdoc::SNode &  node,
const String key,
bool *  found = nullptr 
)
static

Find integer grant value by name

Parameters
nodeParent JSON element
keyGrant name
foundOptional (output) flag, true is found
Returns
grant value

◆ get_js_string()

static String sptk::JWT::get_js_string ( const xdoc::SNode &  node,
const String key,
bool *  found = nullptr 
)
static

Find string grant value by name

Parameters
nodeParent JSON element
keyGrant name
foundOptional (output) flag, true is found
Returns
grant value

◆ set()

void sptk::JWT::set ( const String name,
const Variant data 
) const
inline

Set JSON element in JSON object element by name. If element doesn't exist in JSON object yet, it's created as JSON null element. If this element is not JSON object, an exception is thrown.

Parameters
nameName of the element in the object element
Returns
Element for the name, or NULL if not found

◆ set_alg()

void sptk::JWT::set_alg ( Algorithm  _alg,
const String _key 
)

Set signature encryption algorithm

Parameters
_algSignature encryption algorithm
_keySignature encryption key

◆ sign()

void sptk::JWT::sign ( Buffer token,
const char *  str 
) const

Sign token

Parameters
tokenOutput token data
strData to sign

◆ sign_sha_hmac()

void sptk::JWT::sign_sha_hmac ( Buffer out,
const char *  str 
) const

Sign using SHA algorithm to HMAC format

Parameters
outOutput data
strInput data

◆ sign_sha_pem()

void sptk::JWT::sign_sha_pem ( Buffer out,
const char *  str 
) const

Sign using SHA algorithm to PEM format

Parameters
outOutput data
strInput data

◆ str_alg()

static Algorithm sptk::JWT::str_alg ( const char *  alg)
static

Get signature encryption algorithm from name

Parameters
algSignature encryption algorithm name
Returns

◆ verify()

void sptk::JWT::verify ( const Buffer head,
const Buffer sig 
) const

Verify token

Parameters
headToken head
sigSignature

◆ verify_sha_hmac()

void sptk::JWT::verify_sha_hmac ( const char *  head,
const char *  sig 
) const

Verify using SHA algorithm in HMAC format

Parameters
headToken head
sigSignature

◆ verify_sha_pem()

void sptk::JWT::verify_sha_pem ( const char *  head,
const char *  sig_b64 
) const

Verify using SHA algorithm in PEM format

Parameters
headToken head
sig_b64Signature

◆ write_body()

void sptk::JWT::write_body ( std::ostream &  output,
bool  pretty 
) const

Write token body to output stream

Parameters
outputOutput stream
prettyIf true then produce formatted output

◆ write_head()

void sptk::JWT::write_head ( std::ostream &  output,
bool  pretty 
) const

Write token head to output stream

Parameters
outputOutput stream
prettyIf true then produce formatted output

The documentation for this class was generated from the following file:

Fri Oct 14 2022 09:58:43: SPTK 5.4.1