summaryrefslogtreecommitdiff
path: root/src/reducer/anastasis_api_redux.h
blob: 4d62d5e9c624633a9382782913c2927c1cf3ef1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
  This file is part of Anastasis
  Copyright (C) 2020, 2021 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file reducer/anastasis_api_redux.h
 * @brief anastasis reducer api, internal data structures
 * @author Christian Grothoff
 * @author Dominik Meister
 * @author Dennis Neufeld
 */
#ifndef ANASTASIS_API_REDUX_H
#define ANASTASIS_API_REDUX_H


#define ANASTASIS_GENERIC_STATES(REDUX_STATE) \
  REDUX_STATE (ERROR) \
  REDUX_STATE (CONTINENT_SELECTING)   \
  REDUX_STATE (COUNTRY_SELECTING)  \
  REDUX_STATE (USER_ATTRIBUTES_COLLECTING)

#define GENERATE_GENERIC_ENUM(ENUM) ANASTASIS_GENERIC_STATE_ ## ENUM,

enum ANASTASIS_GenericState
{
  ANASTASIS_GENERIC_STATES (GENERATE_GENERIC_ENUM)
};

#undef GENERATE_GENERIC_ENUM

#define ANASTASIS_BACKUP_STATES(REDUX_STATE) \
  ANASTASIS_GENERIC_STATES (REDUX_STATE) \
  REDUX_STATE (AUTHENTICATIONS_EDITING)  \
  REDUX_STATE (POLICIES_REVIEWING)   \
  REDUX_STATE (SECRET_EDITING) \
  REDUX_STATE (TRUTHS_PAYING) \
  REDUX_STATE (POLICIES_PAYING) \
  REDUX_STATE (BACKUP_FINISHED)

#define GENERATE_BACKUP_ENUM(ENUM) ANASTASIS_BACKUP_STATE_ ## ENUM,

enum ANASTASIS_BackupState
{
  ANASTASIS_BACKUP_STATES (GENERATE_BACKUP_ENUM)
};

#undef GENERATE_BACKUP_ENUM

#define ANASTASIS_RECOVERY_STATES(REDUX_STATE) \
  ANASTASIS_GENERIC_STATES (REDUX_STATE) \
  REDUX_STATE (SECRET_SELECTING)  \
  REDUX_STATE (CHALLENGE_SELECTING)  \
  REDUX_STATE (CHALLENGE_PAYING)   \
  REDUX_STATE (CHALLENGE_SOLVING)  \
  REDUX_STATE (RECOVERY_FINISHED)

#define GENERATE_RECOVERY_ENUM(ENUM) ANASTASIS_RECOVERY_STATE_ ## ENUM,

enum ANASTASIS_RecoveryState
{
  ANASTASIS_RECOVERY_STATES (GENERATE_RECOVERY_ENUM)
};

#undef GENERATE_RECOVERY_ENUM


/**
 * CURL context to be used by all operations.
 */
extern struct GNUNET_CURL_Context *ANASTASIS_REDUX_ctx_;


/**
 * Initialize reducer subsystem.
 *
 * @param ctx context to use for CURL requests.
 */
void
ANASTASIS_redux_init (struct GNUNET_CURL_Context *ctx);


/**
 * Terminate reducer subsystem.
 */
void
ANASTASIS_redux_done (void);


/**
 * Produce an initial state with an initialized list of
 * continents.
 *
 * @return list of continents
 */
json_t *
ANASTASIS_REDUX_load_continents_ (void);


/**
 * Returns the enum value to a string value of a state.
 *
 * @param state_string string to convert
 * @return ANASTASIS_GENERIC_STATE_ERROR on error
 */
enum ANASTASIS_GenericState
ANASTASIS_generic_state_from_string_ (const char *state_string);


/**
 * Returns the string value of a state.
 *
 * @param gs state value to convert
 * @return NULL on error
 */
const char *
ANASTASIS_generic_state_to_string_ (enum ANASTASIS_GenericState gs);


/**
 * Returns the enum value to a string value of a state.
 *
 * @param state_string string to convert
 * @return ANASTASIS_BACKUP_STATE_ERROR on error
 */
enum ANASTASIS_BackupState
ANASTASIS_backup_state_from_string_ (const char *state_string);


/**
 * Returns the string value of a state.
 *
 * @param bs state to convert to a string
 * @return NULL on error
 */
const char *
ANASTASIS_backup_state_to_string_ (enum ANASTASIS_BackupState bs);


/**
 * Returns the enum value to a string value of a state.
 *
 * @param state_string value to convert
 * @return ANASTASIS_RECOVERY_STATE_ERROR on error
 */
enum ANASTASIS_RecoveryState
ANASTASIS_recovery_state_from_string_ (const char *state_string);


/**
 * Returns the string value of a state.
 *
 * @param rs value to convert
 * @return NULL on error
 */
const char *
ANASTASIS_recovery_state_to_string_ (enum ANASTASIS_RecoveryState rs);


/**
 * Function to return a json error response.
 *
 * @param cb callback to give error to
 * @param cb_cls callback closure
 * @param ec error code
 * @param detail error detail
 */
void
ANASTASIS_redux_fail_ (ANASTASIS_ActionCallback cb,
                       void *cb_cls,
                       enum TALER_ErrorCode ec,
                       const char *detail);


/**
 * DispatchHandler/Callback function which is called for a
 * "add_provider" action.  Adds another Anastasis provider
 * to the list of available providers for storing information.
 *
 * @param state state to operate on
 * @param arguments arguments with a provider URL to add
 * @param cb callback to call during/after operation
 * @param cb_cls callback closure
 * @return true if @a cb was invoked
 */
bool
ANASTASIS_add_provider_ (json_t *state,
                         const json_t *arguments,
                         ANASTASIS_ActionCallback cb,
                         void *cb_cls);


/**
 * Adds the server configuration of the Anastasis provider
 * at @a url to the json @a state.  Checks if we have
 * the provider information already available. If so,
 * imports it into @a state. If not, queries the provider,
 * generating a success or failure outcome asynchronously.
 *
 * @param url the provider's base URL to add
 * @param[in,out] state the json state to operate on
 * @param cb callback to call during/after operation
 * @param cb_cls callback closure
 * @return handle to cancel asynchronous operation, NULL if
 *         we completed synchronously
 */
struct ANASTASIS_ReduxAction *
ANASTASIS_REDUX_add_provider_to_state_ (const char *url,
                                        json_t *state,
                                        ANASTASIS_ActionCallback cb,
                                        void *cb_cls);


/**
 * A generic DispatchHandler/Callback function which is called for a
 * "back" action.
 *
 * @param[in,out] state state to operate on
 * @param arguments arguments to use for operation on state
 * @param cb callback to call during/after operation
 * @param cb_cls callback closure for @a cb
 * @return NULL (no asynchronous action)
 */
struct ANASTASIS_ReduxAction *
ANASTASIS_back_generic_decrement_ (json_t *state,
                                   const json_t *arguments,
                                   ANASTASIS_ActionCallback cb,
                                   void *cb_cls);


/**
 * Function to load json containing all countries.
 * Returns the countries.
 *
 * @return list of countries
 */
const json_t *
ANASTASIS_redux_countries_init_ (void);


/**
 * Operates on a recovery state. The new state is returned
 * by a callback function.
 * This function can do network access to talk to anastasis service providers.
 *
 * @param[in,out] state input/output state (to be modified)
 * @param action what action to perform
 * @param arguments data for the @a action
 * @param cb function to call with the result
 * @param cb_cls closure for @a cb
 */
struct ANASTASIS_ReduxAction *
ANASTASIS_recovery_action_ (json_t *state,
                            const char *action,
                            const json_t *arguments,
                            ANASTASIS_ActionCallback cb,
                            void *cb_cls);


/**
 * DispatchHandler/Callback function which is called for a
 * "enter_user_attributes" action after verifying that the
 * arguments provided were OK and the state transition was
 * initiated.  Begins the actual recovery logic.
 *
 * Returns an #ANASTASIS_ReduxAction.
 *
 * @param state state to operate on
 * @param arguments data for the operation
 * @param cb callback to call during/after operation
 * @param cb_cls callback closure for @a cb
 * @return NULL
 */
struct ANASTASIS_ReduxAction *
ANASTASIS_REDUX_recovery_challenge_begin_ (json_t *state,
                                           const json_t *arguments,
                                           ANASTASIS_ActionCallback cb,
                                           void *cb_cls);


/**
 * DispatchHandler/Callback function which is called for a
 * "enter_user_attributes" action after verifying that the
 * arguments provided were OK and the state transition was
 * initiated.  Begins the actual backup logic.
 *
 * Returns a `struct ANASTASIS_ReduxAction`.
 *
 * @param state state to operate on
 * @param arguments data for the operation
 * @param cb callback to call during/after operation
 * @param cb_cls callback closure
 */
struct ANASTASIS_ReduxAction *
ANASTASIS_REDUX_backup_begin_ (json_t *state,
                               const json_t *arguments,
                               ANASTASIS_ActionCallback cb,
                               void *cb_cls);


/**
 * Operates on a backup state and returns the new state via a
 * callback function.
 * This function can do network access to talk to anastasis service providers.
 *
 * @param[in,out] state input/output state (to be modified)
 * @param action what action to perform
 * @param arguments data for the @a action
 * @param cb function to call with the result
 * @param cb_cls closure for @a cb
 */
struct ANASTASIS_ReduxAction *
ANASTASIS_backup_action_ (json_t *state,
                          const char *action,
                          const json_t *arguments,
                          ANASTASIS_ActionCallback cb,
                          void *cb_cls);


/**
 * Generic container for an action with asynchronous activities.
 */
struct ANASTASIS_ReduxAction
{
  /**
   * Function to call to clean up.
   */
  void (*cleanup)(void *cls);

  /**
   * Action-specific state, closure for @e cleanup.
   */
  void *cleanup_cls;
};


#endif