From b0bb97c80418aae25359e8b011b9cc6fa461e3d7 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 27 Feb 2018 16:16:48 +0100 Subject: [PATCH] Remove the refresh_token It was removed from the spec, and from HS implementations for a while. --- README.md | 1 - .../matrix-glib-sdk-sections.txt | 3 - src/matrix-api.c | 74 --------------- src/matrix-api.h | 14 --- src/matrix-http-api.c | 95 +------------------ src/matrix-http-api.h | 2 +- src/matrix-http-client.c | 31 ------ src/test-api-client.c | 5 +- vapi/c-api.vapi | 10 -- 9 files changed, 4 insertions(+), 231 deletions(-) diff --git a/README.md b/README.md index b51e8a8..89b88eb 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,6 @@ For a working example, see [test-client.c](src/test-client.c). // Set tokens for the session. Alternatively you may want to login with matrix_api_login() or matrix_client_login_with_password() matrix_api_set_token(MATRIX_API(client), "your_access_token"); - matrix_api_set_refresh_token(MATRIX_API(client), "your_refresh_token"); // Connect a callback that gets called when a m.room.message event arrives matrix_client_connect_event(client, MATRIX_EVENT_TYPE_ROOM_MESSAGE, message_callback, NULL); 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 aadd3c1..1944908 100644 --- a/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt +++ b/docs/reference/matrix-glib-sdk/matrix-glib-sdk-sections.txt @@ -51,7 +51,6 @@ matrix_api_leave_room matrix_api_unban_user matrix_api_login matrix_api_logout -matrix_api_token_refresh matrix_api_get_presence_list matrix_api_update_presence_list matrix_api_get_presence @@ -71,8 +70,6 @@ matrix_api_media_thumbnail matrix_api_media_upload matrix_api_get_token matrix_api_set_token -matrix_api_get_refresh_token -matrix_api_set_refresh_token matrix_api_get_user_id matrix_api_get_homeserver diff --git a/src/matrix-api.c b/src/matrix-api.c index 54aa13c..159dd54 100644 --- a/src/matrix-api.c +++ b/src/matrix-api.c @@ -81,7 +81,6 @@ * @unban_user: the virtual function pointer to matrix_api_unban_user() * @login: the virtual function pointer to matrix_api_login() * @logout: the virtual function pointer to matrix_api_logout() - * @token_refresh: the virtual function pointer to matrix_api_get_token_refresh() * @get_presence_list: the virtual function pointer to matrix_api_get_presence_list() * @update_presence_list: the virtual function pointer to matrix_api_update_presence_list() * @get_presence: the virtual function pointer to matrix_api_get_presence() @@ -101,8 +100,6 @@ * @media_upload: the virtual function pointer to matrix_api_media_upload() * @get_token: the virtual function pointer to matrix_api_get_token() * @set_token: the virtual function pointer to matrix_api_set_token() - * @get_refresh_token: the virtual function pointer to matrix_api_get_refresh_token() - * @set_refresh_token: the virtual function pointer to matrix_api_set_refresh_token() * @get_user_id: the virtual function pointer to matrix_api_get_user_id() * @get_homeserver: the virtual function pointer to matrix_api_get_homeserver() * @@ -1433,31 +1430,6 @@ matrix_api_logout(MatrixAPI *matrix_api, MATRIX_API_GET_IFACE(matrix_api)->logout(matrix_api, callback, user_data, error); } -/** - * matrix_api_token_refresh: - * @api: a #MatrixAPI - * @refresh_token: the refresh token that was issued by the server - * @callback: the function to call when the request is finished - * @user_data: user data to be passed to @callback - * @error: (nullable): a #GError, or %NULL to ignore errors - * - * Exchanges a refresh token for a new access token. This is intended to be used if the access - * token has expired. If @refresh_token is %NULL, implementations MUST send the stored refresh - * token. If it is not pesent(e.g. because login hasn’t happened yet), this function MUST - * return with error. - */ -void -matrix_api_token_refresh(MatrixAPI *matrix_api, - const gchar *refresh_token, - MatrixAPICallback callback, - gpointer user_data, - GError **error) -{ - g_return_if_fail(matrix_api != NULL); - - MATRIX_API_GET_IFACE(matrix_api)->token_refresh(matrix_api, callback, user_data, refresh_token, error); -} - /** * matrix_api_get_presence_list: * @api: a #MatrixAPI @@ -1945,39 +1917,6 @@ matrix_api_set_token(MatrixAPI *matrix_api, const gchar *value) MATRIX_API_GET_IFACE(matrix_api)->set_token(matrix_api, value); } -/** - * matrix_api_get_refresh_token: - * @api: a #MatrixAPI - * - * Get the refresh token used in @api. - * - * The returned value is owned by @api and should not be freed. - * - * Returns: (transfer none) (nullable): a refresh token, or %NULL if not yet set - */ -const gchar * -matrix_api_get_refresh_token(MatrixAPI *matrix_api) -{ - g_return_val_if_fail(matrix_api != NULL, NULL); - - return MATRIX_API_GET_IFACE(matrix_api)->get_refresh_token(matrix_api); -} - -/** - * matrix_api_set_refresh_token: - * @api: a #MatrixAPI - * @refresh_token: a refresh token - * - * Set the refresh token to be used for access token refresh while using @api. - */ -void -matrix_api_set_refresh_token(MatrixAPI *matrix_api, const gchar *value) -{ - g_return_if_fail(matrix_api != NULL); - - MATRIX_API_GET_IFACE(matrix_api)->set_refresh_token(matrix_api, value); -} - /** * matrix_api_get_user_id: * @api: a #MatrixAPI @@ -2034,19 +1973,6 @@ matrix_api_default_init(MatrixAPIInterface *iface) NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); - /** - * MatrixAPI:refresh-token: - * - * The token to use for refreshing the authorization token. It is - * issued by the server after a successful registration, login or - * token refresh. - */ - g_object_interface_install_property(iface, - g_param_spec_string( - "refresh-token", "refresh-token", "refresh-token", - NULL, - G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); - /** * MatrixAPI:user-id: * diff --git a/src/matrix-api.h b/src/matrix-api.h index 39b9bf3..6e783d8 100644 --- a/src/matrix-api.h +++ b/src/matrix-api.h @@ -344,11 +344,6 @@ struct _MatrixAPIInterface { MatrixAPICallback callback, gpointer user_data, GError **error); - void (*token_refresh)(MatrixAPI *api, - MatrixAPICallback callback, - gpointer user_data, - const gchar *refresh_token, - GError **error); void (*get_presence_list)(MatrixAPI *api, MatrixAPICallback callback, gpointer user_data, @@ -460,8 +455,6 @@ struct _MatrixAPIInterface { GError **error); const gchar *(*get_token)(MatrixAPI *api); void (*set_token)(MatrixAPI *api, const gchar *token); - const gchar *(*get_refresh_token)(MatrixAPI *api); - void (*set_refresh_token)(MatrixAPI *api, const gchar *value); const gchar *(*get_user_id)(MatrixAPI *api); const gchar *(*get_homeserver)(MatrixAPI *api); }; @@ -770,11 +763,6 @@ void matrix_api_logout(MatrixAPI *api, MatrixAPICallback callback, gpointer user_data, GError **error); -void matrix_api_token_refresh(MatrixAPI *api, - const gchar *refresh_token, - MatrixAPICallback callback, - gpointer user_data, - GError **error); void matrix_api_get_presence_list(MatrixAPI *api, const gchar *user_id, MatrixAPICallback callback, @@ -886,8 +874,6 @@ void matrix_api_media_upload(MatrixAPI *api, GError **error); const gchar *matrix_api_get_token(MatrixAPI *api); void matrix_api_set_token(MatrixAPI *api, const gchar *token); -const gchar *matrix_api_get_refresh_token(MatrixAPI *api); -void matrix_api_set_refresh_token(MatrixAPI *api, const gchar *refresh_token); const gchar *matrix_api_get_user_id(MatrixAPI *api); const gchar *matrix_api_get_homeserver(MatrixAPI *api); diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index f0a4719..b204902 100644 --- a/src/matrix-http-api.c +++ b/src/matrix-http-api.c @@ -39,7 +39,6 @@ enum { PROP_VALIDATE_CERTIFICATE, PROP_USER_ID, PROP_TOKEN, - PROP_REFRESH_TOKEN, PROP_HOMESERVER, NUM_PROPERTIES }; @@ -52,7 +51,6 @@ typedef struct { SoupURI *api_uri; SoupURI *media_uri; gchar *token; - gchar *refresh_token; } MatrixHTTPAPIPrivate; static void matrix_http_api_matrix_api_interface_init(MatrixAPIInterface * iface); @@ -102,14 +100,13 @@ _matrix_http_api_set_url(MatrixHTTPAPI *matrix_http_api, SoupURI **uri, const gc * matrix_http_api_new: * @base_url: the base URL of the homeserver to use * @token: an access token to use - * @refresh_token: a refresh token to use * * Create a new #MatrixHTTPAPI object. * * Returns: (transfer full): a new #MatrixHTTPAPI object */ MatrixHTTPAPI * -matrix_http_api_new(const gchar *base_url, const gchar *token, const gchar *refresh_token) +matrix_http_api_new(const gchar *base_url, const gchar *token) { MatrixHTTPAPI *ret; MatrixHTTPAPIPrivate *priv; @@ -119,7 +116,6 @@ matrix_http_api_new(const gchar *base_url, const gchar *token, const gchar *refr ret = (MatrixHTTPAPI*) g_object_new(MATRIX_TYPE_HTTP_API, "base-url", base_url, "token", token, - "refresh-token", refresh_token, NULL); priv = matrix_http_api_get_instance_private(ret); @@ -212,21 +208,6 @@ _matrix_http_api_response_callback(SoupSession *session, SoupMessage *msg, gpoin } } - /* Check if the response holds a refresh token; if it - * does, set it as our new refresh token */ - if ((node = json_object_get_member(root, "refresh_token")) != NULL) { - const gchar *refresh_token; - - if ((refresh_token = json_node_get_string(node)) != NULL) { -#if DEBUG - g_debug("Got new refresh token: %s", refresh_token); -#endif - - g_free(priv->refresh_token); - priv->refresh_token = g_strdup(refresh_token); - } - } - /* Check if the response holds a homeserver name */ if ((node = json_object_get_member(root, "home_server")) != NULL) { const gchar *homeserver = json_node_get_string(node); @@ -1764,38 +1745,6 @@ matrix_http_api_login(MatrixAPI *matrix_api, MatrixAPICallback cb, void *cb_targ json_node_unref(body); } -static void -matrix_http_api_token_refresh(MatrixAPI *matrix_api, MatrixAPICallback cb, void *cb_target, const gchar *refresh_token, GError **error) -{ - JsonBuilder *builder; - MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(MATRIX_HTTP_API(matrix_api)); - JsonNode *root_node; - - if ((refresh_token == NULL) && (priv->refresh_token == NULL)) { - g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_M_MISSING_TOKEN, "No token available"); - - return; - } - - builder = json_builder_new(); - - json_builder_begin_object(builder); - - json_builder_set_member_name(builder, "refresh_token"); - json_builder_add_string_value(builder, (refresh_token != NULL) ? refresh_token : priv->refresh_token); - - json_builder_end_object(builder); - root_node = json_builder_get_root(builder); - - _matrix_http_api_send(MATRIX_HTTP_API(matrix_api), - cb, cb_target, - CALL_TYPE_API, "POST", "tokenrefresh", - NULL, NULL, root_node, NULL, FALSE, error); - - json_node_unref(root_node); - g_object_unref(builder); -} - static void matrix_http_api_logout(MatrixAPI *matrix_api, MatrixAPICallback cb, void *cb_target, GError **error) { @@ -2282,11 +2231,9 @@ matrix_http_api_set_base_url(MatrixHTTPAPI *matrix_http_api, const gchar *base_u priv->base_url = g_strdup(base_url); g_free(priv->token); - g_free(priv->refresh_token); g_free(matrix_http_api->_homeserver); g_free(matrix_http_api->_user_id); priv->token = NULL; - priv->refresh_token = NULL; matrix_http_api->_homeserver = NULL; matrix_http_api->_user_id = NULL; @@ -2364,27 +2311,6 @@ matrix_http_api_set_token(MatrixAPI *matrix_api, const gchar *token) } } -static const gchar * -matrix_http_api_get_refresh_token (MatrixAPI *matrix_api) -{ - MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(MATRIX_HTTP_API(matrix_api)); - - return priv->refresh_token; -} - -static void -matrix_http_api_set_refresh_token(MatrixAPI *matrix_api, const gchar *refresh_token) -{ - MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(MATRIX_HTTP_API(matrix_api)); - - if (g_strcmp0(refresh_token, priv->refresh_token) != 0) { - g_free(priv->refresh_token); - priv->refresh_token = g_strdup(refresh_token); - - g_object_notify_by_pspec((GObject *)matrix_api, matrix_http_api_properties[PROP_REFRESH_TOKEN]); - } -} - static const gchar * matrix_http_api_get_homeserver(MatrixAPI *api) { return MATRIX_HTTP_API(api)->_homeserver; @@ -2409,7 +2335,6 @@ matrix_http_api_finalize(GObject *gobject) g_free(matrix_http_api->_user_id); g_free(priv->token); - g_free(priv->refresh_token); g_free(matrix_http_api->_homeserver); G_OBJECT_CLASS(matrix_http_api_parent_class)->finalize(gobject); @@ -2436,10 +2361,6 @@ matrix_http_api_get_property(GObject *gobject, guint property_id, GValue *value, case PROP_TOKEN: g_value_set_string(value, matrix_api_get_token((MatrixAPI*) matrix_http_api)); - break; - case PROP_REFRESH_TOKEN: - g_value_set_string(value, matrix_api_get_refresh_token((MatrixAPI*) matrix_http_api)); - break; case PROP_HOMESERVER: g_value_set_string(value, matrix_api_get_homeserver((MatrixAPI*) matrix_http_api)); @@ -2469,10 +2390,6 @@ matrix_http_api_set_property(GObject *gobject, guint property_id, const GValue * case PROP_TOKEN: matrix_api_set_token((MatrixAPI*) matrix_http_api, g_value_get_string(value)); - break; - case PROP_REFRESH_TOKEN: - matrix_api_set_refresh_token((MatrixAPI*) matrix_http_api, g_value_get_string(value)); - break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); @@ -2523,12 +2440,6 @@ matrix_http_api_class_init(MatrixHTTPAPIClass *klass) G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_TOKEN, matrix_http_api_properties[PROP_TOKEN]); - matrix_http_api_properties[PROP_REFRESH_TOKEN] = g_param_spec_string( - "refresh-token", "refresh-token", "refresh-token", - NULL, - G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); - g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_REFRESH_TOKEN, matrix_http_api_properties[PROP_REFRESH_TOKEN]); - matrix_http_api_properties[PROP_HOMESERVER] = g_param_spec_string( "homeserver", "homeserver", "homeserver", NULL, @@ -2587,7 +2498,6 @@ matrix_http_api_matrix_api_interface_init(MatrixAPIInterface * iface) iface->whois = matrix_http_api_whois; iface->versions = matrix_http_api_versions; iface->login = matrix_http_api_login; - iface->token_refresh = matrix_http_api_token_refresh; iface->logout = matrix_http_api_logout; iface->get_3pids = matrix_http_api_get_3pids; iface->add_3pid = matrix_http_api_add_3pid; @@ -2608,8 +2518,6 @@ matrix_http_api_matrix_api_interface_init(MatrixAPIInterface * iface) iface->get_user_id = matrix_http_api_get_user_id; iface->get_token = matrix_http_api_get_token; iface->set_token = matrix_http_api_set_token; - iface->get_refresh_token = matrix_http_api_get_refresh_token; - iface->set_refresh_token = matrix_http_api_set_refresh_token; iface->get_homeserver = matrix_http_api_get_homeserver; } @@ -2623,5 +2531,4 @@ matrix_http_api_init(MatrixHTTPAPI *matrix_http_api) priv->api_uri = NULL; priv->media_uri = NULL; priv->token = NULL; - priv->refresh_token = NULL; } diff --git a/src/matrix-http-api.h b/src/matrix-http-api.h index 9d1698e..e79c3dc 100644 --- a/src/matrix-http-api.h +++ b/src/matrix-http-api.h @@ -47,7 +47,7 @@ struct _MatrixHTTPAPIClass { }; GType matrix_http_api_get_type(void) G_GNUC_CONST; -MatrixHTTPAPI *matrix_http_api_new(const gchar *base_url, const gchar *token, const gchar *refresh_token); +MatrixHTTPAPI *matrix_http_api_new(const gchar *base_url, const gchar *token); const gchar *matrix_http_api_get_base_url(MatrixHTTPAPI *http_api); void matrix_http_api_set_base_url(MatrixHTTPAPI *http_api, const gchar *base_url); gboolean matrix_http_api_get_validate_certificate(MatrixHTTPAPI *http_api); diff --git a/src/matrix-http-client.c b/src/matrix-http-client.c index 18a9fd7..b1f76ab 100644 --- a/src/matrix-http-client.c +++ b/src/matrix-http-client.c @@ -112,7 +112,6 @@ logout_callback(MatrixAPI *matrix_api, const gchar *content_type, JsonNode *json { matrix_api_abort_pending(matrix_api); matrix_api_set_token(matrix_api, NULL); - matrix_api_set_refresh_token(matrix_api, NULL); } static void @@ -323,19 +322,6 @@ _process_event_list_obj(MatrixHTTPClient *matrix_http_client, JsonNode* node, co } } -static void -refresh_callback(MatrixAPI *matrix_api, const gchar *content_type, JsonNode *json_content, GByteArray *raw_content, GError *err, gpointer user_data) -{ - g_signal_emit_by_name(MATRIX_CLIENT(matrix_api), "login-finished", g_error_matches(err, MATRIX_ERROR, MATRIX_ERROR_NONE)); - - if (matrix_api_get_token(matrix_api) == NULL) { - matrix_api_set_refresh_token(matrix_api, NULL); - g_signal_emit_by_name(MATRIX_CLIENT(matrix_api), "polling-stopped", err); - - matrix_client_stop_polling(MATRIX_CLIENT(matrix_api), FALSE, NULL); - } -} - static void cb_sync(MatrixAPI *matrix_api, const gchar *content_type, JsonNode *json_content, GByteArray *raw_content, GError *error, gpointer user_data) { @@ -442,10 +428,6 @@ cb_sync(MatrixAPI *matrix_api, const gchar *content_type, JsonNode *json_content (error->code == MATRIX_ERROR_M_UNKNOWN_TOKEN) || (error->code == MATRIX_ERROR_M_UNAUTHORIZED))) { matrix_api_set_token(matrix_api, NULL); - matrix_api_token_refresh(matrix_api, - NULL, - refresh_callback, NULL, - NULL); } // It is possible that polling has been disabled while we were processing events. Don’t @@ -722,7 +704,6 @@ matrix_http_client_real_save_state(MatrixClient *matrix_client, const gchar *fil const gchar *user_id; const gchar *homeserver; const gchar *token; - const gchar *refresh_token; JsonNode *node; JsonGenerator *generator; @@ -746,10 +727,6 @@ matrix_http_client_real_save_state(MatrixClient *matrix_client, const gchar *fil json_object_set_string_member(root, "access_token", token); } - if ((refresh_token = matrix_api_get_refresh_token(MATRIX_API(matrix_client))) != NULL) { - json_object_set_string_member(root, "refresh_token", refresh_token); - } - node = json_node_new(JSON_NODE_OBJECT); json_node_set_object(node, root); @@ -850,14 +827,6 @@ matrix_http_client_real_load_state(MatrixClient *matrix_client, const gchar *fil #endif } - if ((node = json_object_get_member(root, "refresh_token")) != NULL) { - matrix_api_set_refresh_token(MATRIX_API(matrix_client), json_node_get_string(node)); - -#if DEBUG - g_debug("Loaded refresh token %s", matrix_api_get_refresh_token(MATRIX_API(matrix_client))); -#endif - } - json_node_unref(root_node); } diff --git a/src/test-api-client.c b/src/test-api-client.c index 8ab4600..e7e95d3 100644 --- a/src/test-api-client.c +++ b/src/test-api-client.c @@ -189,9 +189,8 @@ main(int argc, char *argv[]) g_info("Starting up: %s with %s:%s", *homeserver, user, password); - api = MATRIX_API(matrix_http_api_new(*homeserver, NULL, NULL)); - matrix_http_api_set_validate_certificate(MATRIX_HTTP_API(api), - !no_validate_certs); + api = MATRIX_API(matrix_http_api_new(*homeserver, NULL)); + matrix_http_api_set_validate_certificate(MATRIX_HTTP_API(api), !no_validate_certs); builder = json_builder_new(); json_builder_begin_object(builder); json_builder_set_member_name(builder, "user"); diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index 6464417..d5e4aa3 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -489,7 +489,6 @@ namespace Matrix { [CCode (cheader_filename = "matrix-api.h", type_cname = "MatrixAPIInterface")] public interface API : GLib.Object { public abstract string? token { get; set; default = null; } - public abstract string? refresh_token { get; set; default = null; } public abstract string? user_id { get; default = null; } public abstract string? homeserver { get; default = null; } @@ -649,9 +648,6 @@ namespace Matrix { public abstract void logout([CCode (delegate_target_pos = 1.5, scope = "async")] owned Matrix.API.Callback? @callback) throws Matrix.Error; - public abstract void token_refresh([CCode (delegate_target_pos = 1.5, scope = "async")] owned Matrix.API.Callback? @callback, string? refresh_token) - throws Matrix.Error; - public abstract void get_presence_list([CCode (delegate_target_pos = 1.5, scope = "async")] owned Matrix.API.Callback? @callback, string user_id) throws Matrix.Error; @@ -711,12 +707,9 @@ namespace Matrix { protected string? _user_id; public string? user_id { get; default = null; } public string? token { get; set; default = null; } - public string? refresh_token { get; set; default = null; } protected string? _homeserver; public string? homeserver { get; default = null; } - protected HTTPAPI(string base_url, string? token = null, string? refresh_token = null); - /* Media */ public void media_download(API.Callback? cb, string server_name, string media_id) @@ -888,9 +881,6 @@ namespace Matrix { public void login(API.Callback? cb, string login_type, Json.Node? content) throws Matrix.Error; - public void token_refresh(API.Callback? cb, string? refresh_token) - throws Matrix.Error; - public void logout(API.Callback? cb) throws Matrix.Error;