10efc6a03SBarry Smith 20efc6a03SBarry Smith #include <petscwebclient.h> 3bb04b57dSBarry Smith #pragma clang diagnostic ignored "-Wdeprecated-declarations" 445e40e47SBarry Smith #pragma gcc diagnostic ignored "-Wdeprecated-declarations" 50efc6a03SBarry Smith 60efc6a03SBarry Smith /* 70efc6a03SBarry Smith These variables identify the code as a PETSc application to Google. 80efc6a03SBarry Smith 9a8d69d7bSBarry Smith See - https://stackoverflow.com/questions/4616553/using-oauth-in-free-open-source-software 100efc6a03SBarry Smith Users can get their own application IDs - https://code.google.com/p/google-apps-manager/wiki/GettingAnOAuthConsoleKey 110efc6a03SBarry Smith 120efc6a03SBarry Smith */ 130efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ID "521429262559-i19i57eek8tnt9ftpp4p91rcl0bo9ag5.apps.googleusercontent.com" 140efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ST "vOds_A71I3_S_aHMq_kZAI0t" 155708bc22SBarry Smith #define PETSC_GOOGLE_API_KEY "AIzaSyDRZsOcySpWVzsUvIBL2UG3J2tcg-MXbyk" 160efc6a03SBarry Smith 170efc6a03SBarry Smith /*@C 180efc6a03SBarry Smith PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token 190efc6a03SBarry Smith 20*811af0c4SBarry Smith Not collective, only the first process in the `MPI_Comm` does anything 210efc6a03SBarry Smith 220efc6a03SBarry Smith Input Parameters: 230efc6a03SBarry Smith + comm - MPI communicator 24*811af0c4SBarry Smith . refresh token - obtained with `PetscGoogleDriveAuthorize()`, if NULL PETSc will first look for one in the options data 25*811af0c4SBarry Smith if not found it will call `PetscGoogleDriveAuthorize()` 260efc6a03SBarry Smith - tokensize - size of the output string access_token 270efc6a03SBarry Smith 280efc6a03SBarry Smith Output Parameter: 29*811af0c4SBarry Smith . access_token - token that can be passed to `PetscGoogleDriveUpload()` 300efc6a03SBarry Smith 31*811af0c4SBarry Smith Options Database Key: 32*811af0c4SBarry Smith . -google_refresh_token XXX - where XXX was obtained from `PetscGoogleDriveAuthorize()` 334a285bdaSBarry Smith 342b26979fSBarry Smith Level: intermediate 354a285bdaSBarry Smith 36db781477SPatrick Sanan .seealso: `PetscURLShorten()`, `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveUpload()` 370efc6a03SBarry Smith @*/ 389371c9d4SSatish Balay PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm, const char refresh_token[], char access_token[], size_t tokensize) { 390efc6a03SBarry Smith SSL_CTX *ctx; 400efc6a03SBarry Smith SSL *ssl; 410efc6a03SBarry Smith int sock; 425dc0f0a4SBarry Smith char buff[8 * 1024], body[1024]; 430efc6a03SBarry Smith PetscMPIInt rank; 440efc6a03SBarry Smith char *refreshtoken = (char *)refresh_token; 455dc0f0a4SBarry Smith PetscBool found; 460efc6a03SBarry Smith 470efc6a03SBarry Smith PetscFunctionBegin; 489566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 49dd400576SPatrick Sanan if (rank == 0) { 500efc6a03SBarry Smith if (!refresh_token) { 510efc6a03SBarry Smith PetscBool set; 529566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(512, &refreshtoken)); 539566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-google_refresh_token", refreshtoken, sizeof(refreshtoken), &set)); 540efc6a03SBarry Smith if (!set) { 559566063dSJacob Faibussowitsch PetscCall(PetscGoogleDriveAuthorize(comm, access_token, refreshtoken, 512 * sizeof(char))); 569566063dSJacob Faibussowitsch PetscCall(PetscFree(refreshtoken)); 570efc6a03SBarry Smith PetscFunctionReturn(0); 580efc6a03SBarry Smith } 590efc6a03SBarry Smith } 609566063dSJacob Faibussowitsch PetscCall(PetscSSLInitializeContext(&ctx)); 619566063dSJacob Faibussowitsch PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl)); 629566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(body, "client_id=")); 639566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, PETSC_GOOGLE_CLIENT_ID)); 649566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, "&client_secret=")); 659566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, PETSC_GOOGLE_CLIENT_ST)); 669566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, "&refresh_token=")); 679566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, refreshtoken)); 689566063dSJacob Faibussowitsch if (!refresh_token) PetscCall(PetscFree(refreshtoken)); 699566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, "&grant_type=refresh_token")); 700efc6a03SBarry Smith 719566063dSJacob Faibussowitsch PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff))); 729566063dSJacob Faibussowitsch PetscCall(PetscSSLDestroyContext(ctx)); 730efc6a03SBarry Smith close(sock); 740efc6a03SBarry Smith 759566063dSJacob Faibussowitsch PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found)); 7628b400f6SJacob Faibussowitsch PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token"); 770efc6a03SBarry Smith } 780efc6a03SBarry Smith PetscFunctionReturn(0); 790efc6a03SBarry Smith } 800efc6a03SBarry Smith 810efc6a03SBarry Smith #include <sys/stat.h> 820efc6a03SBarry Smith 830efc6a03SBarry Smith /*@C 840efc6a03SBarry Smith PetscGoogleDriveUpload - Loads a file to the Google Drive 850efc6a03SBarry Smith 86*811af0c4SBarry Smith Not collective, only the first process in the `MPI_Comm` uploads the file 870efc6a03SBarry Smith 880efc6a03SBarry Smith Input Parameters: 890efc6a03SBarry Smith + comm - MPI communicator 900efc6a03SBarry Smith . access_token - obtained with PetscGoogleDriveRefresh(), pass NULL to have PETSc generate one 910efc6a03SBarry Smith - filename - file to upload; if you upload multiple times it will have different names each time on Google Drive 920efc6a03SBarry Smith 93*811af0c4SBarry Smith Options Database Key: 94147403d9SBarry Smith . -google_refresh_token XXX - pass the access token for the operation 950efc6a03SBarry Smith 960efc6a03SBarry Smith Usage Patterns: 97*811af0c4SBarry Smith .vb 984a285bdaSBarry Smith With PETSc option -google_refresh_token XXX given 990efc6a03SBarry Smith PetscGoogleDriveUpload(comm,NULL,filename); will upload file with no user interaction 1000efc6a03SBarry Smith 1014a285bdaSBarry Smith Without PETSc option -google_refresh_token XXX given 1024683183fSBarry Smith PetscGoogleDriveUpload(comm,NULL,filename); for first use will prompt user to authorize access to Google Drive with their browser 1030efc6a03SBarry Smith 1044a285bdaSBarry Smith With PETSc option -google_refresh_token XXX given 1050efc6a03SBarry Smith PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token)); 1060efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 1070efc6a03SBarry Smith 1080efc6a03SBarry Smith With refresh token entered in some way by the user 1090efc6a03SBarry Smith PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token)); 1100efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 1110efc6a03SBarry Smith 1120efc6a03SBarry Smith PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token)); 1130efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 114*811af0c4SBarry Smith .ve 1150efc6a03SBarry Smith 1162b26979fSBarry Smith Level: intermediate 1172b26979fSBarry Smith 118db781477SPatrick Sanan .seealso: `PetscURLShorten()`, `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveRefresh()` 1190efc6a03SBarry Smith @*/ 1209371c9d4SSatish Balay PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm, const char access_token[], const char filename[]) { 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) { 1349566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(head, "Authorization: Bearer ")); 1359566063dSJacob Faibussowitsch PetscCall(PetscStrcat(head, access_token)); 1369566063dSJacob Faibussowitsch PetscCall(PetscStrcat(head, "\r\n")); 1379566063dSJacob Faibussowitsch PetscCall(PetscStrcat(head, "uploadType: multipart\r\n")); 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)); 143d0609cedSBarry Smith PetscCall(PetscStrcpy(body, "--foo_bar_baz\r\n" 1440efc6a03SBarry Smith "Content-Type: application/json\r\n\r\n" 145d0609cedSBarry Smith "{")); 1469566063dSJacob Faibussowitsch PetscCall(PetscPushJSONValue(body, "title", filename, len)); 1479566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, ",")); 1489566063dSJacob Faibussowitsch PetscCall(PetscPushJSONValue(body, "mimeType", "text.html", len)); 1499566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, ",")); 1509566063dSJacob Faibussowitsch PetscCall(PetscPushJSONValue(body, "description", "a file", len)); 151d0609cedSBarry Smith PetscCall(PetscStrcat(body, "}\r\n\r\n" 1520efc6a03SBarry Smith "--foo_bar_baz\r\n" 153d0609cedSBarry Smith "Content-Type: text/html\r\n\r\n")); 1549566063dSJacob Faibussowitsch PetscCall(PetscStrlen(body, &blen)); 1550efc6a03SBarry Smith fd = fopen(filename, "r"); 15628b400f6SJacob Faibussowitsch PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open file: %s", filename); 1570efc6a03SBarry Smith rd = fread(body + blen, sizeof(unsigned char), sb.st_size, fd); 158d8174014SToby 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); 1590efc6a03SBarry Smith fclose(fd); 1600efc6a03SBarry Smith body[blen + rd] = 0; 161d0609cedSBarry Smith PetscCall(PetscStrcat(body, "\r\n\r\n" 162d0609cedSBarry Smith "--foo_bar_baz\r\n")); 1639566063dSJacob Faibussowitsch PetscCall(PetscSSLInitializeContext(&ctx)); 1649566063dSJacob Faibussowitsch PetscCall(PetscHTTPSConnect("www.googleapis.com", 443, ctx, &sock, &ssl)); 1659566063dSJacob Faibussowitsch PetscCall(PetscHTTPSRequest("POST", "www.googleapis.com/upload/drive/v2/files/", head, "multipart/related; boundary=\"foo_bar_baz\"", body, ssl, buff, sizeof(buff))); 1669566063dSJacob Faibussowitsch PetscCall(PetscFree(body)); 1679566063dSJacob Faibussowitsch PetscCall(PetscSSLDestroyContext(ctx)); 1680efc6a03SBarry Smith close(sock); 1699566063dSJacob Faibussowitsch PetscCall(PetscStrstr(buff, "\"title\"", &title)); 17028b400f6SJacob Faibussowitsch PetscCheck(title, PETSC_COMM_SELF, PETSC_ERR_LIB, "Upload of file %s failed", filename); 1710efc6a03SBarry Smith } 1720efc6a03SBarry Smith PetscFunctionReturn(0); 1730efc6a03SBarry Smith } 1740efc6a03SBarry Smith 17568e69593SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) 17668e69593SBarry Smith #include <unistd.h> 17768e69593SBarry Smith #endif 17868e69593SBarry Smith 1790efc6a03SBarry Smith /*@C 1800efc6a03SBarry Smith PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc 1810efc6a03SBarry Smith 182*811af0c4SBarry Smith Not collective, only the first process in `MPI_Comm` does anything 1830efc6a03SBarry Smith 1840efc6a03SBarry Smith Input Parameters: 1850efc6a03SBarry Smith + comm - the MPI communicator 1860efc6a03SBarry Smith - tokensize - size of the token arrays 1870efc6a03SBarry Smith 1880efc6a03SBarry Smith Output Parameters: 189*811af0c4SBarry Smith + access_token - can be used with `PetscGoogleDriveUpload()` for this one session 190*811af0c4SBarry Smith - refresh_token - can be used for ever to obtain new access_tokens with `PetscGoogleDriveRefresh()`, guard this like a password 1910efc6a03SBarry Smith it gives access to your Google Drive 1920efc6a03SBarry Smith 19395452b02SPatrick Sanan Notes: 19495452b02SPatrick Sanan This call requires stdout and stdin access from process 0 on the MPI communicator 1950efc6a03SBarry Smith 196c4762a1bSJed Brown You can run src/sys/webclient/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to 1974a285bdaSBarry Smith PETSc programs with -google_refresh_token XXX 1980efc6a03SBarry Smith 1992b26979fSBarry Smith Level: intermediate 2002b26979fSBarry Smith 201db781477SPatrick Sanan .seealso: `PetscGoogleDriveRefresh()`, `PetscGoogleDriveUpload()`, `PetscURLShorten()` 2020efc6a03SBarry Smith @*/ 2039371c9d4SSatish Balay PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm, char access_token[], char refresh_token[], size_t tokensize) { 2040efc6a03SBarry Smith SSL_CTX *ctx; 2050efc6a03SBarry Smith SSL *ssl; 2060efc6a03SBarry Smith int sock; 2075dc0f0a4SBarry Smith char buff[8 * 1024], *ptr, body[1024]; 2080efc6a03SBarry Smith PetscMPIInt rank; 2090efc6a03SBarry Smith size_t len; 2105dc0f0a4SBarry Smith PetscBool found; 2110efc6a03SBarry Smith 2120efc6a03SBarry Smith PetscFunctionBegin; 2139566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 214dd400576SPatrick Sanan if (rank == 0) { 215cc73adaaSBarry Smith PetscCheck(isatty(fileno(PETSC_STDOUT)), PETSC_COMM_SELF, PETSC_ERR_USER, "Requires users input/output"); 216d0609cedSBarry Smith PetscCall(PetscPrintf(comm, "Cut and paste the following into your browser:\n\n" 2170efc6a03SBarry Smith "https://accounts.google.com/o/oauth2/auth?" 2180efc6a03SBarry Smith "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&" 2190efc6a03SBarry Smith "redirect_uri=urn:ietf:wg:oauth:2.0:oob&" 2200efc6a03SBarry Smith "response_type=code&" 2219371c9d4SSatish Balay "client_id=" PETSC_GOOGLE_CLIENT_ID "\n\n")); 2229566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Paste the result here:")); 2230efc6a03SBarry Smith ptr = fgets(buff, 1024, stdin); 22428b400f6SJacob Faibussowitsch PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno); 2259566063dSJacob Faibussowitsch PetscCall(PetscStrlen(buff, &len)); 2260efc6a03SBarry Smith buff[len - 1] = 0; /* remove carriage return at end of line */ 2270efc6a03SBarry Smith 2289566063dSJacob Faibussowitsch PetscCall(PetscSSLInitializeContext(&ctx)); 2299566063dSJacob Faibussowitsch PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl)); 2309566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(body, "code=")); 2319566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, buff)); 2329566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, "&client_id=")); 2339566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, PETSC_GOOGLE_CLIENT_ID)); 2349566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, "&client_secret=")); 2359566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, PETSC_GOOGLE_CLIENT_ST)); 2369566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, "&redirect_uri=urn:ietf:wg:oauth:2.0:oob&")); 2379566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, "grant_type=authorization_code")); 2380efc6a03SBarry Smith 2399566063dSJacob Faibussowitsch PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff))); 2409566063dSJacob Faibussowitsch PetscCall(PetscSSLDestroyContext(ctx)); 2410efc6a03SBarry Smith close(sock); 2420efc6a03SBarry Smith 2439566063dSJacob Faibussowitsch PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found)); 24428b400f6SJacob Faibussowitsch PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token"); 2459566063dSJacob Faibussowitsch PetscCall(PetscPullJSONValue(buff, "refresh_token", refresh_token, tokensize, &found)); 24628b400f6SJacob Faibussowitsch PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return refresh_token"); 2470efc6a03SBarry Smith 2489566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n")); 2499566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "programs with the option -google_refresh_token %s\n", refresh_token)); 2509566063dSJacob Faibussowitsch PetscCall(PetscPrintf(comm, "to access Google Drive automatically\n")); 2510efc6a03SBarry Smith } 2520efc6a03SBarry Smith PetscFunctionReturn(0); 2530efc6a03SBarry Smith } 2540efc6a03SBarry Smith 2550efc6a03SBarry Smith /*@C 2560efc6a03SBarry Smith PetscURLShorten - Uses Google's service to get a short url for a long url 2570efc6a03SBarry Smith 2580efc6a03SBarry Smith Input Parameters: 2590efc6a03SBarry Smith + url - long URL you want shortened 2600efc6a03SBarry Smith - lenshorturl - length of buffer to contain short URL 2610efc6a03SBarry Smith 2620efc6a03SBarry Smith Output Parameter: 2630efc6a03SBarry Smith . shorturl - the shortened URL 2640efc6a03SBarry Smith 2652b26979fSBarry Smith Level: intermediate 2662b26979fSBarry Smith 267*811af0c4SBarry Smith Note: 268*811af0c4SBarry Smith Google no longer provides this service so this routine will no longer function 269*811af0c4SBarry Smith 270db781477SPatrick Sanan .seealso: `PetscGoogleDriveRefresh()`, `PetscGoogleDriveUpload()`, `PetscGoogleDriveAuthorize()` 2710efc6a03SBarry Smith @*/ 2729371c9d4SSatish Balay PetscErrorCode PetscURLShorten(const char url[], char shorturl[], size_t lenshorturl) { 2730efc6a03SBarry Smith SSL_CTX *ctx; 2740efc6a03SBarry Smith SSL *ssl; 2750efc6a03SBarry Smith int sock; 2765708bc22SBarry Smith char buff[1024], body[512], post[1024]; 2775dc0f0a4SBarry Smith PetscBool found; 2780efc6a03SBarry Smith 2790efc6a03SBarry Smith PetscFunctionBegin; 2809566063dSJacob Faibussowitsch PetscCall(PetscSSLInitializeContext(&ctx)); 2819566063dSJacob Faibussowitsch PetscCall(PetscHTTPSConnect("www.googleapis.com", 443, ctx, &sock, &ssl)); 2829566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(body, "{")); 2839566063dSJacob Faibussowitsch PetscCall(PetscPushJSONValue(body, "longUrl", url, sizeof(body) - 2)); 2849566063dSJacob Faibussowitsch PetscCall(PetscStrcat(body, "}")); 2859566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(post, sizeof(post), "www.googleapis.com/urlshortener/v1/url?key=%s", PETSC_GOOGLE_API_KEY)); 2869566063dSJacob Faibussowitsch PetscCall(PetscHTTPSRequest("POST", post, NULL, "application/json", body, ssl, buff, sizeof(buff))); 2879566063dSJacob Faibussowitsch PetscCall(PetscSSLDestroyContext(ctx)); 2880efc6a03SBarry Smith close(sock); 2895dc0f0a4SBarry Smith 2909566063dSJacob Faibussowitsch PetscCall(PetscPullJSONValue(buff, "id", shorturl, lenshorturl, &found)); 29128b400f6SJacob Faibussowitsch PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return short URL"); 2920efc6a03SBarry Smith PetscFunctionReturn(0); 2930efc6a03SBarry Smith } 294