问题描述
我们正在使用 Twilio CLIENT 从浏览器到电话号码进行语音通话。
在 Twilio 服务器端,我们基于本文构建了一个 getToken 函数: https://www.twilio.com/blog/generate-access-token-twilio-chat-video-voice-using-twilio-functions
在客户端,您认为发送没有加密的“identity”、“secretKey”、“accountSid”是否正确?安全条款?
这是推荐的方法吗?
这是'getToken'函数:
exports.handler = function(context,event,callback) {
let response = new Twilio.Response();
const identity = event.identity;
const secretKey = event.secretKey;
const accountSid = event.accountSid;
const twilioAccountSid = context.ACCOUNT_SID;
const twilioApiKey = context.API_KEY;
const twilioApiSecret = context.API_SECRET;
if (identity !== undefined && twilioApiKey !== undefined
&& secretKey !== undefined && twilioApiSecret !== undefined && secretKey === twilioApiSecret
&& accountSid !== undefined && twilioAccountSid !== undefined && accountSid === twilioAccountSid ) {
const Accesstoken = Twilio.jwt.Accesstoken;
const token = new Accesstoken(
twilioAccountSid,twilioApiKey,twilioApiSecret,{identity: identity}
);
const VoiceGrant = Accesstoken.VoiceGrant;
const voiceGrant = new VoiceGrant({
outgoingApplicationSid: context.TWIML_APP_SID,incomingallow: false
});
token.addGrant(voiceGrant);
let headers = {
"Access-Control-Allow-Origin": "*","Access-Control-Allow-Methods": "GET","Content-Type": "application/json"
};
response.setHeaders(headers);
response.setBody({
'token': token.toJwt()
});
response.setStatusCode(200);
} else {
response.setBody({
'mensaje': 'Unauthorized','codigo': 403
});
response.setStatusCode(403);
}
callback(null,response);
};
解决方法
这里是 Twilio 开发者布道者。
我完全不建议您在客户端公开您的密钥。
如果您想限制对这个端点的访问,那么我会首先限制您可以访问它的来源。现在,您使用 *
运算符允许来自所有来源的请求。
let headers = {
"Access-Control-Allow-Origin": "*","Access-Control-Allow-Methods": "GET","Content-Type": "application/json"
};
更新 Access-Control-Allow-Origin
标头以限制为您的域:
Access-Control-Allow-Origin: https://example.com
您也可以使用不同的密钥来保护端点,而不是来自您的 Twilio 帐户或 API 密钥的密钥。
exports.handler = function(context,event,callback) {
let response = new Twilio.Response();
const identity = event.identity;
const secret = event.secret;
const twilioAccountSid = context.ACCOUNT_SID;
const twilioApiKey = context.API_KEY;
const twilioApiSecret = context.API_SECRET;
const customSecret = context.CUSTOM_SECRET;
if (identity !== undefined && twilioApiKey !== undefined
&& twilioApiSecret !== undefined && twilioAccountSid !== undefined
&& secret !== undefined && customSecret !== undefined && secret === customSecret) {
// We're all good,create the token
} else {
// Unauthorised
}
callback(null,response);
};
另外,请注意,当您向 Twilio 函数发出请求时,它们默认启用 HTTPS,您应该使用 HTTPS 发出请求。这样,您的参数就会通过网络加密。