File: snapd-auth-data.c

package info (click to toggle)
snapd-glib 1.45-1.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,184 kB
  • sloc: ansic: 22,492; cpp: 14,089; sh: 4,667; makefile: 300; xml: 220
file content (188 lines) | stat: -rw-r--r-- 5,564 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2016 Canonical Ltd.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2 or version 3 of the License.
 * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
 */

#include "config.h"

#include "snapd-auth-data.h"

/**
 * SECTION: snapd-auth-data
 * @short_description: Authorization data
 * @include: snapd-glib/snapd-glib.h
 *
 * A #SnapdAuthData contains authorization data to communicate with snapd.
 * Authenticating with snapd_login_sync() or snapd_client_login_sync() returns
 * authorization data that can be used for requests by calling
 * snapd_client_set_auth_data().
 *
 * It is recommended that the data is securely stored between sessions so
 * authentication is not required to be repeated. The authorization data is
 * made up of printable strings that can be easily written to a file/database.
 */

/**
 * SnapdAuthData:
 *
 * #SnapdAuthData contains authorization data used to communicate with snapd.
 *
 * The authorization data is in the form of a [Macaroon](https://research.google.com/pubs/pub41892.html).
 *
 * Since: 1.0
 */

struct _SnapdAuthData
{
    GObject parent_instance;

    gchar *macaroon;
    GStrv discharges;
};

enum
{
    PROP_MACAROON = 1,
    PROP_DISCHARGES,
    PROP_LAST
};

G_DEFINE_TYPE (SnapdAuthData, snapd_auth_data, G_TYPE_OBJECT)

/**
 * snapd_auth_data_new:
 * @macaroon: serialized macaroon used to authorize access to snapd.
 * @discharges: (array zero-terminated=1): serialized discharges.
 *
 * Create some authorization data.
 *
 * Returns: a new #SnapdAuthData
 *
 * Since: 1.0
 **/
SnapdAuthData *
snapd_auth_data_new (const gchar *macaroon, GStrv discharges)
{
    g_return_val_if_fail (macaroon != NULL, NULL);
    return g_object_new (SNAPD_TYPE_AUTH_DATA,
                         "macaroon", macaroon,
                         "discharges", discharges,
                         NULL);
}

/**
 * snapd_auth_data_get_macaroon:
 * @auth_data: a #SnapdAuthData.
 *
 * Get the Macaroon that this authorization uses.
 *
 * Returns: the serialized Macaroon used to authorize access to snapd.
 *
 * Since: 1.0
 */
const gchar *
snapd_auth_data_get_macaroon (SnapdAuthData *auth_data)
{
    g_return_val_if_fail (SNAPD_IS_AUTH_DATA (auth_data), NULL);
    return auth_data->macaroon;
}

/**
 * snapd_auth_data_get_discharges:
 * @auth_data: a #SnapdAuthData.
 *
 * Get the discharges that this authorization uses.
 *
 * Returns: (transfer none) (array zero-terminated=1): the discharges as serialized strings.
 *
 * Since: 1.0
 */
GStrv
snapd_auth_data_get_discharges (SnapdAuthData *auth_data)
{
    g_return_val_if_fail (SNAPD_IS_AUTH_DATA (auth_data), NULL);
    return auth_data->discharges;
}

static void
snapd_auth_data_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    SnapdAuthData *auth_data = SNAPD_AUTH_DATA (object);

    switch (prop_id) {
    case PROP_MACAROON:
        g_free (auth_data->macaroon);
        auth_data->macaroon = g_strdup (g_value_get_string (value));
        break;
    case PROP_DISCHARGES:
        g_strfreev (auth_data->discharges);
        auth_data->discharges = g_strdupv (g_value_get_boxed (value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
snapd_auth_data_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    SnapdAuthData *auth_data = SNAPD_AUTH_DATA (object);

    switch (prop_id) {
    case PROP_MACAROON:
        g_value_set_string (value, auth_data->macaroon);
        break;
    case PROP_DISCHARGES:
        g_value_set_boxed (value, auth_data->discharges);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
snapd_auth_data_finalize (GObject *object)
{
    SnapdAuthData *auth_data = SNAPD_AUTH_DATA (object);

    g_clear_pointer (&auth_data->macaroon, g_free);
    g_clear_pointer (&auth_data->discharges, g_strfreev);

    G_OBJECT_CLASS (snapd_auth_data_parent_class)->finalize (object);
}

static void
snapd_auth_data_class_init (SnapdAuthDataClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

    gobject_class->set_property = snapd_auth_data_set_property;
    gobject_class->get_property = snapd_auth_data_get_property;
    gobject_class->finalize = snapd_auth_data_finalize;

    g_object_class_install_property (gobject_class,
                                     PROP_MACAROON,
                                     g_param_spec_string ("macaroon",
                                                          "macaroon",
                                                          "Serialized macaroon",
                                                          NULL,
                                                          G_PARAM_READWRITE));
    g_object_class_install_property (gobject_class,
                                     PROP_DISCHARGES,
                                     g_param_spec_boxed ("discharges",
                                                         "discharges",
                                                         "Serialized discharges",
                                                         G_TYPE_STRV,
                                                         G_PARAM_READWRITE));
}

static void
snapd_auth_data_init (SnapdAuthData *auth_data)
{
}