You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2006/09/11 06:24:38 UTC

svn commit: r442090 - in /incubator/abdera/java/trunk: build/ client/src/main/java/org/apache/abdera/protocol/client/ extensions/ extensions/src/main/java/org/apache/abdera/ext/gdata/

Author: jmsnell
Date: Sun Sep 10 21:24:36 2006
New Revision: 442090

URL: http://svn.apache.org/viewvc?view=rev&rev=442090
Log:
Provide an implementation of the GoogleLogin authentication scheme used by GData APP
services like Blogger and Google Calendar.

Added:
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/gdata/
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/gdata/GoogleLoginAuthScheme.java
Modified:
    incubator/abdera/java/trunk/build/build.xml
    incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java
    incubator/abdera/java/trunk/extensions/pom.xml

Modified: incubator/abdera/java/trunk/build/build.xml
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/build/build.xml?view=diff&rev=442090&r1=442089&r2=442090
==============================================================================
--- incubator/abdera/java/trunk/build/build.xml (original)
+++ incubator/abdera/java/trunk/build/build.xml Sun Sep 10 21:24:36 2006
@@ -233,16 +233,16 @@
     </copy>
   </target>
   
-  <target name="compile.extensions" depends="init, compile.core, compile.parser, compile.dependencies">
+  <target name="compile.extensions" depends="init, compile.core, compile.parser, compile.dependencies, compile.client">
     <mkdir dir="${extensions.work}" />
     <javac srcdir="${extensions.src}"
            destdir="${extensions.work}"
            classpathref="jar.dependencies"
-           classpath="${core.work};${parser.work}" />
+           classpath="${core.work};${parser.work};${client.work};${protocol.work}" />
     <javac srcdir="${extensions.test.java}"
            destdir="${test}"
            classpathref="jar.dependencies"
-           classpath="${core.work};${parser.work};${extensions.work}" />
+           classpath="${core.work};${parser.work};${extensions.work};${client.work};${protocol.work}" />
     <mkdir dir="${extensions.work}/META-INF" />
     <copy todir="${extensions.work}/META-INF">
       <fileset dir="${basedir}">

Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java?view=diff&rev=442090&r1=442089&r2=442090
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java (original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/Client.java Sun Sep 10 21:24:36 2006
@@ -236,9 +236,9 @@
    * @param name
    * @param scheme
    */
-  public static void registerScheme(
+  public static <T extends AuthScheme>void registerScheme(
     String name, 
-    Class<AuthScheme> scheme) {
+    Class<T> scheme) {
       AuthPolicy.registerAuthScheme(name, scheme);
   }
   

Modified: incubator/abdera/java/trunk/extensions/pom.xml
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/pom.xml?view=diff&rev=442090&r1=442089&r2=442090
==============================================================================
--- incubator/abdera/java/trunk/extensions/pom.xml (original)
+++ incubator/abdera/java/trunk/extensions/pom.xml Sun Sep 10 21:24:36 2006
@@ -46,6 +46,18 @@
     </dependency>   
     <dependency>
       <groupId>org.apache.abdera</groupId>
+      <artifactId>abdera-protocol</artifactId>
+      <version>0.2.0-incubating-SNAPSHOT</version>
+      <scope>compile</scope>
+    </dependency>  
+    <dependency>
+      <groupId>org.apache.abdera</groupId>
+      <artifactId>abdera-client</artifactId>
+      <version>0.2.0-incubating-SNAPSHOT</version>
+      <scope>compile</scope>
+    </dependency>  
+    <dependency>
+      <groupId>org.apache.abdera</groupId>
       <artifactId>json</artifactId>
       <version>1.0</version>
       <scope>compile</scope>

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/gdata/GoogleLoginAuthScheme.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/gdata/GoogleLoginAuthScheme.java?view=auto&rev=442090
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/gdata/GoogleLoginAuthScheme.java (added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/gdata/GoogleLoginAuthScheme.java Sun Sep 10 21:24:36 2006
@@ -0,0 +1,145 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.gdata;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.net.URLEncoder;
+import java.util.Formatter;
+
+import org.apache.abdera.protocol.client.Client;
+import org.apache.abdera.protocol.client.ClientResponse;
+import org.apache.abdera.protocol.client.CommonsClient;
+import org.apache.abdera.protocol.client.RequestOptions;
+import org.apache.abdera.util.Version;
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScheme;
+import org.apache.commons.httpclient.auth.AuthenticationException;
+import org.apache.commons.httpclient.auth.MalformedChallengeException;
+import org.apache.commons.httpclient.auth.RFC2617Scheme;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+
+/**
+ * <p>Implements the GoogleLogin auth scheme used by gdata (Blogger, Google Calendar, etc).
+ * Warning: this scheme is slow!</p>
+ * 
+ * <pre>
+ *   GoogleLoginAuthScheme.register();
+ *   
+ *   Client client = new CommonsClient();
+ *   client.addCredentials(
+ *     "http://beta.blogger.com", 
+ *     null, "GoogleLogin", 
+ *     new UsernamePasswordCredentials(
+ *       "email","password"));
+ * </pre>
+ */
+public class GoogleLoginAuthScheme
+  extends RFC2617Scheme
+  implements AuthScheme {
+  
+  public static void register() {
+    Client.registerScheme("GoogleLogin", GoogleLoginAuthScheme.class);
+  }
+  
+  private String service = null;
+  
+  @Override
+  public void processChallenge(
+    String challenge) 
+      throws MalformedChallengeException {
+    super.processChallenge(challenge);
+    if (getParameter("service") == null) 
+      throw new MalformedChallengeException("Missing service in challenge");
+    service = getParameter("service");
+  }
+  
+  @Override
+  public String authenticate(
+    Credentials credentials, 
+    HttpMethod method) 
+      throws AuthenticationException {
+    
+    if (!(credentials instanceof UsernamePasswordCredentials)) {
+      throw new AuthenticationException(
+        "Cannot use credentials for GoogleLogin authentication");
+    }
+    UsernamePasswordCredentials usercreds = 
+      (UsernamePasswordCredentials) credentials;
+    String id = usercreds.getUserName();
+    String pwd = usercreds.getPassword();
+    String auth = getAuth(id, pwd);
+    StringBuffer buf = new StringBuffer("GoogleLogin ");
+    buf.append(auth);
+    return buf.toString();
+  }
+  
+  @Override
+  public String authenticate(
+    Credentials credentials, 
+    String method, 
+    String uri) 
+      throws AuthenticationException {
+    return authenticate(credentials, null);
+  }
+  
+  @Override
+  public String getSchemeName() {
+    return "GoogleLogin";
+  }
+  
+  @Override
+  public boolean isComplete() {
+    return true;
+  }
+  
+  @Override
+  public boolean isConnectionBased() {
+    return false;
+  }
+  
+  private String getAuth(String id, String pwd) {
+    try {
+      Client client = new CommonsClient();
+      Formatter f = new Formatter();
+      f.format(
+        "Email=%s&Passwd=%s&service=%s&source=%s",
+        URLEncoder.encode(id, "utf-8"),
+        URLEncoder.encode(pwd, "utf-8"),
+        URLEncoder.encode(service, "utf-8"),
+        URLEncoder.encode(Version.APP_NAME, "utf-8"));
+      StringRequestEntity stringreq = new StringRequestEntity(f.toString());
+      String uri = "https://www.google.com/accounts/ClientLogin";
+      RequestOptions options = client.getDefaultRequestOptions();
+      options.setContentType("application/x-www-form-urlencoded");
+      ClientResponse response = client.post(uri, stringreq, options);
+      InputStream in = response.getInputStream();
+      ByteArrayOutputStream out = new ByteArrayOutputStream();
+      int n = -1;
+      while ((n = in.read()) != -1) { out.write(n); }
+      out.flush();
+      response.release();
+      String auth = new String(out.toByteArray());
+      return auth.split("\n")[2].replaceAll("Auth=", "auth=");
+    } catch (Exception e) {}
+    return null;
+  }
+
+}
\ No newline at end of file