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