Added:
/branches/2009q4rewrite/java/src/org/opensocial/auth/FCAuthScheme.java
/branches/2009q4rewrite/java/src/org/opensocial/auth/SecurityTokenScheme.java
/branches/2009q4rewrite/java/test/org/opensocial/auth/FCAuthSchemeTest.java
/branches/2009q4rewrite/java/test/org/opensocial/auth/SecurityTokenSchemeTest.java
Modified:
/branches/2009q4rewrite/java/src/org/opensocial/auth/AuthScheme.java
=======================================
--- /dev/null
+++ /branches/2009q4rewrite/java/src/org/opensocial/auth/FCAuthScheme.java
Mon Dec 28 22:11:51 2009
@@ -0,0 +1,23 @@
+/* Copyright (c) 2009 Google Inc.
+ *
+ * Licensed 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.
+ */
+
+package org.opensocial.auth;
+
+public class FCAuthScheme extends SecurityTokenScheme {
+
+ public FCAuthScheme(String token) {
+ super("fcauth", token);
+ }
+}
=======================================
--- /dev/null
+++
/branches/2009q4rewrite/java/src/org/opensocial/auth/SecurityTokenScheme.java
Mon Dec 28 22:11:51 2009
@@ -0,0 +1,84 @@
+/* Copyright (c) 2009 Google Inc.
+ *
+ * Licensed 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.
+ */
+
+package org.opensocial.auth;
+
+import net.oauth.http.HttpMessage;
+
+import org.opensocial.RequestException;
+import org.opensocial.providers.Provider;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Map;
+
+public class SecurityTokenScheme implements AuthScheme {
+
+ private String tokenName;
+ private String token;
+
+ public SecurityTokenScheme(String token) {
+ this("st", token);
+ }
+
+ public SecurityTokenScheme(String tokenName, String token) {
+ this.tokenName = tokenName;
+ this.token = token;
+ }
+
+ public HttpMessage getHttpMessage(Provider provider, String method,
+ String url, Map<String, String> headers, String body) throws
+ RequestException, IOException {
+ url = appendTokenToQueryString(url);
+
+ HttpMessage message = new HttpMessage(method, new URL(url),
+ stringToInputStream(body));
+ for (Map.Entry<String, String> header : headers.entrySet()) {
+ message.headers.add(header);
+ }
+
+ return message;
+ }
+
+ private String appendTokenToQueryString(String url) {
+ if (token == null) {
+ return url;
+ }
+
+ StringBuilder builder = new StringBuilder(url);
+
+ if (url.indexOf('?') == -1) {
+ builder.append("?");
+ } else {
+ builder.append("&");
+ }
+
+ builder.append(tokenName);
+ builder.append("=");
+ builder.append(token);
+
+ return builder.toString();
+ }
+
+ private InputStream stringToInputStream(String string) {
+ if (string == null) {
+ return null;
+ }
+
+ return new ByteArrayInputStream(string.getBytes());
+ }
+}
=======================================
--- /dev/null
+++
/branches/2009q4rewrite/java/test/org/opensocial/auth/FCAuthSchemeTest.java
Mon Dec 28 22:11:51 2009
@@ -0,0 +1,46 @@
+/* Copyright (c) 2009 Google Inc.
+ *
+ * Licensed 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.
+ */
+
+package org.opensocial.auth;
+
+import static org.junit.Assert.assertEquals;
+
+import net.oauth.http.HttpMessage;
+
+import org.junit.Test;
+import org.opensocial.RequestException;
+import org.opensocial.providers.OrkutProvider;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class FCAuthSchemeTest {
+
+ private static final String TOKEN = "TOKEN";
+
+ @Test
+ public void getHttpMessage() throws RequestException,
+ IOException {
+ final String url = "http://example.org/test";
+ final Map<String, String> headers = new HashMap<String, String>();
+
+ FCAuthScheme authScheme = new FCAuthScheme(TOKEN);
+
+ HttpMessage message = authScheme.getHttpMessage(new
OrkutProvider(), "GET",
+ url, headers, null);
+ assertEquals(url + "?fcauth=" + TOKEN, message.url.toString());
+ }
+}
=======================================
--- /dev/null
+++
/branches/2009q4rewrite/java/test/org/opensocial/auth/SecurityTokenSchemeTest.java
Mon Dec 28 22:11:51 2009
@@ -0,0 +1,74 @@
+/* Copyright (c) 2009 Google Inc.
+ *
+ * Licensed 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.
+ */
+
+package org.opensocial.auth;
+
+import static org.junit.Assert.assertEquals;
+
+import net.oauth.http.HttpMessage;
+
+import org.junit.Test;
+import org.opensocial.RequestException;
+import org.opensocial.providers.OrkutProvider;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class SecurityTokenSchemeTest {
+
+ private static final String TOKEN = "TOKEN";
+ private static final String TOKEN_NAME = "TOKEN_NAME";
+
+ @Test
+ public void getHttpMessageForUrlWithoutParameters() throws
RequestException,
+ IOException {
+ final String url = "http://example.org/test";
+ final Map<String, String> headers = new HashMap<String, String>();
+
+ SecurityTokenScheme authScheme = new SecurityTokenScheme(TOKEN);
+
+ HttpMessage message = authScheme.getHttpMessage(new
OrkutProvider(), "GET",
+ url, headers, null);
+ assertEquals(url + "?st=" + TOKEN, message.url.toString());
+ }
+
+ @Test
+ public void getHttpMessageForUrlWithParameters() throws RequestException,
+ IOException {
+ final String url = "http://example.org/test?arg=value";
+ final Map<String, String> headers = new HashMap<String, String>();
+
+ SecurityTokenScheme authScheme = new SecurityTokenScheme(TOKEN);
+
+ HttpMessage message = authScheme.getHttpMessage(new
OrkutProvider(), "GET",
+ url, headers, null);
+ assertEquals(url + "&st=" + TOKEN, message.url.toString());
+ }
+
+ @Test
+ public void getHttpMessageUsingCustomToken() throws RequestException,
+ IOException {
+ final String url = "http://example.org/test";
+ final Map<String, String> headers = new HashMap<String, String>();
+
+ SecurityTokenScheme authScheme = new SecurityTokenScheme(TOKEN_NAME,
+ TOKEN);
+
+ HttpMessage message = authScheme.getHttpMessage(new
OrkutProvider(), "GET",
+ url, headers, null);
+ assertEquals(url + "?" + TOKEN_NAME + "=" + TOKEN,
message.url.toString());
+ }
+}
=======================================
--- /branches/2009q4rewrite/java/src/org/opensocial/auth/AuthScheme.java
Sun Dec 27 23:33:00 2009
+++ /branches/2009q4rewrite/java/src/org/opensocial/auth/AuthScheme.java
Mon Dec 28 22:11:51 2009
@@ -15,14 +15,14 @@
package org.opensocial.auth;
-import java.io.IOException;
-import java.util.Map;
-
import net.oauth.http.HttpMessage;
import org.opensocial.RequestException;
import org.opensocial.providers.Provider;
+import java.io.IOException;
+import java.util.Map;
+
public interface AuthScheme {
public HttpMessage getHttpMessage(Provider provider, String method,