Google 認証な部品について

試しにプロトタイプとして作ってみたナニをパスワードを間違ったソレで試験してみたら 403 Forbidden が戻ってきた。
てコトは

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        response = httpClient.execute(httpost);

の後に response.getStatusLine().getStatusCode() が戻す値が 400 未満かどうか、について確認を行なう必要がありそげ。てか 400 未満か、ってチェックするのが妥当なのかどうかも微妙。
しかも試験もしなきゃ、ですね。で、無理矢理でっち上げた実装が以下で

package jp.shuri.yamanetoshi.gaeaccess;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;

public class GoogleAuth {
	public static String getAuthKey(HttpClient httpClient, String source, String id, String passwd) throws Exception {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("Email", id));
        nvps.add(new BasicNameValuePair("Passwd", passwd));
        nvps.add(new BasicNameValuePair("service", "ah"));
        nvps.add(new BasicNameValuePair("source", source));
        nvps.add(new BasicNameValuePair("accountType", "HOSTED_OR_GOOGLE"));

        //Login at Google.com
        HttpPost httpost = new HttpPost("https://www.google.com/accounts/ClientLogin");
        HttpResponse response = null;
        
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        response = httpClient.execute(httpost);
        
        if(response.getStatusLine().getStatusCode() >= 400) return "";
        
        String authKey = null;
        BufferedReader br = null;

        br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = null;
        while ((line = br.readLine()) != null) {
        	String[] s = line.split("=");
        	if (s.length == 2 && s[0].equalsIgnoreCase("auth")) {
        		authKey = s[1];
        		break;
            }
        }

        if (br != null) br.close();
        
        return authKey;
	}
}

試験が以下。

import java.io.IOException;
import java.io.InputStream;

import jp.shuri.yamanetoshi.gaeaccess.GoogleAuth;
import junit.framework.TestCase;
import java.io.ByteArrayInputStream;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

public class GoogleAuthTest extends TestCase {
	class TmpEntity {
		public InputStream getContent() {
			ByteArrayInputStream ret = new ByteArrayInputStream("auth=ABC".getBytes());
			return (InputStream)ret;
		}
	}
	
	class TmpStatusLine {
		private int status;
		public TmpStatusLine() {
			status = 200;
		}
		
		public void setStatus(int i) {
			status = i;
		}
		public int getStatus() {
			return status;
		}
	}

	class HttpResponse {
		private TmpEntity myEntity;
		private TmpStatusLine myStatusLine;
		
		HttpResponse () {
			myEntity = new TmpEntity();
			myStatusLine = new TmpStatusLine();
		}
		
		HttpResponse(int status) {
			this();
			myStatusLine.setStatus(status);
		}
		
		public TmpStatusLine getStatusLine() {
			return myStatusLine;
		}
		
		public TmpEntity getEntity() {
			return myEntity;
		}
	}

	class MyHttpClient implements HttpClient {
		HttpResponse myResponse;
		
		public MyHttpClient() {
			myResponse = new HttpResponse();
		}
		
		public HttpResponse getResponse() {return myResponse;}
		
		public HttpResponse execute(HttpPost p) {
			return myResponse;
		}

		public org.apache.http.HttpResponse execute(HttpUriRequest arg0)
				throws IOException, ClientProtocolException {
			// TODO Auto-generated method stub
			return null;
		}

		public org.apache.http.HttpResponse execute(HttpUriRequest arg0,
				HttpContext arg1) throws IOException, ClientProtocolException {
			// TODO Auto-generated method stub
			return null;
		}

		public org.apache.http.HttpResponse execute(HttpHost arg0,
				HttpRequest arg1) throws IOException, ClientProtocolException {
			// TODO Auto-generated method stub
			return null;
		}

		public <T> T execute(HttpUriRequest arg0,
				ResponseHandler<? extends T> arg1) throws IOException,
				ClientProtocolException {
			// TODO Auto-generated method stub
			return null;
		}

		public org.apache.http.HttpResponse execute(HttpHost arg0,
				HttpRequest arg1, HttpContext arg2) throws IOException,
				ClientProtocolException {
			// TODO Auto-generated method stub
			return null;
		}

		public <T> T execute(HttpUriRequest arg0,
				ResponseHandler<? extends T> arg1, HttpContext arg2)
				throws IOException, ClientProtocolException {
			// TODO Auto-generated method stub
			return null;
		}

		public <T> T execute(HttpHost arg0, HttpRequest arg1,
				ResponseHandler<? extends T> arg2) throws IOException,
				ClientProtocolException {
			// TODO Auto-generated method stub
			return null;
		}

		public <T> T execute(HttpHost arg0, HttpRequest arg1,
				ResponseHandler<? extends T> arg2, HttpContext arg3)
				throws IOException, ClientProtocolException {
			// TODO Auto-generated method stub
			return null;
		}

		public ClientConnectionManager getConnectionManager() {
			// TODO Auto-generated method stub
			return null;
		}

		public HttpParams getParams() {
			// TODO Auto-generated method stub
			return null;
		}
	}

	public GoogleAuthTest(String name) {
		super(name);
	}

	public void testgetAuthKeyNormal() {
		try {
			String tmp = GoogleAuth.getAuthKey(new MyHttpClient(), "", "", "");
			assertEquals("ABC", tmp);
		} catch (Exception e) {
		}
	}
	
	public void testgetAuthKey403() {
		try {
			MyHttpClient myClient = new MyHttpClient();
			myClient.getResponse().getStatusLine().setStatus(403);
			String tmp = GoogleAuth.getAuthKey(myClient, "", "", "");
			assertEquals("", tmp);
		} catch (Exception e) {
		}
	}
}

正直どうなんだろうという気もしますが。。

残作業

もう一つ試験書かなきゃいけないクラスがあったり、JSON な String を JSON なオブジェクトに変換する実装を書かなきゃいけなかったり。ざっくりな残は他に以下?

  • javadoc
  • jar を出力する ant なタスク