Skip to content

Creating a custom Authenticator #29

@jonaskappa

Description

@jonaskappa

Hello,

Iam trying to create a custom Authenticator for Token Authentification, but I have a hard time doing so. As I understand it, I need to implement the AuthenticatorIntf and the authenticate function. Within this function I need to construct the AuthInfo Object and the AuthenticatedUser, eg TokenAuthUser. I also need to create a new HttpSession with req->getSession(TRUE). This is a point where I get a Memory fault. When using the other Authenticator everything works fine, but they require me to use username:password for example when using BasicAuthenticator.

So the question is, how do I properly implement my own custom Authenticator.

Im running this Server on a QNX7.1 platform.

With kind regards

Here is the code I have so far

AuthenticatedUser* TokenAuthenticator::authenticate(AuthenticatorIntf* super, const char * relPath, HttpCommand * cmd)
{
   TokenAuthenticator* self = (TokenAuthenticator*)super;
   HttpRequest * req = cmd->getRequest();
   AuthInfo authInfo;
   
   // check if already a authenticated session exists
   printf("check if already a authenticated session exists\n");
   
   HttpSession * session = req->getSession(FALSE);
   if (session) 
   {
      AuthenticatedUser * user = AuthenticatedUser::get(session);
      if (user) 
      {
         return user;
      }
   }

   AuthInfo_constructor(&authInfo, 0, cmd, AuthenticatedUserType_Token); // I Added a new AuthenticatedUserType in AuthenticatedUser.h
                                                                         // also adjusted the references in BAS.c

   // check for the correct Header and get the value
   printf("check for the correct Header and get the value\n");

   const char* token = req->getHeaderValue("x-api-key"); // I have an additional header for the token

   if (token) 
   {    
      TokenAuthUser* tokenAuthUser; // New struct, but looks like BasicAuthUser

      // set token as username so UserDB uses it to verify
      authInfo.username = token;

      // get auth user list from server
      printf("get auth user list from server\n");

      authInfo.authUserList = cmd->getServer()->getAuthUserList(token);

      // ask the UserIntf to check the api token
      printf("ask the UserIntf to check the api token\n");

      self->userDB->getPwdFp(self->userDB, &authInfo);
      if (authInfo.ct != AuthInfoCT::AuthInfoCT_Valid) 
      {
         self->loginResp->serviceFp(self->loginResp, &authInfo);
         AuthUserList_termIfEmpty(authInfo.authUserList);
         // TokenAuthenticator::termIfEmpty(authInfo.authUserList);
         return 0;
      }

      // AuthUSerList createOrCheck (BAS.c Line 80139)
      printf("AuthUserList createOrCheck\n");

      if (AuthUserList_createOrCheck(&authInfo, self->userDB, (void**)&tokenAuthUser, sizeof(TokenAuthUser))) 
      {
         // somethings gone wrong
         printf("somethings gone wrong\n");
         self->loginResp->serviceFp(self->loginResp, &authInfo);
         AuthUserList_termIfEmpty(authInfo.authUserList);
         // TokenAuthenticator::termIfEmpty(authInfo.authUserList);
         return 0;
      }

      // create new Authenticated user with TokenAuthUser and AuthInfo
      printf("create new Authenticated user with TokenAuthUser and AuthInfo\n");
      
      // using "TAU" here for TokenAuthUser
      AuthenticatedUser_constructor((AuthenticatedUser*)&tokenAuthUser, "TAU", authInfo.authUserList, (HttpSessionAttribute_Destructor)destruct);
      authInfo.user = (AuthenticatedUser*)&tokenAuthUser;

      // get Session and set user as attribute
      printf("get Session and set user as attribute\n");

      // !!!!!!!!!!!!!! here I get the Memory fault !!!!!!!!!!!!!!!!!!!
      session = req->getSession(TRUE);

      printf("check session\n");

      bool er = false;
      if (!session) 
      {
         er = true;
         printf("ERROR: No session\n");
      }
      if (session->setAttribute((HttpSessionAttribute*)&tokenAuthUser))
      {
         er = true;
         printf("ERROR: session->setAttribute failed\n");
      }
      if (!AuthenticatedUser::get(session))
      {
         er = true;
         printf("ERROR: Could not fetch Authenticated User from session\n");
      }
      if (er) 
      {
         // somethings gone wrong
         printf("somethings gone wrong\n");
         self->loginResp->serviceFp(self->loginResp, &authInfo);
         AuthUserList_termIfEmpty(authInfo.authUserList);
         // TokenAuthenticator::termIfEmpty(authInfo.authUserList);
         return 0;
      }
      return (AuthenticatedUser*)&tokenAuthUser;
   }
   printf("No token. Exit\n");
   self->loginResp->serviceFp(self->loginResp, &authInfo);
   AuthUserList_termIfEmpty(authInfo.authUserList);
   // TokenAuthenticator::termIfEmpty(authInfo.authUserList);
   return 0;
}

// used for the AuthenticatedUser_constructor
void TokenAuthenticator::destruct(TokenAuthUser* o)
{
   printf("void TokenAuthenticator::destruct(TokenAuthUser* o)\n");
   AuthenticatedUser_destructor((AuthenticatedUser*)o);
   baFree(o);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions