From a994a24bef9de981fd357a606aac7e69e7b422d5 Mon Sep 17 00:00:00 2001 From: Des Hartman Date: Thu, 12 Aug 2021 08:57:04 +1000 Subject: [PATCH 1/5] Added Sync Token Examples --- rest/access-tokens/sync-example/meta.json | 4 ++ .../sync-example/sync-example.3.x.js | 31 ++++++++++++++ .../sync-example/sync-example.5.x.cs | 41 +++++++++++++++++++ .../sync-example/sync-example.5.x.php | 38 +++++++++++++++++ .../sync-example/sync-example.5.x.rb | 30 ++++++++++++++ .../sync-example/sync-example.6.x.py | 26 ++++++++++++ .../sync-example/sync-example.8.x.java | 34 +++++++++++++++ 7 files changed, 204 insertions(+) create mode 100644 rest/access-tokens/sync-example/meta.json create mode 100644 rest/access-tokens/sync-example/sync-example.3.x.js create mode 100644 rest/access-tokens/sync-example/sync-example.5.x.cs create mode 100644 rest/access-tokens/sync-example/sync-example.5.x.php create mode 100644 rest/access-tokens/sync-example/sync-example.5.x.rb create mode 100644 rest/access-tokens/sync-example/sync-example.6.x.py create mode 100644 rest/access-tokens/sync-example/sync-example.8.x.java diff --git a/rest/access-tokens/sync-example/meta.json b/rest/access-tokens/sync-example/meta.json new file mode 100644 index 000000000..c9ce2fa76 --- /dev/null +++ b/rest/access-tokens/sync-example/meta.json @@ -0,0 +1,4 @@ +{ + "title": "Creating an Access Token (Sync)", + "type": "server" +} diff --git a/rest/access-tokens/sync-example/sync-example.3.x.js b/rest/access-tokens/sync-example/sync-example.3.x.js new file mode 100644 index 000000000..37a151d08 --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.3.x.js @@ -0,0 +1,31 @@ +const AccessToken = require('twilio').jwt.AccessToken; +const SyncGrant = AccessToken.SyncGrant; + +// Used when generating any kind of tokens +// To set up environmental variables, see http://twil.io/secure +const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID; +const twilioApiKey = process.env.TWILIO_API_KEY; +const twilioApiSecret = process.env.TWILIO_API_SECRET; + +// Used specifically for creating Sync tokens +const outgoingApplicationSid = 'APxxxxxxxxxxxxx'; +const identity = 'user'; + +// Create a "grant" which enables a client to use Sync as a given user +const syncGrant = new SyncGrant({ + outgoingApplicationSid: outgoingApplicationSid, + incomingAllow: true, // Optional: add to allow incoming calls +}); + +// Create an access token which we will sign and return to the client, +// containing the grant we just created +const token = new AccessToken( + twilioAccountSid, + twilioApiKey, + twilioApiSecret, + { identity: identity } +); +token.addGrant(syncGrant); + +// Serialize the token to a JWT string +console.log(token.toJwt()); diff --git a/rest/access-tokens/sync-example/sync-example.5.x.cs b/rest/access-tokens/sync-example/sync-example.5.x.cs new file mode 100644 index 000000000..823d2d749 --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.5.x.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using Twilio.Jwt.AccessToken; + +class Example +{ + static void Main(string[] args) + { + // These values are necessary for any access token + // To set up environmental variables, see http://twil.io/secure + const string twilioAccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"); + const string twilioApiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY"); + const string twilioApiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET"); + + // These are specific to Sync + const string outgoingApplicationSid = "APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; + const string identity = "user"; + + // Create a Sync grant for this token + var grant = new SyncGrant(); + grant.OutgoingApplicationSid = outgoingApplicationSid; + + // Optional: add to allow incoming calls + grant.IncomingAllow = true; + + var grants = new HashSet + { + { grant } + }; + + // Create an Access Token generator + var token = new Token( + twilioAccountSid, + twilioApiKey, + twilioApiSecret, + identity, + grants: grants); + + Console.WriteLine(token.ToJwt()); + } +} diff --git a/rest/access-tokens/sync-example/sync-example.5.x.php b/rest/access-tokens/sync-example/sync-example.5.x.php new file mode 100644 index 000000000..3748d1e06 --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.5.x.php @@ -0,0 +1,38 @@ +setOutgoingApplicationSid($outgoingApplicationSid); + +// Optional: add to allow incoming calls +$syncGrant->setIncomingAllow(true); + +// Add grant to token +$token->addGrant($syncGrant); + +// render token to string +echo $token->toJWT(); diff --git a/rest/access-tokens/sync-example/sync-example.5.x.rb b/rest/access-tokens/sync-example/sync-example.5.x.rb new file mode 100644 index 000000000..acd9ceba3 --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.5.x.rb @@ -0,0 +1,30 @@ +require 'twilio-ruby' + +# Required for any Twilio Access Token +# To set up environmental variables, see http://twil.io/secure +account_sid = ENV['TWILIO_ACCOUNT_SID'] +api_key = ENV['TWILIO_API_KEY'] +api_secret = ENV['TWILIO_API_KEY_SECRET'] + +# Required for Sync +outgoing_application_sid = 'APxxxxxxxxxxxx' +identity = 'user' + +# Create Sync grant for our token +grant = Twilio::JWT::AccessToken::SyncGrant.new +grant.outgoing_application_sid = outgoing_application_sid + +# Optional: add to allow incoming calls +grant.incoming_allow = true + +# Create an Access Token +token = Twilio::JWT::AccessToken.new( + account_sid, + api_key, + api_secret, + [grant], + identity: identity +) + +# Generate the token +puts token.to_jwt diff --git a/rest/access-tokens/sync-example/sync-example.6.x.py b/rest/access-tokens/sync-example/sync-example.6.x.py new file mode 100644 index 000000000..a7a31749f --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.6.x.py @@ -0,0 +1,26 @@ +import os +from twilio.jwt.access_token import AccessToken +from twilio.jwt.access_token.grants import SyncGrant + +# required for all twilio access tokens +# To set up environmental variables, see http://twil.io/secure +account_sid = os.environ['TWILIO_ACCOUNT_SID'] +api_key = os.environ['TWILIO_API_KEY'] +api_secret = os.environ['TWILIO_API_KEY_SECRET'] + +# required for Sync grant +outgoing_application_sid = 'APxxxxxxxxxxxxx' +identity = 'user' + +# Create access token with credentials +token = AccessToken(account_sid, api_key, api_secret, identity=identity) + +# Create a Sync grant and add to token +sync_grant = SyncGrant( + outgoing_application_sid=outgoing_application_sid, + incoming_allow=True, # Optional: add to allow incoming calls +) +token.add_grant(sync_grant) + +# Return token info as JSON +print(token.to_jwt()) diff --git a/rest/access-tokens/sync-example/sync-example.8.x.java b/rest/access-tokens/sync-example/sync-example.8.x.java new file mode 100644 index 000000000..d8807fdc5 --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.8.x.java @@ -0,0 +1,34 @@ +import com.twilio.jwt.accesstoken.AccessToken; +import com.twilio.jwt.accesstoken.SyncGrant; + +public class TokenGenerator { + + public static void main(String[] args) { + // Get your Account SID from https://twilio.com/console + // To set up environment variables, see http://twil.io/secure + // Required for all types of tokens + String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID"); + String twilioApiKey = System.getenv("TWILIO_API_KEY"); + String twilioApiSecret = System.getenv("TWILIO_API_SECRET"); + + // Required for Sync + String outgoingApplicationSid = System.getenv("TWILIO_APP_SID"); + String identity = "user"; + + // Create Sync grant + SyncGrant grant = new SyncGrant(); + grant.setOutgoingApplicationSid(outgoingApplicationSid); + + // Optional: add to allow incoming calls + grant.setIncomingAllow(true); + + // Create access token + AccessToken token = new AccessToken.Builder( + twilioAccountSid, + twilioApiKey, + twilioApiSecret + ).identity(identity).grant(grant).build(); + + System.out.println(token.toJwt()); + } +} From add26ecd164420051875a0440a55fdf4b350c4b9 Mon Sep 17 00:00:00 2001 From: Des Hartman Date: Thu, 12 Aug 2021 09:06:27 +1000 Subject: [PATCH 2/5] Removed outgoingApplicationSid --- rest/access-tokens/sync-example/sync-example.3.x.js | 1 - rest/access-tokens/sync-example/sync-example.5.x.cs | 1 - rest/access-tokens/sync-example/sync-example.5.x.php | 1 - rest/access-tokens/sync-example/sync-example.5.x.rb | 1 - rest/access-tokens/sync-example/sync-example.6.x.py | 1 - rest/access-tokens/sync-example/sync-example.8.x.java | 1 - 6 files changed, 6 deletions(-) diff --git a/rest/access-tokens/sync-example/sync-example.3.x.js b/rest/access-tokens/sync-example/sync-example.3.x.js index 37a151d08..db13deead 100644 --- a/rest/access-tokens/sync-example/sync-example.3.x.js +++ b/rest/access-tokens/sync-example/sync-example.3.x.js @@ -8,7 +8,6 @@ const twilioApiKey = process.env.TWILIO_API_KEY; const twilioApiSecret = process.env.TWILIO_API_SECRET; // Used specifically for creating Sync tokens -const outgoingApplicationSid = 'APxxxxxxxxxxxxx'; const identity = 'user'; // Create a "grant" which enables a client to use Sync as a given user diff --git a/rest/access-tokens/sync-example/sync-example.5.x.cs b/rest/access-tokens/sync-example/sync-example.5.x.cs index 823d2d749..a4e58f61a 100644 --- a/rest/access-tokens/sync-example/sync-example.5.x.cs +++ b/rest/access-tokens/sync-example/sync-example.5.x.cs @@ -13,7 +13,6 @@ static void Main(string[] args) const string twilioApiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET"); // These are specific to Sync - const string outgoingApplicationSid = "APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const string identity = "user"; // Create a Sync grant for this token diff --git a/rest/access-tokens/sync-example/sync-example.5.x.php b/rest/access-tokens/sync-example/sync-example.5.x.php index 3748d1e06..309c48938 100644 --- a/rest/access-tokens/sync-example/sync-example.5.x.php +++ b/rest/access-tokens/sync-example/sync-example.5.x.php @@ -11,7 +11,6 @@ $twilioApiSecret = getenv('TWILIO_API_KEY_SECRET'); // Required for Sync grant -$outgoingApplicationSid = 'APxxxxxxxxxxxx'; // An identifier for your app - can be anything you'd like $identity = "john_doe"; diff --git a/rest/access-tokens/sync-example/sync-example.5.x.rb b/rest/access-tokens/sync-example/sync-example.5.x.rb index acd9ceba3..4c5ca3124 100644 --- a/rest/access-tokens/sync-example/sync-example.5.x.rb +++ b/rest/access-tokens/sync-example/sync-example.5.x.rb @@ -12,7 +12,6 @@ # Create Sync grant for our token grant = Twilio::JWT::AccessToken::SyncGrant.new -grant.outgoing_application_sid = outgoing_application_sid # Optional: add to allow incoming calls grant.incoming_allow = true diff --git a/rest/access-tokens/sync-example/sync-example.6.x.py b/rest/access-tokens/sync-example/sync-example.6.x.py index a7a31749f..11726772a 100644 --- a/rest/access-tokens/sync-example/sync-example.6.x.py +++ b/rest/access-tokens/sync-example/sync-example.6.x.py @@ -9,7 +9,6 @@ api_secret = os.environ['TWILIO_API_KEY_SECRET'] # required for Sync grant -outgoing_application_sid = 'APxxxxxxxxxxxxx' identity = 'user' # Create access token with credentials diff --git a/rest/access-tokens/sync-example/sync-example.8.x.java b/rest/access-tokens/sync-example/sync-example.8.x.java index d8807fdc5..db89cb34b 100644 --- a/rest/access-tokens/sync-example/sync-example.8.x.java +++ b/rest/access-tokens/sync-example/sync-example.8.x.java @@ -12,7 +12,6 @@ public static void main(String[] args) { String twilioApiSecret = System.getenv("TWILIO_API_SECRET"); // Required for Sync - String outgoingApplicationSid = System.getenv("TWILIO_APP_SID"); String identity = "user"; // Create Sync grant From 223bc79964caabc766d2a0d22cec3e79b19b1c38 Mon Sep 17 00:00:00 2001 From: Des Hartman Date: Tue, 17 Aug 2021 10:10:15 +1000 Subject: [PATCH 3/5] Removed voice related grant components --- rest/access-tokens/sync-example/sync-example.3.x.js | 4 ++-- rest/access-tokens/sync-example/sync-example.5.x.cs | 6 +++--- rest/access-tokens/sync-example/sync-example.5.x.php | 8 +++++--- rest/access-tokens/sync-example/sync-example.5.x.rb | 6 ++++-- rest/access-tokens/sync-example/sync-example.6.x.py | 6 ++++-- rest/access-tokens/sync-example/sync-example.8.x.java | 9 +++++---- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/rest/access-tokens/sync-example/sync-example.3.x.js b/rest/access-tokens/sync-example/sync-example.3.x.js index db13deead..499774b6f 100644 --- a/rest/access-tokens/sync-example/sync-example.3.x.js +++ b/rest/access-tokens/sync-example/sync-example.3.x.js @@ -6,14 +6,14 @@ const SyncGrant = AccessToken.SyncGrant; const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID; const twilioApiKey = process.env.TWILIO_API_KEY; const twilioApiSecret = process.env.TWILIO_API_SECRET; +const twilioSyncService = process.env.TWILIO_SYNC_SERVICE_SID; // Used specifically for creating Sync tokens const identity = 'user'; // Create a "grant" which enables a client to use Sync as a given user const syncGrant = new SyncGrant({ - outgoingApplicationSid: outgoingApplicationSid, - incomingAllow: true, // Optional: add to allow incoming calls + serviceSid: twilioSyncService, }); // Create an access token which we will sign and return to the client, diff --git a/rest/access-tokens/sync-example/sync-example.5.x.cs b/rest/access-tokens/sync-example/sync-example.5.x.cs index a4e58f61a..195bc5b7a 100644 --- a/rest/access-tokens/sync-example/sync-example.5.x.cs +++ b/rest/access-tokens/sync-example/sync-example.5.x.cs @@ -11,16 +11,16 @@ static void Main(string[] args) const string twilioAccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"); const string twilioApiKey = Environment.GetEnvironmentVariable("TWILIO_API_KEY"); const string twilioApiSecret = Environment.GetEnvironmentVariable("TWILIO_API_SECRET"); + const string twilioSyncService = Environment.GetEnvironmentVariable("TWILIO_SYNC_SERVICE_SID"); // These are specific to Sync const string identity = "user"; // Create a Sync grant for this token var grant = new SyncGrant(); - grant.OutgoingApplicationSid = outgoingApplicationSid; - // Optional: add to allow incoming calls - grant.IncomingAllow = true; + // Create a "grant" which enables a client to use Sync as a given user + grant.serviceSid = twilioSyncService; var grants = new HashSet { diff --git a/rest/access-tokens/sync-example/sync-example.5.x.php b/rest/access-tokens/sync-example/sync-example.5.x.php index 309c48938..1ff9df30a 100644 --- a/rest/access-tokens/sync-example/sync-example.5.x.php +++ b/rest/access-tokens/sync-example/sync-example.5.x.php @@ -9,6 +9,8 @@ $twilioAccountSid = getenv('TWILIO_ACCOUNT_SID'); $twilioApiKey = getenv('TWILIO_API_KEY'); $twilioApiSecret = getenv('TWILIO_API_KEY_SECRET'); +$twilioSyncService = getenv("TWILIO_SYNC_SERVICE_SID"); + // Required for Sync grant // An identifier for your app - can be anything you'd like @@ -25,10 +27,10 @@ // Create Sync grant $syncGrant = new SyncGrant(); -$syncGrant->setOutgoingApplicationSid($outgoingApplicationSid); -// Optional: add to allow incoming calls -$syncGrant->setIncomingAllow(true); +// Create a "grant" which enables a client to use Sync as a given user +$syncGrant->setServiceSid($twilioSyncService); + // Add grant to token $token->addGrant($syncGrant); diff --git a/rest/access-tokens/sync-example/sync-example.5.x.rb b/rest/access-tokens/sync-example/sync-example.5.x.rb index 4c5ca3124..d65f4b166 100644 --- a/rest/access-tokens/sync-example/sync-example.5.x.rb +++ b/rest/access-tokens/sync-example/sync-example.5.x.rb @@ -5,6 +5,8 @@ account_sid = ENV['TWILIO_ACCOUNT_SID'] api_key = ENV['TWILIO_API_KEY'] api_secret = ENV['TWILIO_API_KEY_SECRET'] +twilioSyncService = ENV['TWILIO_SYNC_SERVICE_SID'] + # Required for Sync outgoing_application_sid = 'APxxxxxxxxxxxx' @@ -13,8 +15,8 @@ # Create Sync grant for our token grant = Twilio::JWT::AccessToken::SyncGrant.new -# Optional: add to allow incoming calls -grant.incoming_allow = true +# Create a "grant" which enables a client to use Sync as a given user +grant.serviceSid = twilioSyncService # Create an Access Token token = Twilio::JWT::AccessToken.new( diff --git a/rest/access-tokens/sync-example/sync-example.6.x.py b/rest/access-tokens/sync-example/sync-example.6.x.py index 11726772a..861580b53 100644 --- a/rest/access-tokens/sync-example/sync-example.6.x.py +++ b/rest/access-tokens/sync-example/sync-example.6.x.py @@ -7,6 +7,7 @@ account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_KEY_SECRET'] +twilioSyncService = os.environ[process.env.['TWILIO_SYNC_SERVICE_SID'] # required for Sync grant identity = 'user' @@ -16,9 +17,10 @@ # Create a Sync grant and add to token sync_grant = SyncGrant( - outgoing_application_sid=outgoing_application_sid, - incoming_allow=True, # Optional: add to allow incoming calls + # Create a "grant" which enables a client to use Sync as a given user + serviceSid=twilioSyncService ) + token.add_grant(sync_grant) # Return token info as JSON diff --git a/rest/access-tokens/sync-example/sync-example.8.x.java b/rest/access-tokens/sync-example/sync-example.8.x.java index db89cb34b..c814e848c 100644 --- a/rest/access-tokens/sync-example/sync-example.8.x.java +++ b/rest/access-tokens/sync-example/sync-example.8.x.java @@ -10,17 +10,18 @@ public static void main(String[] args) { String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID"); String twilioApiKey = System.getenv("TWILIO_API_KEY"); String twilioApiSecret = System.getenv("TWILIO_API_SECRET"); + String twilioSyncService = System.getenv("TWILIO_SYNC_SERVICE_SID"); + // Required for Sync String identity = "user"; // Create Sync grant SyncGrant grant = new SyncGrant(); - grant.setOutgoingApplicationSid(outgoingApplicationSid); - // Optional: add to allow incoming calls - grant.setIncomingAllow(true); - + // Create a "grant" which enables a client to use Sync as a given user + grant.setServiceSid(twilioSyncService); + // Create access token AccessToken token = new AccessToken.Builder( twilioAccountSid, From ba61cad9b5b3d4fafdfe87aae0aee423feb9208f Mon Sep 17 00:00:00 2001 From: Des Hartman Date: Wed, 18 Aug 2021 07:36:32 +1000 Subject: [PATCH 4/5] updated as per @anabenites suggestions --- rest/access-tokens/sync-example/sync-example.5.x.rb | 5 ++--- rest/access-tokens/sync-example/sync-example.6.x.py | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/rest/access-tokens/sync-example/sync-example.5.x.rb b/rest/access-tokens/sync-example/sync-example.5.x.rb index d65f4b166..6a121b7bf 100644 --- a/rest/access-tokens/sync-example/sync-example.5.x.rb +++ b/rest/access-tokens/sync-example/sync-example.5.x.rb @@ -5,18 +5,17 @@ account_sid = ENV['TWILIO_ACCOUNT_SID'] api_key = ENV['TWILIO_API_KEY'] api_secret = ENV['TWILIO_API_KEY_SECRET'] -twilioSyncService = ENV['TWILIO_SYNC_SERVICE_SID'] +twilio_sync_service = ENV['TWILIO_SYNC_SERVICE_SID'] # Required for Sync -outgoing_application_sid = 'APxxxxxxxxxxxx' identity = 'user' # Create Sync grant for our token grant = Twilio::JWT::AccessToken::SyncGrant.new # Create a "grant" which enables a client to use Sync as a given user -grant.serviceSid = twilioSyncService +grant.service_sid = twilio_sync_service # Create an Access Token token = Twilio::JWT::AccessToken.new( diff --git a/rest/access-tokens/sync-example/sync-example.6.x.py b/rest/access-tokens/sync-example/sync-example.6.x.py index 861580b53..0bd28bb93 100644 --- a/rest/access-tokens/sync-example/sync-example.6.x.py +++ b/rest/access-tokens/sync-example/sync-example.6.x.py @@ -7,7 +7,7 @@ account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_KEY_SECRET'] -twilioSyncService = os.environ[process.env.['TWILIO_SYNC_SERVICE_SID'] +twilio_sync_service = os.environ[process.env.['TWILIO_SYNC_SERVICE_SID'] # required for Sync grant identity = 'user' @@ -18,7 +18,7 @@ # Create a Sync grant and add to token sync_grant = SyncGrant( # Create a "grant" which enables a client to use Sync as a given user - serviceSid=twilioSyncService + serviceSid=twilio_sync_service ) token.add_grant(sync_grant) From 1325f944cc637f1074045db4fcf99afb585a16ad Mon Sep 17 00:00:00 2001 From: Des Hartman Date: Thu, 19 Aug 2021 07:47:35 +1000 Subject: [PATCH 5/5] updated serviceSid to service_sid --- rest/access-tokens/sync-example/sync-example.6.x.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest/access-tokens/sync-example/sync-example.6.x.py b/rest/access-tokens/sync-example/sync-example.6.x.py index 0bd28bb93..03b5ef409 100644 --- a/rest/access-tokens/sync-example/sync-example.6.x.py +++ b/rest/access-tokens/sync-example/sync-example.6.x.py @@ -18,7 +18,7 @@ # Create a Sync grant and add to token sync_grant = SyncGrant( # Create a "grant" which enables a client to use Sync as a given user - serviceSid=twilio_sync_service + service_sid=twilio_sync_service ) token.add_grant(sync_grant)