105
122
GPtrArray *methods;
126
snapd_login_request_async_result_init (GAsyncResultIface *iface)
130
G_DEFINE_TYPE_WITH_CODE (SnapdLoginRequest, snapd_login_request, G_TYPE_OBJECT,
131
G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT, snapd_login_request_async_result_init))
134
snapd_login_request_finalize (GObject *object)
136
SnapdLoginRequest *request = SNAPD_LOGIN_REQUEST (object);
138
g_clear_object (&request->cancellable);
139
g_free (request->username);
140
g_free (request->password);
141
g_free (request->otp);
142
g_clear_object (&request->auth_data);
143
g_clear_object (&request->error);
147
snapd_login_request_class_init (SnapdLoginRequestClass *klass)
149
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151
gobject_class->finalize = snapd_login_request_finalize;
155
snapd_login_request_init (SnapdLoginRequest *request)
161
* @username: usename to log in with.
162
* @password: password to log in with.
163
* @otp: (allow-none): response to one-time password challenge.
164
* @cancellable: (allow-none): a #GCancellable or %NULL.
165
* @error: (allow-none): #GError location to store the error occurring, or %NULL to ignore.
167
* Returns: (transfer full): a #SnapdAuthData or %NULL on error.
170
snapd_login_sync (const gchar *username, const gchar *password, const gchar *otp,
171
GCancellable *cancellable, GError **error)
173
g_autoptr(GDBusConnection) c = NULL;
174
g_autoptr(GVariant) result = NULL;
175
const gchar *macaroon;
178
g_return_val_if_fail (username != NULL, NULL);
179
g_return_val_if_fail (password != NULL, NULL);
184
c = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
187
result = g_dbus_connection_call_sync (c,
188
"io.snapcraft.SnapdLoginService",
189
"/io/snapcraft/SnapdLoginService",
190
"io.snapcraft.SnapdLoginService",
192
g_variant_new ("(sss)", username, password, otp),
193
G_VARIANT_TYPE ("(sas)"),
194
G_DBUS_CALL_FLAGS_NONE,
201
g_variant_get (result, "(&s&as)", &macaroon, &discharges);
203
return snapd_auth_data_new (macaroon, discharges);
207
login_complete_cb (gpointer user_data)
209
g_autoptr(SnapdLoginRequest) request = user_data;
211
if (request->ready_callback != NULL)
212
request->ready_callback (NULL, G_ASYNC_RESULT (request), request->ready_callback_data);
214
return G_SOURCE_REMOVE;
218
login_cb (GObject *object, GAsyncResult *result, gpointer user_data)
220
g_autoptr(SnapdLoginRequest) request = user_data;
221
g_autoptr(GVariant) r = NULL;
222
const gchar *macaroon;
224
g_autoptr(GError) error = NULL;
226
r = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error);
228
request->error = g_error_new (SNAPD_ERROR,
229
SNAPD_ERROR_CONNECTION_FAILED,
230
"Failed to get call login: %s", error->message);
234
g_variant_get (r, "(&s&as)", &macaroon, &discharges);
235
request->auth_data = snapd_auth_data_new (macaroon, discharges);
237
g_idle_add (login_complete_cb, g_steal_pointer (&request));
241
bus_cb (GObject *object, GAsyncResult *result, gpointer user_data)
243
g_autoptr(SnapdLoginRequest) request = user_data;
244
g_autoptr(GDBusConnection) c = NULL;
245
g_autoptr(GError) error = NULL;
247
c = g_bus_get_finish (result, &error);
249
request->error = g_error_new (SNAPD_ERROR,
250
SNAPD_ERROR_CONNECTION_FAILED,
251
"Failed to get system bus: %s", error->message);
252
g_idle_add (login_complete_cb, g_steal_pointer (&request));
256
g_dbus_connection_call (c,
257
"io.snapcraft.SnapdLoginService",
258
"/io/snapcraft/SnapdLoginService",
259
"io.snapcraft.SnapdLoginService",
261
g_variant_new ("(sss)", request->username, request->password, request->otp),
262
G_VARIANT_TYPE ("(sas)"),
263
G_DBUS_CALL_FLAGS_NONE,
265
request->cancellable,
268
g_steal_pointer (&request);
273
* @username: usename to log in with.
274
* @password: password to log in with.
275
* @otp: (allow-none): response to one-time password challenge.
276
* @cancellable: (allow-none): a #GCancellable or %NULL.
277
* @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied.
278
* @user_data: (closure): the data to pass to callback function.
281
snapd_login_async (const gchar *username, const gchar *password, const gchar *otp,
282
GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
284
SnapdLoginRequest *request;
286
g_return_if_fail (username != NULL);
287
g_return_if_fail (password != NULL);
289
request = g_object_new (snapd_login_request_get_type (), NULL);
290
request->ready_callback = callback;
291
request->ready_callback_data = user_data;
292
request->cancellable = g_object_ref (cancellable);
293
request->username = g_strdup (username);
294
request->password = g_strdup (password);
295
request->otp = otp != NULL ? g_strdup (otp) : "";
297
g_bus_get (G_BUS_TYPE_SYSTEM, cancellable, bus_cb, NULL);
301
* snapd_login_finish:
302
* @result: a #GAsyncResult.
303
* @error: (allow-none): #GError location to store the error occurring, or %NULL to ignore.
305
* Returns: (transfer full): a #SnapdAuthData or %NULL on error.
308
snapd_login_finish (GAsyncResult *result, GError **error)
310
SnapdLoginRequest *request = SNAPD_LOGIN_REQUEST (result);
313
g_propagate_error (error, request->error);
315
return g_steal_pointer (&request->auth_data);
109
319
complete_cb (gpointer user_data)
111
SnapdRequest *request = user_data;
321
g_autoptr(SnapdRequest) request = user_data;
112
322
SnapdClientPrivate *priv = snapd_client_get_instance_private (request->client);
324
priv->requests = g_list_remove (priv->requests, request);
114
326
if (request->ready_callback != NULL)
115
327
request->ready_callback (G_OBJECT (request->client), G_ASYNC_RESULT (request), request->ready_callback_data);
117
priv->requests = g_list_remove (priv->requests, request);
118
g_object_unref (request);
120
329
return G_SOURCE_REMOVE;
479
688
if (g_strcmp0 (kind, "login-required") == 0) {
480
689
g_set_error_literal (error,
482
SNAPD_CLIENT_ERROR_LOGIN_REQUIRED,
691
SNAPD_ERROR_LOGIN_REQUIRED,
486
695
else if (g_strcmp0 (kind, "invalid-auth-data") == 0) {
487
696
g_set_error_literal (error,
489
SNAPD_CLIENT_ERROR_INVALID_AUTH_DATA,
698
SNAPD_ERROR_INVALID_AUTH_DATA,
493
702
else if (g_strcmp0 (kind, "two-factor-required") == 0) {
494
703
g_set_error_literal (error,
496
SNAPD_CLIENT_ERROR_TWO_FACTOR_REQUIRED,
705
SNAPD_ERROR_TWO_FACTOR_REQUIRED,
500
709
else if (g_strcmp0 (kind, "two-factor-failed") == 0) {
501
710
g_set_error_literal (error,
503
SNAPD_CLIENT_ERROR_TWO_FACTOR_FAILED,
712
SNAPD_ERROR_TWO_FACTOR_FAILED,
507
716
else if (status_code == SOUP_STATUS_BAD_REQUEST) {
508
717
g_set_error_literal (error,
510
SNAPD_CLIENT_ERROR_BAD_REQUEST,
719
SNAPD_ERROR_BAD_REQUEST,
515
724
g_set_error_literal (error,
517
SNAPD_CLIENT_ERROR_GENERAL_ERROR,
726
SNAPD_ERROR_GENERAL_ERROR,