public async Task <bool> TryAuthenticateWithUiAsync(ICloudAuthentication authentication)
        {
            if (IsAuthenticated)
            {
                await SignOutAsync();
            }

            IsAuthenticated = !(await authentication.AuthenticateAsync("http://authenticationUri", "http://exceptedUi")).IsCanceled;
            return(IsAuthenticated);
        }
        /// <inheritdoc/>
        public async Task <bool> TryAuthenticateWithUiAsync(ICloudAuthentication authentication)
        {
            var authenticationUriString = $"https://login.live.com/oauth20_authorize.srf?" + $"client_id={SecurityHelper.ToUnsecureString(_clientId)}&redirect_uri={Uri.EscapeDataString(SecurityHelper.ToUnsecureString(_redirectUri))}&response_type=code&display=touch&scope={Uri.EscapeDataString(string.Join(" ", _scope))}";

            var authenticationResult = await authentication.AuthenticateAsync(authenticationUriString, SecurityHelper.ToUnsecureString(_redirectUri));

            if (authenticationResult.IsCanceled)
            {
                IsAuthenticated = false;
                return(false);
            }

            try
            {
                var code = authenticationResult.RedirectedUri?.ToString().Split('=').Last();

                using (var handler = new HttpClientHandler())
                {
                    handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
                    handler.UseDefaultCredentials    = true;
                    handler.Credentials = System.Net.CredentialCache.DefaultCredentials;

                    using (var httpProvider = new HttpProvider(handler, true))
                    {
                        var createMessage = new HttpRequestMessage(HttpMethod.Post, "https://login.live.com/oauth20_token.srf")
                        {
                            Content = new StringContent($"client_id={SecurityHelper.ToUnsecureString(_clientId)}&code={code}&grant_type=authorization_code&redirect_uri={SecurityHelper.ToUnsecureString(_redirectUri)}", Encoding.UTF8, "application/x-www-form-urlencoded")
                        };

                        var response = await httpProvider.SendAsync(createMessage);

                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            var json         = JObject.Parse(await response.Content.ReadAsStringAsync());
                            var refreshToken = json["refresh_token"]?.ToString();
                            TokenProvider.SetToken("RefreshToken", SecurityHelper.EncryptString(SecurityHelper.ToSecureString(refreshToken)));
                            await TryAuthenticateAsync();
                        }
                        else
                        {
                            IsAuthenticated = false;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                IsAuthenticated = false;
            }

            return(IsAuthenticated);
        }
Beispiel #3
0
        /// <summary>
        /// Try to authenticate on a cloud storage provider with the UI and if it succeed, load the informations about the user.
        /// </summary>
        /// <param name="provider">The provider that the user chosen</param>
        /// <param name="authenticationUserControl">The authentication form</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task AuthenticateCloudAsync(CloudStorageProvider provider, ICloudAuthentication authenticationUserControl)
        {
            Requires.NotNull(provider, nameof(provider));
            Requires.NotNull(authenticationUserControl, nameof(authenticationUserControl));
            IsAuthenticatingCloudStorageService = true;

            Logger.Instance.Information($"Authentication to {provider.Name}.");
            if (await _cloudStorageService.GetProviderFromName(provider.Name).TryAuthenticateWithUiAsync(authenticationUserControl))
            {
                Logger.Instance.Information($"Authentication to {provider.Name} succeeded.");
                await LoadCloudUserInfoAsync();
            }
            else
            {
                Logger.Instance.Information($"Authentication to {provider.Name} failed.");
            }

            IsLoadingInfo = false;
            RaisePropertyChanged(nameof(IsLinkedToCloudStorageService));
            _settingProvider.SaveAndApplySettings();
        }
Beispiel #4
0
        /// <inheritdoc/>
        public async Task <bool> TryAuthenticateWithUiAsync(ICloudAuthentication authentication)
        {
            var oauth2State       = Guid.NewGuid().ToString("N");
            var authenticationUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, SecurityHelper.ToUnsecureString(_appKey), new Uri(SecurityHelper.ToUnsecureString(_redirectUri)), oauth2State);

            var authenticationResult = await authentication.AuthenticateAsync(authenticationUri.ToString(), SecurityHelper.ToUnsecureString(_redirectUri));

            if (authenticationResult.IsCanceled)
            {
                IsAuthenticated = false;
                return(IsAuthenticated);
            }

            var result = DropboxOAuth2Helper.ParseTokenFragment(authenticationResult.RedirectedUri);

            if (result.State != oauth2State)
            {
                IsAuthenticated = false;
                return(IsAuthenticated);
            }

            TokenProvider.SetToken("AccessToken", SecurityHelper.EncryptString(SecurityHelper.ToSecureString(result.AccessToken)));
            return(await TryAuthenticateAsync());
        }
Beispiel #5
0
 public Task <bool> TryAuthenticateWithUiAsync(ICloudAuthentication authentication)
 {
     return(TryAuthenticateAsync());
 }