From c218f60ab105f5c030d048c99e2c683506a09c6e Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 28 Feb 2018 11:05:12 +0100 Subject: [PATCH] Add API method for email based account registration --- .../matrix-glib-sdk-sections.txt | 1 + src/matrix-api.c | 23 ++++++++++ src/matrix-api.h | 16 +++++++ src/matrix-http-api.c | 43 +++++++++++++++++++ src/matrix-types.c | 1 + src/matrix-types.h | 1 + 6 files changed, 85 insertions(+) diff --git a/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt b/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt index c5a0ebc..30dbdf9 100644 --- a/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt +++ b/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt @@ -15,6 +15,7 @@ matrix_api_set_avatar_url matrix_api_get_display_name matrix_api_set_display_name matrix_api_register_account +matrix_api_register_account_email matrix_api_set_account_data matrix_api_get_room_tags matrix_api_delete_room_tag diff --git a/src/matrix-api.c b/src/matrix-api.c index 12dd0df..83a5ebc 100644 --- a/src/matrix-api.c +++ b/src/matrix-api.c @@ -45,6 +45,7 @@ * @get_display_name: the virtual function pointer to matrix_api_get_display_name() * @set_display_name: the virtual function pointer to matrix_api_set_display_name() * @register_account: the virtual function pointer to matrix_api_register_account() + * @register_account_email: the virtual function pointer to matrix_api_register_account_email() * @set_account_data: the virtual function pointer to matrix_api_set_account_data() * @get_room_tags: the virtual function pointer to matrix_api_get_room_tags() * @delete_room_tag: the virtual function pointer to matrix_api_delete_room_tag() @@ -404,6 +405,28 @@ matrix_api_register_account(MatrixAPI *matrix_api, MATRIX_API_GET_IFACE(matrix_api)->register_account(matrix_api, callback, user_data, account_kind, bind_email, username, password, error); } +/** + * matrix_api_register_account_email: + * @callback: (scope async): the function to call when the request is finished + */ +void +matrix_api_register_account_email(MatrixAPI *api, + const gchar *id_server, + const gchar *client_secret, + const gchar *email, + guint send_attempt, + MatrixAPICallback callback, + gpointer user_data, + GError **error) +{ + g_return_if_fail(api != NULL); + g_return_if_fail(id_server); + g_return_if_fail(client_secret); + g_return_if_fail(email); + + MATRIX_API_GET_IFACE(api)->register_account_email(api, callback, user_data, id_server, client_secret, email, send_attempt, error); +} + /** * matrix_api_set_account_data: * @api: a #MatrixAPI diff --git a/src/matrix-api.h b/src/matrix-api.h index c71f018..695c9fd 100644 --- a/src/matrix-api.h +++ b/src/matrix-api.h @@ -105,6 +105,14 @@ struct _MatrixAPIInterface { const gchar *username, const gchar *password, GError **error); + void (*register_account_email)(MatrixAPI *api, + MatrixAPICallback callback, + gpointer user_data, + const gchar *id_server, + const gchar *client_secret, + const gchar *email, + guint send_attempt, + GError **error); void (*set_account_data)(MatrixAPI *api, MatrixAPICallback callback, gpointer user_data, @@ -526,6 +534,14 @@ void matrix_api_register_account(MatrixAPI *api, MatrixAPICallback callback, gpointer user_data, GError **error); +void matrix_api_register_account_email(MatrixAPI *api, + const gchar *id_server, + const gchar *client_secret, + const gchar *email, + guint send_attempt, + MatrixAPICallback callback, + gpointer user_data, + GError **error); void matrix_api_set_account_data(MatrixAPI *api, const gchar *user_id, const gchar *room_id, diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index ade45a0..3a57de2 100644 --- a/src/matrix-http-api.c +++ b/src/matrix-http-api.c @@ -2006,6 +2006,48 @@ matrix_http_api_register_account(MatrixAPI *matrix_api, MatrixAPICallback cb, vo g_hash_table_unref(parms); } +static void +register_account_email(MatrixAPI *api, + MatrixAPICallback callback, + gpointer user_data, + const gchar *id_server, + const gchar *client_secret, + const gchar *email, + guint send_attempt, + GError **error) +{ + JsonBuilder *builder; + JsonNode *root_node; + + builder = json_builder_new(); + + json_builder_begin_object(builder); + + json_builder_set_member_name(builder, "id_server"); + json_builder_add_string_value(builder, id_server); + + json_builder_set_member_name(builder, "client_secret"); + json_builder_add_string_value(builder, client_secret); + + json_builder_set_member_name(builder, "email"); + json_builder_add_string_value(builder, email); + + json_builder_set_member_name(builder, "send_attempt"); + json_builder_add_int_value(builder, send_attempt); + + json_builder_end_object(builder); + + root_node = json_builder_get_root(builder); + + _matrix_http_api_send(MATRIX_HTTP_API(api), + callback, user_data, + CALL_TYPE_API, "POST", "register/email/requestToken", + NULL, NULL, root_node, NULL, FALSE, error); + + json_node_unref(root_node); + g_object_unref(builder); +} + static void matrix_http_api_set_account_data(MatrixAPI *matrix_api, MatrixAPICallback cb, void *cb_target, const gchar *user_id, const gchar *room_id, const gchar *event_type, JsonNode *content, GError **error) { @@ -2522,6 +2564,7 @@ matrix_http_api_matrix_api_interface_init(MatrixAPIInterface * iface) iface->get_display_name = matrix_http_api_get_display_name; iface->set_display_name = matrix_http_api_set_display_name; iface->register_account = matrix_http_api_register_account; + iface->register_account_email = register_account_email; iface->set_account_data = matrix_http_api_set_account_data; iface->get_room_tags = matrix_http_api_get_room_tags; iface->delete_room_tag = matrix_http_api_delete_room_tag; diff --git a/src/matrix-types.c b/src/matrix-types.c index 9f40561..c785fbe 100644 --- a/src/matrix-types.c +++ b/src/matrix-types.c @@ -70,6 +70,7 @@ * @MATRIX_ERROR_M_THREEPID_AUTH_FAILED: 3rd party authentication failed * @MATRIX_ERROR_M_THREEPID_IN_USE: the provided 3rd party ID is already in use * @MATRIX_ERROR_M_INVALID_USERNAME: the given username is invalid + * @MATRIX_ERROR_M_THREEPID_NOT_FOUND: the provided 3rd party ID is not found * @MATRIX_ERROR_UNSPECIFIED: no error code was sent by the homeserver. If you see this error, * that usually indicates a homeserver bug * @MATRIX_ERROR_UNKNOWN_ERROR: an error unknown to this library diff --git a/src/matrix-types.h b/src/matrix-types.h index 151bebc..6f8cc16 100644 --- a/src/matrix-types.h +++ b/src/matrix-types.h @@ -64,6 +64,7 @@ typedef enum { MATRIX_ERROR_M_THREEPID_AUTH_FAILED, MATRIX_ERROR_M_THREEPID_IN_USE, MATRIX_ERROR_M_INVALID_USERNAME, + MATRIX_ERROR_M_THREEPID_NOT_FOUND, /* Allow for a lot of Matrix.org defined codes. Do not define * Matrix-specific error codes after this! */