微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将没有加密的数据发送到 Twilio getToken 函数

如何解决将没有加密的数据发送到 Twilio getToken 函数

我们正在使用 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 发出请求。这样,您的参数就会通过网络加密。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。