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..499774b6f --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.3.x.js @@ -0,0 +1,30 @@ +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; +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({ + serviceSid: twilioSyncService, +}); + +// 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..195bc5b7a --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.5.x.cs @@ -0,0 +1,40 @@ +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"); + 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(); + + // Create a "grant" which enables a client to use Sync as a given user + grant.serviceSid = twilioSyncService; + + 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..1ff9df30a --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.5.x.php @@ -0,0 +1,39 @@ +setServiceSid($twilioSyncService); + + +// 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..6a121b7bf --- /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'] +twilio_sync_service = ENV['TWILIO_SYNC_SERVICE_SID'] + + +# Required for Sync +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.service_sid = twilio_sync_service + +# 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..03b5ef409 --- /dev/null +++ b/rest/access-tokens/sync-example/sync-example.6.x.py @@ -0,0 +1,27 @@ +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'] +twilio_sync_service = os.environ[process.env.['TWILIO_SYNC_SERVICE_SID'] + +# required for Sync grant +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( + # Create a "grant" which enables a client to use Sync as a given user + service_sid=twilio_sync_service +) + +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..c814e848c --- /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"); + String twilioSyncService = System.getenv("TWILIO_SYNC_SERVICE_SID"); + + + // Required for Sync + String identity = "user"; + + // Create Sync grant + SyncGrant grant = new SyncGrant(); + + // 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, + twilioApiKey, + twilioApiSecret + ).identity(identity).grant(grant).build(); + + System.out.println(token.toJwt()); + } +}