Ejemplo n.º 1
0
        public void TestAddGrant()
        {
            var grants = new HashSet <IGrant>
            {
                { new ConversationsGrant() }
            };
            var token = new TestToken("AC456", "SK123", Secret, grants: grants).ToJwt();

            Assert.IsNotNull(token);
            Assert.IsNotEmpty(token);

            var decoded = new DecodedJwt(token, Secret);
            var payload = decoded.Payload;

            Assert.IsNotNull(payload);

            Assert.AreEqual("SK123", payload["iss"]);
            Assert.AreEqual("AC456", payload["sub"]);
            Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));

            var decodedGrants = ToDict(payload["grants"]);

            Assert.AreEqual(1, decodedGrants.Count);
            Assert.IsNotNull(decodedGrants["rtc"]);
        }
Ejemplo n.º 2
0
        public void TestCreateSyncGrant()
        {
            var grants = new HashSet <IGrant>
            {
                {
                    new SyncGrant
                    {
                        ServiceSid = "IS123",
                        EndpointId = "foobar"
                    }
                }
            };
            var token = new TestToken("AC456", "SK123", Secret, grants: grants).ToJwt();

            Assert.IsNotNull(token);
            Assert.IsNotEmpty(token);

            var decoded = new DecodedJwt(token, Secret);
            var payload = decoded.Payload;

            Assert.IsNotNull(payload);

            Assert.AreEqual("SK123", payload["iss"]);
            Assert.AreEqual("AC456", payload["sub"]);
            Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));

            var decodedGrants = ToDict(payload["grants"]);

            Assert.AreEqual(1, decodedGrants.Count);

            var decodedSg = ToDict(decodedGrants["data_sync"]);

            Assert.AreEqual("IS123", decodedSg["service_sid"]);
            Assert.AreEqual("foobar", decodedSg["endpoint_id"]);
        }
Ejemplo n.º 3
0
        public void TestCreateVideoGrant()
        {
            var grants = new HashSet <IGrant>
            {
                { new VideoGrant {
                      ConfigurationProfileSid = "CP123"
                  } }
            };
            var token = new TestToken("AC456", "SK123", Secret, grants: grants).ToJwt();

            Assert.IsNotNull(token);
            Assert.IsNotEmpty(token);

            var decoded = new DecodedJwt(token, Secret);
            var payload = decoded.Payload;

            Assert.IsNotNull(payload);

            Assert.AreEqual("SK123", payload["iss"]);
            Assert.AreEqual("AC456", payload["sub"]);
            Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));

            var decodedGrants = ToDict(payload["grants"]);

            Assert.AreEqual(1, decodedGrants.Count);

            var decodedVg = ToDict(decodedGrants["video"]);

            Assert.AreEqual("CP123", decodedVg["configuration_profile_sid"]);
        }
Ejemplo n.º 4
0
        public void TestBuildToken()
        {
            var jwt = new TestToken("AC456", "SK123", Secret).ToJwt();

            var decoded = new DecodedJwt(jwt, Secret);
            var payload = decoded.Payload;
            Assert.IsNotNull(payload);

            Assert.AreEqual("SK123", payload["iss"]);
            Assert.AreEqual("AC456", payload["sub"]);

            Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));
            Assert.AreEqual("{}", payload["grants"].ToString());
        }
Ejemplo n.º 5
0
        public void TestHaveRegion()
        {
            var now   = DateTime.UtcNow;
            var token = new TestToken("AC456", "SK123", Secret, region: "foo").ToJwt();

            Assert.IsNotNull(token);
            Assert.IsNotEmpty(token);

            var decoded = new DecodedJwt(token, Secret);
            var header  = decoded.Header;

            Assert.IsNotNull(header);
            Assert.AreEqual("twilio-fpa;v=1", header["cty"]);
            Assert.AreEqual("foo", header["twr"]);
        }
Ejemplo n.º 6
0
        public void TestCreateVoiceGrant()
        {
            var grants = new HashSet <IGrant>
            {
                {
                    new VoiceGrant
                    {
                        IncomingAllow             = true,
                        OutgoingApplicationSid    = "AP123",
                        OutgoingApplicationParams = new Dictionary <string, string> {
                            { "foo", "bar" }
                        }
                    }
                }
            };
            var token = new TestToken("AC456", "SK123", Secret, grants: grants).ToJwt();

            Assert.IsNotNull(token);
            Assert.IsNotEmpty(token);

            var decoded = new DecodedJwt(token, Secret);
            var payload = decoded.Payload;

            Assert.IsNotNull(payload);

            Assert.AreEqual("SK123", payload["iss"]);
            Assert.AreEqual("AC456", payload["sub"]);
            Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));

            var decodedGrants = ToDict(payload["grants"]);

            Assert.AreEqual(1, decodedGrants.Count);

            var decodedPvg = decodedGrants["voice"];
            var incoming   = ToDict(decodedPvg.ToString())["incoming"];

            Assert.AreEqual(true, ToDict(incoming)["allow"]);

            var outgoing = ToDict(decodedPvg.ToString())["outgoing"];

            Assert.AreEqual("AP123", ToDict(outgoing)["application_sid"]);

            var decodedParams = ToDict(outgoing)["params"];

            Assert.AreEqual("bar", ToDict(decodedParams)["foo"]);
        }
Ejemplo n.º 7
0
        public void TestHaveNbf()
        {
            var now = DateTime.UtcNow;
            var token = new TestToken("AC456", "SK123", Secret, nbf: now).ToJwt();
            Assert.IsNotNull(token);
            Assert.IsNotEmpty(token);

            var decoded = new DecodedJwt(token, Secret);
            var payload = decoded.Payload;
            Assert.IsNotNull(payload);

            Assert.AreEqual("SK123", payload["iss"]);
            Assert.AreEqual("AC456", payload["sub"]);
            Assert.AreEqual(BaseJwt.ConvertToUnixTimestamp(now), Convert.ToInt64(payload["nbf"]));
            Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));

            Assert.AreEqual("{}", payload["grants"].ToString());
        }
Ejemplo n.º 8
0
        public void TestEmptyRegion()
        {
            var now   = DateTime.UtcNow;
            var token = new TestToken("AC456", "SK123", Secret).ToJwt();

            Assert.IsNotNull(token);
            Assert.IsNotEmpty(token);

            var decoded = new DecodedJwt(token, Secret);
            var header  = decoded.Header;

            Assert.IsNotNull(header);
            Assert.AreEqual("twilio-fpa;v=1", header["cty"]);

            try {
                var twr = header["twr"];
                Assert.Fail();
            } catch (KeyNotFoundException) {
                // Pass
            }
        }