// 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); }
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"]); }