about summary refs log tree commit diff
diff options
context:
space:
mode:
authormaride <maride@darknebu.la>2018-09-08 03:07:20 +0200
committermaride <maride@darknebu.la>2018-09-08 03:07:20 +0200
commit61dd0e66d0be7c94779f3ecf52eb12b23324df1a (patch)
tree799f957af6fa5392fbd079ae5cf3152886fbaf72
parent475c5999b57135fefd6129c00e87d7f5aa9a75cc (diff)
Fix: really download the certificate, avoid Blobs
-rw-r--r--hosted/access.html12
-rw-r--r--src/http.go19
2 files changed, 17 insertions, 14 deletions
diff --git a/hosted/access.html b/hosted/access.html
index 119ac80..11e1fe5 100644
--- a/hosted/access.html
+++ b/hosted/access.html
@@ -42,7 +42,9 @@
                     Access to the challenge containers is provided via OpenVPN.
                     <hr>
                     <button class="btn btn-secondary" onclick="loadConfig()">Reload</button>
-                    <button class="btn btn-primary" onclick="downloadConfig()">Download</button>
+                    <a href="/api/getAccess?download=true">
+                        <button class="btn btn-primary">Download</button>
+                    </a>
                     <hr>
                     <pre><code id="config">Loading...</code></pre>
                 </div>
@@ -58,14 +60,6 @@
                 });
             }
 
-            function downloadConfig() {
-                $.get("/api/getAccess").done(function(data) {
-                    var result = jQuery.parseJSON(data);
-                    var configBlob = new Blob([result["credentials"]], {'type':'application/x-openvpn-config'});
-                    window.location = URL.createObjectURL(configBlob);
-                });
-            }
-
             $(document).ready(
                 function(){
                     loadConfig()
diff --git a/src/http.go b/src/http.go
index d1e3507..81d55b7 100644
--- a/src/http.go
+++ b/src/http.go
@@ -316,10 +316,19 @@ func getAccessHandler(w http.ResponseWriter, r *http.Request) {
 			errorString = err.Error()
 		}
 
-		jsonAnswer, _ := json.Marshal(map[string]string{
-			"error": errorString,
-			"credentials": credentials,
-		})
-		w.Write([]byte(jsonAnswer))
+		// Check if we are asked to prepare the config file as download...
+		if errorString == "" && r.URL.Query().Get("download") == "true" {
+			// We are asked to do so, and we didn't encounter an error either
+			w.Header().Set("Content-Disposition", "attachment; filename=config.ovpn")
+			w.Header().Set("Content-Type", "x-openvpn-config")
+			w.Write([]byte(credentials))
+		} else {
+			// Normal operation - provide JSON formatted data
+			jsonAnswer, _ := json.Marshal(map[string]string{
+				"error":       errorString,
+				"credentials": credentials,
+			})
+			w.Write([]byte(jsonAnswer))
+		}
 	}
 }