Fix OAuth2 pk12 key download prompt.
authorBen Thompson <ben@xtuple.com>
Mon, 9 Jun 2014 17:04:53 +0000 (13:04 -0400)
committerBen Thompson <ben@xtuple.com>
Mon, 9 Jun 2014 17:04:53 +0000 (13:04 -0400)
enyo-client/extensions/source/oauth2/client/models/oauth2.js
node-datasource/routes/generate_oauth_key.js

index 3b2fe3c..11cd7fd 100644 (file)
@@ -64,9 +64,36 @@ white:true*/
 
         options.success = function (model, resp, options) {
           if (status === XM.Model.READY_NEW && that.get("clientType") === 'jwt bearer') {
-            // download the private key
-            window.open(XT.getOrganizationPath() + '/oauth/generate-key?id=' + that.id,
-              '_newtab');
+            // Download the private key.
+
+            // The javascript download method avoids target = "_blank" and the need to
+            // reload the app to see the new client. Pop up blockers should not be an issue.
+            // @See: http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery/3749395#3749395
+            var downloadURL = function downloadURL(url) {
+              var hiddenIFrameID = 'hiddenDownloader',
+                iframe = document.getElementById(hiddenIFrameID);
+
+              if (iframe === null) {
+                iframe = document.createElement('iframe');
+                iframe.id = hiddenIFrameID;
+                iframe.style.display = 'none';
+                document.body.appendChild(iframe);
+              }
+
+              iframe.src = url;
+            };
+
+            downloadURL(XT.getOrganizationPath() + '/oauth/generate-key?id=' + that.id);
+
+            // This prompts for the download without using target = "_blank" and a new tab.
+            // It should avoid pop up blockers from preventing the download. It does warn about
+            // leaving the page, but it returns right back to the original path without needing to
+            // reload the app. Downside... it requires reloading the app to see the client just added.
+            //window.location.assign(XT.getOrganizationPath() + '/oauth/generate-key?id=' + that.id);
+
+            // This opens the download in a new tab and causes pop up blockers to block this action.
+            //window.open(XT.getOrganizationPath() + '/oauth/generate-key?id=' + that.id,
+            //  '_newtab');
           }
 
           if (success) { success(model, resp, options); }
index 31bdba3..44ad1a5 100644 (file)
@@ -28,7 +28,7 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
         res.send({isError: true, error: err});
       },
       convertToP12 = function (publicKey, privateKey) {
-        var filenamePrefix = path.join(process.cwd(), "lib/private/temp_" + id),
+        var filenamePrefix = path.join("/tmp/temp_" + id),
           publicKeyFilename = filenamePrefix + "_public_key.pem",
           csrFilename = filenamePrefix + "_csr.pem",
           certFilename = filenamePrefix + "_cert.pem",