public AuthorizationModel(IEnumerable<IResourceScope> availableServices, ILocalStore localStore, ILoggerFactory loggerFactory)
        {
            _localStore = localStore;
            _logger = loggerFactory.GetLogger();
            _availableServices = availableServices.ToArray();
            _selectedServices = new ObservableCollection<IResourceScope>(_availableServices);

            _currentSession = LoadSession();
            if (_currentSession != null)
            {
                _status.OnNext(AuthorizationStatus.Authorized);
            }
        }
        private IObservable<Session> RequestAccessToken(string authorizationCode)
        {
            return Observable.Create<Session>(
                o =>
                {
                    try
                    {
                        var request = CreateAccessTokenWebRequest(authorizationCode);
                        var requestedAt = DateTimeOffset.Now;

                        var q = from response in request.GetResponseAsync().ToObservable()
                                let responseStream = response.GetResponseStream()
                                let reader = new StreamReader(responseStream)
                                from x in reader.ReadToEndAsync().ToObservable()
                                select x;

                        return q.Select(payload => JObject.Parse(payload))
                                .Select(json => new Session(
                                    (string)json["access_token"],
                                    (string)json["refresh_token"],
                                    TimeSpan.FromSeconds((int)json["expires_in"]),
                                    requestedAt))
                                .Subscribe(o);

                        //Old code
                        using (var response = request.GetResponse())
                        {
                            var responseStream = response.GetResponseStream();
                            using (var reader = new StreamReader(responseStream))
                            {
                                var result = reader.ReadToEnd();
                                var payload = JObject.Parse(result);
                                var session = new Session(
                                    (string)payload["access_token"],
                                    (string)payload["refresh_token"],
                                    TimeSpan.FromSeconds((int)payload["expires_in"]),
                                    requestedAt);
                                o.OnNext(session);
                                o.OnCompleted();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        o.OnError(e);
                    }
                    return Disposable.Empty;    //TODO: Provide real Cancellation feature.
                })
                .Log(_logger, string.Format("requestAccessToken({0})", authorizationCode));
        }
 private IObservable<Session> RequestRefreshedAccessToken(string refreshToken)
 {
     return Observable.Create<Session>(
         o =>
         {
             try
             {
                 var request = CreateRefreshTokenWebRequest(refreshToken);
                 var requestedAt = DateTimeOffset.Now;
                 using (var response = request.GetResponse())
                 {
                     var responseStream = response.GetResponseStream();
                     using (var reader = new StreamReader(responseStream))
                     {
                         var result = reader.ReadToEnd();
                         var payload = JObject.Parse(result);
                         var session = new Session(
                             (string)payload["access_token"],
                             refreshToken,
                             TimeSpan.FromSeconds((int)payload["expires_in"]),
                             requestedAt);
                         o.OnNext(session);
                         o.OnCompleted();
                     }
                 }
             }
             catch (Exception e)
             {
                 o.OnError(e);
             }
             return Disposable.Empty;    //TODO: Provide real Cancellation feature.
         })
         .Log(_logger, string.Format("requestRefreshedAccessToken({0})", refreshToken));
 }
        public IObservable<string> RequestAccessToken()
        {
            var currentSession = Observable.Return(CurrentSession);
            var refreshSession = Observable.Defer(RefreshSession);
            var createSession = Observable.Defer(CreateSession);

            var sessionPriorities = ObservableExtensions.LazyConcat(currentSession, refreshSession, createSession);

            var sequence = sessionPriorities
                .Where(session => session != null && !session.HasExpired())
                .Do(session => CurrentSession = session)
                .Take(1)
                .Select(session => session.AccessToken);
            return sequence.Log(_logger, "RequestAccessToken()");
        }