aboutsummaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_private-post-account.c
blob: f9be253c4078d8dc15e52d48ad6915ea6d522721 (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
/*
  This file is part of TALER
  (C) 2020-2023 Taler Systems SA

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

  TALER 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 General Public License for more details.

  You should have received a copy of the GNU General Public
  License along with TALER; see the file COPYING.  If not,
  see <http://www.gnu.org/licenses/>
*/

/**
 * @file taler-merchant-httpd_private-post-account.c
 * @brief implementing POST /private/account request handling
 * @author Christian Grothoff
 */
#include "platform.h"
#include "taler-merchant-httpd_private-post-account.h"
#include "taler-merchant-httpd_helper.h"
#include "taler_merchant_bank_lib.h"
#include <taler/taler_dbevents.h>
#include <taler/taler_json_lib.h>


MHD_RESULT
TMH_private_post_account (const struct TMH_RequestHandler *rh,
                          struct MHD_Connection *connection,
                          struct TMH_HandlerContext *hc)
{
  struct TMH_MerchantInstance *mi = hc->instance;
  const char *credit_facade_url = NULL;
  const json_t *credit_facade_credentials = NULL;
  const char *uri;
  struct GNUNET_JSON_Specification ispec[] = {
    GNUNET_JSON_spec_string ("payto_uri",
                             &uri),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_string ("credit_facade_url",
                               &credit_facade_url),
      NULL),
    GNUNET_JSON_spec_mark_optional (
      GNUNET_JSON_spec_object_const ("credit_facade_credentials",
                                     &credit_facade_credentials),
      NULL),
    GNUNET_JSON_spec_end ()
  };
  struct TMH_WireMethod *wm;

  {
    enum GNUNET_GenericReturnValue res;

    res = TALER_MHD_parse_json_data (connection,
                                     hc->request_body,
                                     ispec);
    if (GNUNET_OK != res)
      return (GNUNET_NO == res)
             ? MHD_YES
             : MHD_NO;
  }

  {
    char *err;

    if (NULL !=
        (err = TALER_payto_validate (uri)))
    {
      MHD_RESULT mret;

      GNUNET_break_op (0);
      mret = TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_BAD_REQUEST,
                                         TALER_EC_GENERIC_PAYTO_URI_MALFORMED,
                                         err);
      GNUNET_free (err);
      return mret;
    }
  }

  if ( (NULL == credit_facade_url) !=
       (NULL == credit_facade_credentials) )
  {
    GNUNET_break_op (0);
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_BAD_REQUEST,
                                       TALER_EC_GENERIC_PARAMETER_MISSING,
                                       (NULL == credit_facade_url)
                                       ? "credit_facade_url"
                                       : "credit_facade_credentials");
  }
  if ( (NULL != credit_facade_url) ||
       (NULL != credit_facade_credentials) )
  {
    struct TALER_MERCHANT_BANK_AuthenticationData auth;

    if (GNUNET_OK !=
        TALER_MERCHANT_BANK_auth_parse_json (credit_facade_credentials,
                                             credit_facade_url,
                                             &auth))
    {
      GNUNET_break_op (0);
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_BAD_REQUEST,
                                         TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                         "credit_facade_credentials");
    }
    TALER_MERCHANT_BANK_auth_free (&auth);
  }

  /* convert provided payto URI into internal data structure with salts */
  wm = TMH_setup_wire_account (uri,
                               credit_facade_url,
                               credit_facade_credentials);
  GNUNET_assert (NULL != wm);
  {
    struct TALER_MERCHANTDB_AccountDetails ad = {
      .payto_uri = wm->payto_uri,
      .salt = wm->wire_salt,
      .h_wire = wm->h_wire,
      .credit_facade_url = wm->credit_facade_url,
      .credit_facade_credentials = wm->credit_facade_credentials,
      .active = wm->active
    };
    enum GNUNET_DB_QueryStatus qs;

    qs = TMH_db->insert_account (TMH_db->cls,
                                 mi->settings.id,
                                 &ad);
    switch (qs)
    {
    case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
      break;
    case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
      /* conflict: account exists */
      TMH_wire_method_free (wm);
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_CONFLICT,
                                         TALER_EC_MERCHANT_PRIVATE_ACCOUNT_EXISTS,
                                         uri);
    case GNUNET_DB_STATUS_SOFT_ERROR:
    case GNUNET_DB_STATUS_HARD_ERROR:
      GNUNET_break (0);
      TMH_wire_method_free (wm);
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
                                         TALER_EC_GENERIC_DB_STORE_FAILED,
                                         "insert_account");
    }
  }

  {
    struct GNUNET_DB_EventHeaderP es = {
      .size = htons (sizeof (es)),
      .type = htons (TALER_DBEVENT_MERCHANT_ACCOUNTS_CHANGED)
    };

    TMH_db->event_notify (TMH_db->cls,
                          &es,
                          NULL,
                          0);
  }
  /* Finally, also update our running process */
  GNUNET_CONTAINER_DLL_insert (mi->wm_head,
                               mi->wm_tail,
                               wm);
  /* Note: we may not need to do this, as we notified
     about the account change above. But also hardly hurts. */
  TMH_reload_instances (mi->settings.id);
  return TALER_MHD_reply_static (connection,
                                 MHD_HTTP_NO_CONTENT,
                                 NULL,
                                 NULL,
                                 0);
}


/* end of taler-merchant-httpd_private-post-account.c */