xref: /petsc/src/sys/webclient/google.c (revision e8e8640d1cb9a3a2f50c0c0d7b26e5c4d521e2e4)
10efc6a03SBarry Smith #include <petscwebclient.h>
21c7e414eSJacob Faibussowitsch PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN("-Wdeprecated-declarations")
30efc6a03SBarry Smith 
40efc6a03SBarry Smith /*
50efc6a03SBarry Smith    These variables identify the code as a PETSc application to Google.
60efc6a03SBarry Smith 
7a8d69d7bSBarry Smith    See -   https://stackoverflow.com/questions/4616553/using-oauth-in-free-open-source-software
80efc6a03SBarry Smith    Users can get their own application IDs - https://code.google.com/p/google-apps-manager/wiki/GettingAnOAuthConsoleKey
90efc6a03SBarry Smith 
100efc6a03SBarry Smith */
110efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ID "521429262559-i19i57eek8tnt9ftpp4p91rcl0bo9ag5.apps.googleusercontent.com"
120efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ST "vOds_A71I3_S_aHMq_kZAI0t"
135708bc22SBarry Smith #define PETSC_GOOGLE_API_KEY   "AIzaSyDRZsOcySpWVzsUvIBL2UG3J2tcg-MXbyk"
140efc6a03SBarry Smith 
150efc6a03SBarry Smith /*@C
160efc6a03SBarry Smith   PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token
170efc6a03SBarry Smith 
1820f4b53cSBarry Smith   Not Collective, only the first process in the `MPI_Comm` does anything
190efc6a03SBarry Smith 
200efc6a03SBarry Smith   Input Parameters:
210efc6a03SBarry Smith + comm          - MPI communicator
22aec76313SJacob Faibussowitsch . refresh_token - obtained with `PetscGoogleDriveAuthorize()`, if NULL PETSc will first look for one in the options data
23811af0c4SBarry Smith                     if not found it will call `PetscGoogleDriveAuthorize()`
240efc6a03SBarry Smith - tokensize     - size of the output string access_token
250efc6a03SBarry Smith 
260efc6a03SBarry Smith   Output Parameter:
27811af0c4SBarry Smith . access_token - token that can be passed to `PetscGoogleDriveUpload()`
280efc6a03SBarry Smith 
29811af0c4SBarry Smith   Options Database Key:
30811af0c4SBarry Smith . -google_refresh_token XXX - where XXX was obtained from `PetscGoogleDriveAuthorize()`
314a285bdaSBarry Smith 
322b26979fSBarry Smith   Level: intermediate
334a285bdaSBarry Smith 
34c30dda00SJacob Faibussowitsch .seealso: `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveUpload()`
350efc6a03SBarry Smith @*/
PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize)36d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm, const char refresh_token[], char access_token[], size_t tokensize)
37d71ae5a4SJacob Faibussowitsch {
380efc6a03SBarry Smith   SSL_CTX    *ctx;
390efc6a03SBarry Smith   SSL        *ssl;
400efc6a03SBarry Smith   int         sock;
415dc0f0a4SBarry Smith   char        buff[8 * 1024], body[1024];
420efc6a03SBarry Smith   PetscMPIInt rank;
430efc6a03SBarry Smith   char       *refreshtoken = (char *)refresh_token;
445dc0f0a4SBarry Smith   PetscBool   found;
450efc6a03SBarry Smith 
460efc6a03SBarry Smith   PetscFunctionBegin;
479566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
48dd400576SPatrick Sanan   if (rank == 0) {
490efc6a03SBarry Smith     if (!refresh_token) {
500efc6a03SBarry Smith       PetscBool set;
519566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(512, &refreshtoken));
529566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetString(NULL, NULL, "-google_refresh_token", refreshtoken, sizeof(refreshtoken), &set));
530efc6a03SBarry Smith       if (!set) {
549566063dSJacob Faibussowitsch         PetscCall(PetscGoogleDriveAuthorize(comm, access_token, refreshtoken, 512 * sizeof(char)));
559566063dSJacob Faibussowitsch         PetscCall(PetscFree(refreshtoken));
563ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
570efc6a03SBarry Smith       }
580efc6a03SBarry Smith     }
599566063dSJacob Faibussowitsch     PetscCall(PetscSSLInitializeContext(&ctx));
609566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl));
61c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(body, "client_id=", sizeof(body)));
62c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ID, sizeof(body)));
63c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&client_secret=", sizeof(body)));
64c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ST, sizeof(body)));
65c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&refresh_token=", sizeof(body)));
66c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, refreshtoken, sizeof(body)));
679566063dSJacob Faibussowitsch     if (!refresh_token) PetscCall(PetscFree(refreshtoken));
68c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&grant_type=refresh_token", sizeof(body)));
690efc6a03SBarry Smith 
709566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff)));
719566063dSJacob Faibussowitsch     PetscCall(PetscSSLDestroyContext(ctx));
720efc6a03SBarry Smith     close(sock);
730efc6a03SBarry Smith 
749566063dSJacob Faibussowitsch     PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found));
7528b400f6SJacob Faibussowitsch     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token");
760efc6a03SBarry Smith   }
773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
780efc6a03SBarry Smith }
790efc6a03SBarry Smith 
800efc6a03SBarry Smith #include <sys/stat.h>
810efc6a03SBarry Smith 
820efc6a03SBarry Smith /*@C
830efc6a03SBarry Smith   PetscGoogleDriveUpload - Loads a file to the Google Drive
840efc6a03SBarry Smith 
8520f4b53cSBarry Smith   Not Collective, only the first process in the `MPI_Comm` uploads the file
860efc6a03SBarry Smith 
870efc6a03SBarry Smith   Input Parameters:
880efc6a03SBarry Smith + comm         - MPI communicator
8920f4b53cSBarry Smith . access_token - obtained with PetscGoogleDriveRefresh(), pass `NULL` to have PETSc generate one
900efc6a03SBarry Smith - filename     - file to upload; if you upload multiple times it will have different names each time on Google Drive
910efc6a03SBarry Smith 
92811af0c4SBarry Smith   Options Database Key:
93147403d9SBarry Smith . -google_refresh_token XXX - pass the access token for the operation
940efc6a03SBarry Smith 
95*10450e9eSJacob Faibussowitsch   Example Usage:
96811af0c4SBarry Smith .vb
974a285bdaSBarry Smith     With PETSc option -google_refresh_token  XXX given
980efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,NULL,filename);        will upload file with no user interaction
990efc6a03SBarry Smith 
1004a285bdaSBarry Smith     Without PETSc option -google_refresh_token XXX given
1014683183fSBarry Smith     PetscGoogleDriveUpload(comm,NULL,filename);        for first use will prompt user to authorize access to Google Drive with their browser
1020efc6a03SBarry Smith 
1034a285bdaSBarry Smith     With PETSc option -google_refresh_token  XXX given
1040efc6a03SBarry Smith     PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token));
1050efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1060efc6a03SBarry Smith 
1070efc6a03SBarry Smith     With refresh token entered in some way by the user
1080efc6a03SBarry Smith     PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token));
1090efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1100efc6a03SBarry Smith 
1110efc6a03SBarry Smith     PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token));
1120efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
113811af0c4SBarry Smith .ve
1140efc6a03SBarry Smith 
1152b26979fSBarry Smith   Level: intermediate
1162b26979fSBarry Smith 
117c30dda00SJacob Faibussowitsch .seealso: `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveRefresh()`
1180efc6a03SBarry Smith @*/
PetscGoogleDriveUpload(MPI_Comm comm,const char access_token[],const char filename[])119d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm, const char access_token[], const char filename[])
120d71ae5a4SJacob Faibussowitsch {
1210efc6a03SBarry Smith   SSL_CTX    *ctx;
1220efc6a03SBarry Smith   SSL        *ssl;
1230efc6a03SBarry Smith   int         sock;
1240efc6a03SBarry Smith   char        head[1024], buff[8 * 1024], *body, *title;
1250efc6a03SBarry Smith   PetscMPIInt rank;
1260efc6a03SBarry Smith   struct stat sb;
1270efc6a03SBarry Smith   size_t      len, blen, rd;
1280efc6a03SBarry Smith   FILE       *fd;
1292da392ccSBarry Smith   int         err;
1300efc6a03SBarry Smith 
1310efc6a03SBarry Smith   PetscFunctionBegin;
1329566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
133dd400576SPatrick Sanan   if (rank == 0) {
134c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(head, "Authorization: Bearer ", sizeof(head)));
135c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(head, access_token, sizeof(head)));
136c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(head, "\r\n", sizeof(head)));
137c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(head, "uploadType: multipart\r\n", sizeof(head)));
1380efc6a03SBarry Smith 
1392da392ccSBarry Smith     err = stat(filename, &sb);
14028b400f6SJacob Faibussowitsch     PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to stat file: %s", filename);
1410efc6a03SBarry Smith     len = 1024 + sb.st_size;
1429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(len, &body));
143c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(body,
1440efc6a03SBarry Smith                            "--foo_bar_baz\r\n"
145c6a7a370SJeremy L Thompson                            "Content-Type: application/json\r\n\r\n"
146c6a7a370SJeremy L Thompson                            "{",
147c6a7a370SJeremy L Thompson                            sizeof(body)));
148c6a7a370SJeremy L Thompson     PetscCall(PetscPushJSONValue(body, "title", filename, len));
149c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, ",", sizeof(body)));
150c6a7a370SJeremy L Thompson     PetscCall(PetscPushJSONValue(body, "mimeType", "text.html", len));
151c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, ",", sizeof(body)));
152c6a7a370SJeremy L Thompson     PetscCall(PetscPushJSONValue(body, "description", "a file", len));
153c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body,
154c6a7a370SJeremy L Thompson                            "}\r\n\r\n"
155c6a7a370SJeremy L Thompson                            "--foo_bar_baz\r\n"
156c6a7a370SJeremy L Thompson                            "Content-Type: text/html\r\n\r\n",
157c6a7a370SJeremy L Thompson                            sizeof(body)));
1589566063dSJacob Faibussowitsch     PetscCall(PetscStrlen(body, &blen));
1590efc6a03SBarry Smith     fd = fopen(filename, "r");
16028b400f6SJacob Faibussowitsch     PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open file: %s", filename);
1610efc6a03SBarry Smith     rd = fread(body + blen, sizeof(unsigned char), sb.st_size, fd);
162d8174014SToby Isaac     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);
1630efc6a03SBarry Smith     fclose(fd);
1640efc6a03SBarry Smith     body[blen + rd] = 0;
165c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body,
166c6a7a370SJeremy L Thompson                            "\r\n\r\n"
167c6a7a370SJeremy L Thompson                            "--foo_bar_baz\r\n",
168c6a7a370SJeremy L Thompson                            sizeof(body)));
1699566063dSJacob Faibussowitsch     PetscCall(PetscSSLInitializeContext(&ctx));
1709566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSConnect("www.googleapis.com", 443, ctx, &sock, &ssl));
1719566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSRequest("POST", "www.googleapis.com/upload/drive/v2/files/", head, "multipart/related; boundary=\"foo_bar_baz\"", body, ssl, buff, sizeof(buff)));
1729566063dSJacob Faibussowitsch     PetscCall(PetscFree(body));
1739566063dSJacob Faibussowitsch     PetscCall(PetscSSLDestroyContext(ctx));
1740efc6a03SBarry Smith     close(sock);
1759566063dSJacob Faibussowitsch     PetscCall(PetscStrstr(buff, "\"title\"", &title));
17628b400f6SJacob Faibussowitsch     PetscCheck(title, PETSC_COMM_SELF, PETSC_ERR_LIB, "Upload of file %s failed", filename);
1770efc6a03SBarry Smith   }
1783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1790efc6a03SBarry Smith }
1800efc6a03SBarry Smith 
18168e69593SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
18268e69593SBarry Smith   #include <unistd.h>
18368e69593SBarry Smith #endif
18468e69593SBarry Smith 
1850efc6a03SBarry Smith /*@C
1860efc6a03SBarry Smith   PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc
1870efc6a03SBarry Smith 
18820f4b53cSBarry Smith   Not Collective, only the first process in `MPI_Comm` does anything
1890efc6a03SBarry Smith 
1900efc6a03SBarry Smith   Input Parameters:
1910efc6a03SBarry Smith + comm      - the MPI communicator
1920efc6a03SBarry Smith - tokensize - size of the token arrays
1930efc6a03SBarry Smith 
1940efc6a03SBarry Smith   Output Parameters:
195811af0c4SBarry Smith + access_token  - can be used with `PetscGoogleDriveUpload()` for this one session
196811af0c4SBarry Smith - refresh_token - can be used for ever to obtain new access_tokens with `PetscGoogleDriveRefresh()`, guard this like a password
1970efc6a03SBarry Smith                    it gives access to your Google Drive
1980efc6a03SBarry Smith 
19920f4b53cSBarry Smith   Level: intermediate
20020f4b53cSBarry Smith 
20195452b02SPatrick Sanan   Notes:
20220f4b53cSBarry Smith   This call requires `stdout` and `stdin` access from process 0 on the MPI communicator
2030efc6a03SBarry Smith 
204c4762a1bSJed Brown   You can run src/sys/webclient/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to
20520f4b53cSBarry Smith   PETSc programs with `-google_refresh_token XXX`
2062b26979fSBarry Smith 
207c30dda00SJacob Faibussowitsch .seealso: `PetscGoogleDriveRefresh()`, `PetscGoogleDriveUpload()`
2080efc6a03SBarry Smith @*/
PetscGoogleDriveAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize)209d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm, char access_token[], char refresh_token[], size_t tokensize)
210d71ae5a4SJacob Faibussowitsch {
2110efc6a03SBarry Smith   SSL_CTX    *ctx;
2120efc6a03SBarry Smith   SSL        *ssl;
2130efc6a03SBarry Smith   int         sock;
2145dc0f0a4SBarry Smith   char        buff[8 * 1024], *ptr, body[1024];
2150efc6a03SBarry Smith   PetscMPIInt rank;
2160efc6a03SBarry Smith   size_t      len;
2175dc0f0a4SBarry Smith   PetscBool   found;
2180efc6a03SBarry Smith 
2190efc6a03SBarry Smith   PetscFunctionBegin;
2209566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
221dd400576SPatrick Sanan   if (rank == 0) {
222cc73adaaSBarry Smith     PetscCheck(isatty(fileno(PETSC_STDOUT)), PETSC_COMM_SELF, PETSC_ERR_USER, "Requires users input/output");
223d0609cedSBarry Smith     PetscCall(PetscPrintf(comm, "Cut and paste the following into your browser:\n\n"
2240efc6a03SBarry Smith                                 "https://accounts.google.com/o/oauth2/auth?"
2250efc6a03SBarry Smith                                 "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&"
2260efc6a03SBarry Smith                                 "redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
2270efc6a03SBarry Smith                                 "response_type=code&"
2289371c9d4SSatish Balay                                 "client_id=" PETSC_GOOGLE_CLIENT_ID "\n\n"));
2299566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "Paste the result here:"));
2300efc6a03SBarry Smith     ptr = fgets(buff, 1024, stdin);
23128b400f6SJacob Faibussowitsch     PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno);
2329566063dSJacob Faibussowitsch     PetscCall(PetscStrlen(buff, &len));
2330efc6a03SBarry Smith     buff[len - 1] = 0; /* remove carriage return at end of line */
2340efc6a03SBarry Smith 
2359566063dSJacob Faibussowitsch     PetscCall(PetscSSLInitializeContext(&ctx));
2369566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl));
237c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(body, "code=", sizeof(body)));
238c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, buff, sizeof(body)));
239c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&client_id=", sizeof(body)));
240c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ID, sizeof(body)));
241c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&client_secret=", sizeof(body)));
242c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ST, sizeof(body)));
243c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&redirect_uri=urn:ietf:wg:oauth:2.0:oob&", sizeof(body)));
244c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "grant_type=authorization_code", sizeof(body)));
2450efc6a03SBarry Smith 
2469566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff)));
2479566063dSJacob Faibussowitsch     PetscCall(PetscSSLDestroyContext(ctx));
2480efc6a03SBarry Smith     close(sock);
2490efc6a03SBarry Smith 
2509566063dSJacob Faibussowitsch     PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found));
25128b400f6SJacob Faibussowitsch     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token");
2529566063dSJacob Faibussowitsch     PetscCall(PetscPullJSONValue(buff, "refresh_token", refresh_token, tokensize, &found));
25328b400f6SJacob Faibussowitsch     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return refresh_token");
2540efc6a03SBarry Smith 
2559566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n"));
2569566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "programs with the option -google_refresh_token %s\n", refresh_token));
2579566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "to access Google Drive automatically\n"));
2580efc6a03SBarry Smith   }
2593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2600efc6a03SBarry Smith }
261