Actual source code: google.c

petsc-3.6.1 2015-08-06
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"


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

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

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

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

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

 36:    Level: intermediate

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

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

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

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

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

 86: #include <sys/stat.h>

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

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

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

100:   Options Database:
101: .  -google_refresh_token   XXX

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

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

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

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

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

121:    Level: intermediate

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

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

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

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

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

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

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

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

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

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

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

207:    Level: intermediate

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

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

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

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

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

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

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


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

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

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

281:    Level: intermediate

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

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

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