2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
24 #include <afb/afb-auth.h>
27 #include "afb-context.h"
32 static int check_permission(const char *permission, struct afb_xreq *xreq);
34 int afb_auth_check(const struct afb_auth *auth, struct afb_xreq *xreq)
42 return afb_context_check(&xreq->context);
45 return afb_context_check_loa(&xreq->context, auth->loa);
47 case afb_auth_Permission:
48 if (xreq->cred && auth->text)
49 return check_permission(auth->text, xreq);
50 /* TODO: handle case of self permission */
54 return afb_auth_check(auth->first, xreq) || afb_auth_check(auth->next, xreq);
57 return afb_auth_check(auth->first, xreq) && afb_auth_check(auth->next, xreq);
60 return !afb_auth_check(auth->first, xreq);
67 /*********************************************************************************/
68 #ifdef BACKEND_PERMISSION_IS_CYNARA
71 #include <cynara-client.h>
73 static cynara *handle;
74 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
76 static int check_permission(const char *permission, struct afb_xreq *xreq)
80 /* cynara isn't reentrant */
81 pthread_mutex_lock(&mutex);
83 /* lazy initialisation */
85 rc = cynara_initialize(&handle, NULL);
86 if (rc != CYNARA_API_SUCCESS) {
88 ERROR("cynara initialisation failed with code %d", rc);
93 /* query cynara permission */
94 rc = cynara_check(handle, xreq->cred->label, afb_context_uuid(&xreq->context), xreq->cred->user, permission);
96 pthread_mutex_unlock(&mutex);
97 return rc == CYNARA_API_ACCESS_ALLOWED;
100 /*********************************************************************************/
102 static int check_permission(const char *permission, struct afb_xreq *xreq)
104 WARNING("Granting permission %s by default of backend", permission);