Actual source code: google.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

  2:  #include <petscwebclient.h>
  3: #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  4: #pragma gcc diagnostic ignored "-Wdeprecated-declarations"

  6: /*
  7:    These variables identify the code as a PETSc application to Google.

  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

 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"


 18: /*@C
 19:      PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token

 21:    Not collective, only the first process in the MPI_Comm does anything

 23:    Input Parameters:
 24: +   comm - MPI communicator
 25: .   refresh token - obtained with PetscGoogleDriveAuthorize(), if NULL PETSc will first look for one in the options data 
 26:                     if not found it will call PetscGoogleDriveAuthorize()
 27: -   tokensize - size of the output string access_token

 29:    Output Parameter:
 30: .   access_token - token that can be passed to PetscGoogleDriveUpload()

 32:    Options Database:
 33: .  -google_refresh_token XXX   where XXX was obtained from PetscGoogleDriveAuthorize()

 35:    Level: intermediate

 37: .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveUpload()

 39: @*/
 40: PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize)
 41: {
 42:   SSL_CTX        *ctx;
 43:   SSL            *ssl;
 44:   int            sock;
 46:   char           buff[8*1024],body[1024];
 47:   PetscMPIInt    rank;
 48:   char           *refreshtoken = (char*)refresh_token;
 49:   PetscBool      found;

 52:   MPI_Comm_rank(comm,&rank);
 53:   if (!rank) {
 54:     if (!refresh_token) {
 55:       PetscBool set;
 56:       PetscMalloc1(512,&refreshtoken);
 57:       PetscOptionsGetString(NULL,NULL,"-google_refresh_token",refreshtoken,512,&set);
 58:       if (!set) {
 59:         PetscGoogleDriveAuthorize(comm,access_token,refreshtoken,512*sizeof(char));
 60:         PetscFree(refreshtoken);
 61:         return(0);
 62:       }
 63:     }
 64:     PetscSSLInitializeContext(&ctx);
 65:     PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);
 66:     PetscStrcpy(body,"client_id=");
 67:     PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);
 68:     PetscStrcat(body,"&client_secret=");
 69:     PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);
 70:     PetscStrcat(body,"&refresh_token=");
 71:     PetscStrcat(body,refreshtoken);
 72:     if (!refresh_token) {PetscFree(refreshtoken);}
 73:     PetscStrcat(body,"&grant_type=refresh_token");

 75:     PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));
 76:     PetscSSLDestroyContext(ctx);
 77:     close(sock);

 79:     PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);
 80:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token");
 81:   }
 82:   return(0);
 83: }

 85: #include <sys/stat.h>

 87: /*@C
 88:      PetscGoogleDriveUpload - Loads a file to the Google Drive

 90:      Not collective, only the first process in the MPI_Comm uploads the file

 92:   Input Parameters:
 93: +   comm - MPI communicator
 94: .   access_token - obtained with PetscGoogleDriveRefresh(), pass NULL to have PETSc generate one
 95: -   filename - file to upload; if you upload multiple times it will have different names each time on Google Drive

 97:   Options Database:
 98: .  -google_refresh_token   XXX

100:   Usage Patterns:
101:     With PETSc option -google_refresh_token  XXX given
102:     PetscGoogleDriveUpload(comm,NULL,filename);        will upload file with no user interaction

104:     Without PETSc option -google_refresh_token XXX given
105:     PetscGoogleDriveUpload(comm,NULL,filename);        for first use will prompt user to authorize access to Google Drive with their browser

107:     With PETSc option -google_refresh_token  XXX given
108:     PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token));
109:     PetscGoogleDriveUpload(comm,access_token,filename);

111:     With refresh token entered in some way by the user
112:     PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token));
113:     PetscGoogleDriveUpload(comm,access_token,filename);

115:     PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token));
116:     PetscGoogleDriveUpload(comm,access_token,filename);

118:    Level: intermediate

120: .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveRefresh()

122: @*/
123: PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm,const char access_token[],const char filename[])
124: {
125:   SSL_CTX        *ctx;
126:   SSL            *ssl;
127:   int            sock;
129:   char           head[1024],buff[8*1024],*body,*title;
130:   PetscMPIInt    rank;
131:   struct stat    sb;
132:   size_t         len,blen,rd;
133:   FILE           *fd;

136:   MPI_Comm_rank(comm,&rank);
137:   if (!rank) {
138:     PetscStrcpy(head,"Authorization: Bearer ");
139:     PetscStrcat(head,access_token);
140:     PetscStrcat(head,"\r\n");
141:     PetscStrcat(head,"uploadType: multipart\r\n");

143:     stat(filename,&sb);
144:     if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to stat file: %s",filename);
145:     len = 1024 + sb.st_size;
146:     PetscMalloc1(len,&body);
147:     PetscStrcpy(body,"--foo_bar_baz\r\n"
148:                             "Content-Type: application/json\r\n\r\n"
149:                             "{");
150:     PetscPushJSONValue(body,"title",filename,len);
151:     PetscStrcat(body,",");
152:     PetscPushJSONValue(body,"mimeType","text.html",len);
153:     PetscStrcat(body,",");
154:     PetscPushJSONValue(body,"description","a file",len);
155:     PetscStrcat(body,"}\r\n\r\n"
156:                             "--foo_bar_baz\r\n"
157:                             "Content-Type: text/html\r\n\r\n");
158:     PetscStrlen(body,&blen);
159:     fd = fopen (filename, "r");
160:     if (!fd) SETERRQ1(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:     if (rd != (size_t) sb.st_size) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to read entire file: %s %d %d",filename,(int)rd,sb.st_size);
163:     fclose(fd);
164:     body[blen + rd] = 0;
165:     PetscStrcat(body,"\r\n\r\n"
166:                             "--foo_bar_baz\r\n");
167:     PetscSSLInitializeContext(&ctx);
168:     PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);
169:     PetscHTTPSRequest("POST","www.googleapis.com/upload/drive/v2/files/",head,"multipart/related; boundary=\"foo_bar_baz\"",body,ssl,buff,sizeof(buff));
170:     PetscFree(body);
171:     PetscSSLDestroyContext(ctx);
172:     close(sock);
173:     PetscStrstr(buff,"\"title\"",&title);
174:     if (!title) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Upload of file %s failed",filename);
175:   }
176:   return(0);
177: }

179: #if defined(PETSC_HAVE_UNISTD_H)
180: #include <unistd.h>
181: #endif

183: /*@C
184:      PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc

186:    Not collective, only the first process in MPI_Comm does anything

188:    Input Parameters:
189: +  comm - the MPI communicator
190: -  tokensize - size of the token arrays

192:    Output Parameters:
193: +  access_token - can be used with PetscGoogleDriveUpload() for this one session
194: -  refresh_token - can be used for ever to obtain new access_tokens with PetscGoogleDriveRefresh(), guard this like a password
195:                    it gives access to your Google Drive

197:    Notes:
198:     This call requires stdout and stdin access from process 0 on the MPI communicator

200:    You can run src/sys/webclient/examples/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to
201:    PETSc programs with -google_refresh_token XXX

203:    Level: intermediate

205: .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten()

207: @*/
208: PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize)
209: {
210:   SSL_CTX        *ctx;
211:   SSL            *ssl;
212:   int            sock;
214:   char           buff[8*1024],*ptr,body[1024];
215:   PetscMPIInt    rank;
216:   size_t         len;
217:   PetscBool      found;

220:   MPI_Comm_rank(comm,&rank);
221:   if (!rank) {
222:     if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output");
223:     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="
229:                             PETSC_GOOGLE_CLIENT_ID
230:                             "\n\n");
231:     PetscPrintf(comm,"Paste the result here:");
232:     ptr  = fgets(buff, 1024, stdin);
233:     if (!ptr) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno);
234:     PetscStrlen(buff,&len);
235:     buff[len-1] = 0; /* remove carriage return at end of line */

237:     PetscSSLInitializeContext(&ctx);
238:     PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);
239:     PetscStrcpy(body,"code=");
240:     PetscStrcat(body,buff);
241:     PetscStrcat(body,"&client_id=");
242:     PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);
243:     PetscStrcat(body,"&client_secret=");
244:     PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);
245:     PetscStrcat(body,"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&");
246:     PetscStrcat(body,"grant_type=authorization_code");

248:     PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));
249:     PetscSSLDestroyContext(ctx);
250:     close(sock);

252:     PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);
253:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token");
254:     PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);
255:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return refresh_token");

257:     PetscPrintf(comm,"Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n");
258:     PetscPrintf(comm,"programs with the option -google_refresh_token %s\n",refresh_token);
259:     PetscPrintf(comm,"to access Google Drive automatically\n");
260:   }
261:   return(0);
262: }


265: /*@C
266:      PetscURLShorten - Uses Google's service to get a short url for a long url

268:     Input Parameters:
269: +    url - long URL you want shortened
270: -    lenshorturl - length of buffer to contain short URL

272:     Output Parameter:
273: .    shorturl - the shortened URL

275:    Level: intermediate

277: .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscGoogleDriveAuthorize()
278: @*/
279: PetscErrorCode PetscURLShorten(const char url[],char shorturl[],size_t lenshorturl)
280: {
281:   SSL_CTX        *ctx;
282:   SSL            *ssl;
283:   int            sock;
285:   char           buff[1024],body[512],post[1024];
286:   PetscBool      found;

289:   PetscSSLInitializeContext(&ctx);
290:   PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);
291:   PetscStrcpy(body,"{");
292:   PetscPushJSONValue(body,"longUrl",url,sizeof(body)-2);
293:   PetscStrcat(body,"}");
294:   PetscSNPrintf(post,sizeof(post),"www.googleapis.com/urlshortener/v1/url?key=%s",PETSC_GOOGLE_API_KEY);
295:   PetscHTTPSRequest("POST",post,NULL,"application/json",body,ssl,buff,sizeof(buff));
296:   PetscSSLDestroyContext(ctx);
297:   close(sock);

299:   PetscPullJSONValue(buff,"id",shorturl,lenshorturl,&found);
300:   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return short URL");
301:   return(0);
302: }