Enable CORS on a token generation function.

518 views
Skip to first unread message

Testman

unread,
Aug 10, 2017, 2:34:41 PM8/10/17
to Twilio Functions
I'm trying to use Twilio Functions instead of a current Express.JS server that we use to generate tokens. I've created the following function in Twilio functions:
exports.handler = function(context, event, callback) {
  const ClientCapability = require('twilio').jwt.ClientCapability;
let identity = "sampleIdentity";
  const capability = new ClientCapability({
           accountSid: context.ACCOUNT_SID,
           authToken: context.AUTH_TOKEN
        });
  capability.addScope(new ClientCapability.IncomingClientScope(identity));
  capability.addScope(new ClientCapability.OutgoingClientScope({
           applicationSid: context.TWILIO_TWIML_APP_SID
        }));
  callback(null, {identity: identity, token: capability.toJwt()});
};

But I'm receiving the error 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
I know that's because I haven't set the Access-Control-Allow-Origin to a specific domain / '*' but I am having trouble figuring out how to do that on a plain node.js function like this. I know how to set this in Express, but I can't find how to do it for this. Has anyone else done this?

Martin Amps

unread,
Aug 10, 2017, 3:09:55 PM8/10/17
to Twilio Functions
Hi!

Thanks for trying out Functions. You can absolutely set CORS headers, there's an example from the docs @ https://www.twilio.com/docs/api/runtime/functions/faq#how-do-i-send-cors-headers

exports.handler = function(context, event, callback) { const response = new Twilio.Response(); response.appendHeader('Access-Control-Allow-Origin', 'example.com'); response.appendHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); response.appendHeader('Access-Control-Allow-Headers', 'Content-Type'); callback(null, response);};

Let us know if you have any further questions!

Martin


Testman

unread,
Aug 10, 2017, 3:48:37 PM8/10/17
to Twilio Functions
Using this code, I can make my code no longer produce a CORS error, but I how to I return the token I generated and assign it to a local variable?<br>
My Client-Side code:
     $('#new-twilio').click(function(){
       
var toNum = this.value;
       
if(token == undefined) {
            $
.getJSON('https://my-twilio-function/endpoint').done(function(data){
                token
= data.token;
               
Twilio.Device.setup(token, {debug: true});
               
Twilio.Device.ready(function(device){
                   
Twilio.Device.connect({"PhoneNumber": toNum});  
               
});
           
}).fail(function(error){
                alert
("Failure!");
                alert
(JSON.stringify(error));
           
});
       
} else {
           
Twilio.Device.connect({"PhoneNumber": toNum});
       
}
   
});

my function code now:
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
  const ClientCapability = require('twilio').jwt.ClientCapability;
const response = new Twilio.Response();
  response.appendHeader('Access-Control-Allow-Origin', '*');
  response.appendHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
  let identity = "sampleIdentity";
  const capability = new ClientCapability({
      accountSid: context.ACCOUNT_SID,
      authToken: context.AUTH_TOKEN
    });
  capability.addScope(new ClientCapability.IncomingClientScope(identity));
  capability.addScope(new ClientCapability.OutgoingClientScope({
      applicationSid: context.TWILIO_TWIML_APP_SID
    }));
  console.log(capability.toJwt());
  callback(null, response, {identity: identity, token: capability.toJwt()});
};

I tried placing the token in the response headers, but I'm not able to assign it in the JQuery function.
Thanks so much for the help!

Martin Amps

unread,
Aug 10, 2017, 4:02:59 PM8/10/17
to Twilio Functions
Close!

Instead of the json being in the 3rd arg, make it

callback(null, response);

and add something like:

response.setBody({identity: identity, token: capability.toJwt()});
response
.appendHeader('Content-Type', 'application/json');


that should work for ya! Some relevant docs are here: https://www.twilio.com/docs/api/runtime/functions/environment

Testman

unread,
Aug 10, 2017, 4:11:57 PM8/10/17
to Twilio Functions
Thank you! It's working great now.

Martin Amps

unread,
Aug 10, 2017, 4:30:48 PM8/10/17
to Twilio Functions
Great news!
Reply all
Reply to author
Forward
0 new messages