Rework Matrix3PidCredential in Vala
This commit is contained in:
		@@ -118,12 +118,6 @@ namespace Matrix {
 | 
			
		||||
        public Json.Node? get_json_node();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    [CCode (cheader_filename = "matrix-types.h")]
 | 
			
		||||
    public class @3PidCredential {
 | 
			
		||||
        public Json.Node? get_json_node()
 | 
			
		||||
            throws Matrix.Error;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    [CCode (cheader_filename = "utils.h", cname = "_json_node_deep_copy")]
 | 
			
		||||
    public Json.Node?
 | 
			
		||||
    _json_node_deep_copy(Json.Node? node);
 | 
			
		||||
 
 | 
			
		||||
@@ -358,4 +358,58 @@ namespace Matrix {
 | 
			
		||||
            return builder.get_root();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Class to hold 3rd party credential related data.
 | 
			
		||||
     */
 | 
			
		||||
    public class @3PidCredential : JsonCompact {
 | 
			
		||||
        /**
 | 
			
		||||
         * The Identity Server used for this credential.
 | 
			
		||||
         */
 | 
			
		||||
        public string? id_server { get; set; default = null; }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * The session identifier got from the Identity Server.
 | 
			
		||||
         */
 | 
			
		||||
        public string? session_id { get; set; default = null; }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * The client secret that was used in the session with the
 | 
			
		||||
         * Identity Server.
 | 
			
		||||
         */
 | 
			
		||||
        public string? client_secret { get; set; default = null; }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Get 3rd party credential related data as a JSON node.
 | 
			
		||||
         */
 | 
			
		||||
        public override Json.Node?
 | 
			
		||||
        get_json_node()
 | 
			
		||||
            throws Matrix.Error
 | 
			
		||||
        {
 | 
			
		||||
            if ((id_server == null)
 | 
			
		||||
                || (session_id == null)
 | 
			
		||||
                || (client_secret == null))
 | 
			
		||||
            {
 | 
			
		||||
                throw new Matrix.Error.INCOMPLETE(
 | 
			
		||||
                        "All fields of a 3PID credential must be filled!");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var builder = new Json.Builder();
 | 
			
		||||
 | 
			
		||||
            builder.begin_object();
 | 
			
		||||
 | 
			
		||||
            builder.set_member_name("id_server");
 | 
			
		||||
            builder.add_string_value(id_server);
 | 
			
		||||
 | 
			
		||||
            builder.set_member_name("session_id");
 | 
			
		||||
            builder.add_string_value(session_id);
 | 
			
		||||
 | 
			
		||||
            builder.set_member_name("client_secret");
 | 
			
		||||
            builder.add_string_value(client_secret);
 | 
			
		||||
 | 
			
		||||
            builder.end_object();
 | 
			
		||||
 | 
			
		||||
            return builder.get_root();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -247,261 +247,6 @@ G_DEFINE_QUARK(matrix-error-quark, matrix_error);
 | 
			
		||||
 * Receipt types of acknowledgment.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Matrix3PidCredential:
 | 
			
		||||
 *
 | 
			
		||||
 * An opaque structure to store credentials to use with Identity
 | 
			
		||||
 * Server communication.
 | 
			
		||||
 */
 | 
			
		||||
struct _Matrix3PidCredential {
 | 
			
		||||
    gchar *id_server;
 | 
			
		||||
    gchar *session_id;
 | 
			
		||||
    gchar *client_secret;
 | 
			
		||||
    guint refcount;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
G_DEFINE_BOXED_TYPE(Matrix3PidCredential, matrix_3pid_credential,
 | 
			
		||||
                    (GBoxedCopyFunc)matrix_3pid_credential_ref,
 | 
			
		||||
                    (GBoxedFreeFunc)matrix_3pid_credential_unref);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_new:
 | 
			
		||||
 *
 | 
			
		||||
 * Create a new #Matrix3PidCredential object with reference count
 | 
			
		||||
 * of 1.
 | 
			
		||||
 *
 | 
			
		||||
 * Returns: (transfer full): a new #Matrix3PidCredential
 | 
			
		||||
 */
 | 
			
		||||
Matrix3PidCredential *
 | 
			
		||||
matrix_3pid_credential_new(void)
 | 
			
		||||
{
 | 
			
		||||
    Matrix3PidCredential *credential;
 | 
			
		||||
 | 
			
		||||
    credential = g_new0(Matrix3PidCredential, 1);
 | 
			
		||||
    credential->refcount = 1;
 | 
			
		||||
 | 
			
		||||
    return credential;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
matrix_3pid_credential_free(Matrix3PidCredential *credential)
 | 
			
		||||
{
 | 
			
		||||
    g_free(credential->id_server);
 | 
			
		||||
    g_free(credential->session_id);
 | 
			
		||||
    g_free(credential->client_secret);
 | 
			
		||||
    g_free(credential);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_ref:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 *
 | 
			
		||||
 * Increase reference count of @credential by one.
 | 
			
		||||
 *
 | 
			
		||||
 * Returns: (transfer none): the same #Matrix3PidCredential
 | 
			
		||||
 */
 | 
			
		||||
Matrix3PidCredential *
 | 
			
		||||
matrix_3pid_credential_ref(Matrix3PidCredential *credential)
 | 
			
		||||
{
 | 
			
		||||
    credential->refcount++;
 | 
			
		||||
 | 
			
		||||
    return credential;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_unref:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 *
 | 
			
		||||
 * Decrease reference count of @credential by one. If reference count
 | 
			
		||||
 * reaches zero, @credential gets freed.
 | 
			
		||||
 */
 | 
			
		||||
void
 | 
			
		||||
matrix_3pid_credential_unref(Matrix3PidCredential *credential)
 | 
			
		||||
{
 | 
			
		||||
    if (--credential->refcount == 0) {
 | 
			
		||||
        matrix_3pid_credential_free(credential);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_set_id_server:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 * @id_server: the Identity Server to use
 | 
			
		||||
 *
 | 
			
		||||
 * Set the Identity Server to use for this credential.
 | 
			
		||||
 */
 | 
			
		||||
void
 | 
			
		||||
matrix_3pid_credential_set_id_server(Matrix3PidCredential *credential,
 | 
			
		||||
                                     const gchar *id_server)
 | 
			
		||||
{
 | 
			
		||||
    g_free(credential->id_server);
 | 
			
		||||
    credential->id_server = g_strdup(id_server);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_get_id_server:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 *
 | 
			
		||||
 * Get the Identity Server used for this credential.
 | 
			
		||||
 *
 | 
			
		||||
 * Returns: the Identity Server's name. The returned value is owned by
 | 
			
		||||
 *          @credential and should not be freed nor modified
 | 
			
		||||
 */
 | 
			
		||||
const gchar *
 | 
			
		||||
matrix_3pid_credential_get_id_server(Matrix3PidCredential *credential)
 | 
			
		||||
{
 | 
			
		||||
    return credential->id_server;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_set_session_id:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 * @session_id: the session identifier given by the Identity Server
 | 
			
		||||
 *
 | 
			
		||||
 * Set the session identifier got from the Identity Server.
 | 
			
		||||
 */
 | 
			
		||||
void
 | 
			
		||||
matrix_3pid_credential_set_session_id(Matrix3PidCredential *credential,
 | 
			
		||||
                                      const gchar *session_id)
 | 
			
		||||
{
 | 
			
		||||
    g_free(credential->session_id);
 | 
			
		||||
    credential->session_id = g_strdup(session_id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_get_session_id:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 *
 | 
			
		||||
 * Get the session identifier for this credential.
 | 
			
		||||
 *
 | 
			
		||||
 * Returns: the session identifier. The returned value is owned by
 | 
			
		||||
 *          @credential and should not be freed nor modified.
 | 
			
		||||
 */
 | 
			
		||||
const gchar *
 | 
			
		||||
matrix_3pid_credential_get_session_id(Matrix3PidCredential *credential)
 | 
			
		||||
{
 | 
			
		||||
    return credential->session_id;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_set_client_secret:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 * @client_secret: the client secret used in the session with the
 | 
			
		||||
 *                 Identity Server
 | 
			
		||||
 *
 | 
			
		||||
 * Set the client secret that was used in the session with the
 | 
			
		||||
 * Identity Server.
 | 
			
		||||
 */
 | 
			
		||||
void
 | 
			
		||||
matrix_3pid_credential_set_client_secret(Matrix3PidCredential *credential,
 | 
			
		||||
                                         const gchar *client_secret)
 | 
			
		||||
{
 | 
			
		||||
    g_free(credential->client_secret);
 | 
			
		||||
    credential->client_secret = g_strdup(client_secret);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_get_client_secret:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 *
 | 
			
		||||
 * Get the client secret that was used in the session with the
 | 
			
		||||
 * Identity Server.
 | 
			
		||||
 *
 | 
			
		||||
 * Returns: the client secret. The returned value is owned by
 | 
			
		||||
 *          @credential and should not be freed nor modified.
 | 
			
		||||
 */
 | 
			
		||||
const gchar *
 | 
			
		||||
matrix_3pid_credential_get_client_secret(Matrix3PidCredential *credential)
 | 
			
		||||
{
 | 
			
		||||
    return credential->client_secret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_get_json_node:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 * @error: return location for a #GError, or %NULL
 | 
			
		||||
 *
 | 
			
		||||
 * Get the JSON representation of @credential as a #JsonNode. If any
 | 
			
		||||
 * fields of @credential are empty, it will yield an error, as all
 | 
			
		||||
 * fields are mandatory. In this case, the return value is %NULL.
 | 
			
		||||
 *
 | 
			
		||||
 * Returns: (transfer full) (allow-none): the #JsonNode representation
 | 
			
		||||
 *          of @credential
 | 
			
		||||
 */
 | 
			
		||||
JsonNode *
 | 
			
		||||
matrix_3pid_credential_get_json_node(Matrix3PidCredential *credential,
 | 
			
		||||
                                     GError **error)
 | 
			
		||||
{
 | 
			
		||||
    JsonBuilder *builder;
 | 
			
		||||
    JsonNode *node;
 | 
			
		||||
 | 
			
		||||
    if (!credential->id_server
 | 
			
		||||
        || !credential->session_id
 | 
			
		||||
        || !credential->client_secret) {
 | 
			
		||||
        g_set_error(error,
 | 
			
		||||
                    MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE,
 | 
			
		||||
                    "All fields of the 3PID credential must be filled!");
 | 
			
		||||
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    builder = json_builder_new();
 | 
			
		||||
    json_builder_begin_object(builder);
 | 
			
		||||
 | 
			
		||||
    json_builder_set_member_name(builder, "id_server");
 | 
			
		||||
    json_builder_add_string_value(builder, credential->id_server);
 | 
			
		||||
 | 
			
		||||
    json_builder_set_member_name(builder, "session_id");
 | 
			
		||||
    json_builder_add_string_value(builder, credential->session_id);
 | 
			
		||||
 | 
			
		||||
    json_builder_set_member_name(builder, "client_secret");
 | 
			
		||||
    json_builder_add_string_value(builder, credential->client_secret);
 | 
			
		||||
 | 
			
		||||
    json_builder_end_object(builder);
 | 
			
		||||
    node = json_builder_get_root(builder);
 | 
			
		||||
    g_object_unref(builder);
 | 
			
		||||
 | 
			
		||||
    return node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * matrix_3pid_credential_get_json_data:
 | 
			
		||||
 * @credential: a #Matrix3PidCredential
 | 
			
		||||
 * @datalen: (out): storage location for the length of the JSON data,
 | 
			
		||||
 *           or %NULL
 | 
			
		||||
 * @error: a #GError
 | 
			
		||||
 *
 | 
			
		||||
 * Get the JSON representation of @credential as a string. If any
 | 
			
		||||
 * fields of @credential is %NULL, this function returns %NULL and
 | 
			
		||||
 * fills @error wich %MATRIX_ERROR_INCOMPLETE.
 | 
			
		||||
 *
 | 
			
		||||
 * Returns: (transfer full) (allow-none): the JSON representation of
 | 
			
		||||
 *          @credential, or %NULL
 | 
			
		||||
 */
 | 
			
		||||
gchar *
 | 
			
		||||
matrix_3pid_credential_get_json_data(Matrix3PidCredential *credential,
 | 
			
		||||
                                     gsize *datalen,
 | 
			
		||||
                                     GError **error)
 | 
			
		||||
{
 | 
			
		||||
    JsonGenerator *generator;
 | 
			
		||||
    JsonNode *node;
 | 
			
		||||
    gchar *data;
 | 
			
		||||
 | 
			
		||||
    if ((node = matrix_3pid_credential_get_json_node(
 | 
			
		||||
                 credential, error)) == NULL) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    generator = json_generator_new();
 | 
			
		||||
    json_generator_set_root(generator, node);
 | 
			
		||||
    json_node_free(node);
 | 
			
		||||
 | 
			
		||||
    data = json_generator_to_data(generator, datalen);
 | 
			
		||||
    g_object_unref(generator);
 | 
			
		||||
 | 
			
		||||
    return data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * MatrixPusher:
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
@@ -129,29 +129,6 @@ typedef enum {
 | 
			
		||||
    MATRIX_ACCOUNT_KIND_GUEST
 | 
			
		||||
} MatrixAccountKind;
 | 
			
		||||
 | 
			
		||||
typedef struct _Matrix3PidCredential Matrix3PidCredential;
 | 
			
		||||
 | 
			
		||||
GType matrix_3pid_credential_get_type(void);
 | 
			
		||||
#define MATRIX_TYPE_3PID_CREDENTIAL (matrix_3pid_credential_get_type())
 | 
			
		||||
 | 
			
		||||
Matrix3PidCredential *matrix_3pid_credential_new(void);
 | 
			
		||||
Matrix3PidCredential *matrix_3pid_credential_ref(Matrix3PidCredential *credential);
 | 
			
		||||
void matrix_3pid_credential_unref(Matrix3PidCredential *credential);
 | 
			
		||||
void matrix_3pid_credential_set_id_server(Matrix3PidCredential *credential,
 | 
			
		||||
                                          const gchar *id_server);
 | 
			
		||||
const gchar *matrix_3pid_credential_get_id_server(Matrix3PidCredential *credential);
 | 
			
		||||
void matrix_3pid_credential_set_session_id(Matrix3PidCredential *credential,
 | 
			
		||||
                                           const gchar *session_id);
 | 
			
		||||
const gchar *matrix_3pid_credential_get_session_id(Matrix3PidCredential *credential);
 | 
			
		||||
void matrix_3pid_credential_set_client_secret(Matrix3PidCredential *credential,
 | 
			
		||||
                                              const gchar *client_secret);
 | 
			
		||||
const gchar *matrix_3pid_credential_get_client_secret(Matrix3PidCredential *credential);
 | 
			
		||||
JsonNode *matrix_3pid_credential_get_json_node(Matrix3PidCredential *credential,
 | 
			
		||||
                                               GError **error);
 | 
			
		||||
gchar *matrix_3pid_credential_get_json_data(Matrix3PidCredential *credential,
 | 
			
		||||
                                            gsize *datalen,
 | 
			
		||||
                                            GError **error);
 | 
			
		||||
 | 
			
		||||
typedef struct _MatrixPusher MatrixPusher;
 | 
			
		||||
 | 
			
		||||
GType matrix_pusher_get_type(void);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user