Skip to content
Permalink
Browse files

cleanup: include/: move zephyr/jwt.h to data/jwt.h

move zephyr/jwt.h to data/jwt.h and
create a shim for backward-compatibility.

No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.

Related to #16539

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
  • Loading branch information...
nashif committed Jun 25, 2019
1 parent 0abdacf commit 4be2e9ebb6c761365c99c1e9c962852dc9aafb0e
Showing with 107 additions and 92 deletions.
  1. +96 −0 include/data/jwt.h
  2. +8 −89 include/zephyr/jwt.h
  3. +1 −1 samples/net/google_iot_mqtt/src/protocol.c
  4. +1 −1 subsys/jwt/jwt.c
  5. +1 −1 tests/subsys/jwt/src/main.c
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2018 Linaro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_DATA_JWT_H_
#define ZEPHYR_INCLUDE_DATA_JWT_H_

#include <zephyr/types.h>
#include <stdbool.h>

/**
* @brief JSON Web Token (JWT)
* @defgroup jwt JSON Web Token (JWT)
* @ingroup structured_data
* @{
*/

/**
* @brief JWT data tracking.
*
* JSON Web Tokens contain several sections, each encoded in base-64.
* This structure tracks the token as it is being built, including
* limits on the amount of available space. It should be initialized
* with jwt_init().
*/
struct jwt_builder {
/** The base of the buffer we are writing to. */
char *base;

/** The place in this buffer where we are currently writing.
*/
char *buf;

/** The length remaining to write. */
size_t len;

/**
* Flag that is set if we try to write past the end of the
* buffer. If set, the token is not valid.
*/
bool overflowed;

/* Pending bytes yet to be converted to base64. */
unsigned char wip[3];

/* Number of pending bytes. */
int pending;
};

/**
* @brief Initialize the JWT builder.
*
* Initialize the given JWT builder for the creation of a fresh token.
* The buffer size should at least be as long as JWT_BUILDER_MAX_SIZE
* returns.
*
* @param builder The builder to initialize.
* @param buffer The buffer to write the token to.
* @param buffer_size The size of this buffer. The token will be NULL
* terminated, which needs to be allowed for in this size.
*
* @retval 0 Success
* @retval -ENOSPC Buffer is insufficient to initialize
*/
int jwt_init_builder(struct jwt_builder *builder,
char *buffer,
size_t buffer_size);

/**
* @brief add JWT primary payload.
*/
int jwt_add_payload(struct jwt_builder *builder,
s32_t exp,
s32_t iat,
const char *aud);

/**
* @brief Sign the JWT token.
*/
int jwt_sign(struct jwt_builder *builder,
const char *der_key,
size_t der_key_len);


static inline size_t jwt_payload_len(struct jwt_builder *builder)
{
return (builder->buf - builder->base);
}

/**
* @}
*/

#endif /* ZEPHYR_INCLUDE_DATA_JWT_H_ */
@@ -1,96 +1,15 @@
/*
* Copyright (c) 2018 Linaro Ltd
* Copyright (c) 2019 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_ZEPHYR_JWT_H_
#define ZEPHYR_INCLUDE_ZEPHYR_JWT_H_

#ifndef ZEPHYR_INCLUDE_JWT_H_
#define ZEPHYR_INCLUDE_JWT_H_

#include <zephyr/types.h>
#include <stdbool.h>

/**
* @brief JSON Web Token (JWT)
* @defgroup jwt JSON Web Token (JWT)
* @ingroup structured_data
* @{
*/

/**
* @brief JWT data tracking.
*
* JSON Web Tokens contain several sections, each encoded in base-64.
* This structure tracks the token as it is being built, including
* limits on the amount of available space. It should be initialized
* with jwt_init().
*/
struct jwt_builder {
/** The base of the buffer we are writing to. */
char *base;

/** The place in this buffer where we are currently writing.
*/
char *buf;

/** The length remaining to write. */
size_t len;

/**
* Flag that is set if we try to write past the end of the
* buffer. If set, the token is not valid.
*/
bool overflowed;

/* Pending bytes yet to be converted to base64. */
unsigned char wip[3];

/* Number of pending bytes. */
int pending;
};

/**
* @brief Initialize the JWT builder.
*
* Initialize the given JWT builder for the creation of a fresh token.
* The buffer size should at least be as long as JWT_BUILDER_MAX_SIZE
* returns.
*
* @param builder The builder to initialize.
* @param buffer The buffer to write the token to.
* @param buffer_size The size of this buffer. The token will be NULL
* terminated, which needs to be allowed for in this size.
*
* @retval 0 Success
* @retval -ENOSPC Buffer is insufficient to initialize
*/
int jwt_init_builder(struct jwt_builder *builder,
char *buffer,
size_t buffer_size);

/**
* @brief add JWT primary payload.
*/
int jwt_add_payload(struct jwt_builder *builder,
s32_t exp,
s32_t iat,
const char *aud);

/**
* @brief Sign the JWT token.
*/
int jwt_sign(struct jwt_builder *builder,
const char *der_key,
size_t der_key_len);


static inline size_t jwt_payload_len(struct jwt_builder *builder)
{
return (builder->buf - builder->base);
}
#ifndef CONFIG_COMPAT_INCLUDES
#warning "This header file has moved, include <data/jwt.h> instead."
#endif

/**
* @}
*/
#include <data/jwt.h>

#endif
#endif /* ZEPHYR_INCLUDE_ZEPHYR_JWT_H_ */
@@ -12,7 +12,7 @@ LOG_MODULE_DECLARE(net_google_iot_mqtt, LOG_LEVEL_DBG);

#include <zephyr.h>
#include <string.h>
#include <zephyr/jwt.h>
#include <data/jwt.h>
#include <entropy.h>

#include <net/tls_credentials.h>
@@ -8,7 +8,7 @@
#include <zephyr/types.h>
#include <errno.h>

#include <zephyr/jwt.h>
#include <data/jwt.h>
#include <data/json.h>

#ifdef CONFIG_JWT_SIGN_RSA
@@ -21,7 +21,7 @@
#include <stdbool.h>
#include <ztest.h>
#include <data/json.h>
#include <zephyr/jwt.h>
#include <data/jwt.h>

#include <mbedtls/pk.h>
#include <mbedtls/rsa.h>

0 comments on commit 4be2e9e

Please sign in to comment.
You can’t perform that action at this time.