Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: APKAM enrollmentId support and atkeys_file refactor #400

Merged
merged 18 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions examples/desktop/at_talk/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <atchops/iv.h>
#include <atclient/atclient.h>
#include <atclient/atclient_utils.h>
#include <atclient/atkeysfile.h>
#include <atclient/atkeys_file.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This header file was renamed to conform to our naming standards

#include <atclient/constants.h>
#include <atclient/encryption_key_helpers.h>
#include <atclient/monitor.h>
Expand Down Expand Up @@ -145,23 +145,23 @@ int main(int argc, char *argv[]) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_create_shared_key: %d\n", ret);
}

if((ret = atclient_notify_params_set_operation(&params, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) {
if ((ret = atclient_notify_params_set_operation(&params, ATCLIENT_NOTIFY_OPERATION_UPDATE)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_operation: %d\n", ret);
}

if((ret = atclient_notify_params_set_atkey(&params, &atkey)) != 0) {
if ((ret = atclient_notify_params_set_atkey(&params, &atkey)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_atkey: %d\n", ret);
}

if((ret = atclient_notify_params_set_value(&params, line)) != 0) {
if ((ret = atclient_notify_params_set_value(&params, line)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_value: %d\n", ret);
}

if((ret = atclient_notify_params_set_should_encrypt(&params, true)) != 0) {
if ((ret = atclient_notify_params_set_should_encrypt(&params, true)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_should_encrypt: %d\n", ret);
}

if((ret = atclient_notify_params_set_notification_expiry(&params, 5000)) != 0) {
if ((ret = atclient_notify_params_set_notification_expiry(&params, 5000)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_notify_params_set_notification_expiry: %d\n", ret);
}

Expand Down Expand Up @@ -235,7 +235,7 @@ static void *monitor_handler(void *xargs) {
const char *from_atsign = args->from_atsign;

pthread_mutex_lock(&monitor_mutex);
if ((ret = atclient_monitor_pkam_authenticate(monitor, atserver_host, atserver_port, atkeys, from_atsign)) != 0) {
if ((ret = atclient_monitor_pkam_authenticate(monitor, from_atsign, atkeys, NULL)) != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signature is changed, host and port are now part of the options struct (which can be NULL, and the pkam_authenticate will look it up for you)

atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_pkam_authenticate: %d\n", ret);
goto exit;
}
Expand Down Expand Up @@ -353,7 +353,7 @@ static int reconnect_clients(atclient *monitor, atclient *ctx, const char *atser
* 2. Reconnect monitor connection
*/
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "Reconnecting monitor connection...\n");
if ((ret = atclient_monitor_pkam_authenticate(monitor, atserver_host, atserver_port, atkeys, from_atsign)) != 0) {
if ((ret = atclient_monitor_pkam_authenticate(monitor, from_atsign, atkeys, NULL)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_pkam_authenticate: %d\n", ret);
return ret;
}
Expand Down
8 changes: 2 additions & 6 deletions examples/desktop/events/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ int main(int argc, char *argv[]) {
atclient monitor_conn;
atclient_monitor_init(&monitor_conn);

atclient_pkam_authenticate_options options;
atclient_pkam_authenticate_options_init(&options);

atclient_monitor_response message;

if ((ret = get_atsign_input(argc, argv, &atsign)) != 0) {
Expand All @@ -50,12 +47,12 @@ int main(int argc, char *argv[]) {
goto exit;
}

if ((ret = atclient_pkam_authenticate(&atclient2, atsign, &atkeys, &options)) != 0) {
if ((ret = atclient_pkam_authenticate(&atclient2, atsign, &atkeys, NULL)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM\n");
goto exit;
}

if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, options.atserver_host, options.atserver_port, &atkeys, atsign)) != 0) {
if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atsign, &atkeys, NULL)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate monitor with PKAM\n");
goto exit;
}
Expand Down Expand Up @@ -122,7 +119,6 @@ exit: {
free(atsign);
atclient_monitor_free(&monitor_conn);
atclient_monitor_response_free(&message);
atclient_pkam_authenticate_options_free(&options);
return ret;
}
}
Expand Down
6 changes: 1 addition & 5 deletions examples/desktop/events/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ int main(int argc, char *argv[]) {
atclient_notify_params notify_params;
atclient_notify_params_init(&notify_params);

atclient_pkam_authenticate_options options;
atclient_pkam_authenticate_options_init(&options);

const char *homedir;

char *atsign_input = NULL;
Expand Down Expand Up @@ -90,7 +87,7 @@ int main(int argc, char *argv[]) {



if ((ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, &options)) != 0) {
if ((ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, NULL)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n");
goto exit;
}
Expand Down Expand Up @@ -139,7 +136,6 @@ int main(int argc, char *argv[]) {
exit: {
atclient_atkeys_free(&atkeys);
atclient_atkey_free(&atkey);
atclient_pkam_authenticate_options_free(&options);
atclient_free(&atclient);
free(atkeystr);
return ret;
Expand Down
10 changes: 3 additions & 7 deletions examples/desktop/events/resilient_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ int main(int argc, char *argv[]) {
atclient monitor_conn;
atclient_monitor_init(&monitor_conn);

atclient_pkam_authenticate_options options;
atclient_pkam_authenticate_options_init(&options);

atclient_monitor_response *message = NULL;

if ((ret = get_atsign_input(argc, argv, &atsign)) != 0) {
Expand All @@ -55,12 +52,12 @@ int main(int argc, char *argv[]) {
goto exit;
}

if ((ret = atclient_pkam_authenticate(&atclient2, atsign, &atkeys, &options)) != 0) {
if ((ret = atclient_pkam_authenticate(&atclient2, atsign, &atkeys, NULL)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate with PKAM\n");
goto exit;
}

if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atserver_host, atserver_port, &atkeys, atsign)) != 0) {
if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atsign, &atkeys, NULL)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate monitor with PKAM\n");
goto exit;
}
Expand Down Expand Up @@ -133,7 +130,7 @@ int main(int argc, char *argv[]) {
if (tries >= max_tries) {
if (!atclient_monitor_is_connected(&monitor_conn)) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "We are not connected :( attempting reconnection\n");
if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atserver_host, atserver_port, &atkeys, atsign)) !=
if ((ret = atclient_monitor_pkam_authenticate(&monitor_conn, atsign, &atkeys, NULL)) !=
0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate monitor with PKAM\n");
continue;
Expand All @@ -158,7 +155,6 @@ exit: {
atclient_monitor_free(&monitor_conn);
atclient_monitor_response_free(message);
atclient_free(&atclient2);
atclient_pkam_authenticate_options_free(&options);
return ret;
}
}
Expand Down
5 changes: 1 addition & 4 deletions examples/desktop/pkam_authenticate/develop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
set -eu
FULL_PATH_TO_SCRIPT="$(realpath "${BASH_SOURCE[0]}")"
SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")"
cd "$SCRIPT_DIRECTORY/../../../packages/atclient"
cmake -S . -B build
sudo cmake --build build --target install
pwd
"$SCRIPT_DIRECTORY/../../../tools/install.sh"
cd "$SCRIPT_DIRECTORY"
cmake -S . -B build
cmake --build build --target all
Expand Down
47 changes: 19 additions & 28 deletions examples/desktop/pkam_authenticate/src/main.c
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
#include <atclient/atclient.h>
#include <atclient/atkeysfile.h>
#include <atclient/atkeys_file.h>
#include <atlogger/atlogger.h>
#include <stdio.h>

#define ROOT_HOST "root.atsign.org"
#define ROOT_PORT 64

#define ATKEYSFILE_PATH "/home/sitaram/.atsign/keys/@actingqualified_key.atKeys"
#define ATSIGN "@actingqualified"
#define ATKEYS_FILE_PATH "/Users/jeremytubongbanua/.atsign/keys/@smoothalligator_key.atKeys"
#define ATSIGN "@smoothalligator"

#define TAG "pkam_authenticate"

int main(int argc, char **argv) {
int ret = 1;

atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_INFO);
atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG);

// 1a. read `atkeysfile` struct
atclient_atkeysfile atkeysfile;
atclient_atkeysfile_init(&atkeysfile);
ret = atclient_atkeysfile_read(&atkeysfile, ATKEYSFILE_PATH);

if (ret != 0) {
goto exit;
}
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_file_read: %d\n", ret);
atclient_atkeysfile atkeys_file;
atclient_atkeysfile_init(&atkeys_file);

// 1b. populate `atkeys` struct
atclient_atkeys atkeys;
atclient_atkeys_init(&atkeys);
ret = atclient_atkeys_populate_from_atkeysfile(&atkeys, atkeysfile);

atclient_pkam_authenticate_options options;
atclient_pkam_authenticate_options_init(&options);

if (ret != 0) {
atclient atclient;
atclient_init(&atclient);

if ((ret = atclient_atkeysfile_from_path(&atkeys_file, ATKEYS_FILE_PATH)) != 0) {
goto exit;
}
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_populate_from_atkeysfile: %d\n", ret);
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeysfile_read: %d\n", ret);

// 2. pkam auth
atclient atclient;
atclient_init(&atclient);
if ((ret = atclient_atkeys_populate_from_atkeys_file(&atkeys, &atkeys_file)) != 0) {
goto exit;
}
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "atclient_atkeys_populate_from_atkeys_file: %d\n", ret);

const char *atsign = ATSIGN;
if ((ret = atclient_pkam_authenticate(&atclient, ATSIGN, &atkeys, &options)) != 0) {

if ((ret = atclient_pkam_authenticate(&atclient, ATSIGN, &atkeys, NULL)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to authenticate\n");
goto exit;
} else {
Expand All @@ -55,10 +47,9 @@ int main(int argc, char **argv) {
goto exit;

exit: {
atclient_atkeysfile_free(&atkeysfile);
atclient_atkeysfile_free(&atkeys_file);
atclient_atkeys_free(&atkeys);
atclient_free(&atclient);
atclient_pkam_authenticate_options_free(&options);
return 0;
}
}
}
16 changes: 8 additions & 8 deletions examples/desktop/repl/src/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <atclient/atclient.h>
#include <atclient/atclient_utils.h>
#include <atclient/atkeys.h>
#include <atclient/atkeysfile.h>
#include <atclient/atkeys_file.h>
#include <atclient/connection.h>
#include <atlogger/atlogger.h>
#include <pwd.h>
Expand All @@ -23,9 +23,9 @@ int main(int argc, char *argv[]) {

atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG);

const short atkeysfilepathsize = 256;
char atkeysfilepath[atkeysfilepathsize];
memset(atkeysfilepath, 0, sizeof(char) * atkeysfilepathsize); // Clear the buffer (for safety)
const short atkeys_filepathsize = 256;
char atkeys_filepath[atkeys_filepathsize];
memset(atkeys_filepath, 0, sizeof(char) * atkeys_filepathsize); // Clear the buffer (for safety)

const size_t buffersize = 2048;
char buffer[buffersize];
Expand Down Expand Up @@ -79,12 +79,12 @@ int main(int argc, char *argv[]) {
goto exit;
}

sprintf(atkeysfilepath, "%s/.atsign/keys/%s_key.atKeys", homedir, atsign);
if ((ret = atclient_atkeys_populate_from_path(&atkeys, atkeysfilepath)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read atKeys file at path \"%s\"\n", atkeysfilepath);
sprintf(atkeys_filepath, "%s/.atsign/keys/%s_key.atKeys", homedir, atsign);
if ((ret = atclient_atkeys_populate_from_path(&atkeys, atkeys_filepath)) != 0) {
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to read atKeys file at path \"%s\"\n", atkeys_filepath);
goto exit;
}
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Successfully read atKeys file at path %s\n", atkeysfilepath);
atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Successfully read atKeys file at path %s\n", atkeys_filepath);

ret = atclient_pkam_authenticate(&atclient, atsign, &atkeys, &options);
if (ret != 0) {
Expand Down
19 changes: 9 additions & 10 deletions packages/atclient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ set(
${CMAKE_CURRENT_LIST_DIR}/src/atclient.c
${CMAKE_CURRENT_LIST_DIR}/src/atkey.c
${CMAKE_CURRENT_LIST_DIR}/src/atkeys.c
${CMAKE_CURRENT_LIST_DIR}/src/atkeysfile.c
${CMAKE_CURRENT_LIST_DIR}/src/atkeys_file.c
${CMAKE_CURRENT_LIST_DIR}/src/atnotification.c
${CMAKE_CURRENT_LIST_DIR}/src/connection_hooks.c
${CMAKE_CURRENT_LIST_DIR}/src/connection.c
Expand Down Expand Up @@ -49,11 +49,11 @@ if(ESP_PLATFORM)
TARGET ${COMPONENT_LIB}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_directory ${ATCLIENT_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/include
${CMAKE_COMMAND} -E copy_directory ${ATCLIENT_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/include
COMMAND
${CMAKE_COMMAND} -E copy $<TARGET_FILE:${COMPONENT_LIB}>
${CMAKE_SOURCE_DIR}/lib/lib${COMPONENT_NAME}.a
${CMAKE_COMMAND} -E copy $<TARGET_FILE:${COMPONENT_LIB}>
${CMAKE_SOURCE_DIR}/lib/lib${COMPONENT_NAME}.a
COMMENT "Copying built archive file and header to lib directory..."
)
endif()
Expand Down Expand Up @@ -111,9 +111,9 @@ if(NOT ESP_PLATFORM)
target_include_directories(
${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${ATCLIENT_INCLUDE_DIR}>
$<BUILD_INTERFACE:${cjson_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${ATCLIENT_INCLUDE_DIR}>
$<BUILD_INTERFACE:${cjson_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# Link dependencies to library targets
Expand Down Expand Up @@ -150,9 +150,8 @@ if(NOT ESP_PLATFORM)
)

# EXPORT
if(NOT ATCLIENT_AS_SUBPROJECT)
if(NOT ATCLIENT_AS_SUBPROJECT AND ATCLIENT_EXPORT)
# Export the library
export(PACKAGE ${PROJECT_NAME})
Comment on lines -153 to -155
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't export the package by default


# install as a config.cmake
install(
Expand Down
Loading