Skip to content

Commit

Permalink
Add basic auth implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjaeger committed Jul 7, 2023
1 parent 3d9f3e4 commit a85cec4
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
west build -p -b esp32c3_devkitm samples/counter
west build -p -b native_posix samples/counter
west build -p -b nucleo_l073rz samples/counter -- -DOVERLAY_CONFIG=storage_eeprom.conf
west build -p -b native_posix samples/counter -- -DOVERLAY_CONFIG=auth.conf
west build -p -b native_posix samples/counter -- -DOVERLAY_CONFIG=can.conf
west build -p -b native_posix samples/counter -- -DOVERLAY_CONFIG=log_backend.conf
west build -p -b native_posix samples/counter -- -DOVERLAY_CONFIG=shell.conf
Expand Down
1 change: 1 addition & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ menuconfig THINGSET_SDK

if THINGSET_SDK

rsource "src/Kconfig.auth"
rsource "src/Kconfig.ble"
rsource "src/Kconfig.can"
rsource "src/Kconfig.log_backend"
Expand Down
4 changes: 4 additions & 0 deletions include/thingset/sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ extern "C" {
#define TS_ID_NODEID THINGSET_ID_NODEID
#define TS_ID_NODENAME 0x1E

/* Authentication */
#define TS_ID_AUTH 0x20
#define TS_ID_AUTH_TOKEN 0x200

/* LoRaWAN group items */
#define TS_ID_LORAWAN 0x27
#define TS_ID_LORAWAN_DEV_EUI 0x270
Expand Down
6 changes: 6 additions & 0 deletions samples/counter/auth.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) The ThingSet Project Contributors
# SPDX-License-Identifier: Apache-2.0

CONFIG_THINGSET_AUTH=y
CONFIG_THINGSET_AUTH_TOKEN_EXPERT="expert"
CONFIG_THINGSET_AUTH_TOKEN_MANUFACTURER="manufacturer"
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

target_sources(app PRIVATE sdk.c)

target_sources_ifdef(CONFIG_THINGSET_AUTH app PRIVATE auth.c)
target_sources_ifdef(CONFIG_THINGSET_BLE app PRIVATE ble.c)
target_sources_ifdef(CONFIG_THINGSET_CAN app PRIVATE can.c)
target_sources_ifdef(CONFIG_THINGSET_LOG_BACKEND app PRIVATE log_backend.c)
Expand Down
20 changes: 20 additions & 0 deletions src/Kconfig.auth
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) The ThingSet Project Contributors
# SPDX-License-Identifier: Apache-2.0

menuconfig THINGSET_AUTH
bool "Basic Authentication"

if THINGSET_AUTH

config THINGSET_AUTH_TOKEN_MAX_SIZE
int "Auth token max size"
range 8 65
default 16

config THINGSET_AUTH_TOKEN_EXPERT
string "Token to authenticate as expert user"

config THINGSET_AUTH_TOKEN_MANUFACTURER
string "Token to authenticate as manufacturer"

endif # THINGSET_AUTH
46 changes: 46 additions & 0 deletions src/auth.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) The ThingSet Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/logging/log.h>

#include <thingset.h>
#include <thingset/sdk.h>

LOG_MODULE_REGISTER(thingset_auth, CONFIG_LOG_DEFAULT_LEVEL);

static char auth_token[CONFIG_THINGSET_AUTH_TOKEN_MAX_SIZE];

static int32_t thingset_auth();

THINGSET_ADD_FN_INT32(TS_ID_ROOT, TS_ID_AUTH, "xAuth", &thingset_auth, THINGSET_ANY_RW);
THINGSET_ADD_ITEM_STRING(TS_ID_AUTH, TS_ID_AUTH_TOKEN, "uToken", auth_token, sizeof(auth_token),
THINGSET_ANY_RW, 0);

static int thingset_auth()
{
static const char token_exp[] = CONFIG_THINGSET_AUTH_TOKEN_EXPERT;
static const char token_mfr[] = CONFIG_THINGSET_AUTH_TOKEN_MANUFACTURER;

if (strlen(token_exp) == strlen(auth_token)
&& strncmp(auth_token, token_exp, strlen(token_exp)) == 0)
{
thingset_set_authentication(&ts, THINGSET_EXP_MASK | THINGSET_USR_MASK);
LOG_INF("Authenticated as expert user");
return 0;
}
else if (strlen(token_mfr) == strlen(auth_token)
&& strncmp(auth_token, token_mfr, strlen(token_mfr)) == 0)
{
thingset_set_authentication(&ts, THINGSET_MFR_MASK | THINGSET_USR_MASK);
LOG_INF("Authenticated as manufacturer");
return 0;
}
else {
thingset_set_authentication(&ts, THINGSET_USR_MASK);
LOG_INF("Authentication reset");
return -EINVAL;
}
}
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ manifest:
- picolibc
- name: thingset-node-c
remote: thingset
revision: 48a32e5d24bd39a0c91a244dfd94366d3f2de5ba
revision: 961bb0ba4c2087499c9ce4ac91173dcce03e5b7a
path: modules/thingset-node-c
import: true

0 comments on commit a85cec4

Please sign in to comment.