bye bye jsonapi
[oweals/gnunet.git] / src / rest-plugins / json_reclaim.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2018 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file rest-plugins/json_reclaim.c
21  * @brief JSON handling of reclaim data
22  * @author Martin Schanzenbach
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_json_lib.h"
27 #include "gnunet_reclaim_service.h"
28 #include "gnunet_reclaim_attribute_lib.h"
29
30 /**
31  * Parse given JSON object to a claim
32  *
33  * @param cls closure, NULL
34  * @param root the json object representing data
35  * @param spec where to write the data
36  * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
37  */
38 static int
39 parse_attr (void *cls,
40               json_t *root,
41               struct GNUNET_JSON_Specification *spec)
42 {
43   struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
44   const char* name_str;
45   const char* val_str;
46   const char* type_str;
47   const char* exp_str;
48   char *data;
49   int unpack_state;
50   uint32_t type;
51   size_t data_size;
52
53   GNUNET_assert(NULL != root);
54
55   if(!json_is_object(root))
56   {
57     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
58                 "Error json is not array nor object!\n");
59     return GNUNET_SYSERR;
60   }
61   //interpret single attribute
62   unpack_state = json_unpack(root,
63                              "{s:s, s:s, s:s, s:s!}",
64                              "name", &name_str,
65                              "type", &type_str,
66                              "value", &val_str,
67                              "exp", &exp_str);
68   if (0 != unpack_state)
69   {
70     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
71                "Error json object has a wrong format!\n");
72     return GNUNET_SYSERR;
73   }
74   type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
75   if (GNUNET_SYSERR == (GNUNET_RECLAIM_ATTRIBUTE_string_to_value (type,
76                                                                   val_str,
77                                                                   (void**)&data,
78                                                                   &data_size)))
79   {
80     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
81                "Attribute value invalid!\n");
82     return GNUNET_SYSERR;
83   }
84   attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str,
85                                              type,
86                                              data,
87                                              data_size);
88   *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr;
89   return GNUNET_OK;
90 }
91
92 /**
93  * Cleanup data left from parsing RSA public key.
94  *
95  * @param cls closure, NULL
96  * @param[out] spec where to free the data
97  */
98 static void
99 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
100 {
101   struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
102   attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
103   if (NULL != *attr)
104   {
105     GNUNET_free(*attr);
106     *attr = NULL;
107   }
108 }
109
110 /**
111  * JSON Specification for Reclaim claims.
112  *
113  * @param ticket struct of GNUNET_RECLAIM_ATTRIBUTE_Claim to fill
114  * @return JSON Specification
115  */
116 struct GNUNET_JSON_Specification
117 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
118 {
119   struct GNUNET_JSON_Specification ret = {
120     .parser = &parse_attr,
121     .cleaner = &clean_attr,
122     .cls = NULL,
123     .field = NULL,
124     .ptr = attr,
125     .ptr_size = 0,
126     .size_ptr = NULL
127   };
128   *attr = NULL;
129   return ret;
130 }
131 /**
132  * Parse given JSON object to a ticket
133  *
134  * @param cls closure, NULL
135  * @param root the json object representing data
136  * @param spec where to write the data
137  * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
138  */
139 static int
140 parse_ticket (void *cls,
141               json_t *root,
142               struct GNUNET_JSON_Specification *spec)
143 {
144   struct GNUNET_RECLAIM_Ticket *ticket;
145   const char* rnd_str;
146   const char* aud_str;
147   const char* id_str;
148   int unpack_state;
149
150   GNUNET_assert(NULL != root);
151
152   if(!json_is_object(root))
153   {
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155                 "Error json is not array nor object!\n");
156     return GNUNET_SYSERR;
157   }
158   //interpret single ticket
159   unpack_state = json_unpack(root,
160                              "{s:s, s:s, s:s!}",
161                              "rnd", &rnd_str,
162                              "audience", &aud_str,
163                              "identity", &id_str);
164   if (0 != unpack_state)
165   {
166     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
167                "Error json object has a wrong format!\n");
168     return GNUNET_SYSERR;
169   }
170   ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
171   if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
172                                                   strlen (rnd_str),
173                                                   &ticket->rnd,
174                                                   sizeof (uint64_t)))
175   {
176     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Rnd invalid\n");
177     GNUNET_free(ticket);
178     return GNUNET_SYSERR;
179   }
180   GNUNET_STRINGS_string_to_data (id_str,
181                                  strlen (id_str),
182                                  &ticket->identity,
183                                  sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
184   {
185     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Identity invalid\n");
186     GNUNET_free(ticket);
187     return GNUNET_SYSERR;
188   }
189
190   GNUNET_STRINGS_string_to_data (aud_str,
191                                  strlen (aud_str),
192                                  &ticket->audience,
193                                  sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
194   {
195     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Audience invalid\n");
196     GNUNET_free(ticket);
197     return GNUNET_SYSERR;
198   }
199
200   *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
201   return GNUNET_OK;
202 }
203
204 /**
205  * Cleanup data left from parsing RSA public key.
206  *
207  * @param cls closure, NULL
208  * @param[out] spec where to free the data
209  */
210 static void
211 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
212 {
213   struct GNUNET_RECLAIM_Ticket **ticket;
214   ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
215   if (NULL != *ticket)
216   {
217     GNUNET_free(*ticket);
218     *ticket = NULL;
219   }
220 }
221
222 /**
223  * JSON Specification for Reclaim tickets.
224  *
225  * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
226  * @return JSON Specification
227  */
228 struct GNUNET_JSON_Specification
229 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
230 {
231   struct GNUNET_JSON_Specification ret = {
232     .parser = &parse_ticket,
233     .cleaner = &clean_ticket,
234     .cls = NULL,
235     .field = NULL,
236     .ptr = ticket,
237     .ptr_size = 0,
238     .size_ptr = NULL
239   };
240   *ticket = NULL;
241   return ret;
242 }