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