Actual source code: google.c

petsc-3.7.5 2017-01-01
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 -   http://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"


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

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

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

 31:    Output Parameter:
 32: .   access_token - token that can be passed to PetscGoogleDriveUpload()

 34:    Options Database:
 35: .  -google_refresh_token XXX   where XXX was obtained from PetscGoogleDriveAuthorize()

 37:    Level: intermediate

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

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

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

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

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

 87: #include <sys/stat.h>

 91: /*@C
 92:      PetscGoogleDriveUpload - Loads a file to the Google Drive

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

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

101:   Options Database:
102: .  -google_refresh_token   XXX

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

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

111:     With PETSc option -google_refresh_token  XXX given
112:     PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token));
113:     PetscGoogleDriveUpload(comm,access_token,filename);

115:     With refresh token entered in some way by the user
116:     PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token));
117:     PetscGoogleDriveUpload(comm,access_token,filename);

119:     PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token));
120:     PetscGoogleDriveUpload(comm,access_token,filename);

122:    Level: intermediate

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

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

140:   MPI_Comm_rank(comm,&rank);
141:   if (!rank) {
142:     PetscStrcpy(head,"Authorization: Bearer ");
143:     PetscStrcat(head,access_token);
144:     PetscStrcat(head,"\r\n");
145:     PetscStrcat(head,"uploadType: multipart\r\n");

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

183: #if defined(PETSC_HAVE_UNISTD_H)
184: #include <unistd.h>
185: #endif

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

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

194:    Input Parameters:
195: +  comm - the MPI communicator
196: -  tokensize - size of the token arrays

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

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

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

208:    Level: intermediate

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

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

225:   MPI_Comm_rank(comm,&rank);
226:   if (!rank) {
227:     if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output");
228:     PetscPrintf(comm,"Cut and paste the following into your browser:\n\n"
229:                             "https://accounts.google.com/o/oauth2/auth?"
230:                             "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&"
231:                             "redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
232:                             "response_type=code&"
233:                             "client_id="
234:                             PETSC_GOOGLE_CLIENT_ID
235:                             "\n\n");
236:     PetscPrintf(comm,"Paste the result here:");
237:     ptr  = fgets(buff, 1024, stdin);
238:     if (!ptr) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno);
239:     PetscStrlen(buff,&len);
240:     buff[len-1] = 0; /* remove carriage return at end of line */

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

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

257:     PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);
258:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token");
259:     PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);
260:     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return refresh_token");

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


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

275:     Input Parameters:
276: +    url - long URL you want shortened
277: -    lenshorturl - length of buffer to contain short URL

279:     Output Parameter:
280: .    shorturl - the shortened URL

282:    Level: intermediate

284: .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscGoogleDriveAuthorize()
285: @*/
286: PetscErrorCode PetscURLShorten(const char url[],char shorturl[],size_t lenshorturl)
287: {
288:   SSL_CTX        *ctx;
289:   SSL            *ssl;
290:   int            sock;
292:   char           buff[1024],body[512],post[1024];
293:   PetscBool      found;

296:   PetscSSLInitializeContext(&ctx);
297:   PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);
298:   PetscStrcpy(body,"{");
299:   PetscPushJSONValue(body,"longUrl",url,sizeof(body)-2);
300:   PetscStrcat(body,"}");
301:   PetscSNPrintf(post,sizeof(post),"www.googleapis.com/urlshortener/v1/url?key=%s",PETSC_GOOGLE_API_KEY);
302:   PetscHTTPSRequest("POST",post,NULL,"application/json",body,ssl,buff,sizeof(buff));
303:   PetscSSLDestroyContext(ctx);
304:   close(sock);

306:   PetscPullJSONValue(buff,"id",shorturl,lenshorturl,&found);
307:   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return short URL");
308:   return(0);
309: }