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)); }
// GET: /token public ActionResult Index(string device) { // Load Twilio configuration from Web.config var accountSid = ConfigurationManager.AppSettings["TwilioAccountSid"]; var apiKey = ConfigurationManager.AppSettings["TwilioApiKey"]; var apiSecret = ConfigurationManager.AppSettings["TwilioApiSecret"]; var ipmServiceSid = ConfigurationManager.AppSettings["TwilioIpmServiceSid"]; // Create a random identity for the client var identity = Internet.UserName(); // Create an Access Token generator var token = new AccessToken(accountSid, apiKey, apiSecret); Token.Identity = identity; // Create an IP messaging grant for this token var grant = new IpMessagingGrant(); grant.EndpointId = $"TwilioChatDemo:{identity}:{device}"; grant.ServiceSid = ipmServiceSid; token.AddGrant(grant); return(Json(new { identity = identity, token = token.ToJWT() }, JsonRequestBehavior.AllowGet)); }
public void ShouldAddGrant() { 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); token.AddGrant(new ConversationsGrant()); 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); Assert.IsNotNull(grants["rtc"]); }
// GET: /token public ActionResult Index(string Device) { // Load Twilio configuration from Web.config var accountSid = ConfigurationManager.AppSettings["TwilioAccountSid"]; var apiKey = ConfigurationManager.AppSettings["TwilioApiKey"]; var apiSecret = ConfigurationManager.AppSettings["TwilioApiSecret"]; var videoConfigSid = ConfigurationManager.AppSettings["TwilioConfigurationSid"]; // Create a random identity for the client var identity = Internet.UserName(); // Create an Access Token generator var token = new AccessToken(accountSid, apiKey, apiSecret); token.Identity = identity; // Create a video grant for this token var grant = new ConversationsGrant(); token.AddGrant(grant); return(Json(new { identity = identity, token = token.ToJWT() }, JsonRequestBehavior.AllowGet)); }
// GET: /token public IActionResult Index(string Device) { // Load Twilio configuration from Web.config var AccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"); var ApiKey = Environment.GetEnvironmentVariable("TWILIO_IPM_KEY"); var ApiSecret = Environment.GetEnvironmentVariable("TWILIO_IPM_SECRET"); var IpmServiceSid = Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"); // Create a random identity for the client var Identity = StringExtensions.MyName.RemoveSpecialCharacters(); // Create an Access Token generator var Token = new AccessToken(AccountSid, ApiKey, ApiSecret); Token.Identity = Identity; // Create an IP messaging grant for this token var grant = new IpMessagingGrant(); grant.EndpointId = $"QuickStartService:{Identity}:{Device}"; grant.ServiceSid = IpmServiceSid; Token.AddGrant(grant); return(Json(new { identity = Identity, token = Token.ToJWT() })); }
// GET: /token public ActionResult Index(string device) { // Load Twilio configuration from Web.config var accountSid = ConfigurationManager.AppSettings["TwilioAccountSid"]; var apiKey = ConfigurationManager.AppSettings["TwilioApiKey"]; var apiSecret = ConfigurationManager.AppSettings["TwilioApiSecret"]; var syncServiceSid = ConfigurationManager.AppSettings["TwilioSyncServiceSid"]; // Create a random identity for the client var identity = Internet.UserName(); // Create an Access Token generator var token = new AccessToken(accountSid, apiKey, apiSecret) { Identity = identity }; // Create a Sync grant for this token var grant = new SyncGrant { EndpointId = $"TwilioSyncQuickstart:{identity}:{device}", ServiceSid = syncServiceSid }; token.AddGrant(grant); return(Json(new { identity, token = token.ToJWT() }, JsonRequestBehavior.AllowGet)); }
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 IP Messaging const string ipmServiceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; const string identity = "*****@*****.**"; const string deviceId = "someiosdevice"; // Create an Access Token generator var token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret); token.Identity = identity; // Create an IP messaging grant for this token var grant = new IpMessagingGrant(); grant.EndpointId = $"HipFlowSlackDockRC:{identity}:{deviceId}"; grant.ServiceSid = ipmServiceSid; token.AddGrant(grant); Console.WriteLine(token.ToJwt()); }
public ChatDataResult GetResponderChatSettings() { var result = new ChatDataResult(); // Load Twilio configuration from Web.config var accountSid = _appOptionsAccessor.Value.TwilioAccountSid; var apiKey = _appOptionsAccessor.Value.TwilioApiKey; var apiSecret = _appOptionsAccessor.Value.TwilioApiSecret; var ipmServiceSid = _appOptionsAccessor.Value.TwilioIpmServiceSid; // Create an Access Token generator var token = new AccessToken(accountSid, apiKey, apiSecret); token.Identity = UserId.ToString(); // Create an IP messaging grant for this token var grant = new IpMessagingGrant(); grant.EndpointId = $"ResponderDepChat:{UserId}:ResponderApp"; grant.ServiceSid = ipmServiceSid; token.AddGrant(grant); var department = _departmentsService.GetDepartmentById(DepartmentId); var groups = _departmentGroupsService.GetAllGroupsForDepartment(DepartmentId); result.Channels = SetupTwilioChatForDepartment(department, groups); result.Did = department.DepartmentId; result.Name = department.Name; result.Groups = new List <GroupInfoResult>(); if (department.IsUserAnAdmin(UserId)) { foreach (var group in groups) { result.Groups.Add(new GroupInfoResult() { Gid = group.DepartmentGroupId, Nme = group.Name }); } } else { var group = _departmentGroupsService.GetGroupForUser(UserId, DepartmentId); if (group != null) { result.Groups.Add(new GroupInfoResult() { Gid = group.DepartmentGroupId, Nme = group.Name }); } } result.Token = token.ToJWT(); return(result); }
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"]); }
static void Main(string[] args) { // Find your Account Sid and Auth Token at twilio.com/user/account string AccountSid = "{{ account_sid }}"; string SigningKeySid = SID; string SigningKeySecret = SECRET; var token = new AccessToken(SigningKeySid, AccountSid, SigningKeySecret); token.AddGrant(new EndpointGrant(ENDPOINT_ADDRESS)); token.EnableNTS(); Console.WriteLine(token.ToJWT()); }
public async Task <HttpResponseMessage> GetIpMessagingToken(string device, string identityId, string name, string picture) { if (string.IsNullOrWhiteSpace(identityId)) { identityId = UserIdentityID; } if (string.IsNullOrWhiteSpace(name)) { name = device == "browser" ? Identity.Name.Split('@')[0] : Identity.Name; } if (string.IsNullOrWhiteSpace(picture)) { picture = Identity.Claims .Where(c => c.Type == ClaimTypes.Uri) .Select(c => c.Value) .SingleOrDefault() ?? string.Empty; } var identity = new Dictionary <string, string> { { "identityId", identityId }, { "name", name }, { "picture", picture } }; var identityJson = JsonConvert.SerializeObject(identity); // create an access token generator w/ specific permission grants var accessToken = new AccessToken(settings.Account.Sid, settings.ApiKey, settings.ApiSecret) { Identity = identityJson }; var ipMessagingGrant = new IpMessagingGrant { EndpointId = $"TwilioChatDemo:{identityJson}:{device}", ServiceSid = settings.IpMessaging.Service.Sid }; accessToken.AddGrant(ipMessagingGrant); var response = new ApiResponse <TwilioToken>(new TwilioToken { Identity = identityJson, Token = accessToken.ToJWT() }); return(SendHttpResponse(response)); }
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 IP Messaging var ipmServiceSid = "ISxxxxxxxxxxxx"; var identity = "*****@*****.**"; // Create an Access Token generator var token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret); token.Identity = identity; // Create an IP messaging grant for this token var grant = new IpMessagingGrant(); grant.ServiceSid = ipmServiceSid; token.AddGrant(grant); Console.WriteLine(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 Video var configurationProfileSid = "VSxxxxxxxxxxxx"; var identity = "user"; // Create an Access Token generator var token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret); token.Identity = identity; // Create a Video grant for this token var grant = new VideoGrant(); grant.ConfigurationProfileSid = configurationProfileSid; token.AddGrant(grant); Console.WriteLine(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()); }