// 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 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;

            // Grant access to Video
            var grant = new VideoGrant();
            grant.ConfigurationProfileSid = videoConfigSid;
            token.AddGrant(grant);

            return Json(new
            {
                identity = identity,
                token = token.ToJWT()
            }, JsonRequestBehavior.AllowGet);
        }
示例#3
0
        public void ShouldAddGrants()
        {
            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());
            token.AddGrant(new IpMessagingGrant());

            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(2, grants.Count);
            Assert.IsNotNull(grants["rtc"]);
            Assert.IsNotNull(grants["ip_messaging"]);
        }
        // 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);
        }
示例#5
0
        public void ShouldHaveNbf()
        {
            var token = new AccessToken("AC456", "SK123", "foobar");
            var now = DateTime.UtcNow;
            token.Nbf = AccessToken.ConvertToUnixTimestamp(now);

            var delta = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var timestamp = (int)Math.Floor(delta.TotalSeconds);
            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"]);
            Assert.AreEqual(AccessToken.ConvertToUnixTimestamp(now), payload["nbf"]);
            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(0, grants.Count);    
        }
示例#6
0
        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"]);
        }
示例#7
0
        public void ShouldCreateVideoGrant()
        {
            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 vg = new VideoGrant();
            vg.ConfigurationProfileSid = "CP123";

            token.AddGrant(vg);

            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 decodedVg = (Dictionary<string, object>)grants["video"];
            Assert.AreEqual("CP123", decodedVg["configuration_profile_sid"]);
        }
        public string Generate(string identity, string endpointId)
        {
            var token = new AccessToken(
                Configuration.AccountSID, Configuration.ApiKey, Configuration.ApiSecret)
            {
                Identity = identity
            };

            var grant = new IpMessagingGrant {EndpointId = endpointId, ServiceSid = Configuration.IpmServiceSID};
            token.AddGrant(grant);

            return token.ToJWT();
        }