summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_private-post-instances-ID-token.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/taler-merchant-httpd_private-post-instances-ID-token.c')
-rw-r--r--src/backend/taler-merchant-httpd_private-post-instances-ID-token.c59
1 files changed, 43 insertions, 16 deletions
diff --git a/src/backend/taler-merchant-httpd_private-post-instances-ID-token.c b/src/backend/taler-merchant-httpd_private-post-instances-ID-token.c
index e5128a56..74de6563 100644
--- a/src/backend/taler-merchant-httpd_private-post-instances-ID-token.c
+++ b/src/backend/taler-merchant-httpd_private-post-instances-ID-token.c
@@ -29,9 +29,9 @@
/**
- * Maximum duration for the validity of a token token.
+ * Default duration for the validity of a login token.
*/
-#define MAX_DURATION GNUNET_TIME_UNIT_DAYS
+#define DEFAULT_DURATION GNUNET_TIME_UNIT_DAYS
MHD_RESULT
@@ -42,9 +42,11 @@ TMH_private_post_instances_ID_token (const struct TMH_RequestHandler *rh,
struct TMH_MerchantInstance *mi = hc->instance;
json_t *jtoken = hc->request_body;
const char *scope;
+ uint32_t iscope = TMH_AS_NONE;
bool refreshable = false;
+ struct TALER_MERCHANTDB_LoginTokenP btoken;
struct GNUNET_TIME_Relative duration
- = MAX_DURATION;
+ = DEFAULT_DURATION;
struct GNUNET_TIME_Timestamp expiration_time;
struct GNUNET_JSON_Specification spec[] = {
GNUNET_JSON_spec_string ("scope",
@@ -59,9 +61,8 @@ TMH_private_post_instances_ID_token (const struct TMH_RequestHandler *rh,
NULL),
GNUNET_JSON_spec_end ()
};
- char *token;
- MHD_RESULT ret;
-
+ enum GNUNET_DB_QueryStatus qs;
+
{
enum GNUNET_GenericReturnValue res;
@@ -71,25 +72,51 @@ TMH_private_post_instances_ID_token (const struct TMH_RequestHandler *rh,
if (GNUNET_OK != res)
return (GNUNET_NO == res) ? MHD_YES : MHD_NO;
}
- duration = GNUNET_TIME_relative_min (duration,
- MAX_DURATION);
expiration_time = GNUNET_TIME_relative_to_timestamp (duration);
- token = GNUNET_strdup ("FIXME-foo");
- (void) mi;
-
- ret = TALER_MHD_REPLY_JSON_PACK (
+ if (0 == strcasecmp (scope,
+ "readonly"))
+ iscope = TMH_AS_READ_ONLY;
+ else if (0 == strcasecmp (scope,
+ "write"))
+ iscope = TMH_AS_ALL;
+ else
+ {
+ GNUNET_break_op (0);
+ return TALER_MHD_reply_with_ec (connection,
+ TALER_EC_GENERIC_PARAMETER_MALFORMED,
+ "scope");
+ }
+ if (refreshable)
+ iscope |= TMH_AS_REFRESHABLE;
+ qs = TMH_db->insert_login_token (TMH_db->cls,
+ mi->settings.id,
+ &btoken,
+ GNUNET_TIME_timestamp_get (),
+ expiration_time,
+ iscope);
+ switch (qs)
+ {
+ case GNUNET_DB_STATUS_HARD_ERROR:
+ case GNUNET_DB_STATUS_SOFT_ERROR:
+ case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
+ GNUNET_break (0);
+ return TALER_MHD_reply_with_ec (connection,
+ TALER_EC_GENERIC_DB_STORE_FAILED,
+ "insert_login_token");
+ case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
+ break;
+ }
+ return TALER_MHD_REPLY_JSON_PACK (
connection,
MHD_HTTP_OK,
- GNUNET_JSON_pack_string ("token",
- token),
+ GNUNET_JSON_pack_data_auto ("token",
+ &btoken),
GNUNET_JSON_pack_string ("scope",
scope),
GNUNET_JSON_pack_bool ("refreshable",
refreshable),
GNUNET_JSON_pack_timestamp ("expiration",
expiration_time));
- GNUNET_free (token);
- return ret;
}