Jelajahi Sumber

added simple logging interface for logins

stefan 1 tahun lalu
induk
melakukan
cbaacd10cf
2 mengubah file dengan 27 tambahan dan 3 penghapusan
  1. 10 1
      api/shellmatta.h
  2. 17 2
      src/shellmatta_auth.c

+ 10 - 1
api/shellmatta.h

@@ -130,6 +130,13 @@ typedef struct
  */
 typedef shellmatta_retCode_t (*shellmatta_auth_check_t)(const uint32_t userId, const char* password);
 
+/**
+ * @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
  */
@@ -210,6 +217,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                     */
 #endif
 } shellmatta_instance_t;
 
@@ -276,7 +284,8 @@ shellmatta_retCode_t shellmatta_auth_init(                  shellmatta_handle_t
                                                             shellmatta_auth_perm_t  *permList,
                                                             uint32_t                permListLength,
                                                             bool                    customLogin,
-                                                            shellmatta_auth_check_t checkFct);
+                                                            shellmatta_auth_check_t checkFct,
+                                                            shellmatta_auth_log_t   logFct);
 shellmatta_retCode_t shellmatta_auth_login(                 shellmatta_handle_t     handle,
                                                             uint32_t                userId);
 shellmatta_retCode_t shellmatta_auth_logout(                shellmatta_handle_t     handle);

+ 17 - 2
src/shellmatta_auth.c

@@ -128,6 +128,12 @@ 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);
+    }
+
     /** -# print login result */
     if (0 == approvedUserId)
     {
@@ -301,6 +307,7 @@ const shellmatta_cmd_t shellmatta_auth_logoutCmd = {"logout"
  * @param[in]   permListLength  length of the perm list
  * @param[in]   customLogin     true: no internal login command is provided
  * @param[in]   checkFct        pointer to custom credential check function of type shellmatta_check_t
+ * @param[in]   logFct          pointer to login log function of type shellmatta_auth_log_t
  * @return      errorcode   #SHELLMATTA_OK
  *                          #SHELLMATTA_USE_FAULT (param err)
  */
@@ -310,7 +317,8 @@ shellmatta_retCode_t shellmatta_auth_init(shellmatta_handle_t       handle,
                                           shellmatta_auth_perm_t    *permList,
                                           uint32_t                  permListLength,
                                           bool                      customLogin,
-                                          shellmatta_auth_check_t   checkFct)
+                                          shellmatta_auth_check_t   checkFct,
+                                          shellmatta_auth_log_t     logFct)
 {
     shellmatta_retCode_t    ret     = SHELLMATTA_OK;
     shellmatta_instance_t   *inst   = (shellmatta_instance_t*)handle;
@@ -337,7 +345,8 @@ shellmatta_retCode_t shellmatta_auth_init(shellmatta_handle_t       handle,
         {
             inst->helpCmd.next = &inst->logoutCmd;
         }
-        inst->checkFct = checkFct;
+        inst->checkFct  = checkFct;
+        inst->logFct    = logFct;
 
         /** -# assign links to the perm list to each command */
         cmd = inst->cmdList;
@@ -401,6 +410,12 @@ shellmatta_retCode_t shellmatta_auth_login(shellmatta_handle_t handle, uint32_t
         {
             inst->userPointer = NULL;
         }
+
+        /** -# call log function */
+        if (inst->logFct)
+        {
+            inst->logFct(userId, userId != 0u ? true : false);
+        }
     }
     else
     {