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