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