90bf757b0667b1d7631d83b78894cbd8c2a56be4
[profile/ivi/gsignond.git] / src / plugins / password / gsignond-password-plugin.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of gsignond
5  *
6  * Copyright (C) 2012 Intel Corporation.
7  *
8  * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include <gsignond/gsignond-plugin-interface.h>
27 #include "gsignond-password-plugin.h"
28 #include <gsignond/gsignond-error.h>
29
30 static void gsignond_plugin_interface_init (GSignondPluginInterface *iface);
31
32 G_DEFINE_TYPE_WITH_CODE (GSignondPasswordPlugin, gsignond_password_plugin, 
33                          G_TYPE_OBJECT,
34                          G_IMPLEMENT_INTERFACE (GSIGNOND_TYPE_PLUGIN,
35                                                 gsignond_plugin_interface_init));
36
37 static void gsignond_password_plugin_cancel (GSignondPlugin *self)
38 {
39     GError* error = g_error_new(GSIGNOND_ERROR, 
40                                 GSIGNOND_ERROR_SESSION_CANCELED,
41                                 "Session canceled");
42     gsignond_plugin_error (self, error); 
43     g_error_free(error);
44 }
45
46 static void gsignond_password_plugin_request (
47     GSignondPlugin *self, GSignondSessionData *session_data)
48 {
49
50 }
51
52 static void gsignond_password_plugin_request_initial (
53     GSignondPlugin *self, GSignondSessionData *session_data, 
54     const gchar *mechanism)
55 {
56     const gchar* username = gsignond_session_data_get_username(session_data);
57     const gchar* secret = gsignond_session_data_get_secret(session_data);
58     
59     if (secret != NULL) {
60         GSignondSessionData *response = gsignond_dictionary_new();
61         if (username != NULL)
62             gsignond_session_data_set_username(response, username);
63         gsignond_session_data_set_secret(response, secret);
64         gsignond_plugin_response_final(self, response);
65         gsignond_dictionary_unref(response);
66         return;
67     }
68     
69     GSignondSignonuiData *user_action_data = gsignond_signonui_data_new();
70     if (username == NULL)
71         gsignond_signonui_data_set_query_username(user_action_data, TRUE);
72     else {
73         gsignond_signonui_data_set_query_username(user_action_data, FALSE);
74         gsignond_signonui_data_set_username(user_action_data, username);
75     }
76     gsignond_signonui_data_set_query_password(user_action_data, TRUE);
77     gsignond_plugin_user_action_required(self, user_action_data);
78     gsignond_dictionary_unref(user_action_data);
79 }
80
81 static void gsignond_password_plugin_user_action_finished (
82     GSignondPlugin *self, 
83     GSignondSignonuiData *session_data)
84 {
85     GSignondSignonuiError query_error;
86     gboolean res = gsignond_signonui_data_get_query_error(session_data,
87                                                           &query_error);
88     if (res == FALSE) {
89         GError* error = g_error_new(GSIGNOND_ERROR, 
90                                 GSIGNOND_ERROR_USER_INTERACTION,
91                                 "userActionFinished did not return an error value");
92         gsignond_plugin_error (self, error); 
93         g_error_free(error);
94         return;
95     }
96     const gchar* username = gsignond_signonui_data_get_username(session_data);
97     const gchar* secret = gsignond_signonui_data_get_password(session_data);
98     
99     if (query_error == SIGNONUI_ERROR_NONE && 
100         username != NULL && 
101         secret != NULL) {
102         GSignondSessionData *response = gsignond_dictionary_new();
103         gsignond_session_data_set_username(response, username);
104         gsignond_session_data_set_secret(response, secret);
105         gsignond_plugin_response_final(self, response);
106         gsignond_dictionary_unref(response);
107         return;
108     } else if (query_error == SIGNONUI_ERROR_CANCELED) {
109         GError* error = g_error_new(GSIGNOND_ERROR, 
110                                 GSIGNOND_ERROR_SESSION_CANCELED,
111                                 "Session canceled");
112         gsignond_plugin_error (self, error); 
113         g_error_free(error);
114     } else {
115         GError* error = g_error_new(GSIGNOND_ERROR, 
116                                 GSIGNOND_ERROR_USER_INTERACTION,
117                                 "userActionFinished error: %d",
118                                 query_error);
119         gsignond_plugin_error (self, error); 
120         g_error_free(error);
121     }
122 }
123
124 static void gsignond_password_plugin_refresh (
125     GSignondPlugin *self, 
126     GSignondSignonuiData *session_data)
127 {
128     gsignond_plugin_refreshed(self, session_data);
129 }
130
131 static void
132 gsignond_plugin_interface_init (GSignondPluginInterface *iface)
133 {
134     iface->cancel = gsignond_password_plugin_cancel;
135     iface->request_initial = gsignond_password_plugin_request_initial;
136     iface->request = gsignond_password_plugin_request;
137     iface->user_action_finished = gsignond_password_plugin_user_action_finished;
138     iface->refresh = gsignond_password_plugin_refresh;
139 }
140
141 static void
142 gsignond_password_plugin_init (GSignondPasswordPlugin *self)
143 {
144     
145     
146 }
147
148 enum
149 {
150     PROP_0,
151     
152     PROP_TYPE,
153     PROP_MECHANISMS
154 };
155
156 static void
157 gsignond_password_plugin_set_property (GObject      *object,
158                                        guint         property_id,
159                                        const GValue *value,
160                                        GParamSpec   *pspec)
161 {
162     switch (property_id)
163     {
164         default:
165             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
166             break;
167     }
168 }
169
170 static void
171 gsignond_password_plugin_get_property (GObject    *object,
172                                        guint       prop_id,
173                                        GValue     *value,
174                                        GParamSpec *pspec)
175 {
176     GSignondPasswordPlugin *password_plugin = GSIGNOND_PASSWORD_PLUGIN (object);
177     (void) password_plugin;
178     gchar *mechanisms[] = { "password", NULL };
179     
180     switch (prop_id)
181     {
182         case PROP_TYPE:
183             g_value_set_string (value, "password");
184             break;
185         case PROP_MECHANISMS:
186             g_value_set_boxed (value, mechanisms);
187             break;
188             
189         default:
190             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191             break;
192     }
193 }
194
195 static void
196 gsignond_password_plugin_class_init (GSignondPasswordPluginClass *klass)
197 {
198     GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
199     
200     gobject_class->set_property = gsignond_password_plugin_set_property;
201     gobject_class->get_property = gsignond_password_plugin_get_property;
202     
203     g_object_class_override_property (gobject_class, PROP_TYPE, "type");
204     g_object_class_override_property (gobject_class, PROP_MECHANISMS, 
205                                       "mechanisms");
206 }