diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-05-22 00:29:22 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-05-22 00:29:22 +0200 |
commit | 457c879ff89bc7f1a8864304974999dba54af9ae (patch) | |
tree | 5d18a69c237fac35896911bda1711a147b05fa98 | |
parent | ed4e7e3b0690680d963bc722bc259a424099e7e5 (diff) |
Fix AFB_SESSION_CREATE behaviour
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>
-rw-r--r-- | include/afb/afb-req-itf.h | 11 | ||||
-rw-r--r-- | src/afb-api-so.c | 8 | ||||
-rw-r--r-- | src/afb-context.c | 39 | ||||
-rw-r--r-- | src/afb-context.h | 7 | ||||
-rw-r--r-- | src/session.c | 13 | ||||
-rw-r--r-- | src/session.h | 2 |
6 files changed, 71 insertions, 9 deletions
diff --git a/include/afb/afb-req-itf.h b/include/afb/afb-req-itf.h index a8980ffc..a3ff9fe0 100644 --- a/include/afb/afb-req-itf.h +++ b/include/afb/afb-req-itf.h @@ -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); diff --git a/src/afb-api-so.c b/src/afb-api-so.c index 408c3f5a..a365294c 100644 --- a/src/afb-api-so.c +++ b/src/afb-api-so.c @@ -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; diff --git a/src/afb-context.c b/src/afb-context.c index b00224e4..48787f79 100644 --- a/src/afb-context.c +++ b/src/afb-context.c @@ -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; + } } + + diff --git a/src/afb-context.h b/src/afb-context.h index 2f5ecc10..3f4301ec 100644 --- a/src/afb-context.h +++ b/src/afb-context.h @@ -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); diff --git a/src/session.c b/src/session.c index 320eab9b..e847a6d5 100644 --- a/src/session.c +++ b/src/session.c @@ -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); diff --git a/src/session.h b/src/session.h index bb37a652..af074100 100644 --- a/src/session.h +++ b/src/session.h @@ -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*)); |