Пример #1
0
        /////////////////////////////////////////////////////////////////////////////



        //////////////////////////////////////////////////////////////////////
        /// <summary>goes to the Google auth service, and gets a new auth token</summary>
        /// <returns>the auth token, or NULL if none received</returns>
        //////////////////////////////////////////////////////////////////////
        internal string QueryAuthToken(GDataCredentials gc)
        {
            Uri authHandler = null;

            // need to copy this to a new object to avoid that people mix and match
            // the old (factory) and the new (requestsettings) and get screwed. So
            // copy the settings from the gc passed in and mix with the settings from the factory
            GDataCredentials gdc = new GDataCredentials(gc.Username, gc.getPassword());

            gdc.CaptchaToken  = this.factory.CaptchaToken;
            gdc.CaptchaAnswer = this.factory.CaptchaAnswer;
            gdc.AccountType   = this.factory.AccountType;

            try
            {
                authHandler = new Uri(this.factory.Handler);
            }
            catch
            {
                throw new GDataRequestException("Invalid authentication handler URI given");
            }

            return(Utilities.QueryClientLoginToken(gdc,
                                                   this.factory.Service,
                                                   this.factory.ApplicationName,
                                                   this.factory.KeepAlive,
                                                   authHandler));
        }
Пример #2
0
 //////////////////////////////////////////////////////////////////////
 /// <summary>sets up the correct credentials for this call, pending
 /// security scheme</summary>
 //////////////////////////////////////////////////////////////////////
 protected override void EnsureCredentials()
 {
     //Tracing.Assert(this.Request!= null, "We should have a webrequest now");
     if (this.Request == null)
     {
         return;
     }
     // if the token is NULL, we need to get a token.
     if (this.factory.GAuthToken == null)
     {
         // we will take the standard credentials for that
         GDataCredentials gc = this.Credentials;
         //Tracing.TraceMsg(gc == null ? "No Network credentials set" : "Network credentials found");
         if (gc != null)
         {
             // only now we have something to do...
             this.factory.GAuthToken = QueryAuthToken(gc);
         }
     }
     if (this.factory.GAuthToken != null && this.factory.GAuthToken.Length > 0)
     {
         // //Tracing.Assert(this.factory.GAuthToken != null, "We should have a token now");
         //Tracing.TraceMsg("Using auth token: " + this.factory.GAuthToken);
         string strHeader = GoogleAuthentication.Header + this.factory.GAuthToken;
         this.Request.Headers.Add(strHeader);
     }
 }
Пример #3
0
 /// <summary>
 ///  a constructor for client login use cases
 /// </summary>
 /// <param name="applicationName">The name of the application</param>
 /// <param name="credentials">the user credentials</param>
 /// <returns></returns>
 public ClientLoginAuthenticator(string applicationName,
                                 string serviceName,
                                 GDataCredentials credentials,
                                 Uri clientLoginHandler)
     : base(applicationName)
 {
     this.credentials  = credentials;
     this.serviceName  = serviceName;
     this.loginHandler = clientLoginHandler == null ?
                         new Uri(GoogleAuthentication.UriHandler) : clientLoginHandler;
 }
Пример #4
0
 /// <summary>
 ///  a constructor for client login use cases
 /// </summary>
 /// <param name="applicationName">The name of the application</param>
 /// <param name="credentials">the user credentials</param>
 /// <returns></returns>
 public ClientLoginAuthenticator(string applicationName, string serviceName,
                                 GDataCredentials credentials)
     : this(applicationName, serviceName, credentials, null)
 {
 }
Пример #5
0
        //////////////////////////////////////////////////////////////////////
        /// <summary>goes to the Google auth service, and gets a new auth token</summary>
        /// <returns>the auth token, or NULL if none received</returns>
        //////////////////////////////////////////////////////////////////////
        public static string QueryClientLoginToken(GDataCredentials gc,
                                                   string serviceName,
                                                   string applicationName,
                                                   bool fUseKeepAlive,
                                                   Uri clientLoginHandler
                                                   )
        {
            //Tracing.Assert(gc != null, "Do not call QueryAuthToken with no network credentials");
            if (gc == null)
            {
                throw new System.ArgumentNullException("nc", "No credentials supplied");
            }

            //ServicePointManager.CertificatePolicy = delegate { return true; };
            ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };
            ServicePointManager.CheckCertificateRevocationList      = false;

            HttpWebRequest authRequest = WebRequest.Create(clientLoginHandler) as HttpWebRequest;

            authRequest.KeepAlive = fUseKeepAlive;

            string accountType = GoogleAuthentication.AccountType;

            if (!String.IsNullOrEmpty(gc.AccountType))
            {
                accountType += gc.AccountType;
            }
            else
            {
                accountType += GoogleAuthentication.AccountTypeDefault;
            }

            WebResponse authResponse = null;

            string authToken = null;

            try
            {
                authRequest.ContentType = HttpFormPost.Encoding;
                authRequest.Method      = HttpMethods.Post;
                ASCIIEncoding encoder = new ASCIIEncoding();

                string user = gc.Username == null ? "" : gc.Username;
                string pwd  = gc.getPassword() == null ? "" : gc.getPassword();

                // now enter the data in the stream
                string postData = GoogleAuthentication.Email + "=" + Utilities.UriEncodeUnsafe(user) + "&";
                postData += GoogleAuthentication.Password + "=" + Utilities.UriEncodeUnsafe(pwd) + "&";
                postData += GoogleAuthentication.Source + "=" + Utilities.UriEncodeUnsafe(applicationName) + "&";
                postData += GoogleAuthentication.Service + "=" + Utilities.UriEncodeUnsafe(serviceName) + "&";
                if (gc.CaptchaAnswer != null)
                {
                    postData += GoogleAuthentication.CaptchaAnswer + "=" + Utilities.UriEncodeUnsafe(gc.CaptchaAnswer) + "&";
                }
                if (gc.CaptchaToken != null)
                {
                    postData += GoogleAuthentication.CaptchaToken + "=" + Utilities.UriEncodeUnsafe(gc.CaptchaToken) + "&";
                }
                postData += accountType;

                byte[] encodedData = encoder.GetBytes(postData);
                authRequest.ContentLength = encodedData.Length;

                Stream requestStream = authRequest.GetRequestStream();
                requestStream.Write(encodedData, 0, encodedData.Length);
                requestStream.Close();
                authResponse = authRequest.GetResponse();
            }
            catch (WebException)
            {
                //Tracing.TraceMsg("QueryAuthtoken failed " + e.Status + " " + e.Message);
                throw;
            }
            HttpWebResponse response = authResponse as HttpWebResponse;

            if (response != null)
            {
                // check the content type, it must be text
                if (!response.ContentType.StartsWith(HttpFormPost.ReturnContentType))
                {
                    throw new GDataRequestException("Execution of authentication request returned unexpected content type: " + response.ContentType, response);
                }
                TokenCollection tokens = Utilities.ParseStreamInTokenCollection(response.GetResponseStream());
                authToken = Utilities.FindToken(tokens, GoogleAuthentication.AuthToken);

                if (authToken == null)
                {
                    throw Utilities.getAuthException(tokens, response);
                }
                // failsafe. if getAuthException did not catch an error...
                int code = (int)response.StatusCode;
                if (code != 200)
                {
                    throw new GDataRequestException("Execution of authentication request returned unexpected result: " + code, response);
                }
            }
            //Tracing.Assert(authToken != null, "did not find an auth token in QueryAuthToken");
            if (authResponse != null)
            {
                authResponse.Close();
            }

            return(authToken);
        }
Пример #6
0
        /////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        /// <summary>Get's an authentication token for the current credentials</summary>
        //////////////////////////////////////////////////////////////////////
        public string QueryAuthToken(GDataCredentials gc)
        {
            GDataGAuthRequest request = new GDataGAuthRequest(GDataRequestType.Query, null, this);

            return(request.QueryAuthToken(gc));
        }