Kaynağa Gözat

changed the bool success to a flexible enum event in log function interface
added a log to the logout function

Strobel, Stefan | Friedrich Lütze GmbH 9 ay önce
ebeveyn
işleme
61ffd6fa42

+ 25 - 14
api/shellmatta.h

@@ -138,6 +138,27 @@ typedef struct
     const uint32_t userIdslength; /**< length of the user list                  */
 } shellmatta_auth_perm_t;
 
+/**
+ * @brief login states
+ */
+typedef enum
+{
+    SHELLMATTA_AUTH_IDLE,       /**< authentication system waits    */
+    SHELLMATTA_AUTH_USERNAME,   /**< input of username              */
+    SHELLMATTA_AUTH_PASSWORD    /**< input of password              */
+} shellmatta_auth_state_t;
+
+/**
+ * @brief log actions - passed to the log function
+ */
+typedef enum
+{
+    SHELLMATTA_AUTH_EVENT_NONE,         /**< no event (init value)      */
+    SHELLMATTA_AUTH_EVENT_LOGIN,        /**< successful login event     */
+    SHELLMATTA_AUTH_EVENT_LOGIN_FAILED, /**< failed login event         */
+    SHELLMATTA_AUTH_EVENT_LOGOUT,       /**< succesful logout event     */
+} shellmatta_auth_log_event_t;
+
 /**
  * @brief custom shellmatta authentication method
  * @param[in]   userId      user id to log in (name of the user role)
@@ -148,20 +169,10 @@ typedef shellmatta_retCode_t (*shellmatta_auth_check_t)(const uint32_t userId, c
 
 /**
  * @brief shellmatta authentication log method - will be called whenever a login attempt is done
- * @param[in]   userId      userId to be logged in (0 on unknown user)
- * @param[in]   success     true: the login succeeded
- */
-typedef void (*shellmatta_auth_log_t)(const uint32_t userId, bool success);
-
-/**
- * @brief login states
+ * @param[in]   userId  userId to be logged in (0 on unknown user)
+ * @param[in]   event   event type to be logged (e.g. successful login)
  */
-typedef enum
-{
-    SHELLMATTA_AUTH_IDLE,       /**< authentication system waits    */
-    SHELLMATTA_AUTH_USERNAME,   /**< input of username              */
-    SHELLMATTA_AUTH_PASSWORD    /**< input of password              */
-} shellmatta_auth_state_t;
+typedef void (*shellmatta_auth_log_t)(const uint32_t userId, shellmatta_auth_log_event_t event);
 
 #endif
 
@@ -233,7 +244,7 @@ typedef struct
     shellmatta_auth_perm_t      *permList;      /**< permission list                        */
     uint32_t                    permListLength; /**< length of the permission list          */
     shellmatta_auth_check_t     checkFct;       /**< custom credential check function       */
-    shellmatta_auth_log_t       logFct;         /**< login log function                     */
+    shellmatta_auth_log_t       logFct;         /**< auth log function                      */
 #endif
 } shellmatta_instance_t;
 

+ 13 - 2
src/shellmatta_auth.c

@@ -138,7 +138,7 @@ void checkPassword(shellmatta_handle_t handle, uint32_t userId, char *password)
     /** -# call log function - only for unsuccessful logins - successful logins are handled by #shellmatta_auth_login */
     if ((inst->logFct) && (0u == approvedUserId))
     {
-        inst->logFct(userId, false);
+        inst->logFct(userId, SHELLMATTA_AUTH_EVENT_LOGIN_FAILED);
     }
 
     /** -# print login result */
@@ -302,6 +302,14 @@ const shellmatta_cmd_t shellmatta_auth_loginCmd = {"login"
  */
 static shellmatta_retCode_t logoutCmdFct(const shellmatta_handle_t handle, const char *arguments, uint32_t length)
 {
+    shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
+
+    /** -# log the logout event */
+    if (inst->logFct)
+    {
+        inst->logFct(inst->userId, SHELLMATTA_AUTH_EVENT_LOGOUT);
+    }
+
     shellmatta_auth_logout(handle);
     shellmatta_write(handle, "good bye\r\n", 10);
 
@@ -440,7 +448,10 @@ shellmatta_retCode_t shellmatta_auth_login(shellmatta_handle_t handle, uint32_t
         /** -# call log function */
         if (inst->logFct)
         {
-            inst->logFct(userId, NULL != inst->userPointer ? true : false);
+            inst->logFct(userId,
+                         NULL != inst->userPointer ?                \
+                                 SHELLMATTA_AUTH_EVENT_LOGIN :      \
+                                 SHELLMATTA_AUTH_EVENT_LOGIN_FAILED);
         }
     }
     else

+ 16 - 6
test/integrationtest_auth/test_integration_auth.cpp

@@ -23,7 +23,7 @@ static char write_data[1024];
 static uint32_t write_length;
 static uint32_t privateCallCount;
 static uint32_t logUserId;
-static bool logSuccess;
+static shellmatta_auth_log_event_t logEvent;
 
 #define TEST_SHELLMATTA_SETUP   shellmatta_retCode_t ret;                   \
                                 shellmatta_instance_t inst;                 \
@@ -46,7 +46,7 @@ static bool logSuccess;
                                 write_length = 0u;                          \
                                 privateCallCount = 0u;                      \
                                 logUserId = 255u;                           \
-                                logSuccess = false;                         \
+                                logEvent = SHELLMATTA_AUTH_EVENT_NONE;      \
                                                                             \
                                 shellmatta_addCmd(handle, &publicCmd);      \
                                 shellmatta_addCmd(handle, &privateCmd);
@@ -655,10 +655,10 @@ shellmatta_retCode_t customLogin(const uint32_t userId, const char *password)
     return SHELLMATTA_ERROR;
 }
 
-void logFct(const uint32_t userId, bool success)
+void logFct(const uint32_t userId, shellmatta_auth_log_event_t event)
 {
     logUserId = userId;
-    logSuccess = success;
+    logEvent = event;
 }
 
 SCENARIO("Check custom login") {
@@ -700,11 +700,21 @@ SCENARIO("Check custom login") {
 
                 AND_THEN("The shimatta user is logged in") {
                     REQUIRE(1 == shellmatta_auth_getLoggedInUserId(handle));
+
+                    AND_WHEN("The shimatta user logs out") {
+                        ret = shellmatta_processData(handle, (char*)"logout\r", 7);
+                        CHECK(ret == SHELLMATTA_OK);
+                        THEN("The logout event is logged") {
+                            CHECK(1 == logUserId);
+                            REQUIRE(SHELLMATTA_AUTH_EVENT_LOGOUT == logEvent);
+                        }
+                    }
+
                 }
 
                 AND_THEN("The login event is logged") {
                     CHECK(1 == logUserId);
-                    REQUIRE(true == logSuccess);
+                    REQUIRE(SHELLMATTA_AUTH_EVENT_LOGIN == logEvent);
                 }
             }
         }
@@ -735,7 +745,7 @@ SCENARIO("Check custom login") {
 
                 AND_THEN("The failed login event is logged") {
                     CHECK(1 == logUserId);
-                    REQUIRE(false == logSuccess);
+                    REQUIRE(SHELLMATTA_AUTH_EVENT_LOGIN_FAILED == logEvent);
                 }
             }
         }