Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2021 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Lesser General Public License as published by the Free Software
7 : Foundation; either version 2.1, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 :
13 : You should have received a copy of the GNU Lesser General Public License along with
14 : TALER; see the file COPYING.LGPL. If not, see
15 : <http://www.gnu.org/licenses/>
16 : */
17 : /**
18 : * @file merchant_api_post_instance_auth.c
19 : * @brief Implementation of the POST /instance/$ID/private/auth request
20 : * @author Christian Grothoff
21 : */
22 : #include "platform.h"
23 : #include <curl/curl.h>
24 : #include <jansson.h>
25 : #include <microhttpd.h> /* just for HTTP status codes */
26 : #include <gnunet/gnunet_util_lib.h>
27 : #include <gnunet/gnunet_curl_lib.h>
28 : #include "taler_merchant_service.h"
29 : #include "merchant_api_curl_defaults.h"
30 : #include <taler/taler_json_lib.h>
31 : #include <taler/taler_signatures.h>
32 : #include <taler/taler_curl_lib.h>
33 :
34 :
35 : /**
36 : * Handle for a POST /instances/$ID/private/auth operation.
37 : */
38 : struct TALER_MERCHANT_InstanceAuthPostHandle
39 : {
40 : /**
41 : * The url for this request.
42 : */
43 : char *url;
44 :
45 : /**
46 : * Handle for the request.
47 : */
48 : struct GNUNET_CURL_Job *job;
49 :
50 : /**
51 : * Function to call with the result.
52 : */
53 : TALER_MERCHANT_InstanceAuthPostCallback cb;
54 :
55 : /**
56 : * Closure for @a cb.
57 : */
58 : void *cb_cls;
59 :
60 : /**
61 : * Reference to the execution context.
62 : */
63 : struct GNUNET_CURL_Context *ctx;
64 :
65 : /**
66 : * Minor context that holds body and headers.
67 : */
68 : struct TALER_CURL_PostContext post_ctx;
69 :
70 : };
71 :
72 :
73 : /**
74 : * Function called when we're done processing the
75 : * HTTP GET /instances/$ID/private/auth request.
76 : *
77 : * @param cls the `struct TALER_MERCHANT_InstanceAuthPostHandle`
78 : * @param response_code HTTP response code, 0 on error
79 : * @param response response body, NULL if not in JSON
80 : */
81 : static void
82 0 : handle_post_instance_auth_finished (void *cls,
83 : long response_code,
84 : const void *response)
85 : {
86 0 : struct TALER_MERCHANT_InstanceAuthPostHandle *iaph = cls;
87 0 : const json_t *json = response;
88 0 : struct TALER_MERCHANT_HttpResponse hr = {
89 0 : .http_status = (unsigned int) response_code,
90 : .reply = json
91 : };
92 :
93 0 : iaph->job = NULL;
94 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 : "Got /instances/$ID response with status code %u\n",
96 : (unsigned int) response_code);
97 0 : switch (response_code)
98 : {
99 0 : case MHD_HTTP_NO_CONTENT:
100 0 : break;
101 0 : case MHD_HTTP_BAD_REQUEST:
102 : /* happens if the auth token is malformed */
103 0 : hr.ec = TALER_JSON_get_error_code (json);
104 0 : hr.hint = TALER_JSON_get_error_hint (json);
105 : /* Nothing really to verify, merchant says we need to authenticate. */
106 0 : break;
107 0 : case MHD_HTTP_UNAUTHORIZED:
108 0 : hr.ec = TALER_JSON_get_error_code (json);
109 0 : hr.hint = TALER_JSON_get_error_hint (json);
110 : /* Nothing really to verify, merchant says we need to authenticate. */
111 0 : break;
112 0 : default:
113 : /* unexpected response code */
114 0 : hr.ec = TALER_JSON_get_error_code (json);
115 0 : hr.hint = TALER_JSON_get_error_hint (json);
116 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 : "Unexpected response code %u/%d\n",
118 : (unsigned int) response_code,
119 : (int) hr.ec);
120 0 : break;
121 : }
122 0 : iaph->cb (iaph->cb_cls,
123 : &hr);
124 0 : TALER_MERCHANT_instance_auth_post_cancel (iaph);
125 0 : }
126 :
127 :
128 : struct TALER_MERCHANT_InstanceAuthPostHandle *
129 0 : TALER_MERCHANT_instance_auth_post (
130 : struct GNUNET_CURL_Context *ctx,
131 : const char *backend_url,
132 : const char *instance_id,
133 : const char *auth_token,
134 : TALER_MERCHANT_InstanceAuthPostCallback cb,
135 : void *cb_cls)
136 : {
137 : struct TALER_MERCHANT_InstanceAuthPostHandle *iaph;
138 : json_t *req_obj;
139 :
140 0 : iaph = GNUNET_new (struct TALER_MERCHANT_InstanceAuthPostHandle);
141 0 : iaph->ctx = ctx;
142 0 : iaph->cb = cb;
143 0 : iaph->cb_cls = cb_cls;
144 0 : if (NULL != instance_id)
145 : {
146 : char *path;
147 :
148 0 : GNUNET_asprintf (&path,
149 : "management/instances/%s/auth",
150 : instance_id);
151 0 : iaph->url = TALER_url_join (backend_url,
152 : path,
153 : NULL);
154 0 : GNUNET_free (path);
155 : }
156 : else
157 : {
158 : /* backend_url is already identifying the instance */
159 0 : iaph->url = TALER_url_join (backend_url,
160 : "private/auth",
161 : NULL);
162 : }
163 0 : if (NULL == iaph->url)
164 : {
165 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 : "Could not construct request URL.\n");
167 0 : GNUNET_free (iaph);
168 0 : return NULL;
169 : }
170 0 : if (NULL == auth_token)
171 : {
172 0 : req_obj = GNUNET_JSON_PACK (
173 : GNUNET_JSON_pack_string ("method",
174 : "external"));
175 : }
176 : else
177 : {
178 0 : if (0 != strncasecmp (RFC_8959_PREFIX,
179 : auth_token,
180 : strlen (RFC_8959_PREFIX)))
181 : {
182 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 : "Authentication token must start with `%s'\n",
184 : RFC_8959_PREFIX);
185 0 : GNUNET_free (iaph->url);
186 0 : GNUNET_free (iaph);
187 0 : return NULL;
188 : }
189 0 : req_obj = GNUNET_JSON_PACK (
190 : GNUNET_JSON_pack_string ("method",
191 : "token"),
192 : GNUNET_JSON_pack_string ("token",
193 : auth_token));
194 : }
195 0 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 : "Requesting URL '%s'\n",
197 : iaph->url);
198 : {
199 : CURL *eh;
200 :
201 0 : eh = TALER_MERCHANT_curl_easy_get_ (iaph->url);
202 0 : if (GNUNET_OK !=
203 0 : TALER_curl_easy_post (&iaph->post_ctx,
204 : eh,
205 : req_obj))
206 : {
207 0 : GNUNET_break (0);
208 0 : curl_easy_cleanup (eh);
209 0 : json_decref (req_obj);
210 0 : GNUNET_free (iaph->url);
211 0 : GNUNET_free (iaph);
212 0 : return NULL;
213 : }
214 0 : json_decref (req_obj);
215 0 : GNUNET_assert (CURLE_OK ==
216 : curl_easy_setopt (eh,
217 : CURLOPT_CUSTOMREQUEST,
218 : MHD_HTTP_METHOD_POST));
219 0 : iaph->job = GNUNET_CURL_job_add2 (ctx,
220 : eh,
221 0 : iaph->post_ctx.headers,
222 : &handle_post_instance_auth_finished,
223 : iaph);
224 : }
225 0 : return iaph;
226 : }
227 :
228 :
229 : void
230 0 : TALER_MERCHANT_instance_auth_post_cancel (
231 : struct TALER_MERCHANT_InstanceAuthPostHandle *iaph)
232 : {
233 0 : if (NULL != iaph->job)
234 0 : GNUNET_CURL_job_cancel (iaph->job);
235 0 : TALER_curl_easy_post_finished (&iaph->post_ctx);
236 0 : GNUNET_free (iaph->url);
237 0 : GNUNET_free (iaph);
238 0 : }
|