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);
}
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;
context->session = session;
context->flags = 0;
context->api_index = -1;
+ context->loa_in = ctxClientGetLOA(session) & 7;
/* check the token */
if (token != NULL) {
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;
}
}
{
if (context->session == NULL || context->closing)
return NULL;
- if (!(context->created || context->refreshing))
+ if (!context->refreshing)
return NULL;
if (!context->refreshed) {
ctxTokenNew (context->session);
void afb_context_refresh(struct afb_context *context)
{
+ assert(context->validated);
context->refreshing = 1;
}
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;
+ }
}
+
+
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;
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);
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
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);
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*));