New Session Model [compile but not tested]
authorFulup Ar Foll <fulup@iot.bzh>
Tue, 22 Dec 2015 22:09:04 +0000 (23:09 +0100)
committerFulup Ar Foll <fulup@iot.bzh>
Tue, 22 Dec 2015 22:09:04 +0000 (23:09 +0100)
include/local-def.h
include/proto-def.h
plugins/audio/audio-api.c
plugins/samples/SamplePost.c
plugins/session/token-api.c
src/helper-api.c
src/rest-api.c
src/session.c

index abbbd61..9c58441 100644 (file)
@@ -97,6 +97,7 @@ extern char *ERROR_LABEL[];
 
 
 typedef json_object* (*AFB_apiCB)();
+typedef void (*AFB_freeCtxCB)(void*, char*);
 
 // Error code are requested through function to manage json usage count
 typedef struct {
@@ -218,23 +219,23 @@ typedef struct {
   AFB_restapi *apis;
   void *handle;
   int  ctxCount;
-  AFB_apiCB freeCtxCB;  // callback to free application context [null for standard free]
+  AFB_freeCtxCB freeCtxCB;  // callback to free application context [null for standard free]
 } AFB_plugin;
 
 
 // User Client Session Context
 typedef struct {
-  int  cid;             // index 0 if global
   char uuid[37];        // long term authentication of remote client
   char token[37];       // short term authentication of remote client
   time_t timeStamp;     // last time token was refresh
   int   restfull;       // client does not use cookie
-  void **ctx;           // application specific context [one per plugin]]
+  void **contexts;      // application specific context [one per plugin]]
   AFB_plugin **plugins; // we need plugins reference to cleanup session outside of call context
 } AFB_clientCtx;
 
 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
 typedef struct {
+  const char *uuid;
   const char *url;
   char *plugin;
   char *api;
index 7f6fc06..0f5ca04 100644 (file)
@@ -43,11 +43,11 @@ PUBLIC json_object *sessionList      (AFB_session *session, AFB_request *request
 PUBLIC json_object *sessionToDisk    (AFB_session *session, AFB_request *request, char *name,json_object *jsonSession);
 PUBLIC json_object *sessionFromDisk  (AFB_session *session, AFB_request *request, char *name);
 
-PUBLIC AFB_error ctxTokenRefresh (AFB_request *request);
-PUBLIC AFB_error ctxTokenCreate (AFB_request *request);
-PUBLIC AFB_error ctxTokenCheck (AFB_request *request);
-PUBLIC AFB_error ctxTokenReset (AFB_request *request);
-PUBLIC AFB_error ctxClientGet (AFB_request *request, AFB_session *session, int idx);
+PUBLIC AFB_error ctxTokenRefresh (AFB_clientCtx *clientCtx, AFB_request *request);
+PUBLIC AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request);
+PUBLIC AFB_error ctxTokenCheck (AFB_clientCtx *clientCtx, AFB_request *request);
+PUBLIC AFB_error ctxTokenReset (AFB_clientCtx *clientCtx, AFB_request *request);
+PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx);
 PUBLIC void      ctxStoreInit (int);
 
 
index 915f8c6..8671a87 100644 (file)
@@ -51,12 +51,8 @@ STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
 }
 
 /* called when client session dies [e.g. client quits for more than 15mns] */
-STATIC json_object* freeAudio (AFB_clientCtx *client) {
-
-    releaseAudio (client->ctx);
-    free (client->ctx);
-    
-    return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
+STATIC void freeAudio (void *context) {
+    free (context);    
 }
 
 
@@ -69,19 +65,18 @@ STATIC json_object* init (AFB_request *request) {       /* AFB_SESSION_CREATE */
     int idx;
 
     /* create a private client context */
-    ctx = initAudioCtx();
-    request->client->ctx = (audioCtxHandleT*)ctx;
+    request->context = initAudioCtx();
     
     _alsa_init("default", ctx);
     
     jresp = json_object_new_object();
-    json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
-    return jresp;
+    json_object_object_add (jresp, "info", json_object_new_string ("Radio initialised"));
+    return (jresp);
 }
 
 STATIC json_object* volume (AFB_request *request) {      /* AFB_SESSION_CHECK */
 
-    audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+    audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
     const char *value = getQueryValue (request, "value");
     json_object *jresp;
     int volume[8], i;
@@ -138,7 +133,7 @@ STATIC json_object* volume (AFB_request *request) {      /* AFB_SESSION_CHECK */
 
 STATIC json_object* channels (AFB_request *request) {    /* AFB_SESSION_CHECK */
 
-    audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+    audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
     const char *value = getQueryValue (request, "value");
     json_object *jresp = json_object_new_object();
     char channels_str[256];
@@ -163,7 +158,7 @@ STATIC json_object* channels (AFB_request *request) {    /* AFB_SESSION_CHECK */
 
 STATIC json_object* mute (AFB_request *request) {        /* AFB_SESSION_CHECK */
 
-    audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+    audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
     const char *value = getQueryValue (request, "value");
     json_object *jresp = json_object_new_object();
 
@@ -196,7 +191,7 @@ STATIC json_object* mute (AFB_request *request) {        /* AFB_SESSION_CHECK */
 
 STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
 
-    audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+    audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
     const char *value = getQueryValue (request, "value");
     json_object *jresp = json_object_new_object();
 
@@ -226,24 +221,16 @@ STATIC json_object* play (AFB_request *request) {        /* AFB_SESSION_CHECK */
     return jresp;
 }
 
-STATIC json_object* refresh (AFB_request *request) {     /* AFB_SESSION_RENEW */
-    json_object *jresp = json_object_new_object();
-    json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
-    return jresp;
-}
-
 STATIC json_object* ping (AFB_request *request) {         /* AFB_SESSION_NONE */
     return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
 }
 
-
 STATIC AFB_restapi pluginApis[]= {
   {"init"    , AFB_SESSION_CREATE, (AFB_apiCB)init      , "Audio API - init"},
   {"volume"  , AFB_SESSION_CHECK,  (AFB_apiCB)volume    , "Audio API - volume"},
   {"channels", AFB_SESSION_CHECK,  (AFB_apiCB)channels  , "Audio API - channels"},
   {"mute"    , AFB_SESSION_CHECK,  (AFB_apiCB)mute      , "Audio API - mute"},
   {"play"    , AFB_SESSION_CHECK,  (AFB_apiCB)play      , "Audio API - play"},
-  {"refresh" , AFB_SESSION_RENEW,  (AFB_apiCB)refresh   , "Audio API - refresh"},
   {"ping"    , AFB_SESSION_NONE,   (AFB_apiCB)ping      , "Audio API - ping"},
   {NULL}
 };
@@ -255,7 +242,7 @@ PUBLIC AFB_plugin *pluginRegister () {
     plugin->prefix = "audio";        
     plugin->apis   = pluginApis;
 
-    plugin->freeCtxCB = freeAudio;
+    plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;
 
     return (plugin);
 };
index 53d065a..beadb77 100644 (file)
@@ -48,7 +48,8 @@ STATIC json_object* UploadAppli (AFB_request *request, AFB_PostItem *item) {
     // This is called after PostForm and then after DonePostForm
     if (item == NULL) {
         AFB_PostCtx *postFileCtx = getPostContext(request);      
-        if (postFileCtx != NULL) {            
+        if (postFileCtx != NULL) {
+            
             // Do something intelligent here to install application
             
             postFileCtx->errcode = MHD_HTTP_OK;   // or error is something went wrong;   
index 585c343..0a5a2be 100644 (file)
@@ -31,7 +31,7 @@ STATIC json_object* clientContextCreate (AFB_request *request) {
     json_object *jresp;
 
     // add an application specific client context to session
-    request->client->ctx = malloc (sizeof (MyClientApplicationHandle));
+    request->context = malloc (sizeof (MyClientApplicationHandle));
     
     // Send response to UI
     jresp = json_object_new_object();               
@@ -66,23 +66,24 @@ STATIC json_object* clientContextCheck (AFB_request *request) {
 STATIC json_object* clientContextReset (AFB_request *request) {
     json_object *jresp;
    
+    /* after this call token will be reset
+     *  - no further access to API will be possible 
+     *  - every context from any used plugin will be freed
+     */
+    
     jresp = json_object_new_object();
-    json_object_object_add(jresp, "uuid", json_object_new_string (request->client->uuid));              
+    json_object_object_add(jresp, "info", json_object_new_string ("Token and all resources are released"));
     
+    // WARNING: if you free context resource manually here do not forget to set request->context=NULL; 
     return (jresp); 
 }
 
-// In this case or handle is quite basic
-typedef struct {
-   int fd; 
-} appPostCtx;
-
 
 // This function is call when Client Session Context is removed
 // Note: when freeCtxCB==NULL standard free/malloc is called
-STATIC void clientContextFree(AFB_clientCtx *client) {
-    fprintf (stderr,"Plugin[%s] Closing Session uuid=[%s]\n", client->plugin->prefix, client->uuid);
-    free (client->ctx);
+STATIC void clientContextFree(void *context, char* uuid) {
+    fprintf (stderr,"Plugin[token] Closing Session uuid=[%s]\n", uuid);
+    free (context);
 }
 
 STATIC  AFB_restapi pluginApis[]= {
index 5863e41..a166027 100644 (file)
@@ -34,9 +34,7 @@ PUBLIC json_object* getPingTest(AFB_request *request) {
     json_object *response;
     char query  [256];
     char session[256];
-
     int len;
-    AFB_clientCtx *client=request->client; // get client context from request
     
     // request all query key/value
     len = getQueryAll (request, query, sizeof(query));
@@ -44,14 +42,10 @@ PUBLIC json_object* getPingTest(AFB_request *request) {
     
     // check if we have some post data
     if (request->post == NULL)  request->post->data="NoData"; 
-    
-    // check is we have a session and a plugin handle
-    if (client == NULL) strncpy (session,"NoSession", sizeof(session));       
-    else snprintf(session, sizeof(session),"uuid=%s token=%s ctx=0x%x handle=0x%x", client->uuid, client->token, client->ctx, client->ctx); 
-        
+          
     // return response to caller
-    response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d CtxtId=%d query={%s} session={%s} PostData: [%s] "
-               , pingcount++, request->client->cid, query, session, request->post->data);
+    response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d uuid=%s query={%s} session={0x%x} PostData: [%s] "
+               , pingcount++, request->uuid, query, session, request->post->data);
     return (response);
 }
 
index 6927139..1a44b33 100644 (file)
@@ -53,6 +53,7 @@ PUBLIC void endPostRequest(AFB_PostHandle *postHandle) {
 STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request, void *context) {
     json_object *jresp, *jcall;
     int idx, status, sig;
+    AFB_clientCtx *clientCtx;
     int signals[]= {SIGALRM, SIGSEGV, SIGFPE, 0};
     
     /*---------------------------------------------------------------
@@ -60,8 +61,8 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request, void *c
     +---------------------------------------------------------------- */
     void pluginError (int signum) {
       sigset_t sigset;
-      AFB_clientCtx *context;
-              
+   
+      
       // unlock signal to allow a new signal to come
       sigemptyset (&sigset);
       sigaddset   (&sigset, signum);
@@ -116,7 +117,8 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request, void *c
                 if (AFB_SESSION_NONE != plugin->apis[idx].session) {
                     
                     // add client context to request
-                    if (ctxClientGet(request, idx) != AFB_SUCCESS) {
+                    clientCtx = ctxClientGet(request, idx);
+                    if (clientCtx != NULL) {
                         request->errcode=MHD_HTTP_INSUFFICIENT_STORAGE;
                         json_object_object_add(jcall, "status", json_object_new_string ("fail"));
                         json_object_object_add(jcall, "info", json_object_new_string ("Client Session Context Full !!!"));
@@ -125,12 +127,12 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request, void *c
                     };
                     
                     if (verbose) fprintf(stderr, "Plugin=[%s] Api=[%s] Middleware=[%d] Client=[0x%x] Uuid=[%s] Token=[%s]\n"
-                           , request->plugin, request->api, plugin->apis[idx].session, request->client, request->client->uuid, request->client->token);                        
+                           , request->plugin, request->api, plugin->apis[idx].session, clientCtx, clientCtx->uuid, clientCtx->token);                        
                     
                     switch(plugin->apis[idx].session) {
 
                         case AFB_SESSION_CREATE:
-                            if (request->client->token[0] != '\0') {
+                            if (clientCtx->token[0] != '\0') {
                                 request->errcode=MHD_HTTP_UNAUTHORIZED;
                                 json_object_object_add(jcall, "status", json_object_new_string ("exist"));
                                 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CREATE Session already exist"));
@@ -138,50 +140,50 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request, void *c
                                 return (AFB_DONE);                              
                             }
                         
-                            if (AFB_SUCCESS != ctxTokenCreate (request)) {
+                            if (AFB_SUCCESS != ctxTokenCreate (clientCtx, request)) {
                                 request->errcode=MHD_HTTP_UNAUTHORIZED;
                                 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
                                 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CREATE Invalid Initial Token"));
                                 json_object_object_add(request->jresp, "request", jcall);
                                 return (AFB_DONE);
                             } else {
-                                json_object_object_add(jcall, "uuid", json_object_new_string (request->client->uuid));                                
-                                json_object_object_add(jcall, "token", json_object_new_string (request->client->token));                                
+                                json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));                                
+                                json_object_object_add(jcall, "token", json_object_new_string (clientCtx->token));                                
                                 json_object_object_add(jcall, "timeout", json_object_new_int (request->config->cntxTimeout));                                
                             }
                             break;
 
 
                         case AFB_SESSION_RENEW:
-                            if (AFB_SUCCESS != ctxTokenRefresh (request)) {
+                            if (AFB_SUCCESS != ctxTokenRefresh (clientCtx, request)) {
                                 request->errcode=MHD_HTTP_UNAUTHORIZED;
                                 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
                                 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_REFRESH Broken Exchange Token Chain"));
                                 json_object_object_add(request->jresp, "request", jcall);
                                 return (AFB_DONE);
                             } else {
-                                json_object_object_add(jcall, "uuid", json_object_new_string (request->client->uuid));                                
-                                json_object_object_add(jcall, "token", json_object_new_string (request->client->token));                                
+                                json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));                                
+                                json_object_object_add(jcall, "token", json_object_new_string (clientCtx->token));                                
                                 json_object_object_add(jcall, "timeout", json_object_new_int (request->config->cntxTimeout));                                
                             }
                             break;
 
                         case AFB_SESSION_CLOSE:
-                            if (AFB_SUCCESS != ctxTokenCheck (request)) {
+                            if (AFB_SUCCESS != ctxTokenCheck (clientCtx, request)) {
                                 request->errcode=MHD_HTTP_UNAUTHORIZED;
                                 json_object_object_add(jcall, "status", json_object_new_string ("empty"));
                                 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CLOSE Not a Valid Access Token"));
                                 json_object_object_add(request->jresp, "request", jcall);
                                 return (AFB_DONE);
                             } else {
-                                json_object_object_add(jcall, "uuid", json_object_new_string (request->client->uuid));                                
+                                json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));                                
                             }
                             break;
                         
                         case AFB_SESSION_CHECK:
                         default: 
                             // default action is check
-                            if (AFB_SUCCESS != ctxTokenCheck (request)) {
+                            if (AFB_SUCCESS != ctxTokenCheck (clientCtx, request)) {
                                 request->errcode=MHD_HTTP_UNAUTHORIZED;
                                 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
                                 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CHECK Invalid Active Token"));
@@ -195,11 +197,11 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request, void *c
                 // Effectively call the API with a subset of the context
                 jresp = plugin->apis[idx].callback(request, context);
                 
-                // handle intemediatry Post Iterates out of band
+                // handle intermediary Post Iterates out of band
                 if ((jresp == NULL) && (request->errcode == MHD_HTTP_OK)) return (AFB_SUCCESS);
 
                 // Session close is done after the API call so API can still use session in closing API
-                if (AFB_SESSION_CLOSE == plugin->apis[idx].session) ctxTokenReset (request);                    
+                if (AFB_SESSION_CLOSE == plugin->apis[idx].session) ctxTokenReset (clientCtx, request);                    
                 
                 // API should return NULL of a valid Json Object
                 if (jresp == NULL) {
@@ -500,9 +502,9 @@ ProcessApiCall:
     webResponse = MHD_create_response_from_buffer(strlen(serialized), (void*) serialized, MHD_RESPMEM_MUST_COPY);
     
     // client did not pass token on URI let's use cookies 
-    if ((!request->restfull) && (request->client != NULL)) {
+    if ((!request->restfull) && (request->context != NULL)) {
        char cookie[64]; 
-       snprintf (cookie, sizeof (cookie), "%s=%s", COOKIE_NAME,  request->client->uuid); 
+       snprintf (cookie, sizeof (cookie), "%s=%s", COOKIE_NAME,  request->uuid); 
        MHD_add_response_header (webResponse, MHD_HTTP_HEADER_SET_COOKIE, cookie);
     }
     
index b5316d0..4769b10 100644 (file)
@@ -317,16 +317,22 @@ OnErrorExit:
 
 // Free context [XXXX Should be protected again memory abort XXXX]
 STATIC void ctxUuidFreeCB (AFB_clientCtx *client) {
-  
+
+    AFB_plugin **plugins = client->plugins;
+    AFB_freeCtxCB freeCtxCB;
+    int idx;
+    
     // If application add a handle let's free it now
-    if (client->ctx != NULL) {
-        int idx;
-        AFB_plugin **plugins = client->plugins;
-        
+    if (client->contexts != NULL) {
+     
         // Free client handle with a standard Free function, with app callback or ignore it
-        for (idx=0; idx < )
-        if (client->plugin->freeCtxCB == NULL) free (client->ctx); 
-        else if (client->plugin->freeCtxCB != (void*)-1) client->plugin->freeCtxCB(client); 
+        for (idx=0; client->plugins[idx] != NULL; idx ++) {
+            if (client->contexts[idx] != NULL) {               
+                freeCtxCB = client->plugins[idx]->freeCtxCB;
+                if (freeCtxCB == NULL) free (client->contexts[idx]); 
+                else if (freeCtxCB != (void*)-1) freeCtxCB(client->contexts[idx], client->uuid); 
+            }
+        }
     }
 }
 
@@ -353,7 +359,6 @@ STATIC AFB_clientCtx *ctxStoreSearch (const char* uuid) {
     
     if (idx == sessions.max) client=NULL;
     else client= sessions.store[idx];
-    
     pthread_mutex_unlock(&sessions.mutex);
     
     return (client);
@@ -364,8 +369,6 @@ STATIC AFB_error ctxStoreDel (AFB_clientCtx *client) {
     int idx;
     int status;
     if (client == NULL) return (AFB_FAIL);
-
-    //fprintf (stderr, "ctxStoreDel request uuid=%s count=%d\n", client->uuid, sessions.count);
     
     pthread_mutex_lock(&sessions.mutex);
     
@@ -376,16 +379,13 @@ STATIC AFB_error ctxStoreDel (AFB_clientCtx *client) {
     if (idx == sessions.max) status=AFB_FAIL;
     else {
         sessions.count --;
+        ctxUuidFreeCB (sessions.store[idx]);
         sessions.store[idx]=NULL;
         status=AFB_SUCCESS;
     }
     
-    pthread_mutex_unlock(&sessions.mutex);
-    
+    pthread_mutex_unlock(&sessions.mutex);   
     return (status);
-    
-    // plugin registered a callback let's release semaphore and cleanup now
-    if ((client->plugin->freeCtxCB != NULL) && client->ctx) client->plugin->freeCtxCB(client);
 }
 
 STATIC AFB_error ctxStoreAdd (AFB_clientCtx *client) {
@@ -408,8 +408,7 @@ STATIC AFB_error ctxStoreAdd (AFB_clientCtx *client) {
         sessions.store[idx]= client;
     }
     
-    pthread_mutex_unlock(&sessions.mutex);
-    
+    pthread_mutex_unlock(&sessions.mutex);   
     return (status);
 }
 
@@ -436,14 +435,13 @@ PUBLIC int ctxStoreGarbage (const int timeout) {
 }
 
 // This function will return exiting client context or newly created client context
-PUBLIC AFB_error ctxClientGet (AFB_request *request, int idx) {
-  static int cid=0;
+PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx) {
   AFB_clientCtx *clientCtx=NULL;
   const char *uuid;
   uuid_t newuuid;
   int ret;
   
-    if (request->config->token == NULL) return AFB_EMPTY;
+    if (request->config->token == NULL) return NULL;
 
     // Check if client as a context or not inside the URL
     uuid  = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, "uuid");
@@ -467,8 +465,9 @@ PUBLIC AFB_error ctxClientGet (AFB_request *request, int idx) {
                 ctxStoreDel (clientCtx);
                 clientCtx=NULL;
             } else {
-                request->client=clientCtx;
-                return (AFB_SUCCESS);            
+                request->context=clientCtx->contexts[idx];
+                request->uuid= uuid;
+                return (clientCtx);            
             }
         }
     }
@@ -476,12 +475,11 @@ PUBLIC AFB_error ctxClientGet (AFB_request *request, int idx) {
     // we have no session let's create one otherwise let's clean any exiting values
     if (clientCtx == NULL) {        
         clientCtx = calloc(1, sizeof(AFB_clientCtx)); // init NULL clientContext
-        clientCtx->ctx = cmalloc (1, request->config->pluginCount * (sizeof (void*)));        
+        clientCtx->contexts = calloc (1, request->config->pluginCount * (sizeof (void*)));        
     }
     
     uuid_generate(newuuid);         // create a new UUID
     uuid_unparse_lower(newuuid, clientCtx->uuid);
-    clientCtx->cid=cid++;   // simple application uniqueID 
     
     // if table is full at 50% let's clean it up
     if(sessions.count > (sessions.max / 2)) ctxStoreGarbage(request->config->cntxTimeout);
@@ -489,29 +487,29 @@ PUBLIC AFB_error ctxClientGet (AFB_request *request, int idx) {
     // finally add uuid into hashtable
     if (AFB_SUCCESS != ctxStoreAdd (clientCtx)) {
         free (clientCtx);
-        return(AFB_FAIL);
+        return(NULL);
     }
     
     // if (verbose) fprintf (stderr, "ctxClientGet New uuid=[%s] token=[%s] timestamp=%d\n", clientCtx->uuid, clientCtx->token, clientCtx->timeStamp);      
-    request->client = clientCtx->ctx[idx];
-
-    return(AFB_SUCCESS);
+    request->context = clientCtx->contexts[idx];
+    request->uuid=clientCtx->uuid;
+    return(clientCtx);
 }
 
 // Sample Generic Ping Debug API
-PUBLIC AFB_error ctxTokenCheck (AFB_request *request) {
+PUBLIC AFB_error ctxTokenCheck (AFB_clientCtx *clientCtx, AFB_request *request) {
     const char *token;
     
-    if (request->client == NULL) return AFB_EMPTY;
+    if (request->context == NULL) return AFB_EMPTY;
     
     // this time have to extract token from query list
     token = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, "token");
     
     // if not token is providing we refuse the exchange
-    if ((token == NULL) || (request->client->token == NULL)) return (AFB_FALSE);
+    if ((token == NULL) || (clientCtx->token == NULL)) return (AFB_FALSE);
     
     // compare current token with previous one
-    if ((0 == strcmp (token, request->client->token)) && (!ctxStoreToOld (request->client, request->config->cntxTimeout))) {
+    if ((0 == strcmp (token, clientCtx->token)) && (!ctxStoreToOld (clientCtx, request->config->cntxTimeout))) {
        return (AFB_SUCCESS);
     }
     
@@ -520,14 +518,13 @@ PUBLIC AFB_error ctxTokenCheck (AFB_request *request) {
 }
 
 // Free Client Session Context
-PUBLIC AFB_error ctxTokenReset (AFB_request *request) {
+PUBLIC AFB_error ctxTokenReset (AFB_clientCtx *clientCtx, AFB_request *request) {
     int ret;
-    AFB_clientCtx *clientCtx;
 
-    if (request->client == NULL) return AFB_EMPTY;
+    if (clientCtx == NULL) return AFB_EMPTY;
     
     // Search for an existing client with the same UUID
-    clientCtx = ctxStoreSearch (request->client->uuid);
+    clientCtx = ctxStoreSearch (clientCtx->uuid);
     if (clientCtx == NULL) return AFB_FALSE;
 
     // Remove client from table
@@ -537,13 +534,13 @@ PUBLIC AFB_error ctxTokenReset (AFB_request *request) {
 }
 
 // generate a new token
-PUBLIC AFB_error ctxTokenCreate (AFB_request *request) {
+PUBLIC AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request) {
     int oldTnkValid;
     const char *ornew;
     uuid_t newuuid;
     const char *token;
 
-    if (request->client == NULL) return AFB_EMPTY;
+    if (clientCtx == NULL) return AFB_EMPTY;
 
     // if config->token!="" then verify that we have the right initial share secret   
     if (request->config->token[0] != '\0') {
@@ -552,16 +549,16 @@ PUBLIC AFB_error ctxTokenCreate (AFB_request *request) {
         token = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, "token");
         if (token == NULL) return AFB_UNAUTH;
         
-        // verify that presented initial tokens fit
+        // verify that it fits with initial tokens fit
         if (strcmp(request->config->token, token)) return AFB_UNAUTH;       
     }
     
     // create a UUID as token value
     uuid_generate(newuuid); 
-    uuid_unparse_lower(newuuid, request->client->token);
+    uuid_unparse_lower(newuuid, clientCtx->token);
     
     // keep track of time for session timeout and further clean up
-    request->client->timeStamp=time(NULL); 
+    clientCtx->timeStamp=time(NULL); 
     
     // Token is also store in context but it might be convenient for plugin to access it directly
     return (AFB_SUCCESS);
@@ -569,19 +566,19 @@ PUBLIC AFB_error ctxTokenCreate (AFB_request *request) {
 
 
 // generate a new token and update client context
-PUBLIC AFB_error ctxTokenRefresh (AFB_request *request) {
+PUBLIC AFB_error ctxTokenRefresh (AFB_clientCtx *clientCtx, AFB_request *request) {
     int oldTnkValid;
     const char *oldornew;
     uuid_t newuuid;
 
-    if (request->client == NULL) return AFB_EMPTY;
+    if (clientCtx == NULL) return AFB_EMPTY;
     
     // Check if the old token is valid
-    if (ctxTokenCheck (request) != AFB_SUCCESS) return (AFB_FAIL);
+    if (ctxTokenCheck (clientCtx, request) != AFB_SUCCESS) return (AFB_FAIL);
         
     // Old token was valid let's regenerate a new one    
     uuid_generate(newuuid);         // create a new UUID
-    uuid_unparse_lower(newuuid, request->client->token);
+    uuid_unparse_lower(newuuid, clientCtx->token);
     return (AFB_SUCCESS);    
     
 }