diff --git a/client/components/FnRouteForm.vue b/client/components/FnRouteForm.vue index 69005982..f1189a5e 100644 --- a/client/components/FnRouteForm.vue +++ b/client/components/FnRouteForm.vue @@ -13,6 +13,12 @@ +
+ +
+ +
+
diff --git a/client/components/FnRunFunction.vue b/client/components/FnRunFunction.vue index 0dc3c18f..6bb17686 100644 --- a/client/components/FnRunFunction.vue +++ b/client/components/FnRunFunction.vue @@ -13,16 +13,26 @@
+
+ +
+ +
+
- +
-
+
cURL command
curl -X POST -d '{{payload}}' {{apiUrl}}r/{{encodeURIComponent(this.app.name)}}/{{encodeURIComponent(this.route.path)}}
+
+
cURL command
+
curl -X POST -H 'Authorization: Bearer {{authToken}}' -d '{{payload}}' {{apiUrl}}r/{{encodeURIComponent(this.app.name)}}/{{encodeURIComponent(this.route.path)}}
+
Output
@@ -40,6 +50,7 @@ import Modal from '../lib/VueBootstrapModal.vue'; import { eventBus } from '../client'; import { defaultErrorHandler, getApiUrl } from '../lib/helpers'; +const jwt = require('jsonwebtoken'); export default { props: ['app'], @@ -52,6 +63,7 @@ export default { show: false, submitting: false, payload: '{}', + authToken: null, output: null, apiUrl: '' } @@ -71,6 +83,9 @@ export default { method: 'POST', data: JSON.stringify({payload: this.payload}), contentType: "application/json", + headers: { + 'X-Jwt-Key': this.route.jwt_key + }, dataType: 'json', success: (res) => { console.log("res", res); @@ -84,6 +99,9 @@ export default { } }) }, + calculateToken: function() { + this.authToken = jwt.sign(this.payload, this.route.jwt_key); + }, }, created: function (){ var t = this; @@ -92,6 +110,12 @@ export default { this.payload = '{}'; this.output = null; this.show = true; + if(this.route.jwt_key) { + this.calculateToken(); + } + }); + eventBus.$on('calculateToken', () => { + this.calculateToken(); }); getApiUrl( url => t.apiUrl = url ); } diff --git a/package.json b/package.json index 53a3b08e..2aafeb1a 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "express": "~4.13.4", "file-loader": "^0.8.5", "imports-loader": "^0.6.5", + "jsonwebtoken": "^8.1.1", "lodash": "^4.13.1", "request": "^2.72.0", "resolve-url-loader": "^1.4.3", diff --git a/server/controllers/routes.js b/server/controllers/routes.js index a803bb02..1442b8f5 100644 --- a/server/controllers/routes.js +++ b/server/controllers/routes.js @@ -1,5 +1,6 @@ var express = require('express'); var router = express.Router(); +var jwt = require('jsonwebtoken'); var helpers = require('../helpers/app-helpers.js'); router.get('/:app/routes', function(req, res) { @@ -62,8 +63,16 @@ router.post('/:app/routes/:route/run', function(req, res) { res.status(400).json({msg: text}); } var data = req.body.payload; + if (req.headers['x-jwt-key']) { + var token = jwt.sign(data, req.headers['x-jwt-key']); + var authHeader = {'Authorization': 'Bearer ' + token }; + } + else { + var authHeader = {}; + } + var path = "/r/" + encodeURIComponent(req.params.app)+ "/" + encodeURIComponent(req.params.route); + helpers.execApiEndpointRaw('POST', req, path, {}, data, successcb, errcb, authHeader); - helpers.execApiEndpointRaw('POST', req, "/r/" + encodeURIComponent(req.params.app)+ "/" + encodeURIComponent(req.params.route), {}, data, successcb, errcb); }); diff --git a/server/helpers/app-helpers.js b/server/helpers/app-helpers.js index baa79067..64b5e4be 100644 --- a/server/helpers/app-helpers.js +++ b/server/helpers/app-helpers.js @@ -30,11 +30,12 @@ exports.postApiEndpoint = function(req, path, params, postfields, successcb, err exports.execApiEndpoint('POST', req, path, params, postfields, successcb, errorcb); } -exports.execApiEndpoint = function(method, req, path, params, postfields, successcb, errorcb) { +exports.execApiEndpoint = function(method, req, path, params, postfields, successcb, errorcb, headers) { var options = { uri: exports.apiFullUrl(req, path), method: method, - json: postfields + json: postfields, + headers: headers }; console.log(options.method + " " + options.uri + ", params: ", options.json); @@ -42,11 +43,12 @@ exports.execApiEndpoint = function(method, req, path, params, postfields, succes request(options, function(error, response, body){exports.requrestCb(successcb, errorcb, error, response, body)}); } -exports.execApiEndpointRaw = function(method, req, path, params, postfields, successcb, errorcb) { +exports.execApiEndpointRaw = function(method, req, path, params, postfields, successcb, errorcb, headers) { var options = { uri: exports.apiFullUrl(req, path), method: method, - body: postfields + body: postfields, + headers: headers }; console.log(options.method + " " + options.uri + ", params: ", options.body);