Search code examples
google-apps-scriptoauthjwtauthy

Building a JWT for Twilio's Authy using Apps Script


The No-PII user registration JWT for adding a user in Twilio's authy requires us to build a JWT from scratch.

I tried looking everywhere on how to get a JWT created using Google Apps Script but wasn't to find the right way to make that happen. It specifically needs to be of HS256 alg.

I require the final payload to look exactly like this -

// Example Payload
{
  "iss": "My Authy App",
  "iat": 1554395479,
  "exp": 1554395879,
  "context": {
    "custom_user_id": "3YgAIZklGPHmwpJfIC0PDy0E7l763OF3BHZo1p2xKhY",
    "authy_app_id": "1111111"
  }
}

// Example Header
{
  "alg": "HS256",
  "typ": "JWT"
}

Can someone please help me with this or perhaps point me to an appropriate article/documentation for this??


Solution

  • The general syntax for URL fetch with Google Apps Script is the following:

    var body={
      "iss": "My Authy App",
      "iat": 1554395479,
      "exp": 1554395879,
      "context": {
        "custom_user_id": "3YgAIZklGPHmwpJfIC0PDy0E7l763OF3BHZo1p2xKhY",
        "authy_app_id": "1111111"
      };
    var header={
      "alg": "HS256",
      "typ": "JWT"
    };
    var url='YOUR URL';
    var options={
      method: 'POST',
      headers: header,
      muteHttpExceptions: true,
      contentType: 'application/json',
      payload: JSON.stringify(body)
    };
    var response=UrlFetchApp.fetch(url, options);
    

    According to the documentation link you provided, you might need to provide an API key. In this case, you URL should be something like var url=basicURL+"apikey="+XXX

    I do not have a Twilio account to test it, but the sample provided above is the general procedure for Apps Script and you can find more references under the following links: