xref: /petsc/src/sys/webclient/google.c (revision 0226ec35a99031c5bdd82a492055a25793359ffb)
1 
2 #include <petscwebclient.h>
3 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
4 #pragma gcc diagnostic   ignored "-Wdeprecated-declarations"
5 
6 /*
7    These variables identify the code as a PETSc application to Google.
8 
9    See -   https://stackoverflow.com/questions/4616553/using-oauth-in-free-open-source-software
10    Users can get their own application IDs - https://code.google.com/p/google-apps-manager/wiki/GettingAnOAuthConsoleKey
11 
12 */
13 #define PETSC_GOOGLE_CLIENT_ID "521429262559-i19i57eek8tnt9ftpp4p91rcl0bo9ag5.apps.googleusercontent.com"
14 #define PETSC_GOOGLE_CLIENT_ST "vOds_A71I3_S_aHMq_kZAI0t"
15 #define PETSC_GOOGLE_API_KEY   "AIzaSyDRZsOcySpWVzsUvIBL2UG3J2tcg-MXbyk"
16 
17 /*@C
18      PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token
19 
20    Not collective, only the first process in the `MPI_Comm` does anything
21 
22    Input Parameters:
23 +   comm - MPI communicator
24 .   refresh token - obtained with `PetscGoogleDriveAuthorize()`, if NULL PETSc will first look for one in the options data
25                     if not found it will call `PetscGoogleDriveAuthorize()`
26 -   tokensize - size of the output string access_token
27 
28    Output Parameter:
29 .   access_token - token that can be passed to `PetscGoogleDriveUpload()`
30 
31    Options Database Key:
32 .  -google_refresh_token XXX - where XXX was obtained from `PetscGoogleDriveAuthorize()`
33 
34    Level: intermediate
35 
36 .seealso: `PetscURLShorten()`, `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveUpload()`
37 @*/
38 PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm, const char refresh_token[], char access_token[], size_t tokensize)
39 {
40   SSL_CTX    *ctx;
41   SSL        *ssl;
42   int         sock;
43   char        buff[8 * 1024], body[1024];
44   PetscMPIInt rank;
45   char       *refreshtoken = (char *)refresh_token;
46   PetscBool   found;
47 
48   PetscFunctionBegin;
49   PetscCallMPI(MPI_Comm_rank(comm, &rank));
50   if (rank == 0) {
51     if (!refresh_token) {
52       PetscBool set;
53       PetscCall(PetscMalloc1(512, &refreshtoken));
54       PetscCall(PetscOptionsGetString(NULL, NULL, "-google_refresh_token", refreshtoken, sizeof(refreshtoken), &set));
55       if (!set) {
56         PetscCall(PetscGoogleDriveAuthorize(comm, access_token, refreshtoken, 512 * sizeof(char)));
57         PetscCall(PetscFree(refreshtoken));
58         PetscFunctionReturn(PETSC_SUCCESS);
59       }
60     }
61     PetscCall(PetscSSLInitializeContext(&ctx));
62     PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl));
63     PetscCall(PetscStrncpy(body, "client_id=", sizeof(body)));
64     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ID, sizeof(body)));
65     PetscCall(PetscStrlcat(body, "&client_secret=", sizeof(body)));
66     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ST, sizeof(body)));
67     PetscCall(PetscStrlcat(body, "&refresh_token=", sizeof(body)));
68     PetscCall(PetscStrlcat(body, refreshtoken, sizeof(body)));
69     if (!refresh_token) PetscCall(PetscFree(refreshtoken));
70     PetscCall(PetscStrlcat(body, "&grant_type=refresh_token", sizeof(body)));
71 
72     PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff)));
73     PetscCall(PetscSSLDestroyContext(ctx));
74     close(sock);
75 
76     PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found));
77     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token");
78   }
79   PetscFunctionReturn(PETSC_SUCCESS);
80 }
81 
82 #include <sys/stat.h>
83 
84 /*@C
85      PetscGoogleDriveUpload - Loads a file to the Google Drive
86 
87      Not collective, only the first process in the `MPI_Comm` uploads the file
88 
89   Input Parameters:
90 +   comm - MPI communicator
91 .   access_token - obtained with PetscGoogleDriveRefresh(), pass NULL to have PETSc generate one
92 -   filename - file to upload; if you upload multiple times it will have different names each time on Google Drive
93 
94   Options Database Key:
95 .  -google_refresh_token XXX - pass the access token for the operation
96 
97   Usage Patterns:
98 .vb
99     With PETSc option -google_refresh_token  XXX given
100     PetscGoogleDriveUpload(comm,NULL,filename);        will upload file with no user interaction
101 
102     Without PETSc option -google_refresh_token XXX given
103     PetscGoogleDriveUpload(comm,NULL,filename);        for first use will prompt user to authorize access to Google Drive with their browser
104 
105     With PETSc option -google_refresh_token  XXX given
106     PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token));
107     PetscGoogleDriveUpload(comm,access_token,filename);
108 
109     With refresh token entered in some way by the user
110     PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token));
111     PetscGoogleDriveUpload(comm,access_token,filename);
112 
113     PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token));
114     PetscGoogleDriveUpload(comm,access_token,filename);
115 .ve
116 
117    Level: intermediate
118 
119 .seealso: `PetscURLShorten()`, `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveRefresh()`
120 @*/
121 PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm, const char access_token[], const char filename[])
122 {
123   SSL_CTX    *ctx;
124   SSL        *ssl;
125   int         sock;
126   char        head[1024], buff[8 * 1024], *body, *title;
127   PetscMPIInt rank;
128   struct stat sb;
129   size_t      len, blen, rd;
130   FILE       *fd;
131   int         err;
132 
133   PetscFunctionBegin;
134   PetscCallMPI(MPI_Comm_rank(comm, &rank));
135   if (rank == 0) {
136     PetscCall(PetscStrncpy(head, "Authorization: Bearer ", sizeof(head)));
137     PetscCall(PetscStrlcat(head, access_token, sizeof(head)));
138     PetscCall(PetscStrlcat(head, "\r\n", sizeof(head)));
139     PetscCall(PetscStrlcat(head, "uploadType: multipart\r\n", sizeof(head)));
140 
141     err = stat(filename, &sb);
142     PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to stat file: %s", filename);
143     len = 1024 + sb.st_size;
144     PetscCall(PetscMalloc1(len, &body));
145     PetscCall(PetscStrncpy(body,
146                            "--foo_bar_baz\r\n"
147                            "Content-Type: application/json\r\n\r\n"
148                            "{",
149                            sizeof(body)));
150     PetscCall(PetscPushJSONValue(body, "title", filename, len));
151     PetscCall(PetscStrlcat(body, ",", sizeof(body)));
152     PetscCall(PetscPushJSONValue(body, "mimeType", "text.html", len));
153     PetscCall(PetscStrlcat(body, ",", sizeof(body)));
154     PetscCall(PetscPushJSONValue(body, "description", "a file", len));
155     PetscCall(PetscStrlcat(body,
156                            "}\r\n\r\n"
157                            "--foo_bar_baz\r\n"
158                            "Content-Type: text/html\r\n\r\n",
159                            sizeof(body)));
160     PetscCall(PetscStrlen(body, &blen));
161     fd = fopen(filename, "r");
162     PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open file: %s", filename);
163     rd = fread(body + blen, sizeof(unsigned char), sb.st_size, fd);
164     PetscCheck(rd == (size_t)sb.st_size, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to read entire file: %s %d %d", filename, (int)rd, (int)sb.st_size);
165     fclose(fd);
166     body[blen + rd] = 0;
167     PetscCall(PetscStrlcat(body,
168                            "\r\n\r\n"
169                            "--foo_bar_baz\r\n",
170                            sizeof(body)));
171     PetscCall(PetscSSLInitializeContext(&ctx));
172     PetscCall(PetscHTTPSConnect("www.googleapis.com", 443, ctx, &sock, &ssl));
173     PetscCall(PetscHTTPSRequest("POST", "www.googleapis.com/upload/drive/v2/files/", head, "multipart/related; boundary=\"foo_bar_baz\"", body, ssl, buff, sizeof(buff)));
174     PetscCall(PetscFree(body));
175     PetscCall(PetscSSLDestroyContext(ctx));
176     close(sock);
177     PetscCall(PetscStrstr(buff, "\"title\"", &title));
178     PetscCheck(title, PETSC_COMM_SELF, PETSC_ERR_LIB, "Upload of file %s failed", filename);
179   }
180   PetscFunctionReturn(PETSC_SUCCESS);
181 }
182 
183 #if defined(PETSC_HAVE_UNISTD_H)
184   #include <unistd.h>
185 #endif
186 
187 /*@C
188      PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc
189 
190    Not collective, only the first process in `MPI_Comm` does anything
191 
192    Input Parameters:
193 +  comm - the MPI communicator
194 -  tokensize - size of the token arrays
195 
196    Output Parameters:
197 +  access_token - can be used with `PetscGoogleDriveUpload()` for this one session
198 -  refresh_token - can be used for ever to obtain new access_tokens with `PetscGoogleDriveRefresh()`, guard this like a password
199                    it gives access to your Google Drive
200 
201    Notes:
202     This call requires stdout and stdin access from process 0 on the MPI communicator
203 
204    You can run src/sys/webclient/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to
205    PETSc programs with -google_refresh_token XXX
206 
207    Level: intermediate
208 
209 .seealso: `PetscGoogleDriveRefresh()`, `PetscGoogleDriveUpload()`, `PetscURLShorten()`
210 @*/
211 PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm, char access_token[], char refresh_token[], size_t tokensize)
212 {
213   SSL_CTX    *ctx;
214   SSL        *ssl;
215   int         sock;
216   char        buff[8 * 1024], *ptr, body[1024];
217   PetscMPIInt rank;
218   size_t      len;
219   PetscBool   found;
220 
221   PetscFunctionBegin;
222   PetscCallMPI(MPI_Comm_rank(comm, &rank));
223   if (rank == 0) {
224     PetscCheck(isatty(fileno(PETSC_STDOUT)), PETSC_COMM_SELF, PETSC_ERR_USER, "Requires users input/output");
225     PetscCall(PetscPrintf(comm, "Cut and paste the following into your browser:\n\n"
226                                 "https://accounts.google.com/o/oauth2/auth?"
227                                 "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&"
228                                 "redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
229                                 "response_type=code&"
230                                 "client_id=" PETSC_GOOGLE_CLIENT_ID "\n\n"));
231     PetscCall(PetscPrintf(comm, "Paste the result here:"));
232     ptr = fgets(buff, 1024, stdin);
233     PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno);
234     PetscCall(PetscStrlen(buff, &len));
235     buff[len - 1] = 0; /* remove carriage return at end of line */
236 
237     PetscCall(PetscSSLInitializeContext(&ctx));
238     PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl));
239     PetscCall(PetscStrncpy(body, "code=", sizeof(body)));
240     PetscCall(PetscStrlcat(body, buff, sizeof(body)));
241     PetscCall(PetscStrlcat(body, "&client_id=", sizeof(body)));
242     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ID, sizeof(body)));
243     PetscCall(PetscStrlcat(body, "&client_secret=", sizeof(body)));
244     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ST, sizeof(body)));
245     PetscCall(PetscStrlcat(body, "&redirect_uri=urn:ietf:wg:oauth:2.0:oob&", sizeof(body)));
246     PetscCall(PetscStrlcat(body, "grant_type=authorization_code", sizeof(body)));
247 
248     PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff)));
249     PetscCall(PetscSSLDestroyContext(ctx));
250     close(sock);
251 
252     PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found));
253     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token");
254     PetscCall(PetscPullJSONValue(buff, "refresh_token", refresh_token, tokensize, &found));
255     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return refresh_token");
256 
257     PetscCall(PetscPrintf(comm, "Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n"));
258     PetscCall(PetscPrintf(comm, "programs with the option -google_refresh_token %s\n", refresh_token));
259     PetscCall(PetscPrintf(comm, "to access Google Drive automatically\n"));
260   }
261   PetscFunctionReturn(PETSC_SUCCESS);
262 }
263 
264 /*@C
265      PetscURLShorten - Uses Google's service to get a short url for a long url
266 
267     Input Parameters:
268 +    url - long URL you want shortened
269 -    lenshorturl - length of buffer to contain short URL
270 
271     Output Parameter:
272 .    shorturl - the shortened URL
273 
274    Level: intermediate
275 
276    Note:
277    Google no longer provides this service so this routine will no longer function
278 
279 .seealso: `PetscGoogleDriveRefresh()`, `PetscGoogleDriveUpload()`, `PetscGoogleDriveAuthorize()`
280 @*/
281 PetscErrorCode PetscURLShorten(const char url[], char shorturl[], size_t lenshorturl)
282 {
283   SSL_CTX  *ctx;
284   SSL      *ssl;
285   int       sock;
286   char      buff[1024], body[512], post[1024];
287   PetscBool found;
288 
289   PetscFunctionBegin;
290   PetscCall(PetscSSLInitializeContext(&ctx));
291   PetscCall(PetscHTTPSConnect("www.googleapis.com", 443, ctx, &sock, &ssl));
292   PetscCall(PetscStrncpy(body, "{", sizeof(body)));
293   PetscCall(PetscPushJSONValue(body, "longUrl", url, sizeof(body) - 2));
294   PetscCall(PetscStrlcat(body, "}", sizeof(body)));
295   PetscCall(PetscSNPrintf(post, sizeof(post), "www.googleapis.com/urlshortener/v1/url?key=%s", PETSC_GOOGLE_API_KEY));
296   PetscCall(PetscHTTPSRequest("POST", post, NULL, "application/json", body, ssl, buff, sizeof(buff)));
297   PetscCall(PetscSSLDestroyContext(ctx));
298   close(sock);
299 
300   PetscCall(PetscPullJSONValue(buff, "id", shorturl, lenshorturl, &found));
301   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return short URL");
302   PetscFunctionReturn(PETSC_SUCCESS);
303 }
304