Fix AFB_SESSION_CREATE behaviour
authorJosé Bollo <jose.bollo@iot.bzh>
Sat, 21 May 2016 22:29:22 +0000 (00:29 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Sat, 21 May 2016 22:29:22 +0000 (00:29 +0200)
This commit allows to call methods having AFB_SESSION_CREATE
at any time.

This commit prepares the future LOA (level of authorization)
implementation that wille soon replace the deprecated mechanism
of AFB_SESSION_CREATE.

Change-Id: Ia3e99186e012fcd55a6c81a7067ab5b4aca21e4d
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/afb/afb-req-itf.h
src/afb-api-so.c
src/afb-context.c
src/afb-context.h
src/session.c
src/session.h

index a8980ff..a3ff9fe 100644 (file)
@@ -97,6 +97,17 @@ static inline void afb_req_context_set(struct afb_req req, void *value, void (*f
        return req.itf->context_set(req.closure, value, free_value);
 }
 
+static inline void *afb_req_context(struct afb_req req, void *(*create_value)(), void (*free_value)(void*))
+{
+       void *result = req.itf->context_get(req.closure);
+       if (result == NULL) {
+               result = create_value();
+               if (result != NULL)
+                       req.itf->context_set(req.closure, result, free_value);
+       }
+       return result;
+}
+
 static inline void afb_req_context_clear(struct afb_req req)
 {
        afb_req_context_set(req, NULL, NULL);
index 408c3f5..a365294 100644 (file)
@@ -108,18 +108,22 @@ static void call_check(struct afb_req req, struct afb_context *context, const st
        }
 
        if ((stag & AFB_SESSION_CREATE) != 0) {
-               if (!afb_context_create(context)) {
+               if (afb_context_check_loa(context, 1)) {
                        afb_context_close(context);
                        afb_req_fail(req, "failed", "invalid creation state");
                        return;
                }
+               afb_context_change_loa(context, 1);
+               afb_context_refresh(context);
        }
        
        if ((stag & (AFB_SESSION_CREATE | AFB_SESSION_RENEW)) != 0)
                afb_context_refresh(context);
 
-       if ((stag & AFB_SESSION_CLOSE) != 0)
+       if ((stag & AFB_SESSION_CLOSE) != 0) {
+               afb_context_change_loa(context, 0);
                afb_context_close(context);
+       }
 
        data.req = req;
        data.action = verb->callback;
index b00224e..48787f7 100644 (file)
@@ -31,6 +31,7 @@ static void init_context(struct afb_context *context, struct AFB_clientCtx *sess
        context->session = session;
        context->flags = 0;
        context->api_index = -1;
+       context->loa_in = ctxClientGetLOA(session) & 7;
 
        /* check the token */
        if (token != NULL) {
@@ -55,19 +56,30 @@ int afb_context_connect(struct afb_context *context, const char *uuid, const cha
        if (session == NULL)
                return -1;
        init_context(context, session, token);
-       if (created)
+       if (created) {
                context->created = 1;
+               context->refreshing = 1;
+       }
        return 0;
 }
 
 void afb_context_disconnect(struct afb_context *context)
 {
        if (context->session != NULL) {
-               if (context->closing && !context->closed) {
-                       context->closed = 1;
+               if (context->refreshing && !context->refreshed) {
+                       ctxTokenNew (context->session);
+                       context->refreshed = 1;
+               }
+               if (context->loa_changing && !context->loa_changed) {
+                       ctxClientSetLOA (context->session, context->loa_out);
+                       context->loa_changed = 1;
+               }
+               if (!context->closed) {
                        ctxClientClose(context->session);
+                       context->closed = 1;
                }
                ctxClientUnref(context->session);
+               context->session = NULL;
        }
 }
 
@@ -75,7 +87,7 @@ const char *afb_context_sent_token(struct afb_context *context)
 {
        if (context->session == NULL || context->closing)
                return NULL;
-       if (!(context->created || context->refreshing))
+       if (!context->refreshing)
                return NULL;
        if (!context->refreshed) {
                ctxTokenNew (context->session);
@@ -112,6 +124,7 @@ void afb_context_close(struct afb_context *context)
 
 void afb_context_refresh(struct afb_context *context)
 {
+       assert(context->validated);
        context->refreshing = 1;
 }
 
@@ -120,7 +133,21 @@ int afb_context_check(struct afb_context *context)
        return context->validated;
 }
 
-int afb_context_create(struct afb_context *context)
+int afb_context_check_loa(struct afb_context *context, unsigned loa)
+{
+       return context->loa_in >= loa;
+}
+
+void afb_context_change_loa(struct afb_context *context, unsigned loa)
 {
-       return context->created;
+       assert(context->validated);
+
+       if (loa == context->loa_in)
+               context->loa_changing = 0;
+       else {
+               context->loa_changing = 1;
+               context->loa_out = loa & 7;
+       }
 }
+
+
index 2f5ecc1..3f4301e 100644 (file)
@@ -32,6 +32,10 @@ struct afb_context
                        unsigned refreshed: 1;
                        unsigned closing: 1;
                        unsigned closed: 1;
+                       unsigned loa_in: 3;
+                       unsigned loa_out: 3;
+                       unsigned loa_changing: 1;
+                       unsigned loa_changed: 1;
                };
        };
        int api_index;
@@ -49,5 +53,6 @@ extern void afb_context_set(struct afb_context *context, void *value, void (*fre
 extern void afb_context_close(struct afb_context *context);
 extern void afb_context_refresh(struct afb_context *context);
 extern int afb_context_check(struct afb_context *context);
-extern int afb_context_create(struct afb_context *context);
+extern int afb_context_check_loa(struct afb_context *context, unsigned loa);
+extern void afb_context_change_loa(struct afb_context *context, unsigned loa);
 
index 320eab9..e847a6d 100644 (file)
@@ -48,6 +48,7 @@ struct afb_event_listener_list
 struct AFB_clientCtx
 {
        unsigned refcount;
+       unsigned loa;
        time_t expiration;    // expiration time of the token
        time_t access;
        char uuid[37];        // long term authentication of remote client
@@ -434,6 +435,18 @@ const char *ctxClientGetToken (struct AFB_clientCtx *clientCtx)
        return clientCtx->token;
 }
 
+unsigned ctxClientGetLOA (struct AFB_clientCtx *clientCtx)
+{
+       assert(clientCtx != NULL);
+       return clientCtx->loa;
+}
+
+void ctxClientSetLOA (struct AFB_clientCtx *clientCtx, unsigned loa)
+{
+       assert(clientCtx != NULL);
+       clientCtx->loa = loa;
+}
+
 void *ctxClientValueGet(struct AFB_clientCtx *clientCtx, int index)
 {
        assert(clientCtx != NULL);
index bb37a65..af07410 100644 (file)
@@ -48,6 +48,8 @@ extern void ctxTokenNew (struct AFB_clientCtx *clientCtx);
 
 extern const char *ctxClientGetUuid (struct AFB_clientCtx *clientCtx);
 extern const char *ctxClientGetToken (struct AFB_clientCtx *clientCtx);
+extern unsigned ctxClientGetLOA (struct AFB_clientCtx *clientCtx);
+extern void ctxClientSetLOA (struct AFB_clientCtx *clientCtx, unsigned loa);
 
 extern void *ctxClientValueGet(struct AFB_clientCtx *clientCtx, int index);
 extern void ctxClientValueSet(struct AFB_clientCtx *clientCtx, int index, void *value, void (*free_value)(void*));