-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle.h
309 lines (284 loc) · 7.79 KB
/
google.h
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/** @file google.h
* @brief Function prototypes for making requests to Google.
*
* This file contains source that is responsible for making
* any connection to Google Api. All connections are made by
* using sockets and OpenSSL, following Google Api regulations.
*
* @author Georgios Tsotsos
* @bug Not any known bugs.
*/
/*
* Copyright 2015 Georgios Tsotsos
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#ifndef GOOGLE_H_
#define GOOGLE_H_
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define GOOGLEAPI_HOST "www.googleapis.com"
#define GOOGLEAUTH_HOST "accounts.google.com"
#define GOOGLEAPI_PORT 443
#define GOOGLEAUTHPAGE "/o/oauth2/auth"
#define GOOGLETOKENPAGE "/oauth2/v3/token"
/**
* @brief Holding json info
* This struct is holding name/value type of Json
* response from google.
*
* @param name: Name of response
* @param value: Value of response
*
*/
typedef struct {
char * name;
char * value;
size_t length;
} Json;
/**
* @brief Holding complete google response
* @param http_code: HTTP status code
* @param http_message: Readable HTTP status
* @param error: Google Error name
* @param error_description: Google error description
* @param message: the full message retrieved by server
*
* Holding Google response message and status.
*/
typedef struct {
int http_code;
char *http_message;
char *error;
char *error_description;
char *message;
} GoogleResponse;
/**
* @brief Holding connection info
* @param socket: stores socket
* @param sslHandle: stores SSL
* @param sslContext: SSL_CTX struct
*
* Holding connection info
*/
typedef struct {
int socket;
SSL *sslHandle;
SSL_CTX *sslContext;
} connection;
/**
* @brief Holding settings
* @param authhost: Google server for authentication (accounts.google.com)
* @param authpage: Authenticatiion page (/o/oauth2/auth)
* @param tokenhost: Token server (googleapis.com)
* @param tokenpage: Token page
* @param client_id: Google application client id
* @param client_secret: Google application client secret
* @param redirect_uri: Give redirect uri
* @param refresh_token: Access token
*
* Holding settings info.
*/
typedef struct {
char * client_id;
char * client_secret;
char * redirect_uri;
char * refresh_token;
char * access_token;
} config;
/**
* @brief Concatenate function.
* @param s1: string
* @param s2: string
*
* Concatenates two strings
**/
char *concat ( char *s1, char *s2 );
/**
* @brief Triming function.
* @param input: string
*
* Remove spaces of a string
**/
char *trim ( char *input );
/**
* @brief Finds between two strings
* @param input: input string
* @param first: string, starting limit
* @param last : string, last limit
*
* Gets the containing string between two others (first and last)
**/
char *find_between ( char *response,char *first, char *last );
/**
* @brief Reads a file
* @param filename: The filename
*
* Reads a file to buffer *char
**/
char * ReadFile ( char * filename );
/**
* @brief Parses a Json style string
* @param json: Json style string
* @param value: The wanted value
*
* Finds a value based on its name in a Json string
**/
char* parseJson ( char *json, char *value );
/**
* @brief Find a value of config
* @param string: Config string
* @param value: The wanted value
*
* Finds a value based on its name in a Config string
**/
char * getValue ( char * string, char * value );
/**
* @brief Get the settings from config file
* @param filename: Config filename
*
* Stores the settings from config file to a config
* struct.
**/
config getSettings ( char *filename );
/**
* @brief Saves settings to file
* @param filename: Config filename
* @param name: Name of value to add o replace
* @param value: The value
*
* Saves/replace the wanted value by its name (preferable to config file)
**/
void setSetting ( char * filename, char *name , char *value );
/**
* @brief Establishes a tcp connection.
* @param server_addr: Server address
* @param server_port: Server port
*
* Establishes a TCP connection.
**/
int tcpConnect ( char * server_addr, int server_port );
/**
* @brief Establishes a connection using SSL layer
* @param server_addr: Server address
* @param server_port: Server port
*
* Establishes a connection using SSL layer and handles
* connection errors.
**/
connection * sslConnect ( char * server_addr,int server_port );
/**
* @brief Stops connection
* @param c: connection struct
*
* Closes socket and free the SSL connection.
**/
void sslDisconnect ( connection *c );
/**
* @brief Error handling for SSL-layer
* @param ssl_error: SSL error cathced by connection
*
* @return Message for SSL error.
*
**/
char *sslError ( int32_t ssl_error );
/**
* @brief HTTP status message and SSL messages
* @param code: HTTP status code or SSL error code.
*
* @return Message of HTTP status or SSL error if any.
*
**/
char * http_status_messages ( int code );
/**
* @brief Converts status line string to integer
* @param buffer: Input Buffer
*
* @return HTTP status code Int.
*
**/
int getIntCode ( char * buffer );
/**
* @brief Wrapper for SSL_read
* @param c: connection struct
*
* Utilize SSL_read to receive chunked encoding connections
* until the expected response is back. This mechanism
* controled by last 5 characters.
* They should be : 0-CR-LF-CR-LF.
*
**/
int sslRead ( char **response, connection *c );
/**
* @brief Wrapper for SSL_write
* @param c: connection struct
* @param text: text to send
*
* Simplify SSL_write if the (struct) connection exists
**/
void sslWrite ( connection * c, char *text );
/**
* @brief Formats a string with link for google authorization
* @param settings: Config struct which icludes settings
* @param scope: Scopes from google api. For its values
* see at : https://developers.google.com/+/web/api/rest/oauth
*
* Use this function to force user authorize your application.
*
**/
char * GoogleAuthLink ( config settings, char * scope );
/**
* @brief Formats a Header string to exchange code with access_token
* @param code: string with access code from user
* @param settings: config struct which icludes settings
*
* This function returns GoogleResponse struct. Almost every time
* 'message' is n json format.
**/
char * HeadersAuthToken ( char *code, config settings );
/**
* @brief Properly formats a char* with headers for token refresh
* @param settings: config struct which icludes settings
*
**/
char * HeadersRefreshToken ( config settings );
/**
* @brief Connects with google using the given headers
* @param headers: Headers as formed by functions provided.
*
**/
GoogleResponse GoogleConnect ( char * headers );
/**
* @brief Handles response from google api.
* @param response: response string
*
* Stores the google response if it is type of "application/json"
* to an 'array' of structs (Json).
**/
Json * parseResponse ( char * response );
#endif // GOOGLE_H_