public async Task ReallyDoFetchToken()
        {
            if (!DoRealWebRequests)
            {
                return;
            }

            // Fetch token
            IOAuth2CloudStorageClient client = new OnedriveCloudStorageClient(ClientId, RedirectUrl);
            CloudStorageToken         token  = await client.FetchTokenAsync(OnedriveRedirectedUrl, State, CodeVerifier);

            Assert.IsNotNull(token.AccessToken);
            Assert.IsNotNull(token.RefreshToken);
        }
        public override async Task Run()
        {
            try
            {
                if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.CloudStorageCredentials, out SerializeableCloudStorageCredentials credentials))
                {
                    throw new ArgumentNullException(nameof(credentials));
                }
                if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthState, out string oauthState))
                {
                    throw new ArgumentNullException(nameof(oauthState));
                }
                if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthCodeVerifier, out string oauthCodeVerifier))
                {
                    throw new ArgumentNullException(nameof(oauthState));
                }
                if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthRedirectUrl, out string redirectUrl))
                {
                    throw new ArgumentNullException(nameof(redirectUrl));
                }

                StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthState);
                StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthCodeVerifier);
                StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthRedirectUrl);

                ICloudStorageClient cloudStorageClient = _cloudStorageClientFactory.GetOrCreate(credentials.CloudStorageId);
                if (cloudStorageClient is IOAuth2CloudStorageClient oauthStorageClient)
                {
                    CloudStorageToken token = await oauthStorageClient.FetchTokenAsync(redirectUrl, oauthState, oauthCodeVerifier);

                    if (token != null)
                    {
                        // User has granted access.
                        credentials.Token = token;
                        await StoryBoard.ContinueWith(SynchronizationStoryStepId.ExistsCloudRepository);
                    }
                    else
                    {
                        // User has rejected access.
                        _feedbackService.ShowToast(_languageService.LoadText("sync_reject"));
                        await StoryBoard.ContinueWith(SynchronizationStoryStepId.StopAndShowRepository);
                    }
                }
            }
            catch (Exception ex)
            {
                // Keep the current page open and show the error message
                ShowExceptionMessage(ex, _feedbackService, _languageService);
            }
        }
Example #3
0
        public void ReallyDoFetchToken()
        {
            if (!DoRealWebRequests)
            {
                return;
            }

            const string redirectedUrl = "InsertRedirectedUrl";

            // Fetch token
            IOAuth2CloudStorageClient client = new GoogleCloudStorageClient(ClientId, RedirectUrl);
            CloudStorageToken         token  = Task.Run(async() => await FetchTokenAsync(client, redirectedUrl)).Result;

            Assert.IsNotNull(token.AccessToken);
            Assert.IsNotNull(token.RefreshToken);
        }
        public void ReallyDoFetchToken()
        {
            if (!DoRealWebRequests)
            {
                return;
            }

            const string redirectedUrl = "ch.martinstoeckli.silentnotes://oauth2redirect/?code=Mee5dffeb-fdaa-aca7-d7ba-f281c8b49903&state=7ysv8L9s4LB9CZpA";

            // Fetch token
            IOAuth2CloudStorageClient client = new OnedriveCloudStorageClient(ClientId, RedirectUrl);
            CloudStorageToken         token  = Task.Run(async() => await FetchTokenAsync(client, redirectedUrl)).Result;

            Assert.IsNotNull(token.AccessToken);
            Assert.IsNotNull(token.RefreshToken);
        }
        public void AreEqualWorksWithSameContent()
        {
            CloudStorageToken token = new CloudStorageToken
            {
                AccessToken = "a", RefreshToken = null, ExpiryDate = new DateTime(1984, 11, 11)
            };

            CloudStorageToken token2 = new CloudStorageToken
            {
                AccessToken = "a", RefreshToken = null, ExpiryDate = new DateTime(1984, 11, 11)
            };

            Assert.IsTrue(token.AreEqualOrNull(token2));
            Assert.IsFalse(token.AreEqualOrNull(null));

            token = null;
            Assert.IsTrue(token.AreEqualOrNull(null));
        }
Example #6
0
        public async Task ReallyDoRefreshToken()
        {
            if (!DoRealWebRequests)
            {
                return;
            }

            CloudStorageToken oldToken = new CloudStorageToken
            {
                RefreshToken = GoogledriveRefreshToken,
            };

            // Refresh token
            IOAuth2CloudStorageClient client   = new GoogleCloudStorageClient(ClientId, RedirectUrl);
            CloudStorageToken         newToken = await client.RefreshTokenAsync(oldToken);

            Assert.IsNotNull(newToken.AccessToken);
            Assert.AreNotEqual(oldToken.AccessToken, newToken.AccessToken);
            Assert.AreEqual(oldToken.RefreshToken, newToken.RefreshToken);
        }
Example #7
0
        public void ReallyDoRefreshToken()
        {
            if (!DoRealWebRequests)
            {
                return;
            }

            CloudStorageToken oldToken = new CloudStorageToken
            {
                RefreshToken = "InsertRefreshToken",
            };

            // Refresh token
            IOAuth2CloudStorageClient client   = new GoogleCloudStorageClient(ClientId, RedirectUrl);
            CloudStorageToken         newToken = Task.Run(async() => await RefreshTokenAsync(client, oldToken)).Result;

            Assert.IsNotNull(newToken.AccessToken);
            Assert.AreNotEqual(oldToken.AccessToken, newToken.AccessToken);
            Assert.AreEqual(oldToken.RefreshToken, newToken.RefreshToken);
        }
        public void ReallyDoRefreshToken()
        {
            if (!DoRealWebRequests)
            {
                return;
            }

            CloudStorageToken oldToken = new CloudStorageToken
            {
                RefreshToken = "MCW*!s3pagVCHVuUCmPqx88abRl9N!dzQOLCovpwhFof2ZUvt9I4rkiJ06GzPH0z14d2NtvN2zXksQgI4wuENAaB3znFQ*1ELcB5ApCA1LTs3Sv3JQPIV3NLj1bNgAFFN*lN*btcBcbsksofEnxNx3qULZ5BDdIWmNhxdWWPXGrzq!AaqQwCYSYcf5Lh1gkLYjwcGeXUkHbGloqE3K1TZmb40t0KrJgsAjKnkAp4*S94*Gl7YFR4rzo1MTUdubNhtJRB7PRo1wNkMIRoV1d8653owOeO6oRJzt9GVWc!GJG3t3F40a3R8VtYSsXIevRaNLRaz5LSygDOiRjEO2wekhu9tb7Hh6KDBVFg1dA**YOGc7NVyAxvBDEM7!8NRM35nIQ$$",
            };

            // Refresh token
            IOAuth2CloudStorageClient client   = new OnedriveCloudStorageClient(ClientId, RedirectUrl);
            CloudStorageToken         newToken = Task.Run(async() => await RefreshTokenAsync(client, oldToken)).Result;

            Assert.IsNotNull(newToken.AccessToken);
            Assert.AreNotEqual(oldToken.AccessToken, newToken.AccessToken);
            Assert.AreEqual(oldToken.RefreshToken, newToken.RefreshToken);
        }
Example #9
0
        public void FetchTokenCanInterpretGoogleResponse()
        {
            _httpTest.RespondWith(
                @"{
  ""access_token"": ""aaaa.BBBBB-CDEF"",
  ""expires_in"": 3600,
  ""refresh_token"": ""8/A1AAbbZZ9"",
  ""scope"": ""https://www.googleapis.com/auth/drive.appdata"",
  ""token_type"": ""Bearer""
}");

            string redirectedUrl = "com.example.myapp://oauth2redirect/?state=7ysv8L9s4LB9CZpA&code=ABCDEF&scope=https://www.googleapis.com/auth/drive.appdata";

            // Fetch token
            IOAuth2CloudStorageClient client = new TestClient(GetGoogleConfig());
            CloudStorageToken         token  = Task.Run(async() => await FetchTokenAsync(client, redirectedUrl)).Result;

            Assert.AreEqual("aaaa.BBBBB-CDEF", token.AccessToken);
            Assert.AreEqual("8/A1AAbbZZ9", token.RefreshToken);
        }
Example #10
0
        public void RefreshTokenCanInterpretGoogleResponse()
        {
            _httpTest.RespondWith(
                @"{
  ""access_token"": ""aaaa.BBBBB-CDEF"",
  ""expires_in"": 3600,
  ""scope"": ""https://www.googleapis.com/auth/drive.appdata"",
  ""token_type"": ""Bearer""
}");

            CloudStorageToken oldToken = new CloudStorageToken
            {
                AccessToken  = "dummy",
                RefreshToken = "8/A1AAbbZZ9",
            };

            IOAuth2CloudStorageClient client   = new TestClient(GetGoogleConfig());
            CloudStorageToken         newToken = Task.Run(async() => await RefreshTokenAsync(client, oldToken)).Result;

            Assert.AreEqual("aaaa.BBBBB-CDEF", newToken.AccessToken);
            Assert.AreEqual("8/A1AAbbZZ9", newToken.RefreshToken);
        }
Example #11
0
 private async Task <CloudStorageToken> RefreshTokenAsync(IOAuth2CloudStorageClient client, CloudStorageToken token)
 {
     return(await client.RefreshTokenAsync(token));
 }