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

        }
Exemple #3
0
        public void TestSetUtcNbf()
        {
            var encoded = new TestJwt(DateTime.UtcNow.AddHours(1), TEST_TIME).ToJwt();
            var decoded = new DecodedJwt(encoded, TestJwt.SECRET);

            Assert.AreEqual(TEST_TIMESTAMP, decoded.Payload["nbf"]);
        }
Exemple #4
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"]);
        }
Exemple #5
0
        public void TestSetUtcExp()
        {
            var encoded = new TestJwt(TEST_TIME).ToJwt();
            var decoded = new DecodedJwt(encoded, TestJwt.SECRET);

            Assert.AreEqual(TEST_TIMESTAMP, decoded.Payload["exp"]);
        }
Exemple #6
0
        public void TestSetLocalTimeNbf()
        {
            var nbf = TimeZoneInfo.ConvertTime(TEST_TIME, OTHER_TZ);

            var encoded = new TestJwt(DateTime.UtcNow.AddHours(1), nbf).ToJwt();
            var decoded = new DecodedJwt(encoded, TestJwt.SECRET);

            Assert.AreEqual(TEST_TIMESTAMP, decoded.Payload["nbf"]);
        }
Exemple #7
0
        public void TestSetLocalTimeExp()
        {
            // Convert to PST before passing to JWT
            var exp = TimeZoneInfo.ConvertTime(TEST_TIME, OTHER_TZ);

            var encoded = new TestJwt(exp).ToJwt();
            var decoded = new DecodedJwt(encoded, TestJwt.SECRET);

            Assert.AreEqual(TEST_TIMESTAMP, decoded.Payload["exp"]);
        }
Exemple #8
0
        public void TestEmptyToken()
        {
            var jwt = new ClientCapability(AccountSid, Secret).ToJwt();

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

            Assert.AreEqual(AccountSid, payload["iss"]);
            Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));
        }
        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());
        }
        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"]);
        }
        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"]);
        }
        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());
        }
Exemple #13
0
        public void TestToken()
        {
            var policies = new List <Policy>
            {
                { new Policy("https://taskrouter.twilio.com/v1/Workspaces", HttpMethod.Get) }
            };

            var jwt = new TaskRouterCapability(AccountSid, AuthToken, WorkspaceSid, WorkerSid, policies: policies).ToJwt();

            var token   = new DecodedJwt(jwt, AuthToken);
            var payload = token.Payload;

            Assert.AreEqual(WorkspaceSid, payload["workspace_sid"]);
            Assert.AreEqual(WorkerSid, payload["channel"]);
            Assert.AreEqual(AccountSid, payload["account_sid"]);
            Assert.AreEqual(AccountSid, payload["iss"]);

            Assert.Greater(Convert.ToInt64(payload["exp"]), BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow));
        }
        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
            }
        }
Exemple #15
0
        public void TestToken()
        {
            var scopes = new HashSet <IScope>
            {
                { new IncomingClientScope("incomingClient") },
                { new EventStreamScope() },
                { new OutgoingClientScope("AP123") }
            };

            var jwt = new ClientCapability(AccountSid, Secret, scopes: scopes).ToJwt();

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

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

            Assert.AreEqual(
                "scope:client:incoming?clientName=incomingClient " +
                "scope:stream:subscribe?path=/2010-04-01/Events " +
                "scope:client:outgoing?appSid=AP123",
                payload["scope"]
                );
        }