1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2018 Red Hat, Inc. (www.redhat.com)
4  *
5  * This library is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #if !defined (__LIBEDATASERVER_H_INSIDE__) && !defined (LIBEDATASERVER_COMPILATION)
19 #error "Only <libedataserver/libedataserver.h> should be included directly."
20 #endif
21 
22 #ifndef E_OAUTH2_SERVICE_H
23 #define E_OAUTH2_SERVICE_H
24 
25 #include <glib.h>
26 #include <libsoup/soup.h>
27 
28 #include <libedataserver/e-source.h>
29 
30 /* Standard GObject macros */
31 #define E_TYPE_OAUTH2_SERVICE \
32 	(e_oauth2_service_get_type ())
33 #define E_OAUTH2_SERVICE(obj) \
34 	(G_TYPE_CHECK_INSTANCE_CAST \
35 	((obj), E_TYPE_OAUTH2_SERVICE, EOAuth2Service))
36 #define E_IS_OAUTH2_SERVICE(obj) \
37 	(G_TYPE_CHECK_INSTANCE_TYPE \
38 	((obj), E_TYPE_OAUTH2_SERVICE))
39 #define E_OAUTH2_SERVICE_GET_INTERFACE(obj) \
40 	(G_TYPE_INSTANCE_GET_INTERFACE \
41 	((obj), E_TYPE_OAUTH2_SERVICE, EOAuth2ServiceInterface))
42 
43 /* Secret key names, saved by the code; not the names returned by the OAuth2 server */
44 #define E_OAUTH2_SECRET_REFRESH_TOKEN "refresh_token"
45 #define E_OAUTH2_SECRET_ACCESS_TOKEN "access_token"
46 #define E_OAUTH2_SECRET_EXPIRES_AFTER "expires_after"
47 
48 G_BEGIN_DECLS
49 
50 /**
51  * EOAuth2ServiceFlags:
52  * @E_OAUTH2_SERVICE_FLAG_NONE: No flag set
53  * @E_OAUTH2_SERVICE_FLAG_EXTRACT_REQUIRES_PAGE_CONTENT: the service requires also page
54  *    content to be passed to e_oauth2_service_extract_authorization_code()
55  *
56  * Flags of the OAuth2 service.
57  *
58  * Since: 3.28
59  **/
60 typedef enum {
61 	E_OAUTH2_SERVICE_FLAG_NONE				= 0,
62 	E_OAUTH2_SERVICE_FLAG_EXTRACT_REQUIRES_PAGE_CONTENT	= (1 << 1)
63 } EOAuth2ServiceFlags;
64 
65 /**
66  * EOAuth2ServiceNavigationPolicy:
67  * @E_OAUTH2_SERVICE_NAVIGATION_POLICY_DENY: Deny navigation to the given web resource
68  * @E_OAUTH2_SERVICE_NAVIGATION_POLICY_ALLOW: Allow navigation to the given web resource
69  * @E_OAUTH2_SERVICE_NAVIGATION_POLICY_ABORT: Abort authentication processing
70  *
71  * A value used during querying authentication URI, to decide whether certain
72  * resource can be used or not. The @E_OAUTH2_SERVICE_NAVIGATION_POLICY_ABORT
73  * can be used to abort the authentication query, like when user cancelled it.
74  *
75  * Since: 3.28
76  **/
77 typedef enum {
78 	E_OAUTH2_SERVICE_NAVIGATION_POLICY_DENY,
79 	E_OAUTH2_SERVICE_NAVIGATION_POLICY_ALLOW,
80 	E_OAUTH2_SERVICE_NAVIGATION_POLICY_ABORT
81 } EOAuth2ServiceNavigationPolicy;
82 
83 /**
84  * EOAuth2ServiceRefSourceFunc:
85  * @user_data: user data, as passed to e_oauth2_service_get_access_token_sync()
86  *    or e_oauth2_service_receive_and_store_token_sync(),
87  *    or e_oauth2_service_refresh_and_store_token_sync()
88  * @uid: an #ESource UID to return
89  *
90  * Returns: (transfer full) (nullable): an #ESource with UID @uid, or %NULL, if not found.
91  *    Dereference the returned non-NULL #ESource with g_object_unref(), when no longer needed.
92  *
93  * Since: 3.28
94  **/
95 typedef ESource * (* EOAuth2ServiceRefSourceFunc)	(gpointer user_data,
96 							 const gchar *uid);
97 
98 typedef struct _EOAuth2Service EOAuth2Service;
99 typedef struct _EOAuth2ServiceInterface EOAuth2ServiceInterface;
100 
101 /**
102  * EOAuth2Service:
103  *
104  * Contains only private data that should be read and manipulated using the
105  * functions below.
106  *
107  * Since: 3.28
108  **/
109 struct _EOAuth2ServiceInterface {
110 	GTypeInterface parent_interface;
111 
112 	gboolean	(* can_process)			(EOAuth2Service *service,
113 							 ESource *source);
114 	gboolean	(* guess_can_process)		(EOAuth2Service *service,
115 							 const gchar *protocol,
116 							 const gchar *hostname);
117 	guint32		(* get_flags)			(EOAuth2Service *service);
118 	const gchar *	(* get_name)			(EOAuth2Service *service);
119 	const gchar *	(* get_display_name)		(EOAuth2Service *service);
120 	const gchar *	(* get_client_id)		(EOAuth2Service *service,
121 							 ESource *source);
122 	const gchar *	(* get_client_secret)		(EOAuth2Service *service,
123 							 ESource *source);
124 	const gchar *	(* get_authentication_uri)	(EOAuth2Service *service,
125 							 ESource *source);
126 	const gchar *	(* get_refresh_uri)		(EOAuth2Service *service,
127 							 ESource *source);
128 	const gchar *	(* get_redirect_uri)		(EOAuth2Service *service,
129 							 ESource *source);
130 	void		(* prepare_authentication_uri_query)
131 							(EOAuth2Service *service,
132 							 ESource *source,
133 							 GHashTable *uri_query);
134 	EOAuth2ServiceNavigationPolicy
135 			(* get_authentication_policy)	(EOAuth2Service *service,
136 							 ESource *source,
137 							 const gchar *uri);
138 	gboolean	(* extract_authorization_code)	(EOAuth2Service *service,
139 							 ESource *source,
140 							 const gchar *page_title,
141 							 const gchar *page_uri,
142 							 const gchar *page_content,
143 							 gchar **out_authorization_code);
144 	void		(* prepare_get_token_form)	(EOAuth2Service *service,
145 							 ESource *source,
146 							 const gchar *authorization_code,
147 							 GHashTable *form);
148 	void		(* prepare_get_token_message)	(EOAuth2Service *service,
149 							 ESource *source,
150 							 SoupMessage *message);
151 	void		(* prepare_refresh_token_form)	(EOAuth2Service *service,
152 							 ESource *source,
153 							 const gchar *refresh_token,
154 							 GHashTable *form);
155 	void		(* prepare_refresh_token_message)
156 							(EOAuth2Service *service,
157 							 ESource *source,
158 							 SoupMessage *message);
159 
160 	/* Padding for future expansion */
161 	gpointer reserved[10];
162 };
163 
164 GType		e_oauth2_service_get_type		(void) G_GNUC_CONST;
165 gboolean	e_oauth2_service_can_process		(EOAuth2Service *service,
166 							 ESource *source);
167 gboolean	e_oauth2_service_guess_can_process	(EOAuth2Service *service,
168 							 const gchar *protocol,
169 							 const gchar *hostname);
170 guint32		e_oauth2_service_get_flags		(EOAuth2Service *service);
171 const gchar *	e_oauth2_service_get_name		(EOAuth2Service *service);
172 const gchar *	e_oauth2_service_get_display_name	(EOAuth2Service *service);
173 const gchar *	e_oauth2_service_get_client_id		(EOAuth2Service *service,
174 							 ESource *source);
175 const gchar *	e_oauth2_service_get_client_secret	(EOAuth2Service *service,
176 							 ESource *source);
177 const gchar *	e_oauth2_service_get_authentication_uri	(EOAuth2Service *service,
178 							 ESource *source);
179 const gchar *	e_oauth2_service_get_refresh_uri	(EOAuth2Service *service,
180 							 ESource *source);
181 const gchar *	e_oauth2_service_get_redirect_uri	(EOAuth2Service *service,
182 							 ESource *source);
183 void		e_oauth2_service_prepare_authentication_uri_query
184 							(EOAuth2Service *service,
185 							 ESource *source,
186 							 GHashTable *uri_query);
187 EOAuth2ServiceNavigationPolicy
188 		e_oauth2_service_get_authentication_policy
189 							(EOAuth2Service *service,
190 							 ESource *source,
191 							 const gchar *uri);
192 gboolean	e_oauth2_service_extract_authorization_code
193 							(EOAuth2Service *service,
194 							 ESource *source,
195 							 const gchar *page_title,
196 							 const gchar *page_uri,
197 							 const gchar *page_content,
198 							 gchar **out_authorization_code);
199 void		e_oauth2_service_prepare_get_token_form	(EOAuth2Service *service,
200 							 ESource *source,
201 							 const gchar *authorization_code,
202 							 GHashTable *form);
203 void		e_oauth2_service_prepare_get_token_message
204 							(EOAuth2Service *service,
205 							 ESource *source,
206 							 SoupMessage *message);
207 void		e_oauth2_service_prepare_refresh_token_form
208 							(EOAuth2Service *service,
209 							 ESource *source,
210 							 const gchar *refresh_token,
211 							 GHashTable *form);
212 void		e_oauth2_service_prepare_refresh_token_message
213 							(EOAuth2Service *service,
214 							 ESource *source,
215 							 SoupMessage *message);
216 
217 gboolean	e_oauth2_service_receive_and_store_token_sync
218 							(EOAuth2Service *service,
219 							 ESource *source,
220 							 const gchar *authorization_code,
221 							 EOAuth2ServiceRefSourceFunc ref_source,
222 							 gpointer ref_source_user_data,
223 							 GCancellable *cancellable,
224 							 GError **error);
225 gboolean	e_oauth2_service_refresh_and_store_token_sync
226 							(EOAuth2Service *service,
227 							 ESource *source,
228 							 const gchar *refresh_token,
229 							 EOAuth2ServiceRefSourceFunc ref_source,
230 							 gpointer ref_source_user_data,
231 							 GCancellable *cancellable,
232 							 GError **error);
233 gboolean	e_oauth2_service_delete_token_sync	(EOAuth2Service *service,
234 							 ESource *source,
235 							 GCancellable *cancellable,
236 							 GError **error);
237 gboolean	e_oauth2_service_get_access_token_sync	(EOAuth2Service *service,
238 							 ESource *source,
239 							 EOAuth2ServiceRefSourceFunc ref_source,
240 							 gpointer ref_source_user_data,
241 							 gchar **out_access_token,
242 							 gint *out_expires_in,
243 							 GCancellable *cancellable,
244 							 GError **error);
245 
246 void		e_oauth2_service_util_set_to_form	(GHashTable *form,
247 							 const gchar *name,
248 							 const gchar *value);
249 void		e_oauth2_service_util_take_to_form	(GHashTable *form,
250 							 const gchar *name,
251 							 gchar *value);
252 
253 G_END_DECLS
254 
255 #endif /* E_OAUTH2_SERVICE_H */
256