fakebank_bank.c (16412B)
1 /* 2 This file is part of TALER 3 (C) 2016-2024 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License 7 as published by the Free Software Foundation; either version 3, 8 or (at your option) any later version. 9 10 TALER is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with TALER; see the file COPYING. If not, 17 see <http://www.gnu.org/licenses/> 18 */ 19 /** 20 * @file bank-lib/fakebank_bank.c 21 * @brief Main dispatcher for the Taler Bank API 22 * @author Christian Grothoff <christian@grothoff.org> 23 */ 24 #include "taler/taler_fakebank_lib.h" 25 #include "taler/taler_bank_service.h" 26 #include "taler/taler_mhd_lib.h" 27 #include <gnunet/gnunet_mhd_compat.h> 28 #include "fakebank.h" 29 #include "fakebank_bank.h" 30 #include "fakebank_tbr.h" 31 #include "fakebank_twg.h" 32 #include "fakebank_bank_get_accounts.h" 33 #include "fakebank_bank_get_withdrawals.h" 34 #include "fakebank_bank_get_root.h" 35 #include "fakebank_bank_post_accounts_token.h" 36 #include "fakebank_bank_post_accounts_withdrawals.h" 37 #include "fakebank_bank_post_withdrawals_id_op.h" 38 #include "fakebank_bank_testing_register.h" 39 40 41 MHD_RESULT 42 TALER_FAKEBANK_bank_main_ ( 43 struct TALER_FAKEBANK_Handle *h, 44 struct MHD_Connection *connection, 45 const char *url, 46 const char *method, 47 const char *upload_data, 48 size_t *upload_data_size, 49 void **con_cls) 50 { 51 if (0 == strcasecmp (method, 52 MHD_HTTP_METHOD_HEAD)) 53 method = MHD_HTTP_METHOD_GET; 54 55 if ( (0 == strcmp (url, 56 "/")) && 57 (0 == strcasecmp (method, 58 MHD_HTTP_METHOD_GET)) ) 59 { 60 /* GET / */ 61 return TALER_FAKEBANK_bank_get_root_ (h, 62 connection); 63 } 64 65 if ( (0 == strcmp (url, 66 "/config")) && 67 (0 == strcasecmp (method, 68 MHD_HTTP_METHOD_GET)) ) 69 { 70 /* GET /config */ 71 return TALER_MHD_REPLY_JSON_PACK ( 72 connection, 73 MHD_HTTP_OK, 74 GNUNET_JSON_pack_string ("version", 75 "8:0:0"), 76 GNUNET_JSON_pack_string ("currency", 77 h->currency), 78 GNUNET_JSON_pack_string ("implementation", 79 "urn:net:taler:specs:bank:fakebank"), 80 GNUNET_JSON_pack_object_steal ( 81 "currency_specification", 82 GNUNET_JSON_PACK ( 83 GNUNET_JSON_pack_string ("name", 84 h->currency), 85 GNUNET_JSON_pack_string ("currency", 86 h->currency), 87 GNUNET_JSON_pack_uint64 ("num_fractional_input_digits", 88 2), 89 GNUNET_JSON_pack_uint64 ("num_fractional_normal_digits", 90 2), 91 GNUNET_JSON_pack_uint64 ("num_fractional_trailing_zero_digits", 92 2), 93 GNUNET_JSON_pack_object_steal ( 94 "alt_unit_names", 95 GNUNET_JSON_PACK ( 96 GNUNET_JSON_pack_string ("0", 97 h->currency))), 98 GNUNET_JSON_pack_string ("name", 99 h->currency))), 100 GNUNET_JSON_pack_string ("name", 101 "taler-corebank")); 102 } 103 104 if ( (0 == strcmp (url, 105 "/public-accounts")) && 106 (0 == strcasecmp (method, 107 MHD_HTTP_METHOD_GET)) ) 108 { 109 /* GET /public-accounts */ 110 return TALER_MHD_REPLY_JSON_PACK ( 111 connection, 112 MHD_HTTP_OK, 113 GNUNET_JSON_pack_array_steal ("public_accounts", 114 json_array ())); 115 } 116 117 /* account registration API */ 118 if ( (0 == strcmp (url, 119 "/accounts")) && 120 (0 == strcasecmp (method, 121 MHD_HTTP_METHOD_POST)) ) 122 { 123 /* POST /accounts */ 124 return TALER_FAKEBANK_bank_testing_register_ (h, 125 connection, 126 upload_data, 127 upload_data_size, 128 con_cls); 129 } 130 131 if ( (0 == strcmp (url, 132 "/accounts")) && 133 (0 == strcasecmp (method, 134 MHD_HTTP_METHOD_GET)) ) 135 { 136 /* GET /accounts */ 137 GNUNET_break (0); /* not implemented */ 138 return TALER_MHD_reply_with_error ( 139 connection, 140 MHD_HTTP_NOT_IMPLEMENTED, 141 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 142 url); 143 } 144 145 if ( (0 == strcmp (url, 146 "/cashout-rate")) && 147 (0 == strcasecmp (method, 148 MHD_HTTP_METHOD_GET)) ) 149 { 150 /* GET /cashout-rate */ 151 GNUNET_break (0); /* not implemented */ 152 return TALER_MHD_reply_with_error ( 153 connection, 154 MHD_HTTP_NOT_IMPLEMENTED, 155 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 156 url); 157 } 158 159 if ( (0 == strcmp (url, 160 "/cashouts")) && 161 (0 == strcasecmp (method, 162 MHD_HTTP_METHOD_GET)) ) 163 { 164 /* GET /cashouts */ 165 GNUNET_break (0); /* not implemented */ 166 return TALER_MHD_reply_with_error ( 167 connection, 168 MHD_HTTP_NOT_IMPLEMENTED, 169 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 170 url); 171 } 172 173 if ( (0 == strncmp (url, 174 "/withdrawals/", 175 strlen ("/withdrawals/"))) && 176 (0 == strcasecmp (method, 177 MHD_HTTP_METHOD_GET)) ) 178 { 179 /* GET /withdrawals/$WID */ 180 const char *wid; 181 182 wid = &url[strlen ("/withdrawals/")]; 183 return TALER_FAKEBANK_bank_get_withdrawals_ (h, 184 connection, 185 wid); 186 } 187 188 if (0 == strncmp (url, 189 "/accounts/", 190 strlen ("/accounts/"))) 191 { 192 const char *acc_name = &url[strlen ("/accounts/")]; 193 const char *end_acc = strchr (acc_name, 194 '/'); 195 196 if ( (NULL != end_acc) && 197 (0 == strncmp (end_acc, 198 "/taler-wire-gateway/", 199 strlen ("/taler-wire-gateway/"))) ) 200 { 201 /* $METHOD /accounts/$ACCOUNT/taler-wire-gateway/ */ 202 char *acc; 203 MHD_RESULT ret; 204 205 acc = GNUNET_strndup (acc_name, 206 end_acc - acc_name); 207 end_acc += strlen ("/taler-wire-gateway"); 208 ret = TALER_FAKEBANK_twg_main_ (h, 209 connection, 210 acc, 211 end_acc, 212 method, 213 upload_data, 214 upload_data_size, 215 con_cls); 216 GNUNET_free (acc); 217 return ret; 218 } 219 220 if ( (NULL != end_acc) && 221 (0 == strncmp (end_acc, 222 "/taler-revenue/", 223 strlen ("/taler-revenue/"))) ) 224 { 225 /* $METHOD /accounts/$ACCOUNT/taler-revenue/ */ 226 char *acc; 227 MHD_RESULT ret; 228 229 acc = GNUNET_strndup (acc_name, 230 end_acc - acc_name); 231 end_acc += strlen ("/taler-revenue"); 232 ret = TALER_FAKEBANK_tbr_main_ (h, 233 connection, 234 acc, 235 end_acc, 236 method, 237 upload_data, 238 upload_data_size, 239 con_cls); 240 GNUNET_free (acc); 241 return ret; 242 } 243 244 if ( (NULL != end_acc) && 245 (0 == strcasecmp (method, 246 MHD_HTTP_METHOD_POST)) && 247 (0 == strncmp (end_acc, 248 "/token", 249 strlen ("/token"))) ) 250 { 251 /* POST /accounts/$ACCOUNT/token */ 252 char *acc; 253 MHD_RESULT ret; 254 255 acc = GNUNET_strndup (acc_name, 256 end_acc - acc_name); 257 ret = TALER_FAKEBANK_bank_post_accounts_token_ (h, 258 connection, 259 acc, 260 upload_data, 261 upload_data_size, 262 con_cls); 263 GNUNET_free (acc); 264 return ret; 265 } 266 267 if ( (NULL == end_acc) && 268 (0 == strcasecmp (method, 269 MHD_HTTP_METHOD_GET)) ) 270 { 271 /* GET /accounts/$ACCOUNT */ 272 return TALER_FAKEBANK_bank_get_accounts_ (h, 273 connection, 274 acc_name); 275 } 276 277 if ( (NULL == end_acc) && 278 (0 == strcasecmp (method, 279 MHD_HTTP_METHOD_PATCH)) ) 280 { 281 /* PATCH /accounts/$USERNAME */ 282 GNUNET_break (0); /* not implemented */ 283 return TALER_MHD_reply_with_error ( 284 connection, 285 MHD_HTTP_NOT_IMPLEMENTED, 286 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 287 url); 288 } 289 290 if ( (NULL == end_acc) && 291 (0 == strcasecmp (method, 292 MHD_HTTP_METHOD_DELETE)) ) 293 { 294 /* DELETE /accounts/$USERNAME */ 295 GNUNET_break (0); /* not implemented */ 296 return TALER_MHD_reply_with_error ( 297 connection, 298 MHD_HTTP_NOT_IMPLEMENTED, 299 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 300 url); 301 } 302 303 if ( (NULL != end_acc) && 304 (0 == strcmp ("/auth", 305 end_acc)) && 306 (0 == strcasecmp (method, 307 MHD_HTTP_METHOD_PATCH)) ) 308 { 309 /* PATCH /accounts/$USERNAME/auth */ 310 GNUNET_break (0); /* not implemented */ 311 return TALER_MHD_reply_with_error ( 312 connection, 313 MHD_HTTP_NOT_IMPLEMENTED, 314 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 315 url); 316 } 317 318 if ( (NULL != end_acc) && 319 (0 == strcasecmp (method, 320 MHD_HTTP_METHOD_GET)) ) 321 { 322 /* GET /accounts/$ACCOUNT/+ */ 323 324 if (0 == strcmp (end_acc, 325 "/transactions")) 326 { 327 /* GET /accounts/$USERNAME/transactions */ 328 GNUNET_break (0); /* not implemented */ 329 return TALER_MHD_reply_with_error ( 330 connection, 331 MHD_HTTP_NOT_IMPLEMENTED, 332 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 333 url); 334 } 335 if (0 == strncmp (end_acc, 336 "/transactions/", 337 strlen ("/transactions/"))) 338 { 339 /* GET /accounts/$USERNAME/transactions/$TID */ 340 GNUNET_break (0); /* not implemented */ 341 return TALER_MHD_reply_with_error ( 342 connection, 343 MHD_HTTP_NOT_IMPLEMENTED, 344 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 345 url); 346 } 347 if (0 == strcmp (end_acc, 348 "/withdrawals")) 349 { 350 /* GET /accounts/$USERNAME/withdrawals */ 351 GNUNET_break (0); /* not implemented */ 352 return TALER_MHD_reply_with_error ( 353 connection, 354 MHD_HTTP_NOT_IMPLEMENTED, 355 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 356 url); 357 } 358 if (0 == strcmp (end_acc, 359 "/cashouts")) 360 { 361 /* GET /accounts/$USERNAME/cashouts */ 362 GNUNET_break (0); /* not implemented */ 363 return TALER_MHD_reply_with_error ( 364 connection, 365 MHD_HTTP_NOT_IMPLEMENTED, 366 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 367 url); 368 } 369 if (0 == strncmp (end_acc, 370 "/cashouts/", 371 strlen ("/cashouts/"))) 372 { 373 /* GET /accounts/$USERNAME/cashouts/$CID */ 374 GNUNET_break (0); /* not implemented */ 375 return TALER_MHD_reply_with_error ( 376 connection, 377 MHD_HTTP_NOT_IMPLEMENTED, 378 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 379 url); 380 } 381 382 383 GNUNET_break_op (0); 384 return TALER_MHD_reply_with_error (connection, 385 MHD_HTTP_NOT_FOUND, 386 TALER_EC_GENERIC_ENDPOINT_UNKNOWN, 387 acc_name); 388 } 389 390 if ( (NULL != end_acc) && 391 (0 == strcasecmp (method, 392 MHD_HTTP_METHOD_POST)) ) 393 { 394 /* POST /accounts/$ACCOUNT/+ */ 395 char *acc; 396 397 acc = GNUNET_strndup (acc_name, 398 end_acc - acc_name); 399 if (0 == strcmp (end_acc, 400 "/cashouts")) 401 { 402 /* POST /accounts/$USERNAME/cashouts */ 403 GNUNET_break (0); /* not implemented */ 404 GNUNET_free (acc); 405 return TALER_MHD_reply_with_error ( 406 connection, 407 MHD_HTTP_NOT_IMPLEMENTED, 408 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 409 url); 410 } 411 if (0 == strncmp (end_acc, 412 "/cashouts/", 413 strlen ("/cashouts/"))) 414 { 415 /* POST /accounts/$USERNAME/cashouts/+ */ 416 const char *cid = end_acc + strlen ("/cashouts/"); 417 const char *opid = strchr (cid, 418 '/'); 419 char *ci; 420 421 if (NULL == opid) 422 { 423 /* POST /accounts/$ACCOUNT/cashouts/$CID (not defined) */ 424 GNUNET_break_op (0); 425 GNUNET_free (acc); 426 return TALER_MHD_reply_with_error (connection, 427 MHD_HTTP_NOT_FOUND, 428 TALER_EC_GENERIC_ENDPOINT_UNKNOWN, 429 acc_name); 430 } 431 ci = GNUNET_strndup (cid, 432 opid - cid); 433 if (0 == strcmp (opid, 434 "/abort")) 435 { 436 GNUNET_break (0); /* not implemented */ 437 GNUNET_free (ci); 438 GNUNET_free (acc); 439 return TALER_MHD_reply_with_error ( 440 connection, 441 MHD_HTTP_NOT_IMPLEMENTED, 442 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 443 url); 444 } 445 if (0 == strcmp (opid, 446 "/confirm")) 447 { 448 GNUNET_break (0); /* not implemented */ 449 GNUNET_free (ci); 450 GNUNET_free (acc); 451 return TALER_MHD_reply_with_error ( 452 connection, 453 MHD_HTTP_NOT_IMPLEMENTED, 454 TALER_EC_GENERIC_CLIENT_INTERNAL_ERROR, 455 url); 456 } 457 } 458 459 if (0 == strcmp (end_acc, 460 "/withdrawals")) 461 { 462 /* POST /accounts/$ACCOUNT/withdrawals */ 463 MHD_RESULT ret; 464 465 ret = TALER_FAKEBANK_bank_post_account_withdrawals_ ( 466 h, 467 connection, 468 acc, 469 upload_data, 470 upload_data_size, 471 con_cls); 472 GNUNET_free (acc); 473 return ret; 474 } 475 476 if (0 == strncmp (end_acc, 477 "/withdrawals/", 478 strlen ("/withdrawals/"))) 479 { 480 /* POST /accounts/$ACCOUNT/withdrawals/$WID/$OP */ 481 MHD_RESULT ret; 482 const char *pos = &end_acc[strlen ("/withdrawals/")]; 483 const char *op = strchr (pos, '/'); 484 485 if (NULL != op) 486 { 487 char *wid = GNUNET_strndup (pos, 488 op - pos); 489 490 ret = TALER_FAKEBANK_bank_withdrawals_id_op_ ( 491 h, 492 connection, 493 acc, 494 wid, 495 op, 496 upload_data, 497 upload_data_size, 498 con_cls); 499 GNUNET_free (wid); 500 GNUNET_free (acc); 501 return ret; 502 } 503 GNUNET_free (acc); 504 } 505 } 506 } 507 508 GNUNET_break_op (0); 509 TALER_LOG_ERROR ("Breaking URL: %s %s\n", 510 method, 511 url); 512 return TALER_MHD_reply_with_error (connection, 513 MHD_HTTP_NOT_FOUND, 514 TALER_EC_GENERIC_ENDPOINT_UNKNOWN, 515 url); 516 }