implement authorisation check
[src/app-framework-binder.git] / src / afb-auth.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
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
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #define _GNU_SOURCE
20 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
21
22 #include <stdlib.h>
23
24 #include <afb/afb-auth.h>
25
26 #include "afb-auth.h"
27 #include "afb-context.h"
28 #include "afb-xreq.h"
29 #include "verbose.h"
30
31 static int check_permission(const char *permission, struct afb_xreq *xreq);
32
33 int afb_auth_check(const struct afb_auth *auth, struct afb_xreq *xreq)
34 {
35         switch (auth->type) {
36         default:
37         case afb_auth_No:
38                 return 0;
39
40         case afb_auth_Token:
41                 return afb_context_check(&xreq->context);
42
43         case afb_auth_LOA:
44                 return afb_context_check_loa(&xreq->context, auth->loa);
45
46         case afb_auth_Permission:
47                 return xreq->cred && auth->text && check_permission(auth->text, xreq);
48
49         case afb_auth_Or:
50                 return afb_auth_check(auth->first, xreq) || afb_auth_check(auth->next, xreq);
51
52         case afb_auth_And:
53                 return afb_auth_check(auth->first, xreq) && afb_auth_check(auth->next, xreq);
54
55         case afb_auth_Not:
56                 return !afb_auth_check(auth->first, xreq);
57
58         case afb_auth_Yes:
59                 return 1;
60         }
61 }
62
63 #ifdef BACKEND_PERMISSION_IS_CYNARA
64 #include <cynara-client.h>
65 static int check_permission(const char *permission, struct afb_xreq *xreq)
66 {
67         static cynara *cynara;
68         char uid[64];
69         int rc;
70
71         if (!cynara) {
72                 rc = cynara_initialize(&cynara, NULL);
73                 if (rc != CYNARA_API_SUCCESS) {
74                         cynara = NULL;
75                         ERROR("cynara initialisation failed with code %d", rc);
76                         return 0;
77                 }
78         }
79         rc = cynara_check(cynara, cred->label, afb_context_uuid(&xreq->context), xreq->cred->user, permission);
80         return rc == CYNARA_API_ACCESS_ALLOWED;
81 }
82 #else
83 static int check_permission(const char *permission, struct afb_xreq *xreq)
84 {
85         WARNING("Granting permission %s by default", permission);
86         return 1;
87 }
88 #endif
89