1 /*
2  * Copyright (C) 2015 Red Hat, Inc. (www.redhat.com)
3  *
4  * This library is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library. If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "evolution-data-server-config.h"
19 
20 #include <glib.h>
21 #include <glib/gi18n-lib.h>
22 
23 #include <libedataserver/libedataserver.h>
24 
25 #include "e-source-credentials-provider-impl-password.h"
26 
27 struct _ESourceCredentialsProviderImplPasswordPrivate {
28 	gboolean dummy;
29 };
30 
G_DEFINE_TYPE_WITH_PRIVATE(ESourceCredentialsProviderImplPassword,e_source_credentials_provider_impl_password,E_TYPE_SOURCE_CREDENTIALS_PROVIDER_IMPL)31 G_DEFINE_TYPE_WITH_PRIVATE (ESourceCredentialsProviderImplPassword, e_source_credentials_provider_impl_password, E_TYPE_SOURCE_CREDENTIALS_PROVIDER_IMPL)
32 
33 static gboolean
34 e_source_credentials_provider_impl_password_can_process (ESourceCredentialsProviderImpl *provider_impl,
35 							 ESource *source)
36 {
37 	g_return_val_if_fail (E_IS_SOURCE_CREDENTIALS_PROVIDER_IMPL_PASSWORD (provider_impl), FALSE);
38 	g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
39 
40 	/* It can process any source by default */
41 	return TRUE;
42 }
43 
44 static gboolean
e_source_credentials_provider_impl_password_can_store(ESourceCredentialsProviderImpl * provider_impl)45 e_source_credentials_provider_impl_password_can_store (ESourceCredentialsProviderImpl *provider_impl)
46 {
47 	g_return_val_if_fail (E_IS_SOURCE_CREDENTIALS_PROVIDER_IMPL_PASSWORD (provider_impl), FALSE);
48 
49 	return TRUE;
50 }
51 
52 static gboolean
e_source_credentials_provider_impl_password_can_prompt(ESourceCredentialsProviderImpl * provider_impl)53 e_source_credentials_provider_impl_password_can_prompt (ESourceCredentialsProviderImpl *provider_impl)
54 {
55 	g_return_val_if_fail (E_IS_SOURCE_CREDENTIALS_PROVIDER_IMPL_PASSWORD (provider_impl), FALSE);
56 
57 	return TRUE;
58 }
59 
60 static gboolean
e_source_credentials_provider_impl_password_lookup_sync(ESourceCredentialsProviderImpl * provider_impl,ESource * source,GCancellable * cancellable,ENamedParameters ** out_credentials,GError ** error)61 e_source_credentials_provider_impl_password_lookup_sync (ESourceCredentialsProviderImpl *provider_impl,
62 							 ESource *source,
63 							 GCancellable *cancellable,
64 							 ENamedParameters **out_credentials,
65 							 GError **error)
66 {
67 	gchar *password = NULL;
68 
69 	g_return_val_if_fail (E_IS_SOURCE_CREDENTIALS_PROVIDER_IMPL_PASSWORD (provider_impl), FALSE);
70 	g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
71 	g_return_val_if_fail (out_credentials != NULL, FALSE);
72 
73 	*out_credentials = NULL;
74 
75 	if (!e_source_lookup_password_sync (source, cancellable, &password, error))
76 		return FALSE;
77 
78 	if (!password) {
79 		g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("Password not found"));
80 		return FALSE;
81 	}
82 
83 	*out_credentials = e_named_parameters_new ();
84 	e_named_parameters_set (*out_credentials, E_SOURCE_CREDENTIAL_PASSWORD, password);
85 
86 	e_util_safe_free_string (password);
87 
88 	return TRUE;
89 }
90 
91 static gboolean
e_source_credentials_provider_impl_password_store_sync(ESourceCredentialsProviderImpl * provider_impl,ESource * source,const ENamedParameters * credentials,gboolean permanently,GCancellable * cancellable,GError ** error)92 e_source_credentials_provider_impl_password_store_sync (ESourceCredentialsProviderImpl *provider_impl,
93 							ESource *source,
94 							const ENamedParameters *credentials,
95 							gboolean permanently,
96 							GCancellable *cancellable,
97 							GError **error)
98 {
99 	g_return_val_if_fail (E_IS_SOURCE_CREDENTIALS_PROVIDER_IMPL_PASSWORD (provider_impl), FALSE);
100 	g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
101 	g_return_val_if_fail (credentials != NULL, FALSE);
102 	g_return_val_if_fail (e_named_parameters_get (credentials, E_SOURCE_CREDENTIAL_PASSWORD) != NULL, FALSE);
103 
104 	return e_source_store_password_sync (source,
105 		e_named_parameters_get (credentials, E_SOURCE_CREDENTIAL_PASSWORD),
106 		permanently, cancellable, error);
107 }
108 
109 static gboolean
e_source_credentials_provider_impl_password_delete_sync(ESourceCredentialsProviderImpl * provider_impl,ESource * source,GCancellable * cancellable,GError ** error)110 e_source_credentials_provider_impl_password_delete_sync (ESourceCredentialsProviderImpl *provider_impl,
111 							 ESource *source,
112 							 GCancellable *cancellable,
113 							 GError **error)
114 {
115 	g_return_val_if_fail (E_IS_SOURCE_CREDENTIALS_PROVIDER_IMPL_PASSWORD (provider_impl), FALSE);
116 	g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
117 
118 	return e_source_delete_password_sync (source, cancellable, error);
119 }
120 
121 static void
e_source_credentials_provider_impl_password_class_init(ESourceCredentialsProviderImplPasswordClass * klass)122 e_source_credentials_provider_impl_password_class_init (ESourceCredentialsProviderImplPasswordClass *klass)
123 {
124 	ESourceCredentialsProviderImplClass *impl_class;
125 
126 	impl_class = E_SOURCE_CREDENTIALS_PROVIDER_IMPL_CLASS (klass);
127 	impl_class->can_process = e_source_credentials_provider_impl_password_can_process;
128 	impl_class->can_store = e_source_credentials_provider_impl_password_can_store;
129 	impl_class->can_prompt = e_source_credentials_provider_impl_password_can_prompt;
130 	impl_class->lookup_sync = e_source_credentials_provider_impl_password_lookup_sync;
131 	impl_class->store_sync = e_source_credentials_provider_impl_password_store_sync;
132 	impl_class->delete_sync = e_source_credentials_provider_impl_password_delete_sync;
133 }
134 
135 static void
e_source_credentials_provider_impl_password_init(ESourceCredentialsProviderImplPassword * provider_impl)136 e_source_credentials_provider_impl_password_init (ESourceCredentialsProviderImplPassword *provider_impl)
137 {
138 	provider_impl->priv = e_source_credentials_provider_impl_password_get_instance_private (provider_impl);
139 }
140