About HTTP Server Authentication

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

About HTTP Server Authentication

Jump to solution
1,361 Views
doron2006
Contributor I

I'm trying to get Basic Authentication working with MQX 4.0 and CW10.3 I followed the advice from HTTP Server Authentication

but I can´t understand point 2 of the explanation:

2. Add to my project authentification callback function like this:

int _http_auth_internal (HTTPD_SESSION_STRUCT* session){  // check user login  if (strncmp(session->request.auth, your_login, YOUR_LOGIN_SIZE) != 0)    return 1;  // check user password  if (strncmp(session->request.authPasswd, your_pass, YOUR_PASSWORD_SIZE) != 0)    return 1;  // all good  return 0;}

And attach this callback with

HTTPD_SET_PARAM_AUTH_FN(server, _http_auth_internal);

I appreciate any sample code that clear that concept. Thank you in advance Doron.

Labels (1)
Tags (2)
0 Kudos
Reply
1 Solution
1,036 Views
pbanta
Contributor IV

You need to write your own authentication function and then insert a pointer to that function into a table that the http server will use when it's time to perform authentication.


/*FUNCTION*------------------------------------------------------
*
* Function Name  : _http_auth_internal()
* Returned Value : 0 if successful, non-zero for error
* Comments       :
*     Performs authentication against authorized user list.
*END*-----------------------------------------------------------*/
int_32 _http_auth_internal (HTTPD_SESSION_STRUCT* session)

    int_32 retval = 1;
    char password[LCEUM_MAX_PASSWORD_SIZE + 1];
    uint_32 index;
    uint_32 result;

   
    index = lceum_get_index_for_username(session->request.auth);
    if (index > 0){
        result = lceum_get_password(index, password);
        if (result == LCEUM_OK){
            if (strncmp(password, session->request.authPasswd, (LCEUM_MAX_PASSWORD_SIZE)) != 0){
                retval = 1;  // invalid
            }
            else{
                retval = 0;  // valid
            }
        }
    }
    return retval;
}


/*FUNCTION*------------------------------------------------------
*
* Function Name  : httpd_server_task()
* Returned Value : 0 if OK, other value if error
* Comments       :
*     HTTPD Server Task
*END*-----------------------------------------------------------*/
_mqx_int httpd_server_task()
{
    extern const HTTPD_CGI_LINK_STRUCT guardian_cgi_lnk_tbl[];
    extern const TFS_DIR_ENTRY tfs_data[];
    uint_32 error;

    error = _io_tfs_install("tfs:", tfs_data);
    if (error == 0) {
        server = (HTTPD_STRUCT *)httpd_server_init((HTTPD_ROOT_DIR_STRUCT *)root_dir, "\\index.html");

        if (server != NULL) {
            HTTPD_SET_PARAM_CGI_TBL(server, (HTTPD_CGI_LINK_STRUCT*)guardian_cgi_lnk_tbl);

            /* Needed for authentication */
            HTTPD_SET_PARAM_AUTH_FN(server, _http_auth_internal);

            httpd_server_run(server);

        }

        else {

            error = -1;

        }
    }
   
    return(error);
   
}

At some point in your init sequence you need to call httpd_server_task().  (Or whatever name you give to it.)


   /* Start the http server */

   error = httpd_server_task();

Good luck.

View solution in original post

0 Kudos
Reply
1 Reply
1,037 Views
pbanta
Contributor IV

You need to write your own authentication function and then insert a pointer to that function into a table that the http server will use when it's time to perform authentication.


/*FUNCTION*------------------------------------------------------
*
* Function Name  : _http_auth_internal()
* Returned Value : 0 if successful, non-zero for error
* Comments       :
*     Performs authentication against authorized user list.
*END*-----------------------------------------------------------*/
int_32 _http_auth_internal (HTTPD_SESSION_STRUCT* session)

    int_32 retval = 1;
    char password[LCEUM_MAX_PASSWORD_SIZE + 1];
    uint_32 index;
    uint_32 result;

   
    index = lceum_get_index_for_username(session->request.auth);
    if (index > 0){
        result = lceum_get_password(index, password);
        if (result == LCEUM_OK){
            if (strncmp(password, session->request.authPasswd, (LCEUM_MAX_PASSWORD_SIZE)) != 0){
                retval = 1;  // invalid
            }
            else{
                retval = 0;  // valid
            }
        }
    }
    return retval;
}


/*FUNCTION*------------------------------------------------------
*
* Function Name  : httpd_server_task()
* Returned Value : 0 if OK, other value if error
* Comments       :
*     HTTPD Server Task
*END*-----------------------------------------------------------*/
_mqx_int httpd_server_task()
{
    extern const HTTPD_CGI_LINK_STRUCT guardian_cgi_lnk_tbl[];
    extern const TFS_DIR_ENTRY tfs_data[];
    uint_32 error;

    error = _io_tfs_install("tfs:", tfs_data);
    if (error == 0) {
        server = (HTTPD_STRUCT *)httpd_server_init((HTTPD_ROOT_DIR_STRUCT *)root_dir, "\\index.html");

        if (server != NULL) {
            HTTPD_SET_PARAM_CGI_TBL(server, (HTTPD_CGI_LINK_STRUCT*)guardian_cgi_lnk_tbl);

            /* Needed for authentication */
            HTTPD_SET_PARAM_AUTH_FN(server, _http_auth_internal);

            httpd_server_run(server);

        }

        else {

            error = -1;

        }
    }
   
    return(error);
   
}

At some point in your init sequence you need to call httpd_server_task().  (Or whatever name you give to it.)


   /* Start the http server */

   error = httpd_server_task();

Good luck.

0 Kudos
Reply