Browse Source

started implementing the interface to the authentication module

stefan 1 year ago
parent
commit
e3a608294f
4 changed files with 111 additions and 7 deletions
  1. 9 7
      README.md
  2. 46 0
      api/shellmatta.h
  3. 27 0
      src/shellmatta_auth.c
  4. 29 0
      src/shellmatta_auth.h

+ 9 - 7
README.md

@@ -35,6 +35,7 @@ The `shellmatta` piled up some features over time:
 2. auto complete
 2. auto complete
 3. heredoc like interface to pass multiline data
 3. heredoc like interface to pass multiline data
 4. option parser (getopt like)
 4. option parser (getopt like)
+5. simple authentication mechanism
 
 
 ## Documentation
 ## Documentation
 
 
@@ -140,13 +141,14 @@ int main(void)
 
 
 There are some defines you can use to change the behaviour of the shellmatta:
 There are some defines you can use to change the behaviour of the shellmatta:
 
 
-| Define                     | Description                                    |
-| -------------------------- | ---------------------------------------------- |
-| SHELLMATTA_STRIP_PRINTF    | removes stdio dependencies to reduce footprint |
-| SHELLMATTA_HELP_COMMAND    | string to overwrite the help command name      |
-| SHELLMATTA_HELP_ALIAS      | string to overwrite the help command alias     |
-| SHELLMATTA_HELP_HELP_TEXT  | string to overwrite the help command help      |
-| SHELLMATTA_HELP_USAGE_TEXT | string to overwrite the help command usage     |
+| Define                                 | Description                                    |
+| -------------------------------------- | ---------------------------------------------- |
+| SHELLMATTA_STRIP_PRINTF                | removes stdio dependencies to reduce footprint |
+| SHELLMATTA_HELP_COMMAND                | string to overwrite the help command name      |
+| SHELLMATTA_HELP_ALIAS                  | string to overwrite the help command alias     |
+| SHELLMATTA_HELP_HELP_TEXT              | string to overwrite the help command help      |
+| SHELLMATTA_HELP_USAGE_TEXT             | string to overwrite the help command usage     |
+| SHELLMATTA_AUTHENTICATION              | if defined this enables the authentication     |
 
 
 ## Example
 ## Example
 
 

+ 46 - 0
api/shellmatta.h

@@ -110,10 +110,35 @@ typedef struct shellmatta_cmd
     char                    *cmdAlias;  /**< command alias                          */
     char                    *cmdAlias;  /**< command alias                          */
     char                    *helpText;  /**< help text to print in "help" command   */
     char                    *helpText;  /**< help text to print in "help" command   */
     char                    *usageText; /**< usage text - printed on "help cmd"     */
     char                    *usageText; /**< usage text - printed on "help cmd"     */
+#ifdef SHELLMATTA_AUTHENTICATION
+    uint32_t                minRoleId;  /**< minimum user role id for this command  */
+#endif
     shellmatta_cmdFct_t     cmdFct;     /**< pointer to the cmd callack function    */
     shellmatta_cmdFct_t     cmdFct;     /**< pointer to the cmd callack function    */
     struct shellmatta_cmd   *next;      /**< pointer to next command or NULL        */
     struct shellmatta_cmd   *next;      /**< pointer to next command or NULL        */
 } shellmatta_cmd_t;
 } shellmatta_cmd_t;
 
 
+
+#ifdef SHELLMATTA_AUTHENTICATION
+/**
+ * @brief user role matrix
+ */
+typedef struct
+{
+    uint32_t    roleId;     /**< id of the user role (!= 0)                         */
+    const char  *username;  /**< name of the user role                              */
+    const char  *password;  /**< password of the user role or NULL (custom auth)    */
+} user_role_t;
+
+/**
+ * @brief custom shellmatta authentication method
+ * @param[in]   username    username to log in (name of the user role)
+ * @param[in]   password    password for the login
+ * @return      #SHELLMATTA_OK if the username and password is correct
+ */
+typedef shellmatta_retCode_t (*shellmatta_auth_check_t)(const char* username, const char* password);
+
+#endif
+
 /**
 /**
  * @brief structure of one shellmatta instance
  * @brief structure of one shellmatta instance
  */
  */
@@ -153,6 +178,10 @@ typedef struct
     bool                cmdListIsConst;     /**< true if the #cmdList was passed during
     bool                cmdListIsConst;     /**< true if the #cmdList was passed during
                                                  initialization                         */
                                                  initialization                         */
     shellmatta_opt_t    optionParser;       /**< option parser sructure                 */
     shellmatta_opt_t    optionParser;       /**< option parser sructure                 */
+#ifdef SHELLMATTA_AUTHENTICATION
+    uint32_t            roleId;             /**< role ID of the current session         */
+    user_role_t         *userRoleMatrix     /**< user role matrix structure             */
+#endif
 } shellmatta_instance_t;
 } shellmatta_instance_t;
 
 
 
 
@@ -210,6 +239,23 @@ shellmatta_retCode_t shellmatta_printf(     shellmatta_handle_t handle,
                                             ...);
                                             ...);
 #endif
 #endif
 
 
+#ifdef SHELLMATTA_AUTHENTICATION
+
+shellmatta_retCode_t shellmatta_auth_init(                  shellmatta_handle_t     handle,
+                                                            user_role_t             *userRoleMatrix,
+                                                            uint32_t                length,
+                                                            bool                    customLogin,
+                                                            shellmatta_auth_check_t checkFct);
+shellmatta_retCode_t shellmatta_auth_login(                 shellmatta_handle_t handle,
+                                                            uint32_t            roleId);
+shellmatta_retCode_t shellmatta_auth_logout(                shellmatta_handle_t handle);
+uint32_t             shellmatta_auth_getLoggedInRole(       shellmatta_handle_t handle);
+shellmatta_retCode_t shellmatta_auth_getLoggedInUserName(   shellmatta_handle_t handle,
+                                                            char                **data,
+                                                            uint32_t            *length);
+
+#endif
+
 #endif
 #endif
 
 
 /** @} */
 /** @} */

+ 27 - 0
src/shellmatta_auth.c

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2022 Stefan Strobel <stefan.strobel@shimatta.net>
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+/**
+ * @file    shellmatta_auth.c
+ * @brief   simple authentication method
+ * @author  Stefan Strobel <stefan.strobel@shimatta.net>
+ */
+
+/**
+ * @addtogroup shellmatta_auth
+ * @{
+ */
+
+#include "shellmatta.h"
+#include "shellmatta_auth.h"
+#include <stdint.h>
+
+
+/**
+ * @}
+ */

+ 29 - 0
src/shellmatta_auth.h

@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2022 Stefan Strobel <stefan.strobel@shimatta.net>
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+/**
+ * @file    shellmatta_auth.c
+ * @brief   simple authentication method
+ * @author  Stefan Strobel <stefan.strobel@shimatta.net>
+ */
+
+/**
+ * @addtogroup shellmatta_auth
+ * @{
+ */
+#ifndef _SHELLMATTA_AUTH_H_
+#define _SHELLMATTA_AUTH_H_
+
+#include "shellmatta.h"
+#include <stdint.h>
+
+
+#endif
+
+/** @} */
+