gpt4 book ai didi

security - Azure 移动服务服务间通信

转载 作者:行者123 更新时间:2023-12-03 03:16:00 25 4
gpt4 key购买 nike

有没有办法通过内部 URL 从同一移动服务上的表拦截器脚本中调用自定义 api 脚本?

或者您是否始终必须使用公共(public)网址(https://.azure-mobile.net)。在本例中与 X-ZUMO-MASTER header 一起使用,因为它是服务到服务通信。自定义 api 只能从此脚本调用,而不能由外部应用程序或经过身份验证的用户调用。我想防止主 key 甚至通过加密 channel 离开服务器。

最佳答案

如果您位于不同的服务中,则需要使用公共(public) URL,并将要调用的 API 标记为您提到的“管理员”访问权限。

如果您想从同一服务中的表脚本调用自定义 API,那么您只需“要求”自定义 API 并将其作为常规 JS 函数进行调用即可。请注意,虽然 API 采用“请求”和“响应”参数,但这是 JavaScript,因此任何看起来像请求/响应的内容都可以工作(鸭子类型)。例如,如果我有一个名为“计算器”的自定义 API,定义如下:

exports.post = function(request, response) {
var x = request.body.x || request.param('x');
var y = request.body.y || request.param('y');
var op = request.body.op || request.body.operation || request.param('op');
calculateAndReturn(x, y, op, response);
};

exports.get = function(request, response) {
var x = request.param('x');
var y = request.param('y');
var op = request.param('op') || request.param('operator');
calculateAndReturn(x, y, op);
};

function calculateAndReturn(x, y, operator, response) {
var result = calculate(x, y, operator);
if (typeof result === 'undefined') {
response.send(400, { error: 'Invalid or missing parameters' });
} else {
response.send(statusCodes.OK, { result : result });
}
}

function calculate(x, y, operator) {
var undef = {}.a;

if (_isUndefined(x) || _isUndefined(y) || _isUndefined(operator)) {
return undef;
}

switch (operator) {
case '+':
case 'add':
return x + y;
case '-':
case 'sub':
return x - y;
case '*':
case 'mul':
return x * y;
case '/':
case 'div':
return x / y;
}

return undef;
}

function _isUndefined(x) {
return typeof x === 'undefined';
}

请注意,对于 POST 操作,它只需要请求中包含三个成员(x、y、op)的“body”参数,并且响应中调用的唯一函数是 send。我们可以通过将其所需的内容传递给计算器来从表格脚本中调用它:

function insert(item, user, request) {
var calculator = require('../api/calculator');
var quantity = item.quantity;
var unitPrice = item.unitPrice;
calculator.post({ body: { x: quantity, y: unitPrice, op: '*' } }, {
send: function(status, body) {
if (status === statusCodes.OK) {
item.totalPrice = body.result;
request.execute();
} else {
request.respond(status, body);
}
}
});
}

关于security - Azure 移动服务服务间通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26414544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com