X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-context.c;h=899baa646c32dc17cbf881c21d53070256f62d5a;hb=65353dce81a629e042800bb7b86fcd869a76727e;hp=3d6dee0627ea91a3192a096941e884f8869a3040;hpb=d8aff62647736c3f17ff15989ec9f90b48efe1c4;p=src%2Fapp-framework-binder.git diff --git a/src/afb-context.c b/src/afb-context.c index 3d6dee06..899baa64 100644 --- a/src/afb-context.c +++ b/src/afb-context.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015-2019 "IoT.bzh" + * Copyright (C) 2015-2020 "IoT.bzh" * Author "Fulup Ar Foll" * Author José Bollo * @@ -27,6 +27,7 @@ #include "afb-context.h" #include "afb-token.h" #include "afb-cred.h" +#include "afb-perm.h" #include "afb-permission-text.h" #include "verbose.h" @@ -160,7 +161,17 @@ void afb_context_on_behalf_other_context(struct afb_context *context, struct afb int afb_context_has_permission(struct afb_context *context, const char *permission) { - return afb_cred_has_permission(context->credentials, permission, context); + return afb_perm_check(context, permission); +} + +void afb_context_has_permission_async( + struct afb_context *context, + const char *permission, + void (*callback)(void *_closure, int _status), + void *closure +) +{ + return afb_perm_check_async(context, permission, callback, closure); } const char *afb_context_uuid(struct afb_context *context) @@ -191,9 +202,34 @@ void afb_context_close(struct afb_context *context) context->closing = 1; } -int afb_context_check(struct afb_context *context) +struct chkctx { + struct afb_context *context; + void (*callback)(void *_closure, int _status); + void *closure; +}; + +static void check_context_cb(void *closure_chkctx, int status) { + struct chkctx *cc = closure_chkctx; + struct afb_context *context = cc->context; + void (*callback)(void*,int) = cc->callback; + void *closure = cc->closure; + + free(cc); + if (status) + context->validated = 1; + else + context->invalidated = 1; + callback(closure, status); +} + +static int check_context( + struct afb_context *context, + void (*callback)(void *_closure, int _status), + void *closure +) { int r; + struct chkctx *cc; if (context->validated) r = 1; @@ -201,9 +237,21 @@ int afb_context_check(struct afb_context *context) r = 0; else { if (context->super) - r = afb_context_check(context->super); - else + r = check_context(context->super, callback, closure); + else if (!callback) r = afb_context_has_permission(context, afb_permission_token_valid); + else { + cc = malloc(sizeof *cc); + if (cc) { + cc->context = context; + cc->callback = callback; + cc->closure = closure; + afb_context_has_permission_async(context, afb_permission_token_valid, check_context_cb, cc); + return -1; + } + ERROR("out-of-memory"); + r = 0; + } if (r) context->validated = 1; else @@ -212,6 +260,21 @@ int afb_context_check(struct afb_context *context) return r; } +int afb_context_check(struct afb_context *context) +{ + return check_context(context, 0, 0); +} + +void afb_context_check_async( + struct afb_context *context, + void (*callback)(void *_closure, int _status), + void *closure +) { + int r = check_context(context, callback, closure); + if (r >= 0) + callback(closure, r); +} + static inline const void *loa_key(struct afb_context *context) { return (const void*)(1+(intptr_t)(context->api_key));