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

        }
Example #2
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"]);
        }
Example #3
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"]);
        }
Example #4
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 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());
        }
Example #8
0
        /// <summary>
        /// Create a new Access Token
        /// </summary>
        /// <param name="accountSid">Account SID</param>
        /// <param name="signingKeySid">Signing key SID</param>
        /// <param name="secret">Secret to encode with</param>
        /// <param name="identity">Token identity</param>
        /// <param name="expiration">Token expiration</param>
        /// <param name="nbf">Token nbf</param>
        /// <param name="grants">Token grants</param>
        public Token(
            string accountSid,
            string signingKeySid,
            string secret,
            string identity         = null,
            DateTime?expiration     = null,
            DateTime?nbf            = null,
            HashSet <IGrant> grants = null
            ) : base(secret, signingKeySid, expiration.HasValue ? expiration.Value : DateTime.UtcNow.AddSeconds(3600))
        {
            var now = BaseJwt.ConvertToUnixTimestamp(DateTime.UtcNow);

            this._id         = $"{signingKeySid}-{now}";
            this._accountSid = accountSid;
            this._identity   = identity;
            this._nbf        = nbf;
            this._grants     = grants;
        }
Example #9
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));
        }
Example #10
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"]
                );
        }