From 8b332eaaeae50e0fdabe372247015e7523c41d2b Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 15 Jan 2016 19:28:07 +0100 Subject: [PATCH] Implement token_refresh --- src/matrix-api.c | 8 ++++++-- src/matrix-http-api.c | 44 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/matrix-api.c b/src/matrix-api.c index 2dabe92..6409bf4 100644 --- a/src/matrix-api.c +++ b/src/matrix-api.c @@ -1707,11 +1707,15 @@ matrix_api_login(MatrixAPI *api, * finished * @user_data: (closure): user data to pass to the callback function * @callback - * @refresh_token: the refresh token that was issued by the server + * @refresh_token: (allow-none): the refresh token that was issued by + * the server * @error: return location for a #GError, or %NULL * * Exchanges a refresh token for a new access token. This is intended - * to be used if the access token has expired. + * to be used if the access token has expired. If @refresh_token is + * %NULL, iplementations MUST send the stored refresh token. If it is + * not pesent (e.g. because login hasn’t happened yet), this function + * MUST yield an error. */ void matrix_api_token_refresh(MatrixAPI *api, diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index 73ab941..a27f2e2 100644 --- a/src/matrix-http-api.c +++ b/src/matrix-http-api.c @@ -2245,6 +2245,48 @@ i_whois(MatrixAPI *api, g_free(path); } +static void +i_token_refresh(MatrixAPI *api, + MatrixAPICallback callback, + gpointer user_data, + const gchar *refresh_token, + GError **error) +{ + MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private( + MATRIX_HTTP_API(api)); + JsonBuilder *builder; + JsonNode *body; + + if (!refresh_token && !priv->token) { + g_set_error(error, + MATRIX_API_ERROR, MATRIX_API_ERROR_MISSING_TOKEN, + "No token available"); + + return; + } + + builder = json_builder_new(); + json_builder_begin_object(builder); + + json_builder_set_member_name(builder, "refresh_token"); + + if (!refresh_token) { + json_builder_add_string_value(builder, priv->refresh_token); + } else { + json_builder_add_string_value(builder, refresh_token); + } + + json_builder_end_object(builder); + body = json_builder_get_root(builder); + g_object_unref(builder); + + _send(MATRIX_HTTP_API(api), + callback, user_data, + CALL_API, + "POST", "tokenreresh", NULL, NULL, body, NULL, + FALSE, error); +} + static void matrix_http_api_matrix_api_init(MatrixAPIInterface *iface) { @@ -2319,7 +2361,7 @@ matrix_http_api_matrix_api_init(MatrixAPIInterface *iface) /* Session management */ iface->login = i_login; - iface->token_refresh = NULL; + iface->token_refresh = i_token_refresh; /* User data */ iface->get_3pids = NULL;