簡體   English   中英

確保API令牌安全,是否可以在我的index.js中使用它?

[英]Securing API Token, is making it a var in my index.js possible?

所以我建立了一個HTML表單來與Slack交互。 目前,我的js代碼如下所示。

$("#submitemail").click(function(){
    $.post(
            "https://openpgh.slack.com/services/hooks/incoming-webhook?token=MY_SECRET_TOKEN",
            JSON.stringify({'text':'invite request from: '+$("#email").val(),'username':'Slack Inviter','icon_emoji':':raising_hand:'})
    ).success(function(){
                $("#email").val("");
            });
});

如果有人要從我的html文件中直接復制此權限,則可以運行控制台命令並更改JSON,並用大量廢話轟炸我的slack組,直到達到API調用限制。

我想知道的是,是否可以將其作為var存儲在index.js(我使用的是node.js模板)中,然后在html中調用它。

任何選擇或建議,我都非常感激。

我的結構是:

Slack App
|_node_modules
| |_express
|_public
| |_index.html
| |_node.svg (idk what this does)
|_.gitignore
|_app.json
|_index.js
|_package.json
|_procfile
|_README.md

我的index.js的代碼只是

var express = require('express');
var app = express();

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));

app.listen(app.get('port'), function() {
  console.log("Node app is running at localhost:" + app.get('port'))
});

如果您只希望它是一個基本模態,則w /一個按鈕可以單擊以執行表單並提取電子郵件,我可以輸入完整的html。

免責聲明:此代碼未經測試

您基本上將執行以下操作:

index.js(評論來解釋我添加的內容):

var express = require('express');
// install request module
var request = require('request');
var app = express();

// make a new route that you can call from the client side
app.get('/getSlackData', function(req, res) {

  //variable to hold your response from slack
  var slackResponse;

  //make the request to slack
  var slackUrl = "https://openpgh.slack.com/services/hooks/incoming-webhook?token=MY_SECRET_TOKEN""
  request(slackUrl, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      slackReponse = response;
    } else {
      console.log(error);
  });
  return slackResponse;
});

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));

app.listen(app.get('port'), function() {
  console.log("Node app is running at localhost:" + app.get('port'))
});

因此,我們添加了一條新路由,該路由基本上是可以從客戶端調用的API,它將返回從Slack獲得的JSON對象。 您幾乎可以將客戶端代碼保持不變,只需更改您要調用的路由即可:

$("#submitemail").click(function(){
$.post("/getSlackData",
    JSON.stringify({'text':'invite request from:'+$("#email").val(),'username':'Slack Inviter','icon_emoji':':raising_hand:'})
  ).success(function(){
    $("#email").val("");
  });
});

我希望我正確理解了您的問題,這至少應該使您指出正確的方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM