Exemple #1
0
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        // To set up environmental variables, see http://twil.io/secure
        const string accountSid   = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
        const string authToken    = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");
        const string workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        var urls = new PolicyUrlUtils(workspaceSid);

        var allowFetchSubresources   = new Policy($"{urls.Workspace}/**", HttpMethod.Get);
        var allowUpdatesSubresources = new Policy($"{urls.Workspace}/**", HttpMethod.Post);
        var allowDeleteSubresources  = new Policy($"{urls.Workspace}/**", HttpMethod.Delete);


        var policies = new List <Policy>
        {
            allowFetchSubresources,
            allowUpdatesSubresources,
            allowUpdatesSubresources
        };

        // By default, tokens are good for one hour.
        // Override this default timeout by specifiying a new value (in seconds).
        // For example, to generate a token good for 8 hours:
        var capability = new TaskRouterCapability(
            accountSid,
            authToken,
            workspaceSid,
            null,
            policies: policies,
            expiration: DateTime.UtcNow.AddSeconds(28800)); // 60 * 60 * 8

        Console.WriteLine(capability.ToJwt());
    }
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid   = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken    = "your_auth_token";
        const string workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string taskQueueSid = "WQXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        var urls = new PolicyUrlUtils(workspaceSid, taskQueueSid);

        var allowFetchSubresources = new Policy($"{urls.TaskQueue}/**",
                                                HttpMethod.Get);

        var allowUpdates = new Policy(urls.TaskQueue, HttpMethod.Post);

        var policies = new List <Policy>
        {
            allowFetchSubresources,
            allowUpdates
        };

        // By default, tokens are good for one hour.
        // Override this default timeout by specifiying a new value (in seconds).
        // For example, to generate a token good for 8 hours:
        var capability = new TaskRouterCapability(
            accountSid,
            authToken,
            workspaceSid,
            null,
            policies: policies,
            expiration: DateTime.UtcNow.AddSeconds(28800) // 60 * 60 * 8
            );

        Console.WriteLine(capability.ToJwt());
    }
        public void ShouldAllowTaskQueueDefaults()
        {
            TaskRouterCapability cap = new TaskRouterCapability("AC123", "foobar", "WS456", "WQ789");
            var token = cap.GenerateToken();

            Assert.IsNotNullOrEmpty(token);

            var payload = JsonWebToken.DecodeToObject(token, "foobar") as IDictionary <string, object>;

            Assert.AreEqual("AC123", payload["iss"]);
            Assert.AreEqual("AC123", payload["account_sid"]);
            Assert.AreEqual("WS456", payload["workspace_sid"]);
            Assert.AreEqual("WQ789", payload["taskqueue_sid"]);
            Assert.AreEqual("WQ789", payload["channel"]);
            Assert.AreEqual("v1", payload["version"]);
            Assert.AreEqual("WQ789", payload["friendly_name"]);

            var policies = payload["policies"] as System.Collections.IList;

            Assert.AreEqual(3, policies.Count);
            var url = "https://event-bridge.twilio.com/v1/wschannels/AC123/WQ789";

            var getPolicy = policies[0] as IDictionary <string, object>;

            Assert.AreEqual(url, getPolicy["url"]);
            Assert.AreEqual("GET", getPolicy["method"]);
            Assert.IsTrue((bool)getPolicy["allow"]);
            var queryFilter = getPolicy["query_filter"] as IDictionary <string, object>;

            Assert.IsEmpty(queryFilter);
            var postFilter = getPolicy["post_filter"] as IDictionary <string, object>;

            Assert.IsEmpty(postFilter);

            var postPolicy = policies[1] as IDictionary <string, object>;

            Assert.AreEqual(url, postPolicy["url"]);
            Assert.AreEqual("POST", postPolicy["method"]);
            Assert.IsTrue((bool)postPolicy["allow"]);
            queryFilter = postPolicy["query_filter"] as IDictionary <string, object>;
            Assert.IsEmpty(queryFilter);
            postFilter = postPolicy["post_filter"] as IDictionary <string, object>;
            Assert.IsEmpty(postFilter);

            var taskQueueFetchPolicy = policies[2] as IDictionary <string, object>;

            Assert.AreEqual("https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789", taskQueueFetchPolicy["url"]);
            Assert.AreEqual("GET", taskQueueFetchPolicy["method"]);
            Assert.IsTrue((bool)taskQueueFetchPolicy["allow"]);
            queryFilter = taskQueueFetchPolicy["query_filter"] as IDictionary <string, object>;
            Assert.IsEmpty(queryFilter);
            postFilter = taskQueueFetchPolicy["post_filter"] as IDictionary <string, object>;
            Assert.IsEmpty(postFilter);
        }
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        // To set up environmental variables, see http://twil.io/secure
        const string accountSid   = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
        const string authToken    = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");
        const string workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string workerSid    = "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        var updateActivityFilter = new Dictionary <string, Policy.FilterRequirement>
        {
            { "ActivitySid", Policy.FilterRequirement.Required }
        };

        var urls = new PolicyUrlUtils(workspaceSid, workerSid);

        var allowActivityUpdates = new Policy(urls.Worker,
                                              HttpMethod.Post,
                                              postFilter: updateActivityFilter);
        var allowTasksUpdate        = new Policy(urls.AllTasks, HttpMethod.Post);
        var allowReservationUpdate  = new Policy(urls.AllReservations, HttpMethod.Post);
        var allowWorkerFetches      = new Policy(urls.Worker, HttpMethod.Get);
        var allowTasksFetches       = new Policy(urls.AllTasks, HttpMethod.Get);
        var allowReservationFetches = new Policy(urls.AllReservations, HttpMethod.Get);
        var allowActivityFetches    = new Policy(urls.Activities, HttpMethod.Get);

        var policies = new List <Policy>
        {
            allowActivityUpdates,
            allowTasksUpdate,
            allowReservationUpdate,
            allowWorkerFetches,
            allowTasksFetches,
            allowReservationFetches
        };

        // By default, tokens are good for one hour.
        // Override this default timeout by specifiying a new value (in seconds).
        // For example, to generate a token good for 8 hours:
        var capability = new TaskRouterCapability(
            accountSid,
            authToken,
            workspaceSid,
            workerSid,
            policies: policies,
            expiration: DateTime.UtcNow.AddSeconds(28800)); // 60 * 60 * 8


        Console.WriteLine(capability.ToJwt());
    }
        public void ShouldAllowTaskQueueDefaults()
        {
            TaskRouterCapability cap = new TaskRouterCapability("AC123", "foobar", "WS456", "WQ789");
            var token = cap.GenerateToken();
            Assert.IsNotNullOrEmpty(token);

            var payload = JsonWebToken.DecodeToObject(token, "foobar") as IDictionary<string, object>;
            Assert.AreEqual("AC123", payload["iss"]);
            Assert.AreEqual("AC123", payload["account_sid"]);
            Assert.AreEqual("WS456", payload["workspace_sid"]);
            Assert.AreEqual("WQ789", payload["taskqueue_sid"]);
            Assert.AreEqual("WQ789", payload["channel"]);
            Assert.AreEqual("v1", payload["version"]);
            Assert.AreEqual("WQ789", payload["friendly_name"]);

            var policies = payload["policies"] as System.Collections.IList;
            Assert.AreEqual(3, policies.Count);
            var url = "https://event-bridge.twilio.com/v1/wschannels/AC123/WQ789";

            var getPolicy = policies[0] as IDictionary<string, object>;
            Assert.AreEqual(url, getPolicy["url"]);
            Assert.AreEqual("GET", getPolicy["method"]);
            Assert.IsTrue((bool) getPolicy["allow"]);
            var queryFilter = getPolicy["query_filter"] as IDictionary<string, object>;
            Assert.IsEmpty(queryFilter);
            var postFilter = getPolicy["post_filter"] as IDictionary<string, object>;
            Assert.IsEmpty(postFilter);

            var postPolicy = policies[1] as IDictionary<string, object>;
            Assert.AreEqual(url, postPolicy["url"]);
            Assert.AreEqual("POST", postPolicy["method"]);
            Assert.IsTrue((bool)postPolicy["allow"]);
            queryFilter = postPolicy["query_filter"] as IDictionary<string, object>;
            Assert.IsEmpty(queryFilter);
            postFilter = postPolicy["post_filter"] as IDictionary<string, object>;
            Assert.IsEmpty(postFilter);

            var taskQueueFetchPolicy = policies[2] as IDictionary<string, object>;
            Assert.AreEqual("https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789", taskQueueFetchPolicy["url"]);
            Assert.AreEqual("GET", taskQueueFetchPolicy["method"]);
            Assert.IsTrue((bool)taskQueueFetchPolicy["allow"]);
            queryFilter = taskQueueFetchPolicy["query_filter"] as IDictionary<string, object>;
            Assert.IsEmpty(queryFilter);
            postFilter = taskQueueFetchPolicy["post_filter"] as IDictionary<string, object>;
            Assert.IsEmpty(postFilter);
        }
Exemple #6
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));
        }
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid   = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken    = "your_auth_token";
        const string workspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string workerSid    = "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        var updateActivityFilter = new Dictionary <string, Policy.FilterRequirement>
        {
            { "ActivitySid", Policy.FilterRequirement.Required }
        };

        var urls = new PolicyUrlUtils(workspaceSid, workerSid);

        var allowActivityUpdates = new Policy(urls.Worker,
                                              HttpMethod.Post,
                                              postFilter: updateActivityFilter);

        var allowTasksUpdate = new Policy(urls.AllTasks, HttpMethod.Post);

        var allowReservationUpdate = new Policy(urls.AllReservations, HttpMethod.Post);

        var policies = new List <Policy>
        {
            allowActivityUpdates,
            allowTasksUpdate,
            allowReservationUpdate
        };

        // By default, tokens are good for one hour.
        // Override this default timeout by specifiying a new value (in seconds).
        // For example, to generate a token good for 8 hours:
        var capability = new TaskRouterCapability(
            accountSid,
            authToken,
            workspaceSid,
            null,
            policies: policies,
            expiration: DateTime.UtcNow.AddSeconds(28800)); // 60 * 60 * 8

        Console.WriteLine(capability.ToJwt());
    }
 public void Setup()
 {
     cap = new TaskRouterCapability("AC123", "foobar", "WS456", "WK789");
 }
        public ActionResult Agent_desktop()
        {
            string workerSid = Request.Params.Get("WorkerSid");

            TwilioClient.Init(_accountSid, _authToken);

            var activityDictionary = new Dictionary <string, string>();


            var activities = ActivityResource.Read(_workspaceSid);

            foreach (var activity in activities)
            {
                activityDictionary.Add(activity.FriendlyName, activity.Sid);
            }


            var updateActivityFilter = new Dictionary <string, Policy.FilterRequirement>
            {
                { "ActivitySid", Policy.FilterRequirement.Required }
            };

            var urls = new PolicyUrlUtils(_workspaceSid, workerSid);

            var allowActivityUpdates = new Policy(urls.Worker,
                                                  HttpMethod.Post,
                                                  postFilter: updateActivityFilter);
            var allowTasksUpdate        = new Policy(urls.AllTasks, HttpMethod.Post);
            var allowReservationUpdate  = new Policy(urls.AllReservations, HttpMethod.Post);
            var allowWorkerFetches      = new Policy(urls.Worker, HttpMethod.Get);
            var allowTasksFetches       = new Policy(urls.AllTasks, HttpMethod.Get);
            var allowReservationFetches = new Policy(urls.AllReservations, HttpMethod.Get);
            var allowActivityFetches    = new Policy(urls.Activities, HttpMethod.Get);

            var policies = new List <Policy>
            {
                allowActivityUpdates,
                allowTasksUpdate,
                allowReservationUpdate,
                allowWorkerFetches,
                allowTasksFetches,
                allowReservationFetches
            };

            var capability = new TaskRouterCapability(
                _accountSid,
                _authToken,
                _workspaceSid,
                workerSid,
                policies: policies);

            var workerToken = capability.ToJwt();

            var scopes = new HashSet <IScope>
            {
                { new IncomingClientScope(Request.Params.Get("WorkerSid")) },
                { new OutgoingClientScope(_applicationSid) }
            };

            var webClientCapability = new ClientCapability(_accountSid, _authToken, scopes: scopes);
            var token = capability.ToJwt();

            ViewBag.worker_token = workerToken;
            ViewBag.client_token = webClientCapability.ToJwt();
            ViewBag.caller_ID    = _called_id;
            ViewBag.activties    = activityDictionary;
            return(View());
        }
 public void Setup()
 {
     cap = new TaskRouterCapability("AC123", "foobar", "WS456", "WK789");
 }