public async Task <HttpResponseMessage> GetConversationsToken(string name) { // create an access token generator w/ specific permission grants var token = new AccessToken( settings.Account.Sid, settings.ApiKey, settings.ApiSecret); token.Identity = name == "browser" ? Identity.Name.Split('@')[0] : name; var convoGrant = new ConversationsGrant { ConfigurationProfileSid = settings.Conversation.Sid }; var voiceGrant = new VoiceGrant(); token.AddGrant(convoGrant); token.AddGrant(voiceGrant); var response = new ApiResponse <TwilioToken>(new TwilioToken { Identity = name, Token = token.ToJWT() }); return(SendHttpResponse(response)); }
static void Main(string[] args) { // These values are necessary for any access token const string twilioAccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const string twilioApiKey = "SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const string twilioApiSecret = "your_secret"; // These are specific to Voice const string outgoingApplicationSid = "APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const string identity = "user"; // Create a Voice grant for this token var grant = new VoiceGrant(); grant.OutgoingApplicationSid = outgoingApplicationSid; var grants = new HashSet <IGrant> { { grant } }; // Create an Access Token generator var token = new Token( twilioAccountSid, twilioApiKey, twilioApiSecret, identity, grants: grants); Console.WriteLine(token.ToJwt()); }
public static string GetTokenForVoice() { // These values are necessary for any access token const string twilioAccountSid = "ACaba23a7d277cde563020cbd18849eaa5"; const string twilioApiKey = "SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const string twilioApiSecret = "your_secret"; // These are specific to Voice const string outgoingApplicationSid = "APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const string identity = "user"; // Create a Voice grant for this token var grant = new VoiceGrant(); grant.OutgoingApplicationSid = outgoingApplicationSid; // Optional: add to allow incoming calls grant.IncomingAllow = true; var grants = new HashSet <IGrant> { { grant } }; // Create an Access Token generator var token = new Token( twilioAccountSid, twilioApiKey, twilioApiSecret, identity, grants: grants); return(token.ToJwt()); }
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 Voice const string outgoingApplicationSid = "APXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const string identity = "user"; // Create a Voice grant for this token var grant = new VoiceGrant(); grant.OutgoingApplicationSid = outgoingApplicationSid; // Optional: add to allow incoming calls grant.IncomingAllow = true; var grants = new HashSet <IGrant> { { grant } }; // Create an Access Token generator var token = new Token( twilioAccountSid, twilioApiKey, twilioApiSecret, identity, grants: grants); Console.WriteLine(token.ToJwt()); }
public void ShouldCreateVoiceGrant() { var token = new AccessToken("AC456", "SK123", "foobar"); var delta = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); var timestamp = (int)Math.Floor(delta.TotalSeconds); var pvg = new VoiceGrant(); pvg.OutgoingApplicationSid = "AP123"; var param = new Dictionary <string, string>(); param.Add("foo", "bar"); pvg.OutgoingApplicationParams = param; token.AddGrant(pvg); var encoded = token.ToJWT(); Assert.IsNotNull(encoded); Assert.IsNotEmpty(encoded); var decoded = JsonWebToken.Decode(encoded, "foobar"); Assert.IsNotEmpty(decoded); var serializer = new JavaScriptSerializer(); var payload = (Dictionary <string, object>)serializer.DeserializeObject(decoded); Assert.IsNotNull(payload); Assert.AreEqual("SK123", payload["iss"]); Assert.AreEqual("AC456", payload["sub"]); var exp = Convert.ToInt64(payload["exp"]); Assert.AreEqual(timestamp + 3600, exp); var jti = (string)payload["jti"]; Assert.AreEqual("SK123-" + timestamp.ToString(), jti); var grants = (Dictionary <string, object>)payload["grants"]; Assert.AreEqual(1, grants.Count); var decodedPvg = (Dictionary <string, object>)grants["voice"]; var outgoing = (Dictionary <string, object>)decodedPvg["outgoing"]; Assert.AreEqual("AP123", outgoing["application_sid"]); var decodedParams = (Dictionary <string, object>)outgoing["params"]; Assert.AreEqual("bar", decodedParams["foo"]); }
public string Generate(string identity, string channelName, string endpointId, string TokenType) { HashSet <IGrant> grants = null; if (TokenType == "Chat") { grants = new HashSet <IGrant> { new ChatGrant { EndpointId = endpointId, ServiceSid = _config["TwilioAccount:ChatServiceSid"], } }; } else if (TokenType == "Voice") { var grant = new VoiceGrant(); grant.OutgoingApplicationSid = _config["TwiloVoiceAppSid"]; // Optional: add to allow incoming calls grant.IncomingAllow = true; grants = new HashSet <IGrant> { { grant } }; } else if (TokenType == "Video") { var grant = new VideoGrant(); grant.Room = channelName; grants = new HashSet <IGrant> { grant }; } var token = new Token( _config["TwilioAccount:AccountSid"], _config["TwilioAccount:ApiKey"], _config["TwilioAccount:ApiSecret"], identity, grants: grants ); return(token.ToJwt()); }
public ActionResult TwilioAccessToken() { var grant = new VoiceGrant(); grant.OutgoingApplicationSid = _twilioSettings.OutgoingApplicationSid; // Optional: add to allow incoming calls grant.IncomingAllow = true; var grants = new HashSet <IGrant> { { grant } }; // Create an Access Token generator var token = new Token( _twilioSettings.TwilioAccountSid, _twilioSettings.TwilioApiKey, _twilioSettings.TwilioApiSecret, _twilioSettings.TwilioIdentity, grants: grants); return(Content(token.ToJwt())); }
static void Main(string[] args) { // These values are necessary for any access token var twilioAccountSid = "ACxxxxxxxxxxxx"; var twilioApiKey = "SKxxxxxxxxxxxx"; var twilioApiSecret = "xxxxxxxxxxxxxx"; // These are specific to Voice var outgoingApplicationSid = "APxxxxxxxxxxxx"; var identity = "user"; // Create an Access Token generator var token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret); token.Identity = identity; // Create a Voice grant for this token var grant = new VoiceGrant(); grant.OutgoingApplicationSid = outgoingApplicationSid; token.AddGrant(grant); Console.WriteLine(token.ToJWT()); }