jwt.c File Reference

JWT module implementation file. More...

#include "jwt.h"
#include "log_util.h"
#include "definitions.h"
#include "memory.h"
#include "string_array.h"
#include "time_util.h"
#include "json_util.h"
Include dependency graph for jwt.c:

Go to the source code of this file.

Macros

#define JWT_HEADER_NAME_ALG   "alg"
 
#define JWT_HEADER_NAME_TYPE   "typ"
 
#define JWT_HEADER_NAME_KID   "kid"
 
#define JWT_PAYLOAD_NAME_ISS   "iss"
 
#define JWT_PAYLOAD_NAME_SUB   "sub"
 
#define JWT_PAYLOAD_NAME_AUD   "aud"
 
#define JWT_PAYLOAD_NAME_IAT   "iat"
 
#define JWT_PAYLOAD_NAME_NBF   "nbf"
 
#define JWT_PAYLOAD_NAME_EXP   "exp"
 
#define JWT_PAYLOAD_NAME_SCHEMAS   "schemas"
 
#define JWT_PAYLOAD_NAME_TEN   "ten"
 
#define JWT_PAYLOAD_NAME_JTI   "jti"
 
#define JWT_PAYLOAD_NAME_PUBLIC_KEY   "public_key"
 
#define JWT_PAYLOAD_NAME_CONTENT_MD5   "content-md5"
 
#define JWT_HEADER_VALUE_ALG_SHARED_SECRET   "HS256"
 
#define JWT_HEADER_VALUE_ALG_RSA_3072   "RS256"
 
#define JWT_HEADER_VALUE_TYP   "JWT"
 
#define JWT_PAYLOAD_VALUE_SCHEMAS   "urn:siemens:mindsphere:v1"
 
#define JWT_PAYLOAD_VALUE_AUD   "southgate"
 

Functions

static E_MCL_ERROR_CODE _create_self_issued_jwt_header (E_MCL_SECURITY_PROFILE security_profile, json_t **header)
 
static E_MCL_ERROR_CODE _create_self_issued_jwt_payload (security_handler_t *security_handler, string_t *tenant, jwt_t *jwt)
 
static E_MCL_ERROR_CODE _add_schema_to_jwt (json_t *payload)
 
static E_MCL_ERROR_CODE _get_header_and_payload_encoded_base64_url (string_t *header, string_t *payload, string_t **header_encoded, string_t **payload_encoded)
 
static E_MCL_ERROR_CODE _join_with_dot (string_t *string_a, string_t *string_b, string_t **joined)
 
static E_MCL_ERROR_CODE _calculate_signature (jwt_t *jwt, string_t *header_and_payload, string_t **signature)
 
static E_MCL_ERROR_CODE _generate_token (jwt_t *jwt, string_t *header, string_t *payload, string_t **token)
 
E_MCL_ERROR_CODE jwt_initialize (security_handler_t *security_handler, E_MCL_SECURITY_PROFILE security_profile, string_t *tenant, jwt_t **jwt)
 JWT Initializer. More...
 
string_tjwt_get_token (jwt_t *jwt)
 Used to generate the JWT Token as json string. More...
 
void jwt_destroy (jwt_t **jwt)
 To destroy the JWT Handler. More...
 

Variables

static string_t dot = { ".", 1, MCL_STRING_NOT_COPY_NOT_DESTROY }
 

Detailed Description

JWT module implementation file.


Date
Jun 28, 2016

Definition in file jwt.c.

Macro Definition Documentation

#define JWT_HEADER_NAME_ALG   "alg"

Definition at line 23 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_HEADER_NAME_KID   "kid"

Definition at line 25 of file jwt.c.

#define JWT_HEADER_NAME_TYPE   "typ"

Definition at line 24 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_HEADER_VALUE_ALG_RSA_3072   "RS256"

Definition at line 40 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_HEADER_VALUE_ALG_SHARED_SECRET   "HS256"

Definition at line 39 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_HEADER_VALUE_TYP   "JWT"

Definition at line 41 of file jwt.c.

Referenced by _create_self_issued_jwt_header().

#define JWT_PAYLOAD_NAME_AUD   "aud"

Definition at line 29 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_CONTENT_MD5   "content-md5"

Definition at line 37 of file jwt.c.

#define JWT_PAYLOAD_NAME_EXP   "exp"

Definition at line 32 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_IAT   "iat"

Definition at line 30 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_ISS   "iss"

Definition at line 27 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_JTI   "jti"

Definition at line 35 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_NBF   "nbf"

Definition at line 31 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_PUBLIC_KEY   "public_key"

Definition at line 36 of file jwt.c.

#define JWT_PAYLOAD_NAME_SCHEMAS   "schemas"

Definition at line 33 of file jwt.c.

Referenced by _add_schema_to_jwt().

#define JWT_PAYLOAD_NAME_SUB   "sub"

Definition at line 28 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_NAME_TEN   "ten"

Definition at line 34 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_VALUE_AUD   "southgate"

Definition at line 44 of file jwt.c.

Referenced by _create_self_issued_jwt_payload().

#define JWT_PAYLOAD_VALUE_SCHEMAS   "urn:siemens:mindsphere:v1"

Definition at line 43 of file jwt.c.

Referenced by _add_schema_to_jwt().

Function Documentation

static E_MCL_ERROR_CODE _add_schema_to_jwt ( json_t payload)
static

Definition at line 354 of file jwt.c.

References ASSERT_CODE_MESSAGE, DEBUG_ENTRY, DEBUG_LEAVE, json_util_add_string(), json_util_finish_array(), json_util_start_array(), JWT_PAYLOAD_NAME_SCHEMAS, JWT_PAYLOAD_VALUE_SCHEMAS, MCL_NULL, and MCL_OK.

Referenced by _create_self_issued_jwt_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

static E_MCL_ERROR_CODE _calculate_signature ( jwt_t jwt,
string_t header_and_payload,
string_t **  signature 
)
static
static E_MCL_ERROR_CODE _create_self_issued_jwt_header ( E_MCL_SECURITY_PROFILE  security_profile,
json_t **  header 
)
static
static E_MCL_ERROR_CODE _generate_token ( jwt_t jwt,
string_t header,
string_t payload,
string_t **  token 
)
static

Definition at line 233 of file jwt.c.

References _calculate_signature(), _get_header_and_payload_encoded_base64_url(), _join_with_dot(), ASSERT_CODE_MESSAGE, DEBUG_ENTRY, DEBUG_LEAVE, MCL_ERROR_RETURN, MCL_FAIL, MCL_NULL, MCL_OK, and string_destroy().

Referenced by jwt_get_token().

Here is the call graph for this function:

Here is the caller graph for this function:

static E_MCL_ERROR_CODE _get_header_and_payload_encoded_base64_url ( string_t header,
string_t payload,
string_t **  header_encoded,
string_t **  payload_encoded 
)
static

Definition at line 262 of file jwt.c.

References ASSERT_CODE_MESSAGE, string_t::buffer, DEBUG_ENTRY, DEBUG_LEAVE, string_t::length, MCL_ERROR_RETURN, MCL_FAIL, MCL_OK, security_handler_base64_url_encode(), and string_destroy().

Referenced by _generate_token().

Here is the call graph for this function:

Here is the caller graph for this function:

static E_MCL_ERROR_CODE _join_with_dot ( string_t string_a,
string_t string_b,
string_t **  joined 
)
static

Definition at line 286 of file jwt.c.

References ASSERT_CODE_MESSAGE, DEBUG_ENTRY, DEBUG_LEAVE, string_array_t::index, MCL_ERROR_RETURN, MCL_FAIL, MCL_FALSE, MCL_NULL, MCL_OK, string_array_add(), string_array_destroy(), string_array_initialize(), string_array_to_string(), and string_destroy().

Referenced by _generate_token().

Here is the call graph for this function:

Here is the caller graph for this function:

void jwt_destroy ( jwt_t **  jwt)

To destroy the JWT Handler.

Will release the resources of JWT Handler. After destroy operation, handler shouldn't be used.

Parameters
[in]jwtJWT Handler to destroy

Definition at line 128 of file jwt.c.

References DEBUG_ENTRY, DEBUG_LEAVE, json_util_destroy(), MCL_DEBUG, MCL_FREE, and MCL_NULL.

Referenced by _compose_access_token_request_payload(), and jwt_initialize().

Here is the call graph for this function:

Here is the caller graph for this function:

string_t* jwt_get_token ( jwt_t jwt)

Used to generate the JWT Token as json string.

Parameters
[in]jwtJWT Handler.
Returns
Pointer to the JWT token of type string_t or NULL in case of an error.

Definition at line 97 of file jwt.c.

References _generate_token(), DEBUG_ENTRY, DEBUG_LEAVE, jwt_t::header, json_util_to_string(), MCL_NULL, MCL_OK, jwt_t::payload, string_destroy(), and string_initialize_dynamic().

Referenced by _compose_access_token_request_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

E_MCL_ERROR_CODE jwt_initialize ( security_handler_t security_handler,
E_MCL_SECURITY_PROFILE  security_profile,
string_t tenant,
jwt_t **  jwt 
)

JWT Initializer.

Parameters
[in]security_handlerAlready initialized security handler. All JWT operations will be done using this object.
[in]security_profileOnboarding security profile. Important to get correct kind of authentication JWT.
[in]tenantTenant which will be set in JWT payload.
[out]jwtThe newly initialized jwt handler.
Returns
  • MCL_OK in case of success.
  • MCL_OUT_OF_MEMORY in case there is not enough memory in the system to proceed.
  • MCL_FAIL in case JSON object can not be created.

Definition at line 70 of file jwt.c.

References _create_self_issued_jwt_header(), _create_self_issued_jwt_payload(), ASSERT_CODE_MESSAGE, ASSERT_STATEMENT_CODE_MESSAGE, DEBUG_ENTRY, DEBUG_LEAVE, jwt_destroy(), MCL_NEW, MCL_NULL, MCL_OK, and MCL_OUT_OF_MEMORY.

Referenced by _compose_access_token_request_payload().

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

string_t dot = { ".", 1, MCL_STRING_NOT_COPY_NOT_DESTROY }
static

Definition at line 68 of file jwt.c.