Author: gonzalo
Date: 2006-10-05 00:59:05 -0400 (Thu, 05 Oct 2006)
New Revision: 66277
Added:
trunk/google-sharp/src/Mono.Google/AuthErrorCode.cs
trunk/google-sharp/src/Mono.Google/CaptchaException.cs
Modified:
trunk/google-sharp/ChangeLog
trunk/google-sharp/src/Makefile.am
trunk/google-sharp/src/Mono.Google/Authentication.cs
trunk/google-sharp/src/Mono.Google/GoogleConnection.cs
trunk/google-sharp/src/Mono.Google/Makefile.am
Log:
2006-10-05 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>
* src/Mono.Google/GoogleConnection.cs:
* src/Mono.Google/Authentication.cs:
* src/Mono.Google/CaptchaException.cs:
* src/Mono.Google/Makefile.am:
* src/Mono.Google/AuthErrorCode.cs:
* src/Makefile.am: handle the case when google sends a captcha
challenge. We will now throw a CaptchaException with the image url and
and token. We should show the user the image and request authentication
again using the token and the user input. If this is not possible,
the user should be told about CaptchaException.UnlockCaptchaURL if he
wants to disable captchas fos his IP/machine/user.
Modified: trunk/google-sharp/ChangeLog
===================================================================
--- trunk/google-sharp/ChangeLog 2006-10-05 04:32:16 UTC (rev 66276)
+++ trunk/google-sharp/ChangeLog 2006-10-05 04:59:05 UTC (rev 66277)
@@ -1,3 +1,17 @@
+2006-10-05 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>
+
+ * src/Mono.Google/GoogleConnection.cs:
+ * src/Mono.Google/Authentication.cs:
+ * src/Mono.Google/CaptchaException.cs:
+ * src/Mono.Google/Makefile.am:
+ * src/Mono.Google/AuthErrorCode.cs:
+ * src/Makefile.am: handle the case when google sends a captcha
+ challenge. We will now throw a CaptchaException with the image url and
+ and token. We should show the user the image and request authentication
+ again using the token and the user input. If this is not possible,
+ the user should be told about CaptchaException.UnlockCaptchaURL if he
+ wants to disable captchas fos his IP/machine/user.
+
2006-09-16 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>
* src/Mono.Google/GoogleConnection.cs: avoid nullref.
Modified: trunk/google-sharp/src/Makefile.am
===================================================================
--- trunk/google-sharp/src/Makefile.am 2006-10-05 04:32:16 UTC (rev 66276)
+++ trunk/google-sharp/src/Makefile.am 2006-10-05 04:59:05 UTC (rev 66277)
@@ -14,6 +14,8 @@
monogoogle_sources = \
Mono.Google/Authentication.cs \
+ Mono.Google/AuthErrorCode.cs \
+ Mono.Google/CaptchaException.cs \
Mono.Google/GoogleService.cs \
Mono.Google/GoogleConnection.cs \
Mono.Google/MultipartRequest.cs \
Added: trunk/google-sharp/src/Mono.Google/AuthErrorCode.cs
===================================================================
--- trunk/google-sharp/src/Mono.Google/AuthErrorCode.cs 2006-10-05 04:32:16 UTC
(rev 66276)
+++ trunk/google-sharp/src/Mono.Google/AuthErrorCode.cs 2006-10-05 04:59:05 UTC
(rev 66277)
@@ -0,0 +1,41 @@
+//
+// Mono.Google.AuthErrorCode
+//
+// Authors:
+// Gonzalo Paniagua Javier ([EMAIL PROTECTED])
+//
+// Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+namespace Mono.Google {
+ enum AuthErrorCode {
+ None,
+ BadAuthentication,
+ NotVerified,
+ TermsNotAgreed,
+ CaptchaRequired,
+ Unknown,
+ AccountDeleted,
+ AccountDisabled,
+ ServiceUnavailable
+ }
+}
+
Property changes on: trunk/google-sharp/src/Mono.Google/AuthErrorCode.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/google-sharp/src/Mono.Google/Authentication.cs
===================================================================
--- trunk/google-sharp/src/Mono.Google/Authentication.cs 2006-10-05
04:32:16 UTC (rev 66276)
+++ trunk/google-sharp/src/Mono.Google/Authentication.cs 2006-10-05
04:59:05 UTC (rev 66277)
@@ -48,7 +48,8 @@
{
}
- public static CookieContainer GetAuthCookies (GoogleConnection
conn, string user, string password, GoogleService service, out string auth)
+ public static CookieContainer GetAuthCookies (GoogleConnection
conn, string user, string password, GoogleService service,
+ string token,
string captcha, out string auth)
{
// No auth performed if no user or no password
auth = null;
@@ -57,16 +58,19 @@
// service == Picasa by now
Authentication authentication = new Authentication ();
- return authentication.GetCookieContainer (conn, user,
password, out auth);
+ return authentication.GetCookieContainer (conn, user,
password, token, captcha, out auth);
}
- CookieContainer GetCookieContainer (GoogleConnection conn,
string user, string password, out string auth)
+ CookieContainer GetCookieContainer (GoogleConnection conn,
string user, string password, string token, string captcha, out string auth)
{
user = HttpUtility.UrlEncode (user);
password = HttpUtility.UrlEncode (password);
StringBuilder content = new StringBuilder ();
string appname = HttpUtility.UrlEncode
(conn.ApplicationName);
content.AppendFormat
("Email={0}&Passwd={1}&source={2}&PersistentCookie=0&accountType=HOSTED%5FOR%5FGOOGLE",
user, password, appname);
+ if (token != null) {
+ content.AppendFormat
("&logintoken={0}&logincaptcha={2}", token, captcha);
+ }
byte [] bytes = Encoding.UTF8.GetBytes
(content.ToString ());
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create (picasa_login_url);
@@ -78,7 +82,17 @@
output.Write (bytes, 0, bytes.Length);
output.Close ();
- HttpWebResponse response = (HttpWebResponse)
request.GetResponse ();
+ HttpWebResponse response = null;
+ try {
+ response = (HttpWebResponse)
request.GetResponse ();
+ } catch (WebException wexc) {
+ response = wexc.Response as HttpWebResponse;
+ if (response == null)
+ throw;
+ ThrowOnError (response);
+ throw; // if the method above does not throw,
we do
+ }
+
string received = "";
string sid = null;
string lsid = null;
@@ -124,6 +138,37 @@
auth = received.Trim ();
return cookies;
}
+
+ void ThrowOnError (HttpWebResponse response)
+ {
+ if (response.StatusCode != HttpStatusCode.Forbidden)
+ return;
+
+ string url = null;
+ AuthErrorCode error = 0;
+ string token = null;
+ string captcha_url = null;
+ using (StreamReader reader = new StreamReader
(response.GetResponseStream ())) {
+ string str;
+ while ((str = reader.ReadLine ()) != null) {
+ if (str.StartsWith ("Url=")) {
+ url = str.Substring (4);
+ } else if (str.StartsWith ("Error=")) {
+ try {
+ error = (AuthErrorCode)
Enum.Parse (typeof (AuthErrorCode), str.Substring (6));
+ } catch {}
+ } else if (str.StartsWith
("CaptchaToken=")) {
+ token = str.Substring (13);
+ } else if (str.StartsWith
("CaptchaUrl=")) {
+ captcha_url = str.Substring
(11);
+ }
+ }
+ }
+ if (error == AuthErrorCode.CaptchaRequired && token !=
null && captcha_url != null)
+ throw new CaptchaException (url, token,
captcha_url);
+
+ throw new UnauthorizedAccessException (String.Format
("Access to '{0}' is denied ({1})", url, error));
+ }
}
}
Added: trunk/google-sharp/src/Mono.Google/CaptchaException.cs
===================================================================
--- trunk/google-sharp/src/Mono.Google/CaptchaException.cs 2006-10-05
04:32:16 UTC (rev 66276)
+++ trunk/google-sharp/src/Mono.Google/CaptchaException.cs 2006-10-05
04:59:05 UTC (rev 66277)
@@ -0,0 +1,87 @@
+//
+// Mono.Google.CaptchaException
+//
+// Authors:
+// Gonzalo Paniagua Javier ([EMAIL PROTECTED])
+//
+// Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Runtime.Serialization;
+
+namespace System {
+ [Serializable]
+ public class CaptchaException : UnauthorizedAccessException,
ISerializable
+ {
+ public static string UnlockCaptchaURL =
"https://www.google.com/accounts/DisplayUnlockCaptcha";
+ string url;
+ string token;
+ string captcha_url;
+
+ public CaptchaException ()
+ {
+ }
+
+ public CaptchaException (string url, string token, string
captcha_url)
+ {
+ this.url = url;
+ this.token = token;
+ this.captcha_url = captcha_url;
+ }
+
+ protected CaptchaException (SerializationInfo info,
StreamingContext context)
+ : base (info, context)
+ {
+ if (info == null)
+ throw new ArgumentNullException ("info");
+
+ url = info.GetString ("url");
+ token = info.GetString ("token");
+ captcha_url = info.GetString ("captcha_url");
+ }
+
+ void ISerializable.GetObjectData (SerializationInfo info,
StreamingContext context)
+ {
+ if (info == null)
+ throw new ArgumentNullException ("info");
+
+ base.GetObjectData (info, context);
+ info.AddValue ("url", url);
+ info.AddValue ("token", token);
+ info.AddValue ("captcha_url", captcha_url);
+ }
+
+ public string Url {
+ get { return url; }
+ }
+
+ public string Token {
+ get { return token; }
+ }
+
+ public string CaptchaUrl {
+ get { return captcha_url; }
+ }
+ }
+}
+
Property changes on: trunk/google-sharp/src/Mono.Google/CaptchaException.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/google-sharp/src/Mono.Google/GoogleConnection.cs
===================================================================
--- trunk/google-sharp/src/Mono.Google/GoogleConnection.cs 2006-10-05
04:32:16 UTC (rev 66276)
+++ trunk/google-sharp/src/Mono.Google/GoogleConnection.cs 2006-10-05
04:59:05 UTC (rev 66277)
@@ -80,13 +80,35 @@
throw new InvalidOperationException
(String.Format ("Already authenticated for {0}", this.user));
this.user = user;
- this.cookies = Authentication.GetAuthCookies (this,
user, password, service, out auth);
+ this.cookies = Authentication.GetAuthCookies (this,
user, password, service, null, null, out auth);
if (this.cookies == null) {
this.user = null;
throw new Exception (String.Format
("Authentication failed for user {0}", user));
}
}
+ public void Authenticate (string user, string password, string
token, string captcha)
+ {
+ if (user == null)
+ throw new ArgumentNullException ("user");
+
+ if (token == null)
+ throw new ArgumentNullException ("token");
+
+ if (captcha == null)
+ throw new ArgumentNullException ("captcha");
+
+ if (this.user != null)
+ throw new InvalidOperationException
(String.Format ("Already authenticated for {0}", this.user));
+
+ this.user = user;
+ this.cookies = Authentication.GetAuthCookies (this,
user, password, service, token, captcha, out auth);
+ if (this.cookies == null) {
+ this.user = null;
+ throw new Exception (String.Format
("Authentication failed for user {0}", user));
+ }
+ }
+
public string DownloadString (string url)
{
if (url == null)
Modified: trunk/google-sharp/src/Mono.Google/Makefile.am
===================================================================
--- trunk/google-sharp/src/Mono.Google/Makefile.am 2006-10-05 04:32:16 UTC
(rev 66276)
+++ trunk/google-sharp/src/Mono.Google/Makefile.am 2006-10-05 04:59:05 UTC
(rev 66277)
@@ -1,6 +1,8 @@
EXTRA_DIST=\
AssemblyInfo.cs.in \
Authentication.cs \
+ AuthErrorCode.cs \
+ CaptchaException.cs \
GoogleConnection.cs \
GoogleService.cs \
MultipartRequest.cs \
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches