From 08fa8c559b42fb07bdadd62369802c348b896607 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 28 Jul 2016 12:24:45 +0200 Subject: Choose device model during connection Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 4 ++-- NK_C_API.h | 2 +- NitrokeyManager.cc | 13 +++++++++++-- include/NitrokeyManager.h | 2 +- unittest/test_bindings.py | 2 +- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/NK_C_API.cc b/NK_C_API.cc index fac8a3c..e6151fc 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -68,10 +68,10 @@ extern uint8_t NK_get_last_command_status(){ return _copy; } -extern int NK_login(const char *admin_pin, const char *temporary_password) { +extern int NK_login(const char *admin_pin, const char *temporary_password, const char *device_model) { auto m = NitrokeyManager::instance(); try { - m->connect(); + m->connect(device_model); m->first_authenticate(admin_pin, temporary_password); NK_last_command_status = 0; } diff --git a/NK_C_API.h b/NK_C_API.h index 458dd31..5520099 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -10,7 +10,7 @@ extern "C" { //Make sure each function's declaration is in one line (for automatic python declaration processing) extern void NK_set_debug(bool state); -extern int NK_login(const char *admin_pin, const char *temporary_password); +extern int NK_login(const char *admin_pin, const char *temporary_password, const char *device_model); extern int NK_logout(); extern const char * NK_status(); extern uint8_t NK_get_last_command_status(); diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index caf9724..3697369 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -36,8 +36,17 @@ namespace nitrokey{ } NitrokeyManager::~NitrokeyManager() {delete _instance; delete device;} - bool NitrokeyManager::connect() { - device = new Stick10(); + bool NitrokeyManager::connect(const char *device_model) { + switch (device_model[0]){ + case 'P': + device = new Stick10(); + break; + case 'S': + device = new Stick20(); + break; + default: + throw std::runtime_error("Unknown model"); + } return device->connect(); } diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 3a86597..3acb229 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -30,7 +30,7 @@ namespace nitrokey { bool get_time(); bool erase_totp_slot(uint8_t slot_number, const char *temporary_password); bool erase_hotp_slot(uint8_t slot_number, const char *temporary_password); - bool connect(); + bool connect(const char *device_model); bool disconnect(); void set_debug(bool state); string get_status(); diff --git a/unittest/test_bindings.py b/unittest/test_bindings.py index cba5ba4..8d6169c 100644 --- a/unittest/test_bindings.py +++ b/unittest/test_bindings.py @@ -39,7 +39,7 @@ def C(request): C = ffi.dlopen("../build/libnitrokey.so") C.NK_set_debug(False) - C.NK_login(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP) + C.NK_login(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP, 'P') assert C.NK_user_authenticate(DefaultPasswords.USER, DefaultPasswords.USER_TEMP) == DeviceErrorCode.STATUS_OK # C.NK_status() -- cgit v1.2.3