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

Sending data without encription to Twilio getToken function

我们正在使用 Twilio CLIENT 从浏览器向 phone 号码发起 语音呼叫

在 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 开发人员布道者。

我完全不建议您在 client-side 中公开您的 secretKey。

如果您希望限制对此端点的访问,那么我将从限制您可以访问它的来源开始。现在您允许来自所有来源的请求,使用 * 运算符。

        let headers = {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "GET",
            "Content-Type": "application/json"
        };

更新 Access-Control-Allow-Origin header 以限制到您的域:

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