/*@C PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc Not collective, only the first process in MPI_Comm does anything Input Parameters: + comm - the MPI communicator - tokensize - size of the token arrays Output Parameters: + access_token - can be used with PetscGoogleDriveUpload() for this one session - refresh_token - can be used for ever to obtain new access_tokens with PetscGoogleDriveRefresh(), guard this like a password it gives access to your Google Drive Notes: This call requires stdout and stdin access from process 0 on the MPI communicator You can run src/sys/webclient/examples/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to PETSc programs with -google_refresh_token XXX Level: intermediate .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten() @*/ PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize) { SSL_CTX *ctx; SSL *ssl; int sock; PetscErrorCode ierr; char buff[8*1024],*ptr,body[1024]; PetscMPIInt rank; size_t len; PetscBool found; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output"); ierr = PetscPrintf(comm,"Cut and paste the following into your browser:\n\n" "https://accounts.google.com/o/oauth2/auth?" "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&" "redirect_uri=urn:ietf:wg:oauth:2.0:oob&" "response_type=code&" "client_id=" PETSC_GOOGLE_CLIENT_ID "\n\n");CHKERRQ(ierr); ierr = PetscPrintf(comm,"Paste the result here:");CHKERRQ(ierr); ptr = fgets(buff, 1024, stdin); if (!ptr) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno); ierr = PetscStrlen(buff,&len);CHKERRQ(ierr); buff[len-1] = 0; /* remove carriage return at end of line */ ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); ierr = PetscStrcpy(body,"code=");CHKERRQ(ierr); ierr = PetscStrcat(body,buff);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_id=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); ierr = PetscStrcat(body,"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&");CHKERRQ(ierr); ierr = PetscStrcat(body,"grant_type=authorization_code");CHKERRQ(ierr); ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); close(sock); ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token"); ierr = PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return refresh_token"); ierr = PetscPrintf(comm,"Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n");CHKERRQ(ierr); ierr = PetscPrintf(comm,"programs with the option -google_refresh_token %s\n",refresh_token);CHKERRQ(ierr); ierr = PetscPrintf(comm,"to access Google Drive automatically\n");CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C PetscBoxRefresh - Get a new authorization token for accessing Box drive from PETSc from a refresh token Not collective, only the first process in the MPI_Comm does anything Input Parameters: + comm - MPI communicator . refresh token - obtained with PetscBoxAuthorize(), if NULL PETSc will first look for one in the options data if not found it will call PetscBoxAuthorize() - tokensize - size of the output string access_token Output Parameter: + access_token - token that can be passed to PetscBoxUpload() - new_refresh_token - the old refresh token is no longer valid, not this is different than Google where the same refresh_token is used forever Level: intermediate .seealso: PetscURLShorten(), PetscBoxAuthorize(), PetscBoxUpload() @*/ PetscErrorCode PetscBoxRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],char new_refresh_token[],size_t tokensize) { SSL_CTX *ctx; SSL *ssl; int sock; PetscErrorCode ierr; char buff[8*1024],body[1024]; PetscMPIInt rank; char *refreshtoken = (char*)refresh_token; PetscBool found; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { if (!refresh_token) { PetscBool set; ierr = PetscMalloc1(512,&refreshtoken);CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL,NULL,"-box_refresh_token",refreshtoken,512,&set);CHKERRQ(ierr); #if defined(PETSC_HAVE_SAWS) if (!set) { ierr = PetscBoxAuthorize(comm,access_token,new_refresh_token,512*sizeof(char));CHKERRQ(ierr); ierr = PetscFree(refreshtoken);CHKERRQ(ierr); PetscFunctionReturn(0); } #else if (!set) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Must provide refresh token with -box_refresh_token XXX"); #endif } ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); ierr = PetscHTTPSConnect("www.box.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); ierr = PetscStrcpy(body,"client_id=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_BOX_CLIENT_ID);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_BOX_CLIENT_ST);CHKERRQ(ierr); ierr = PetscStrcat(body,"&refresh_token=");CHKERRQ(ierr); ierr = PetscStrcat(body,refreshtoken);CHKERRQ(ierr); if (!refresh_token) {ierr = PetscFree(refreshtoken);CHKERRQ(ierr);} ierr = PetscStrcat(body,"&grant_type=refresh_token");CHKERRQ(ierr); ierr = PetscHTTPSRequest("POST","www.box.com/api/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); close(sock); ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return access token"); ierr = PetscPullJSONValue(buff,"refresh_token",new_refresh_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return refresh token"); ierr = PetscPrintf(comm,"Here is your new Box refresh token, save it in a save place, in the future you can run PETSc\n");CHKERRQ(ierr); ierr = PetscPrintf(comm,"programs with the option -box_refresh_token %s\n",new_refresh_token);CHKERRQ(ierr); ierr = PetscPrintf(comm,"to access Box Drive automatically\n");CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C PetscBoxAuthorize - Get authorization and refresh token for accessing Box drive from PETSc Not collective, only the first process in MPI_Comm does anything Input Parameters: + comm - the MPI communicator - tokensize - size of the token arrays Output Parameters: + access_token - can be used with PetscBoxUpload() for this one session - refresh_token - can be used for ever to obtain new access_tokens with PetscBoxRefresh(), guard this like a password it gives access to your Box Drive Notes: This call requires stdout and stdin access from process 0 on the MPI communicator You can run src/sys/webclient/examples/tutorials/boxobtainrefreshtoken to get a refresh token and then in the future pass it to PETSc programs with -box_refresh_token XXX This requires PETSc be installed using --with-saws or --download-saws Requires the user have created a self-signed ssl certificate with $ saws/CA.pl -newcert (using the passphrase of password) $ cat newkey.pem newcert.pem > sslclient.pem and put the resulting file in either the current directory (with the application) or in the home directory. This seems kind of silly but it was all I could figure out. Level: intermediate .seealso: PetscBoxRefresh(), PetscBoxUpload(), PetscURLShorten() @*/ PetscErrorCode PetscBoxAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize) { SSL_CTX *ctx; SSL *ssl; int sock; PetscErrorCode ierr; char buff[8*1024],body[1024]; PetscMPIInt rank; PetscBool flg,found; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output"); ierr = PetscPrintf(comm,"Cut and paste the following into your browser:\n\n" "https://www.box.com/api/oauth2/authorize?" "response_type=code&" "client_id=" PETSC_BOX_CLIENT_ID "&state=PETScState" "\n\n");CHKERRQ(ierr); ierr = PetscBoxStartWebServer_Private();CHKERRQ(ierr); ierr = PetscStrbeginswith((const char*)result,"state=PETScState&code=",&flg);CHKERRQ(ierr); if (!flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Did not get expected string from Box got %s",result); ierr = PetscStrncpy(buff,(const char*)result+22,sizeof(buff));CHKERRQ(ierr); ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); ierr = PetscHTTPSConnect("www.box.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); ierr = PetscStrcpy(body,"code=");CHKERRQ(ierr); ierr = PetscStrcat(body,buff);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_id=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_BOX_CLIENT_ID);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_BOX_CLIENT_ST);CHKERRQ(ierr); ierr = PetscStrcat(body,"&grant_type=authorization_code");CHKERRQ(ierr); ierr = PetscHTTPSRequest("POST","www.box.com/api/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); close(sock); ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return access token"); ierr = PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return refresh token"); ierr = PetscPrintf(comm,"Here is your Box refresh token, save it in a save place, in the future you can run PETSc\n");CHKERRQ(ierr); ierr = PetscPrintf(comm,"programs with the option -box_refresh_token %s\n",refresh_token);CHKERRQ(ierr); ierr = PetscPrintf(comm,"to access Box Drive automatically\n");CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C PetscURLShorten - Uses Google's service to get a short url for a long url Input Parameters: + url - long URL you want shortened - lenshorturl - length of buffer to contain short URL Output Parameter: . shorturl - the shortened URL .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscGoogleDriveAuthorize() @*/ PetscErrorCode PetscURLShorten(const char url[],char shorturl[],size_t lenshorturl) { SSL_CTX *ctx; SSL *ssl; int sock; PetscErrorCode ierr; char buff[1024],body[512]; PetscBool found; PetscFunctionBegin; ierr = PetscSSLInitializeContext(&ctx); CHKERRQ(ierr); ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl); CHKERRQ(ierr); ierr = PetscStrcpy(body,"{"); CHKERRQ(ierr); ierr = PetscPushJSONValue(body,"longUrl",url,sizeof(body)-2); CHKERRQ(ierr); ierr = PetscStrcat(body,"}"); CHKERRQ(ierr); ierr = PetscHTTPSRequest("POST","www.googleapis.com/urlshortener/v1/url",NULL,"application/json",body,ssl,buff,sizeof(buff)); CHKERRQ(ierr); ierr = PetscSSLDestroyContext(ctx); CHKERRQ(ierr); close(sock); ierr = PetscPullJSONValue(buff,"id",shorturl,lenshorturl,&found); CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return short URL"); PetscFunctionReturn(0); }
/*@C PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token Not collective, only the first process in the MPI_Comm does anything Input Parameters: + comm - MPI communicator . refresh token - obtained with PetscGoogleDriveAuthorize(), if NULL PETSc will first look for one in the options data if not found it will call PetscGoogleDriveAuthorize() - tokensize - size of the output string access_token Output Parameter: . access_token - token that can be passed to PetscGoogleDriveUpload() Options Database: . -google_refresh_token XXX where XXX was obtained from PetscGoogleDriveAuthorize() Level: intermediate .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveUpload() @*/ PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize) { SSL_CTX *ctx; SSL *ssl; int sock; PetscErrorCode ierr; char buff[8*1024],body[1024]; PetscMPIInt rank; char *refreshtoken = (char*)refresh_token; PetscBool found; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { if (!refresh_token) { PetscBool set; ierr = PetscMalloc1(512,&refreshtoken);CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL,NULL,"-google_refresh_token",refreshtoken,512,&set);CHKERRQ(ierr); if (!set) { ierr = PetscGoogleDriveAuthorize(comm,access_token,refreshtoken,512*sizeof(char));CHKERRQ(ierr); ierr = PetscFree(refreshtoken);CHKERRQ(ierr); PetscFunctionReturn(0); } } ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); ierr = PetscStrcpy(body,"client_id=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); ierr = PetscStrcat(body,"&refresh_token=");CHKERRQ(ierr); ierr = PetscStrcat(body,refreshtoken);CHKERRQ(ierr); if (!refresh_token) {ierr = PetscFree(refreshtoken);CHKERRQ(ierr);} ierr = PetscStrcat(body,"&grant_type=refresh_token");CHKERRQ(ierr); ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); close(sock); ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token"); } PetscFunctionReturn(0); }