Ejemplo n.º 1
0
        protected override TransientCredentials Login()
        {
            TransientCredentials tc = base.Login();

            VerifyAndRefreshCredentials(tc);
            return(tc);
        }
Ejemplo n.º 2
0
        private void VerifyAndRefreshCredentials(TransientCredentials tc)
        {
            var userCredential = tc.Token as UserCredential;
            var token          = userCredential?.Token;

            if (IsValidToken(token))
            {
                // We already have a valid OAuth token.
                return;
            }

            if (userCredential == null)
            {
                // Attempt to load a cached OAuth token.
                var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecretsStream = ClientSecretsStream,
                    DataStore           = GetCredentialsDataStoreForBlog(tc.Username),
                    Scopes = GoogleAPIScopes,
                });

                var loadTokenTask = flow.LoadTokenAsync(tc.Username, CancellationToken.None);
                loadTokenTask.Wait();
                if (loadTokenTask.IsCompleted)
                {
                    // We were able re-create the user credentials from the cache.
                    userCredential = new UserCredential(flow, tc.Username, loadTokenTask.Result);
                    token          = loadTokenTask.Result;
                }
            }

            if (!IsValidToken(token))
            {
                // The token is invalid, so we need to login again. This likely includes popping out a new browser window.
                if (BlogClientUIContext.SilentModeForCurrentThread)
                {
                    // If we're in silent mode where prompting isn't allowed, throw the verification exception
                    throw new BlogClientAuthenticationException(String.Empty, String.Empty);
                }

                // Start an OAuth flow to renew the credentials.
                var authorizationTask = GetOAuth2AuthorizationAsync(tc.Username, CancellationToken.None);
                authorizationTask.Wait();
                if (authorizationTask.IsCompleted)
                {
                    userCredential = authorizationTask.Result;
                    token          = userCredential?.Token;
                }
            }

            if (!IsValidToken(token))
            {
                // The token is still invalid after all of our attempts to refresh it. The user did not complete the
                // authorization flow, so we interpret that as a cancellation.
                throw new BlogClientOperationCancelledException();
            }

            // Stash the valid user credentials.
            tc.Token = userCredential;
        }
Ejemplo n.º 3
0
        private void RefreshAccessToken(TransientCredentials transientCredentials)
        {
            // Using the BloggerService automatically refreshes the access token, but we call the Picasa endpoint
            // directly and therefore need to force refresh the access token on occasion.
            var userCredential = transientCredentials.Token as UserCredential;

            userCredential?.RefreshTokenAsync(CancellationToken.None).Wait();
        }
Ejemplo n.º 4
0
        protected override void VerifyCredentials(TransientCredentials tc)
        {
            // This sucks. We really want to authenticate against the actual feed,
            // not just the service document.
            Uri uri = FeedServiceUrl;

            xmlRestRequestHelper.Get(ref uri, RequestFilter);
        }
Ejemplo n.º 5
0
        private BloggerService GetService()
        {
            TransientCredentials transientCredentials = Login();

            return(new BloggerService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = (UserCredential)transientCredentials.Token,
                ApplicationName = string.Format(CultureInfo.InvariantCulture, "{0} {1}", ApplicationEnvironment.ProductName, ApplicationEnvironment.ProductVersion),
            }));
        }
Ejemplo n.º 6
0
 public override void DeletePost(string blogId, string postId, bool publish)
 {
     TransientCredentials tc = Login();
     XmlNode result          = CallMethod("blogger.deletePost",
                                          new XmlRpcString(APP_KEY),
                                          new XmlRpcString(postId),
                                          new XmlRpcString(tc.Username),
                                          new XmlRpcString(tc.Password, true),
                                          new XmlRpcBoolean(publish));
 }
Ejemplo n.º 7
0
        public static GDataCredentials FromCredentials(TransientCredentials credentials)
        {
            GDataCredentials cred = credentials.Token as GDataCredentials;

            if (cred == null)
            {
                credentials.Token = cred = new GDataCredentials();
            }
            return(cred);
        }
Ejemplo n.º 8
0
        private HttpCredentialsProvider GetHttpCredentialsProvider()
        {
            TransientCredentials tc = Credentials.TransientCredentials as TransientCredentials;

            if (tc != null)
            {
                HttpCredentialsProvider credentialsProvider = tc.Token as HttpCredentialsProvider;
                return(credentialsProvider);
            }
            return(null);
        }
Ejemplo n.º 9
0
        private void PostNewImage(string albumName, string filename, out string srcUrl, out string editUri)
        {
            TransientCredentials transientCredentials = Credentials.TransientCredentials as TransientCredentials;

            GDataCredentials.FromCredentials(transientCredentials).EnsureLoggedIn(transientCredentials.Username, transientCredentials.Password, GDataCredentials.PICASAWEB_SERVICE_NAME, false);

            string          albumUrl = GetBlogImagesAlbum(albumName);
            HttpWebResponse response = RedirectHelper.GetResponse(albumUrl, new RedirectHelper.RequestFactory(new UploadFileRequestFactory(this, filename, "POST").Create));

            using (Stream s = response.GetResponseStream())
                ParseMediaEntry(s, out srcUrl, out editUri);
        }
        private async Task RefreshAccessToken(TransientCredentials transientCredentials)
        {
            // Using the BloggerService automatically refreshes the access token, but we call the Picasa endpoint
            // directly and therefore need to force refresh the access token on occasion.
            var userCredential    = transientCredentials.Token as UserCredential;
            var refreshTokenAsync = userCredential?.RefreshTokenAsync(CancellationToken.None);

            if (refreshTokenAsync != null)
            {
                await refreshTokenAsync;
            }
        }
        private void VerifyAndRefreshCredentials(TransientCredentials tc)
        {
            GDataCredentials gc = GDataCredentials.FromCredentials(tc);

            if (gc.IsValid(tc.Username, tc.Password, GDataCredentials.BLOGGER_SERVICE_NAME))
                return;
            else
            {
                bool showUI = !BlogClientUIContext.SilentModeForCurrentThread;
                gc.EnsureLoggedIn(tc.Username, tc.Password, GDataCredentials.BLOGGER_SERVICE_NAME, showUI);
                return;
            }
        }
Ejemplo n.º 12
0
        private void InitTransientCredential(TransientCredentials tc)
        {
            HttpCredentialsProvider credentialsProvider = new HttpCredentialsProvider(UrlHelper.SafeToAbsoluteUri(_postApiUrl), Credentials, tc.Username, tc.Password);

            tc.Token = credentialsProvider;

            bool   useMetaweblogCredentials = credentialsProvider.GetAuthenticationScheme() == AuthenticationScheme.MetaWeblog;
            string username = useMetaweblogCredentials ? Credentials.Username : "";
            string password = useMetaweblogCredentials ? Credentials.Password : "";

            tc.Username = username;
            tc.Password = password;
        }
Ejemplo n.º 13
0
        protected virtual HttpRequestFilter CreateCredentialsFilter(string requestUri)
        {
            TransientCredentials tc = Login();

            if (tc != null)
            {
                return(HttpRequestCredentialsFilter.Create(tc.Username, tc.Password, requestUri, true));
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 14
0
        private bool AuthorizeRequest(HttpWebRequest request, string serviceName)
        {
            // This line is required to avoid Error 500 from non-beta Blogger blogs.
            // According to Pete Hopkins it is "something with .NET".
            request.Accept = "*/*";

            TransientCredentials transientCredentials = Login();
            GDataCredentials     cred = GDataCredentials.FromCredentials(transientCredentials);

            return(cred.AttachCredentialsIfValid(
                       request,
                       transientCredentials.Username,
                       transientCredentials.Password,
                       serviceName));
        }
Ejemplo n.º 15
0
        private void VerifyAndRefreshCredentials(TransientCredentials tc)
        {
            GDataCredentials gc = GDataCredentials.FromCredentials(tc);

            if (gc.IsValid(tc.Username, tc.Password, GDataCredentials.BLOGGER_SERVICE_NAME))
            {
                return;
            }
            else
            {
                // bool showUI = !BlogClientUIContext.SilentModeForCurrentThread;
                //gc.EnsureLoggedIn(tc.Username, tc.Password, GDataCredentials.BLOGGER_SERVICE_NAME, showUI);
                return;
            }
        }
Ejemplo n.º 16
0
        public static bool VerifyCredentialsAndDetectAuthScheme(IBlogCredentials blogCredentials, SharePointClient client)
        {
            //Attempt to execute the GetUsersBlogs() operation using standard HTTP authentication
            //If the server challenges with an HTTP 401, but doesn't include with WWW-authentication
            //header, then the server is configured to use MetaWeblog for authentication, so we
            //re-issue the request using that authentication scheme instead.

            AuthenticationScheme authScheme         = AuthenticationScheme.Http;
            AuthenticationScheme requiredAuthScheme = AuthenticationScheme.Unknown;

            while (requiredAuthScheme == AuthenticationScheme.Unknown || authScheme != requiredAuthScheme)
            {
                if (requiredAuthScheme != AuthenticationScheme.Unknown)
                {
                    authScheme = requiredAuthScheme;
                }

                blogCredentials.SetCustomValue(AUTH_SCHEME, authScheme.ToString());
                try
                {
                    TransientCredentials tc = new TransientCredentials(blogCredentials.Username, blogCredentials.Password, null);
                    client.Credentials.TransientCredentials = tc;
                    client.InitTransientCredential(tc);
                    client.GetUsersBlogs();
                    return(true);
                }
                catch (WebException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e);
                }
                catch (BlogClientHttpErrorException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e.Exception);
                }
                catch (BlogClientAuthenticationException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e.WebException);
                }
                catch (Exception)
                {
                    throw;
                }
                Debug.Assert(requiredAuthScheme != AuthenticationScheme.Unknown, "Unexpected authscheme");                 //this would cause an infinite loop!
            }
            return(false);
        }
 protected override void VerifyCredentials(TransientCredentials tc)
 {
     try
     {
         GetUsersBlogs(tc.Username, tc.Password);
         return;
     }
     catch (BlogClientAuthenticationException)
     {
         throw;
     }
     catch (Exception e)
     {
         if (!BlogClientUIContext.SilentModeForCurrentThread)
             ShowError(e.Message);
         throw;
     }
 }
        protected override async Task VerifyCredentials(TransientCredentials tc)
        {
            try
            {
                await GetUsersBlogs(tc.Username, tc.Password);

                return;
            }
            catch (BlogClientAuthenticationException)
            {
                throw;
            }
            catch (Exception e)
            {
                //if(!BlogClientUIContext.SilentModeForCurrentThread)
                //	ShowError(e.Message);
                throw;
            }
        }
Ejemplo n.º 19
0
        public bool Login(string username, string password)
        {
            using (new WaitCursor())
            {
                try
                {
                    _username = username;
                    _password = password;
                    TransientCredentials creds = new TransientCredentials(username, password, null);
                    _credentials = GDataCredentials.FromCredentials(creds);
                    _credentials.EnsureLoggedIn(username, password, GDataCredentials.YOUTUBE_SERVICE_NAME, true, GDataCredentials.YOUTUBE_CLIENT_LOGIN_URL);
                }
                catch (Exception)
                {
                    _credentials = null;
                }
            }

            OnLoginStatusChanged();
            return(IsLoggedIn);
        }
Ejemplo n.º 20
0
 protected override void VerifyCredentials(TransientCredentials tc)
 {
     InitTransientCredential(tc);
     base.VerifyCredentials(tc);
 }
 protected override void VerifyCredentials(TransientCredentials tc)
 {
     // This sucks. We really want to authenticate against the actual feed,
     // not just the service document.
     Uri uri = FeedServiceUrl;
     xmlRestRequestHelper.Get(ref uri, RequestFilter);
 }
Ejemplo n.º 22
0
        private void InitTransientCredential(TransientCredentials tc)
        {
            HttpCredentialsProvider credentialsProvider = new HttpCredentialsProvider(UrlHelper.SafeToAbsoluteUri(_postApiUrl), Credentials, tc.Username, tc.Password);
            tc.Token = credentialsProvider;

            bool useMetaweblogCredentials = credentialsProvider.GetAuthenticationScheme() == AuthenticationScheme.MetaWeblog;
            string username = useMetaweblogCredentials ? Credentials.Username : "";
            string password = useMetaweblogCredentials ? Credentials.Password : "";
            tc.Username = username;
            tc.Password = password;
        }
Ejemplo n.º 23
0
 protected override void VerifyCredentials(TransientCredentials tc)
 {
     VerifyAndRefreshCredentials(tc);
 }
Ejemplo n.º 24
0
        protected virtual async Task <HttpAsyncRequestFilter> CreateCredentialsFilter(string requestUri)
        {
            TransientCredentials tc = await Login();

            return(HttpRequestCredentialsFilter.Create(tc.Username, tc.Password, requestUri, true));
        }
Ejemplo n.º 25
0
        public static bool VerifyCredentialsAndDetectAuthScheme(IBlogCredentials blogCredentials, SharePointClient client)
        {
            //Attempt to execute the GetUsersBlogs() operation using standard HTTP authentication
            //If the server challenges with an HTTP 401, but doesn't include with WWW-authentication
            //header, then the server is configured to use MetaWeblog for authentication, so we
            //re-issue the request using that authentication scheme instead.

            AuthenticationScheme authScheme = AuthenticationScheme.Http;
            AuthenticationScheme requiredAuthScheme = AuthenticationScheme.Unknown;

            while (requiredAuthScheme == AuthenticationScheme.Unknown || authScheme != requiredAuthScheme)
            {
                if (requiredAuthScheme != AuthenticationScheme.Unknown)
                    authScheme = requiredAuthScheme;

                blogCredentials.SetCustomValue(AUTH_SCHEME, authScheme.ToString());
                try
                {
                    TransientCredentials tc = new TransientCredentials(blogCredentials.Username, blogCredentials.Password, null);
                    client.Credentials.TransientCredentials = tc;
                    client.InitTransientCredential(tc);
                    client.GetUsersBlogs();
                    return true;
                }
                catch (WebException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e);
                }
                catch (BlogClientHttpErrorException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e.Exception);
                }
                catch (BlogClientAuthenticationException e)
                {
                    requiredAuthScheme = GetRequiredAuthScheme(e.WebException);
                }
                catch (Exception)
                {
                    throw;
                }
                Debug.Assert(requiredAuthScheme != AuthenticationScheme.Unknown, "Unexpected authscheme"); //this would cause an infinite loop!
            }
            return false;
        }
Ejemplo n.º 26
0
 protected override void VerifyCredentials(TransientCredentials tc)
 {
     InitTransientCredential(tc);
     base.VerifyCredentials(tc);
 }
Ejemplo n.º 27
0
 protected override void VerifyCredentials(TransientCredentials transientCredentials)
 {
 }
        private void VerifyAndRefreshCredentials(TransientCredentials tc)
        {
            var userCredential = tc.Token as UserCredential;
            var token = userCredential?.Token;

            if (IsValidToken(token))
            {
                // We already have a valid OAuth token.
                return;
            }

            if (userCredential == null)
            {
                // Attempt to load a cached OAuth token.
                var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecretsStream = ClientSecretsStream,
                    DataStore = GetCredentialsDataStoreForBlog(tc.Username),
                    Scopes = new List<string>() { BloggerServiceScope, PicasaServiceScope },
                });

                var loadTokenTask = flow.LoadTokenAsync(tc.Username, CancellationToken.None);
                loadTokenTask.Wait();
                if (loadTokenTask.IsCompleted)
                {
                    // We were able re-create the user credentials from the cache.
                    userCredential = new UserCredential(flow, tc.Username, loadTokenTask.Result);
                    token = loadTokenTask.Result;
                }
            }

            if (!IsValidToken(token))
            {
                // The token is invalid, so we need to login again. This likely includes popping out a new browser window.
                if (BlogClientUIContext.SilentModeForCurrentThread)
                {
                    // If we're in silent mode where prompting isn't allowed, throw the verification exception
                    throw new BlogClientAuthenticationException(String.Empty, String.Empty);
                }

                // Start an OAuth flow to renew the credentials.
                var authorizationTask = GetOAuth2AuthorizationAsync(tc.Username, CancellationToken.None);
                authorizationTask.Wait();
                if (authorizationTask.IsCompleted)
                {
                    userCredential = authorizationTask.Result;
                    token = userCredential?.Token;
                }
            }

            if (!IsValidToken(token))
            {
                // The token is still invalid after all of our attempts to refresh it. The user did not complete the 
                // authorization flow, so we interpret that as a cancellation.
                throw new BlogClientOperationCancelledException();
            }

            // Stash the valid user credentials.
            tc.Token = userCredential;
        }
 private void RefreshAccessToken(TransientCredentials transientCredentials)
 {
     // Using the BloggerService automatically refreshes the access token, but we call the Picasa endpoint 
     // directly and therefore need to force refresh the access token on occasion.
     var userCredential = transientCredentials.Token as UserCredential;
     userCredential?.RefreshTokenAsync(CancellationToken.None).Wait();
 }
 protected override void VerifyCredentials(TransientCredentials tc)
 {
     VerifyAndRefreshCredentials(tc);
 }
Ejemplo n.º 31
0
 public static GDataCredentials FromCredentials(TransientCredentials credentials)
 {
     GDataCredentials cred = credentials.Token as GDataCredentials;
     if (cred == null)
     {
         credentials.Token = cred = new GDataCredentials();
     }
     return cred;
 }
Ejemplo n.º 32
0
        public override BlogInfo[] GetUsersBlogs()
        {
            TransientCredentials tc = Login();

            return(GetUsersBlogs(tc.Username, tc.Password));
        }
 protected override Task VerifyCredentials(TransientCredentials tc)
 {
     return(VerifyAndRefreshCredentials(tc));
 }
        //private void ShowError(string error)
        //{
        //	ShowErrorHelper helper =
        //		new ShowErrorHelper(BlogClientUIContext.ContextForCurrentThread, MessageId.UnexpectedErrorLogin,
        //		                    new object[] {error});
        //	if (BlogClientUIContext.ContextForCurrentThread != null)
        //		BlogClientUIContext.ContextForCurrentThread.Invoke(new ThreadStart(helper.Show), null);
        //	else
        //		helper.Show();
        //}

        //private class ShowErrorHelper
        //{
        //	private readonly IWin32Window _owner;
        //	private readonly MessageId _messageId;
        //	private readonly object[] _args;

        //	public ShowErrorHelper(IWin32Window owner, MessageId messageId, object[] args)
        //	{
        //		_owner = owner;
        //		_messageId = messageId;
        //		_args = args;
        //	}

        //	public void Show()
        //	{
        //		DisplayMessage.Show(_messageId, _owner, _args);
        //	}
        //}

        public override async Task <BlogInfo[]> GetUsersBlogsAsync()
        {
            TransientCredentials tc = await Login();

            return(await GetUsersBlogs(tc.Username, tc.Password));
        }