xref: /petsc/src/sys/webclient/google.c (revision 697336901c45ac77e1fd620fe1fca906cf3f95c8)
1 
2 #include <petscwebclient.h>
3 PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN("-Wdeprecated-declarations")
4 
5 /*
6    These variables identify the code as a PETSc application to Google.
7 
8    See -   https://stackoverflow.com/questions/4616553/using-oauth-in-free-open-source-software
9    Users can get their own application IDs - https://code.google.com/p/google-apps-manager/wiki/GettingAnOAuthConsoleKey
10 
11 */
12 #define PETSC_GOOGLE_CLIENT_ID "521429262559-i19i57eek8tnt9ftpp4p91rcl0bo9ag5.apps.googleusercontent.com"
13 #define PETSC_GOOGLE_CLIENT_ST "vOds_A71I3_S_aHMq_kZAI0t"
14 #define PETSC_GOOGLE_API_KEY   "AIzaSyDRZsOcySpWVzsUvIBL2UG3J2tcg-MXbyk"
15 
16 /*@C
17   PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token
18 
19   Not Collective, only the first process in the `MPI_Comm` does anything
20 
21   Input Parameters:
22 + comm          - MPI communicator
23 . refresh_token - obtained with `PetscGoogleDriveAuthorize()`, if NULL PETSc will first look for one in the options data
24                     if not found it will call `PetscGoogleDriveAuthorize()`
25 - tokensize     - size of the output string access_token
26 
27   Output Parameter:
28 . access_token - token that can be passed to `PetscGoogleDriveUpload()`
29 
30   Options Database Key:
31 . -google_refresh_token XXX - where XXX was obtained from `PetscGoogleDriveAuthorize()`
32 
33   Level: intermediate
34 
35 .seealso: `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveUpload()`
36 @*/
37 PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm, const char refresh_token[], char access_token[], size_t tokensize)
38 {
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(PETSC_SUCCESS);
58       }
59     }
60     PetscCall(PetscSSLInitializeContext(&ctx));
61     PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl));
62     PetscCall(PetscStrncpy(body, "client_id=", sizeof(body)));
63     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ID, sizeof(body)));
64     PetscCall(PetscStrlcat(body, "&client_secret=", sizeof(body)));
65     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ST, sizeof(body)));
66     PetscCall(PetscStrlcat(body, "&refresh_token=", sizeof(body)));
67     PetscCall(PetscStrlcat(body, refreshtoken, sizeof(body)));
68     if (!refresh_token) PetscCall(PetscFree(refreshtoken));
69     PetscCall(PetscStrlcat(body, "&grant_type=refresh_token", sizeof(body)));
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(PETSC_SUCCESS);
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   Example Usage:
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: `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveRefresh()`
119 @*/
120 PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm, const char access_token[], const char filename[])
121 {
122   SSL_CTX    *ctx;
123   SSL        *ssl;
124   int         sock;
125   char        head[1024], buff[8 * 1024], *body, *title;
126   PetscMPIInt rank;
127   struct stat sb;
128   size_t      len, blen, rd;
129   FILE       *fd;
130   int         err;
131 
132   PetscFunctionBegin;
133   PetscCallMPI(MPI_Comm_rank(comm, &rank));
134   if (rank == 0) {
135     PetscCall(PetscStrncpy(head, "Authorization: Bearer ", sizeof(head)));
136     PetscCall(PetscStrlcat(head, access_token, sizeof(head)));
137     PetscCall(PetscStrlcat(head, "\r\n", sizeof(head)));
138     PetscCall(PetscStrlcat(head, "uploadType: multipart\r\n", sizeof(head)));
139 
140     err = stat(filename, &sb);
141     PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to stat file: %s", filename);
142     len = 1024 + sb.st_size;
143     PetscCall(PetscMalloc1(len, &body));
144     PetscCall(PetscStrncpy(body,
145                            "--foo_bar_baz\r\n"
146                            "Content-Type: application/json\r\n\r\n"
147                            "{",
148                            sizeof(body)));
149     PetscCall(PetscPushJSONValue(body, "title", filename, len));
150     PetscCall(PetscStrlcat(body, ",", sizeof(body)));
151     PetscCall(PetscPushJSONValue(body, "mimeType", "text.html", len));
152     PetscCall(PetscStrlcat(body, ",", sizeof(body)));
153     PetscCall(PetscPushJSONValue(body, "description", "a file", len));
154     PetscCall(PetscStrlcat(body,
155                            "}\r\n\r\n"
156                            "--foo_bar_baz\r\n"
157                            "Content-Type: text/html\r\n\r\n",
158                            sizeof(body)));
159     PetscCall(PetscStrlen(body, &blen));
160     fd = fopen(filename, "r");
161     PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open file: %s", filename);
162     rd = fread(body + blen, sizeof(unsigned char), sb.st_size, fd);
163     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);
164     fclose(fd);
165     body[blen + rd] = 0;
166     PetscCall(PetscStrlcat(body,
167                            "\r\n\r\n"
168                            "--foo_bar_baz\r\n",
169                            sizeof(body)));
170     PetscCall(PetscSSLInitializeContext(&ctx));
171     PetscCall(PetscHTTPSConnect("www.googleapis.com", 443, ctx, &sock, &ssl));
172     PetscCall(PetscHTTPSRequest("POST", "www.googleapis.com/upload/drive/v2/files/", head, "multipart/related; boundary=\"foo_bar_baz\"", body, ssl, buff, sizeof(buff)));
173     PetscCall(PetscFree(body));
174     PetscCall(PetscSSLDestroyContext(ctx));
175     close(sock);
176     PetscCall(PetscStrstr(buff, "\"title\"", &title));
177     PetscCheck(title, PETSC_COMM_SELF, PETSC_ERR_LIB, "Upload of file %s failed", filename);
178   }
179   PetscFunctionReturn(PETSC_SUCCESS);
180 }
181 
182 #if defined(PETSC_HAVE_UNISTD_H)
183   #include <unistd.h>
184 #endif
185 
186 /*@C
187   PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc
188 
189   Not Collective, only the first process in `MPI_Comm` does anything
190 
191   Input Parameters:
192 + comm      - the MPI communicator
193 - tokensize - size of the token arrays
194 
195   Output Parameters:
196 + access_token  - can be used with `PetscGoogleDriveUpload()` for this one session
197 - refresh_token - can be used for ever to obtain new access_tokens with `PetscGoogleDriveRefresh()`, guard this like a password
198                    it gives access to your Google Drive
199 
200   Level: intermediate
201 
202   Notes:
203   This call requires `stdout` and `stdin` access from process 0 on the MPI communicator
204 
205   You can run src/sys/webclient/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to
206   PETSc programs with `-google_refresh_token XXX`
207 
208 .seealso: `PetscGoogleDriveRefresh()`, `PetscGoogleDriveUpload()`
209 @*/
210 PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm, char access_token[], char refresh_token[], size_t tokensize)
211 {
212   SSL_CTX    *ctx;
213   SSL        *ssl;
214   int         sock;
215   char        buff[8 * 1024], *ptr, body[1024];
216   PetscMPIInt rank;
217   size_t      len;
218   PetscBool   found;
219 
220   PetscFunctionBegin;
221   PetscCallMPI(MPI_Comm_rank(comm, &rank));
222   if (rank == 0) {
223     PetscCheck(isatty(fileno(PETSC_STDOUT)), PETSC_COMM_SELF, PETSC_ERR_USER, "Requires users input/output");
224     PetscCall(PetscPrintf(comm, "Cut and paste the following into your browser:\n\n"
225                                 "https://accounts.google.com/o/oauth2/auth?"
226                                 "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&"
227                                 "redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
228                                 "response_type=code&"
229                                 "client_id=" PETSC_GOOGLE_CLIENT_ID "\n\n"));
230     PetscCall(PetscPrintf(comm, "Paste the result here:"));
231     ptr = fgets(buff, 1024, stdin);
232     PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno);
233     PetscCall(PetscStrlen(buff, &len));
234     buff[len - 1] = 0; /* remove carriage return at end of line */
235 
236     PetscCall(PetscSSLInitializeContext(&ctx));
237     PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl));
238     PetscCall(PetscStrncpy(body, "code=", sizeof(body)));
239     PetscCall(PetscStrlcat(body, buff, sizeof(body)));
240     PetscCall(PetscStrlcat(body, "&client_id=", sizeof(body)));
241     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ID, sizeof(body)));
242     PetscCall(PetscStrlcat(body, "&client_secret=", sizeof(body)));
243     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ST, sizeof(body)));
244     PetscCall(PetscStrlcat(body, "&redirect_uri=urn:ietf:wg:oauth:2.0:oob&", sizeof(body)));
245     PetscCall(PetscStrlcat(body, "grant_type=authorization_code", sizeof(body)));
246 
247     PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff)));
248     PetscCall(PetscSSLDestroyContext(ctx));
249     close(sock);
250 
251     PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found));
252     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token");
253     PetscCall(PetscPullJSONValue(buff, "refresh_token", refresh_token, tokensize, &found));
254     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return refresh_token");
255 
256     PetscCall(PetscPrintf(comm, "Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n"));
257     PetscCall(PetscPrintf(comm, "programs with the option -google_refresh_token %s\n", refresh_token));
258     PetscCall(PetscPrintf(comm, "to access Google Drive automatically\n"));
259   }
260   PetscFunctionReturn(PETSC_SUCCESS);
261 }
262