Use afb_token in contexts
[src/app-framework-binder.git] / src / afb-context.c
1 /*
2  * Copyright (C) 2015-2019 "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
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <errno.h>
25
26 #include "afb-session.h"
27 #include "afb-context.h"
28 #include "afb-token.h"
29
30 static void init_context(struct afb_context *context, struct afb_session *session, struct afb_token *token)
31 {
32         assert(session != NULL);
33
34         /* reset the context for the session */
35         context->session = session;
36         context->flags = 0;
37         context->super = NULL;
38         context->api_key = NULL;
39         context->token = afb_token_addref(token);
40
41         /* check the token */
42         if (token != NULL) {
43                 if (afb_token_check(token))
44                         context->validated = 1;
45                 else
46                         context->invalidated = 1;
47         }
48 }
49
50 void afb_context_init(struct afb_context *context, struct afb_session *session, struct afb_token *token)
51 {
52         init_context(context, afb_session_addref(session), token);
53 }
54
55 void afb_context_init_validated(struct afb_context *context, struct afb_session *session)
56 {
57         afb_context_init(context, session, NULL);
58         context->validated = 1;
59 }
60
61 void afb_context_subinit(struct afb_context *context, struct afb_context *super)
62 {
63         context->session = super->session;
64         context->flags = 0;
65         context->super = super;
66         context->api_key = NULL;
67         context->token = super->token;
68         context->validated = super->validated;
69 }
70
71 int afb_context_connect(struct afb_context *context, const char *uuid, struct afb_token *token)
72 {
73         int created;
74         struct afb_session *session;
75
76         session = afb_session_get (uuid, AFB_SESSION_TIMEOUT_DEFAULT, &created);
77         if (session == NULL)
78                 return -1;
79         init_context(context, session, token);
80         if (created) {
81                 context->created = 1;
82         }
83         return 0;
84 }
85
86 int afb_context_connect_validated(struct afb_context *context, const char *uuid)
87 {
88         int rc = afb_context_connect(context, uuid, NULL);
89         if (!rc)
90                 context->validated = 1;
91         return rc;
92 }
93
94 void afb_context_disconnect(struct afb_context *context)
95 {
96         if (context->session && !context->super) {
97                 if (context->closing && !context->closed) {
98                         afb_context_change_loa(context, 0);
99                         afb_context_set(context, NULL, NULL);
100                         context->closed = 1;
101                 }
102                 afb_token_unref(context->token);
103                 afb_session_unref(context->session);
104                 context->session = NULL;
105         }
106 }
107
108 const char *afb_context_uuid(struct afb_context *context)
109 {
110         return context->session ? afb_session_uuid(context->session) : NULL;
111 }
112
113 void *afb_context_make(struct afb_context *context, int replace, void *(*make_value)(void *closure), void (*free_value)(void *item), void *closure)
114 {
115         assert(context->session != NULL);
116         return afb_session_cookie(context->session, context->api_key, make_value, free_value, closure, replace);
117 }
118
119 void *afb_context_get(struct afb_context *context)
120 {
121         assert(context->session != NULL);
122         return afb_session_get_cookie(context->session, context->api_key);
123 }
124
125 int afb_context_set(struct afb_context *context, void *value, void (*free_value)(void*))
126 {
127         assert(context->session != NULL);
128         return afb_session_set_cookie(context->session, context->api_key, value, free_value);
129 }
130
131 void afb_context_close(struct afb_context *context)
132 {
133         context->closing = 1;
134 }
135
136 int afb_context_check(struct afb_context *context)
137 {
138         if (context->super)
139                 return afb_context_check(context);
140         return context->validated;
141 }
142
143 int afb_context_check_loa(struct afb_context *context, unsigned loa)
144 {
145         return afb_context_get_loa(context) >= loa;
146 }
147
148 static inline const void *loa_key(struct afb_context *context)
149 {
150         return (const void*)(1+(intptr_t)(context->api_key));
151 }
152
153 static inline void *loa2ptr(unsigned loa)
154 {
155         return (void*)(intptr_t)loa;
156 }
157
158 static inline unsigned ptr2loa(void *ptr)
159 {
160         return (unsigned)(intptr_t)ptr;
161 }
162
163 int afb_context_change_loa(struct afb_context *context, unsigned loa)
164 {
165         if (!context->validated || loa > 7) {
166                 errno = EINVAL;
167                 return -1;
168         }
169
170         return afb_session_set_cookie(context->session, loa_key(context), loa2ptr(loa), NULL);
171 }
172
173 unsigned afb_context_get_loa(struct afb_context *context)
174 {
175         assert(context->session != NULL);
176         return ptr2loa(afb_session_get_cookie(context->session, loa_key(context)));
177 }