public async Task Save(Session session)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         serializer.WriteObject(ms, session);
         var bytes = ms.ToArray();
         await SaveAccessToken(Encoding.UTF8.GetString(bytes, 0, bytes.Length), session.accountId);
     }
 }
 public async Task init(Config config)
 {
     this.config = config;
     try
     {
         session = await repository.Read(config.accountId);
     }
     catch (IOException e)
     {
         session = new Session() { accountId = config.accountId };
     }
 }
        public void ShouldTellWhenExpiredAccess()
        {
            //given
            Session session = new Session();
            session.accessTokenExpirationDate = new DateTime(2014, 12, 16);

            //when
            var test = session.TokenIsNotExpired();

            //then
            Assert.IsFalse(test);
        }
        protected override async Task<Session> ParseResponse(Stream respondeStream)
        {
            StreamReader reader = new StreamReader(respondeStream);
            string queryString = await reader.ReadToEndAsync();
            IDictionary<string, string> data = ParseQueryString(queryString);

            Session session = new Session() 
            {
                accessToken = data["access_token"],
                accessTokenExpiration = int.Parse(data["expires"])
            };
            return session;
        }
        public async Task ShouldRequestRenewToken()
        {
            //given
            Session session = new Session()
            {
                accessToken = "dummy",
                accessTokenExpirationDate = DateTime.Now.AddDays(-1),
                refreshTokenExpirationDate = DateTime.Now.AddDays(1),
                refreshToken = "dummy-token"
            };
            MockOAuth2Module module = new MockOAuth2Module(session);

            //when
            await module.RequestAccessAndContinue();

            //then
            module.AssertCalled("RefreshAccessToken");
        }
        public void ShouldCreateAuthenticationTuple()
        {
            //given
            string token = "token";
            Session session = new Session()
            {
                accessToken = token,
                accessTokenExpirationDate = DateTime.Now.AddDays(-1),
                refreshTokenExpirationDate = DateTime.Now.AddDays(1),
                refreshToken = "dummy-token"
            };
            MockOAuth2Module module = new MockOAuth2Module(session);

            //when
            var field = module.AuthorizationFields();

            //then
            Assert.IsNotNull(field);
            Assert.AreEqual("Bearer " + token, field.Item2);
        }
        public void serialisationTest()
        {
            //given
            Session session = new Session();
            session.accessTokenExpirationDate = new DateTime(2014, 12, 16);

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Session));
            Session readBackSession = null;

            //when
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, session);
                var bytes = ms.ToArray();

                using (MemoryStream stream = new MemoryStream(bytes))
                {
                    readBackSession = (Session)serializer.ReadObject(ms);
                }
            }

            //then
            Assert.AreEqual(session.accessTokenExpirationDate, readBackSession.accessTokenExpirationDate);
        }
        private async Task UpdateToken(Dictionary<string, string> parameters, string endpoint)
        {
            var request = WebRequest.Create(config.baseURL + endpoint);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            using (var postStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
            {
                foreach (KeyValuePair<string, string> entry in parameters)
                {
                    var bytes = Encoding.UTF8.GetBytes(entry.Key + "=" + WebUtility.UrlEncode(entry.Value) + "&");
                    postStream.Write(bytes, 0, bytes.Length);
                }
            }

            using (var response = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request))
            {
                session = await ParseResponse(response.GetResponseStream());
                session.accountId = config.accountId;
                await repository.Save(session);
            }
        }
 public MockOAuth2Module(Session session)
 {
     this.session = session;
 }