Beispiel #1
0
        public async Task <Root> Start(Uri uri, AuthToken authToken = null)
        {
            if (authToken != null)
            {
                _proxy.Authorization = string.Format("{0} {1}", authToken.TokenType, authToken.AccessToken);
            }

            var response = await _proxy.ExecuteRequestAsync(new Request
            {
                Uri    = uri,
                Method = HttpMethod.Get
            });

            if (response?.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new AuthenticationException(new Challenge(response.Headers[Constants.WwwAuthenticate]), "Authentication failed due to challenge");
            }
            else
            {
                var root = response?.Data?.FromBytes <Root>();
                if (root != null)
                {
                    root.Baseuri   = new Uri(new Uri(root.Links.Self.Href).GetLeftPart(UriPartial.Authority));
                    _proxy.Baseuri = root.Baseuri;
                    return(root);
                }
            }

            throw new AuthenticationException(null, "Authentication was unable to start properly");
        }
Beispiel #2
0
 private async Task <Response> Discover(string scheme, bool isInternal, string domain)
 {
     return(await _proxy.ExecuteRequestAsync(new Request
     {
         Uri = new Uri(string.Format("{0}://lyncdiscover{1}.{2}", scheme, isInternal ? "internal" : string.Empty, domain)),
         Method = HttpMethod.Get
     }));
 }
Beispiel #3
0
        private void EventLoop()
        {
            var task = _proxy.ExecuteRequestAsync(new Request
            {
                Uri     = EventsUri,
                Method  = HttpMethod.Get,
                Timeout = Timeout.Infinite
            });

            task.Wait();

            var response = task.Result;

            if (response?.StatusCode == HttpStatusCode.OK)
            {
                var evt = response?.Data?.FromBytes <Event>();
                if (evt != null)
                {
                    EventsUri = new Uri(evt.Links?.Resync?.Href ?? evt.Links.Next.Href, UriKind.Relative);
                    if (evt.Links?.Resync == null)
                    {
                        OnEventReceived?.Invoke(this, new EventReceivedEventArgs
                        {
                            Resource = evt
                        });
                    }
                }
            }
            else if (response?.StatusCode == HttpStatusCode.Conflict)
            {
                _error = response?.Data?.FromBytes <Error>();
                Stop();
            }
        }
Beispiel #4
0
        static void ReportMyActivity(object o)
        {
            var task = _proxy.ExecuteRequestAsync(new Request
            {
                Uri    = new Uri(o as string, UriKind.Relative),
                Method = HttpMethod.Post
            });

            task.Wait();
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            _proxy          = new TransportProxy(new SimpleTransport());
            _discovery      = new Discovery(_proxy);
            _authentication = new Authentication(_proxy);

            System.Console.WriteLine("Enter discovery domain");
            var discoveryDomain = System.Console.ReadLine();

            System.Console.WriteLine("Enter username");
            var username = System.Console.ReadLine();

            System.Console.WriteLine("Enter password");
            var password = System.Console.ReadLine();

            System.Console.WriteLine("Starting discovery...");
            var discoveryTask = _discovery.Start(!string.IsNullOrWhiteSpace(discoveryDomain) ? discoveryDomain : username);

            discoveryTask.Wait();
            var discoveryUri = new Uri(discoveryTask.Result.Links.User.Href);

            System.Console.WriteLine("Starting authentication");
            try
            {
                var authenticationTask = _authentication.Start(discoveryUri);
                authenticationTask.Wait();
            }
            catch (AggregateException ex)
            {
                if (ex?.InnerException.GetType() == typeof(AuthenticationException))
                {
                    var challenge     = (ex.InnerException as AuthenticationException).Challenge;
                    var authTokenTask = _authentication.GetAuthToken(challenge.MsRtcOAuth, new Credentials
                    {
                        GrantType = Constants.Password,
                        Username  = username,
                        Password  = password
                    });
                    authTokenTask.Wait();

                    var rootTask = _authentication.Start(discoveryUri, authTokenTask.Result);
                    rootTask.Wait();

                    if (rootTask.Result != null)
                    {
                        System.Console.WriteLine("Creating Application");
                        var applications = new Application
                        {
                            Culture    = "en-US",
                            EndpointId = Guid.NewGuid().ToString(),
                            UserAgent  = "tester"
                        };
                        var appTask = _proxy.ExecuteRequestAsync(new Request
                        {
                            Uri     = new Uri(rootTask.Result.Links.Applications.Href),
                            Method  = HttpMethod.Post,
                            Headers = new Dictionary <string, string>
                            {
                                { Constants.ContentType, Constants.Json }
                            },
                            Data = applications.ToBytes()
                        });
                        appTask.Wait();

                        if (appTask?.Result?.StatusCode == HttpStatusCode.Created)
                        {
                            applications = appTask.Result.Data.FromBytes <Application>();
                            if (applications != null)
                            {
                                var makeMeAvailable = new MakeMeAvailable
                                {
                                    SupportedModalities = new [] { "Messaging" }
                                };
                                var makeMeAvailableTask = _proxy.ExecuteRequestAsync(new Request
                                {
                                    Uri     = new Uri(applications.Embedded.Me.Links.MakeMeAvailable.Href, UriKind.Relative),
                                    Method  = HttpMethod.Post,
                                    Headers = new Dictionary <string, string>
                                    {
                                        { Constants.ContentType, Constants.Json }
                                    },
                                    Data = makeMeAvailable.ToBytes()
                                });
                                makeMeAvailableTask.Wait();

                                if (makeMeAvailableTask?.Result.StatusCode == HttpStatusCode.NoContent)
                                {
                                    // Start events loop
                                    var events = new Events(_proxy, new Uri(applications.Links.Events.Href, UriKind.Relative));
                                    events.OnEventReceived += OnEventReceived;
                                    events.Start();

                                    // Request and start reportMyActivity loop
                                    var meTask = _proxy.ExecuteRequestAsync(new Request
                                    {
                                        Uri    = new Uri(applications.Embedded.Me.Links.Self.Href, UriKind.Relative),
                                        Method = HttpMethod.Get
                                    });
                                    meTask.Wait();

                                    if (meTask?.Result?.StatusCode == HttpStatusCode.OK)
                                    {
                                        var me = meTask.Result.Data.FromBytes <Me>();
                                        _timer = new Timer(ReportMyActivity, me.Links.ReportMyActivity.Href, 0, (int)(3.5 * 60 * 1000));
                                    }

                                    // Request and update communication
                                    var commGetTask = _proxy.ExecuteRequestAsync(new Request
                                    {
                                        Uri    = new Uri(applications.Embedded.Communication.Links.Self.Href, UriKind.Relative),
                                        Method = HttpMethod.Get
                                    });
                                    commGetTask.Wait();

                                    if (commGetTask?.Result?.StatusCode == HttpStatusCode.OK)
                                    {
                                        var comm = commGetTask.Result.Data.FromBytes <Communication>();
                                        comm.SupportedMessageFormats.Add("Html");
                                        var commTask = _proxy.ExecuteRequestAsync(new Request
                                        {
                                            Uri     = new Uri(applications.Embedded.Communication.Links.Self.Href, UriKind.Relative),
                                            Method  = HttpMethod.Put,
                                            Headers = new Dictionary <string, string>
                                            {
                                                { Constants.ContentType, Constants.Json },
                                                { Constants.IfMatch, string.Format("\"{0}\"", comm.ETag) }
                                            },
                                            Data = comm.ToBytes()
                                        });
                                        commTask.Wait();

                                        if (commTask?.Result?.StatusCode == HttpStatusCode.NoContent)
                                        {
                                            System.Console.WriteLine("Communication Updated");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            System.Console.ReadLine();
            _timer.Dispose();
            _timer = null;
        }