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