You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by do...@apache.org on 2008/07/18 22:59:07 UTC
svn commit: r678036 - in /incubator/shindig/trunk: ./
java/server/src/main/webapp/WEB-INF/
java/social-api/src/main/java/org/apache/shindig/social/
java/social-api/src/main/java/org/apache/shindig/social/oauth/
java/social-api/src/test/java/org/apache/...
Author: doll
Date: Fri Jul 18 13:59:06 2008
New Revision: 678036
URL: http://svn.apache.org/viewvc?rev=678036&view=rev
Log:
SHINDIG-290
Patch from Dirk Balfanz. Next step towards providing oauth support for the java social server. Note: A lot of this is still placeholder code.
Added:
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/AuthenticationServletFilter.java
- copied, changed from r678019, incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthServletFilter.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStore.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapper.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenStore.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/DelegatedPrincipal.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/GadgetSecurityTokenPrincipal.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthConsumerStore.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthPrincipal.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenPrincipalMapper.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenStore.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/PrincipalMapperException.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/RequestorIdPrincipal.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStoreTest.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapperTest.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenStoreTest.java
Removed:
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthContext.java
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthServletFilter.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/OAuthContextTest.java
Modified:
incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml
incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.social.xml
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/SocialApiGuiceModule.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java
incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/OAuthServletFilterTest.java
incubator/shindig/trunk/pom.xml
Modified: incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml?rev=678036&r1=678035&r2=678036&view=diff
==============================================================================
--- incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml (original)
+++ incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.full.xml Fri Jul 18 13:59:06 2008
@@ -29,6 +29,16 @@
<param-value>org.apache.shindig.gadgets.servlet.HttpGuiceModule:org.apache.shindig.social.SocialApiGuiceModule</param-value>
</context-param>
+ <filter>
+ <filter-name>oauthFilter</filter-name>
+ <filter-class>org.apache.shindig.social.oauth.AuthenticationServletFilter</filter-class>
+ </filter>
+
+ <filter-mapping>
+ <filter-name>oauthFilter</filter-name>
+ <url-pattern>/social/*</url-pattern>
+ </filter-mapping>
+
<listener>
<listener-class>org.apache.shindig.common.servlet.GuiceServletContextListener</listener-class>
</listener>
Modified: incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.social.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.social.xml?rev=678036&r1=678035&r2=678036&view=diff
==============================================================================
--- incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.social.xml (original)
+++ incubator/shindig/trunk/java/server/src/main/webapp/WEB-INF/web.social.xml Fri Jul 18 13:59:06 2008
@@ -31,7 +31,7 @@
<filter>
<filter-name>oauthFilter</filter-name>
- <filter-class>org.apache.shindig.social.oauth.OAuthServletFilter</filter-class>
+ <filter-class>org.apache.shindig.social.oauth.AuthenticationServletFilter</filter-class>
</filter>
<filter-mapping>
Modified: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/SocialApiGuiceModule.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/SocialApiGuiceModule.java?rev=678036&r1=678035&r2=678036&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/SocialApiGuiceModule.java (original)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/SocialApiGuiceModule.java Fri Jul 18 13:59:06 2008
@@ -21,10 +21,20 @@
import org.apache.shindig.common.servlet.ParameterFetcher;
import org.apache.shindig.social.dataservice.DataServiceServletFetcher;
import org.apache.shindig.social.dataservice.HandlerProvider;
+import org.apache.shindig.social.oauth.AuthenticationServletFilter;
+import org.apache.shindig.social.oauth.BasicOAuthConsumerStore;
+import org.apache.shindig.social.oauth.BasicOAuthTokenPrincipalMapper;
+import org.apache.shindig.social.oauth.BasicOAuthTokenStore;
+import org.apache.shindig.social.oauth.OAuthConsumerStore;
+import org.apache.shindig.social.oauth.OAuthTokenPrincipalMapper;
+import org.apache.shindig.social.oauth.OAuthTokenStore;
import org.apache.shindig.social.samplecontainer.SampleContainerHandlerProvider;
import com.google.inject.AbstractModule;
+import com.google.inject.Scopes;
import com.google.inject.name.Names;
+import net.oauth.OAuthValidator;
+import net.oauth.SimpleOAuthValidator;
/**
* Provides social api component injection
@@ -41,6 +51,20 @@
bind(String.class).annotatedWith(Names.named("canonical.json.db"))
.toInstance("sampledata/canonicaldb.json");
+
+ // OAuth configuration
+
+ bind(Boolean.class)
+ .annotatedWith(Names.named(AuthenticationServletFilter.ALLOW_UNAUTHENTICATED))
+ .toInstance(Boolean.TRUE);
+
+ bind(OAuthValidator.class).to(SimpleOAuthValidator.class);
+ bind(OAuthTokenStore.class)
+ .to(BasicOAuthTokenStore.class).in(Scopes.SINGLETON);
+ bind(OAuthConsumerStore.class)
+ .to(BasicOAuthConsumerStore.class).in(Scopes.SINGLETON);
+ bind(OAuthTokenPrincipalMapper.class)
+ .to(BasicOAuthTokenPrincipalMapper.class).in(Scopes.SINGLETON);
}
}
Copied: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/AuthenticationServletFilter.java (from r678019, incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthServletFilter.java)
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/AuthenticationServletFilter.java?p2=incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/AuthenticationServletFilter.java&p1=incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthServletFilter.java&r1=678019&r2=678036&rev=678036&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthServletFilter.java (original)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/AuthenticationServletFilter.java Fri Jul 18 13:59:06 2008
@@ -17,27 +17,181 @@
*/
package org.apache.shindig.social.oauth;
+import org.apache.shindig.common.SecurityToken;
+import org.apache.shindig.common.SecurityTokenDecoder;
+import org.apache.shindig.common.SecurityTokenException;
+import org.apache.shindig.common.servlet.GuiceServletContextListener;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.name.Named;
import net.oauth.OAuth;
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthException;
import net.oauth.OAuthMessage;
+import net.oauth.OAuthProblemException;
+import net.oauth.OAuthValidator;
import net.oauth.server.OAuthServlet;
-import org.apache.commons.lang.NotImplementedException;
-
-import java.io.IOException;
-
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
+import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.security.Principal;
+import java.util.Collections;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Filter that attempts to authenticate an incoming HTTP request. There are
+ * three different cases:
+ *
+ * (1) If the incoming request is from the container's own gadget server, the
+ * request will include a gadget security token. The gadget security token
+ * includes the owner and the viewer, expressed in identities native to the
+ * container. The principal that will be represented to the servlets as
+ * "making the request" is a {@link DelegatedPrincipal} naming the viewer
+ * as the delegator and the container as the delegatee (i.e., it's
+ * considered to be the container speaking on behalf of the viewer, which
+ * may carry less privileges with it than if the user spoke directly).
+ *
+ * (2) If the incoming request is from a third-party-server that uses "full"
+ * (as opposed to "two-legged") OAuth, then the request will include an
+ * OAuth token that identifies the user. The
+ * {@link OAuthTokenPrincipalMapper} allows containers to map this OAuth
+ * token to a principal that represents the delegation afforded by the
+ * OAuth token. The delegator named in that principal will be the user who
+ * issued the token, and the delegatee will be the OAuth consumer making
+ * the call.
+ *
+ * (3) If the incoming request is from a third-party-server that uses
+ * "two-legged" OAuth (aka Consumer Request), it won't include a gadget
+ * security token. Instead, it must include a xoauth_requestor_id parameter,
+ * which names the delegator in a format native to the container.
+ * The principal that will be represented to the servlets as "making the
+ * request" is the {@linkp DelegatedPrincipal} naming the that delegator,
+ * and naming the OAuth consumer as the delegatee.
+ *
+ * (4) Finally, the filter can also authenticate pure "two-legged" OAuth, i.e.,
+ * an OAuth request authenticated only by the consumer making the call, but
+ * not carrying any information identifying the principal "making the
+ * request".
+ *
+ * In each case a suitable {@link HttpServletRequest} will be passed onto the
+ * next filter and/or gadget in the filter chain. In each of the above cases,
+ * <code>HttpServletRequest.getAuthType()</code> will return an appropriate
+ * string ("GadgetSecurityToken" in case (1), "OAuth" in case (2), and
+ * "OAuth-ConsumerRequest" in cases (3) and (4)). In the
+ * three cases in which he principal making the request can be authenticated,
+ * <code>HttpServletRequest.getUserPrincipal()</code> will return the principal
+ * making the request. In case (4), getUserPrincipal() returns null.
+ *
+ * We resolve the various cases (in case they are overlapping) like this:
+ *
+ * - If there is a gadget security token present we use that to
+ * establish the identity of the delegator, and we ignore any
+ * possibly-present xoauth_requestor_id parameter or OAuth signatures.
+ *
+ * - Else, the request has to carry an OAuth signature. If there is an OAuth
+ * token present we use this to establish the identity of the delegator, and
+ * we ignore any possibly-present xoauth_requestor_id parameters.
+ *
+ * - Else, if there is an xoauth_requestor_id parameter available, we use
+ * that to establish the identity of the delegator.
+ *
+ * - Else, we don't identify the user principal at all, and just return
+ * "OAuth-ConsumerRequest" in HttpServletRequest.getAuthType()
+ */
+public class AuthenticationServletFilter implements Filter {
+
+ public static final String ALLOW_UNAUTHENTICATED = "auth.allow-unauthenticated";
+
+ public static final String AUTH_TYPE_OAUTH = "OAuth";
+ public static final String AUTH_TYPE_OAUTH_CR = "OAuth-ConsumerRequest";
+ public static final String AUTH_TYPE_SECURITY_TOKEN = "GadgetSecurityToken";
+
+ // the name of the URI query parameter that is the gadget security token
+ protected static final String SECURITY_TOKEN_PARAM = "st";
+
+ // the name of the parameter that identifies an explicitly-named requestor
+ // in the case of two-legged OAuth in the absence of a gadget security token
+ protected static final String REQUESTOR_ID_PARAM = "xoauth_requestor_id";
+
+ private static final Logger log =
+ Logger.getLogger(AuthenticationServletFilter.class.getName());
+
+ /*
+ * When this is set to true, the filter will forward HTTP requests down the
+ * filter chain (and, ultimately, to the servlet), even if the request could not be
+ * authenticated by this filter
+ */
+ private boolean allowUnauthenticated;
+
+ // object that can validate OAuth requests (includes signature check)
+ private OAuthValidator oauthValidator;
+
+ // object that can map consumer keys to consumer secrets
+ private OAuthConsumerStore consumerStore;
+
+ // object that can map tokens to token secrets/
+ private OAuthTokenStore tokenStore;
+
+ // object that can map tokens to principals (user ids).
+ private OAuthTokenPrincipalMapper tokenMapper;
+
+ // object that can decode a gadget security token, e.g. extract the viewer
+ // id from it
+ private SecurityTokenDecoder securityTokenDecoder;
+
+ /**
+ * Initializes the filter. We retrieve the Guice injector and ask for all
+ * the injected methods to be called, setting a variety of helper objects
+ * and configuration state.
+ */
+ public void init(FilterConfig filterConfig) throws ServletException {
+ ServletContext context = filterConfig.getServletContext();
+ Injector injector = (Injector)
+ context.getAttribute(GuiceServletContextListener.INJECTOR_ATTRIBUTE);
+ if (injector == null) {
+ throw new UnavailableException(
+ "Guice Injector not found! Make sure you registered " +
+ GuiceServletContextListener.class.getName() + " as a listener");
+ }
+ injector.injectMembers(this);
+ }
-public class OAuthServletFilter implements Filter {
+ @Inject
+ public void setAuthConfiguration(
+ @Named(ALLOW_UNAUTHENTICATED) Boolean allowUnauthenticated) {
+ this.allowUnauthenticated = allowUnauthenticated;
+ }
- public void init(FilterConfig filterConfig) {
+ @Inject
+ public void setOAuthUtils(OAuthValidator oauthValidator,
+ OAuthTokenStore tokenStore,
+ OAuthConsumerStore consumerStore,
+ OAuthTokenPrincipalMapper tokenMapper) {
+
+ this.oauthValidator = oauthValidator;
+ this.consumerStore = consumerStore;
+ this.tokenStore = tokenStore;
+ this.tokenMapper = tokenMapper;
+ }
+
+ @Inject
+ public void setSecurityTokenDecoder(SecurityTokenDecoder decoder) {
+ this.securityTokenDecoder = decoder;
}
public void destroy() {
@@ -46,42 +200,298 @@
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
- if (!(request instanceof HttpServletRequest)) {
- throw new ServletException("OAuth filter can only handle HTTP");
+ if (! ((request instanceof HttpServletRequest)
+ && (response instanceof HttpServletResponse))) {
+ throw new ServletException("Auth filter can only handle HTTP");
+ }
+
+ HttpServletRequest req = (HttpServletRequest)request;
+ HttpServletResponse resp = (HttpServletResponse)response;
+
+ try {
+ HttpServletRequest wrappedRequest = handleRequest(req);
+ chain.doFilter(wrappedRequest, response);
+ } catch(OAuthProblemException e) {
+ if (allowUnauthenticated) {
+ chain.doFilter(request, response);
+ } else {
+ handleException(resp, e);
+ }
}
+ }
- if (!(response instanceof HttpServletResponse)) {
- throw new ServletException("OAuth filter can only handle HTTP");
+ /**
+ * Handles an incoming HTTP request.
+ * @return an {@link HttpServletRequest} suitable for passing on to the next
+ * element in the filter chain.
+ */
+ protected HttpServletRequest handleRequest(HttpServletRequest request)
+ throws ServletException, IOException, OAuthProblemException {
+
+ if (requestUsesSecurityToken(request)) {
+ return handleSecurityTokenRequest(request);
+
+ } else { // this means the request has to use OAuth
+
+ OAuthMessage requestMessage = getOAuthMessageFromRequest(request);
+ requireOAuth(requestMessage);
+
+ // it uses OAuth, but is there an OAuth token?
+ if (requestMessage.getToken() == null) {
+
+ // this means it's either using xoauth_requestor_id or plain signedFetch
+ return handleConsumerRequest(request, requestMessage);
+ } else {
+
+ // this is the case for full OAuth
+ return handleFullOAuth(request, requestMessage);
+ }
}
+ }
- HttpServletRequest req = (HttpServletRequest)request;
- HttpServletResponse res = (HttpServletResponse)response;
- OAuthContext authContext = OAuthContext.newContextForRequest(req);
+ protected boolean requestUsesSecurityToken(HttpServletRequest request) {
+ return (getSecurityToken(request) != null);
+ }
+
+ /**
+ * Handles the case where a GadgetSecurityToken is used to authenticate the
+ * request.
+ */
+ protected HttpServletRequest handleSecurityTokenRequest(HttpServletRequest request) {
+ SecurityToken securityToken = getSecurityToken(request);
+ return new AuthServletRequest(request,
+ new GadgetSecurityTokenPrincipal(securityToken),
+ AUTH_TYPE_SECURITY_TOKEN);
+ }
- OAuthMessage requestMessage = OAuthServlet.getMessage(req, null);
+ /**
+ * Handles a request that includes an OAuth signature, but no OAuth access
+ * token.
+ * @param request the HTTP request.
+ * @param requestMessage the HTTP request, parsed out into an OAuthMessage.
+ * @return an {@link HttpServletRequest} suitable for passing on to the next
+ * element in the filter chain.
+ * @throws OAuthProblemException if there was a problem with the HTTP request.
+ */
+ protected HttpServletRequest handleConsumerRequest(HttpServletRequest request,
+ OAuthMessage requestMessage)
+ throws IOException, ServletException, OAuthProblemException {
+
+ if (requestUsesRequestorId(request)) {
+
+ // handle the case when xoauth_requestor_id is specified
+ return handleRequestorIdRequest(request, requestMessage);
+
+ } else { // no requestorId, but oauth signature
+
+ // if signature verifies, we'll set the auth method to OAuth-ConsumerRequest.
+ // (and a null principal).
+ validateConsumerRequestMessage(requestMessage);
- if (requestMessage.getParameter(OAuth.OAUTH_SIGNATURE) == null) {
- // doesn't seem to be an OAuth request
- chain.doFilter(request, response);
- return;
+ return new AuthServletRequest(request, null, AUTH_TYPE_OAUTH_CR);
}
+ }
+
+ /**
+ * Returns true if the request contains an xoauth_requestor_id parameter.
+ */
+ protected boolean requestUsesRequestorId(HttpServletRequest request) {
+ return (getRequestorId(request) != null);
+ }
- if (requestMessage.getToken() == null) {
- handleSignedFetch(requestMessage, authContext);
+ /**
+ * Reads the requestor id out of an {@link HttpServletRequest}, making sure
+ * to return null if there are problems.
+ */
+ protected String getRequestorId(HttpServletRequest request) {
+ String id = request.getParameter(REQUESTOR_ID_PARAM);
+ if (id == null || id.trim().length() == 0) {
+ return null;
} else {
- handleFullOAuth(requestMessage, authContext);
+ return id.trim();
}
+ }
- chain.doFilter(request, response);
+ /**
+ * Handles a request that includes an xoauth_requestor_id and an OAuth
+ * signature.
+ * @param request the HTTP request.
+ * @param requestMessage the HTTP request, parsed out into an OAuthMessage.
+ * @return an {@link HttpServletRequest} suitable for passing on to the next
+ * element in the filter chain.
+ * @throws OAuthProblemException if there was a problem with the HTTP request.
+ */
+ protected HttpServletRequest handleRequestorIdRequest(HttpServletRequest request,
+ OAuthMessage requestMessage)
+ throws IOException, OAuthProblemException, ServletException {
+
+ validateConsumerRequestMessage(requestMessage);
+
+ String userId = getRequestorId(request);
+
+ return new AuthServletRequest(request,
+ new RequestorIdPrincipal(userId, requestMessage.getConsumerKey()),
+ AUTH_TYPE_OAUTH_CR);
}
- private void handleFullOAuth(OAuthMessage requestMessage,
- OAuthContext authContext) {
- throw new NotImplementedException("full OAuth support not yet implemented");
+ /**
+ * Reads the gadget security token out of an {@link HttpServletRequest},
+ * making sure to return null if there are problems.
+ */
+ protected SecurityToken getSecurityToken(HttpServletRequest request) {
+ String token = request.getParameter(SECURITY_TOKEN_PARAM);
+
+ if (token == null || token.trim().length() == 0) {
+ return null;
+ }
+
+ try {
+ Map<String, String> params =
+ Collections.singletonMap(SecurityTokenDecoder.SECURITY_TOKEN_NAME,
+ token);
+ return securityTokenDecoder.createToken(params);
+ } catch (SecurityTokenException e) {
+ String message = new StringBuilder()
+ .append("found security token, but couldn't decode it ")
+ .append("(treating it as not present). token is: ")
+ .append(token)
+ .toString();
+ log.log(Level.WARNING, message, e);
+ return null;
+ }
+ }
+
+ /**
+ * Handles the normal ("full") OAuth case, i.e., an {@link HttpServletRequest}
+ * that includes an oauth_token alongside the other OAuth parameters.
+ * @param request the {@link HttpServletRequest}
+ * @param requestMessage the {@link HttpServletRequest}, parsed out into a
+ * {@link OAuthMessage}.
+ * @return an {@link HttpServletRequest} suitable for passing on to the next
+ * element in the filter chain.
+ * @throws OAuthProblemException when there is a problem authenticating the
+ * request.
+ */
+ protected HttpServletRequest handleFullOAuth(HttpServletRequest request,
+ OAuthMessage requestMessage)
+ throws IOException, ServletException, OAuthProblemException {
+
+ // this throws if the message signature doesn't check out
+ validateFullOAuthMessage(requestMessage);
+
+ // map from token to identity
+ Principal user;
+ try {
+ user = tokenMapper.getPrincipalForToken(requestMessage);
+ } catch (PrincipalMapperException e) {
+ throw new ServletException(e);
+ }
+
+ return new AuthServletRequest(request, user, AUTH_TYPE_OAUTH);
+ }
+
+ /**
+ * Validates an OAuth message that includes a oauth_token. We look up the
+ * verification key in the tokenStore.
+ */
+ protected void validateFullOAuthMessage(OAuthMessage requestMessage)
+ throws IOException, OAuthProblemException, ServletException {
+ String consumerKey = requestMessage.getConsumerKey();
+ String accessToken = requestMessage.getToken();
+
+ OAuthAccessor accessor = tokenStore.getAccessor(accessToken, consumerKey);
+
+ validateMessage(requestMessage, accessor);
}
- private void handleSignedFetch(OAuthMessage requestMessage,
- OAuthContext context) {
- // TODO implement this method
+ /**
+ * Validates an OAuth ConsumerRequest message (i.e., one that doean't include
+ * an oauth_token). We look up the verification key in the consumerStore.
+ */
+ protected void validateConsumerRequestMessage(OAuthMessage requestMessage)
+ throws IOException, OAuthProblemException, ServletException {
+ String consumerKey = requestMessage.getConsumerKey();
+ OAuthAccessor accessor = consumerStore.getAccessor(consumerKey);
+
+ validateMessage(requestMessage, accessor);
+ }
+
+ /**
+ * Validates an OAuth request, making sure to throw appropriate exceptions
+ * when things don't work out. In particular, an {@link OAuthProblemException}
+ * is thrown when problems about the OAuth request should be communicated
+ * back to the HTTP client.
+ * @param requestMessage the OAuth message (HTTP request) to be verified
+ * @param accessor the OAuth accessor that contains the key material needed
+ * to verify the request.
+ */
+ protected void validateMessage(OAuthMessage requestMessage,
+ OAuthAccessor accessor)
+ throws IOException, ServletException, OAuthProblemException {
+ try {
+ oauthValidator.validateMessage(requestMessage, accessor);
+ } catch (OAuthException e) {
+ if (e instanceof OAuthProblemException) {
+ // this one we'll just re-throw since the caller knows how to handle it
+ // (this includes the case where the signature doesn't verify)
+ throw (OAuthProblemException)e;
+ } else {
+ throw new ServletException(e);
+ }
+ } catch (URISyntaxException e) {
+ throw new ServletException(e);
+ }
+ }
+
+ /**
+ * Determines whether the incoming HTTP request uses OAuth at all.
+ * @throws IOException
+ * @throws OAuthProblemException
+ */
+ protected void requireOAuth(OAuthMessage requestMessage) throws OAuthProblemException, IOException {
+ requestMessage.requireParameters(OAuth.OAUTH_CONSUMER_KEY, OAuth.OAUTH_SIGNATURE);
+ }
+
+ /**
+ * Parses an incoming HTTP request out into an {@link OAuthMessage} object.
+ */
+ protected OAuthMessage getOAuthMessageFromRequest(HttpServletRequest req)
+ throws OAuthProblemException {
+ return OAuthServlet.getMessage(req, null);
+ }
+
+ /**
+ * Sends back a properly-formatted OAuth problem report.
+ */
+ protected void handleException(HttpServletResponse response,
+ OAuthProblemException e) throws IOException, ServletException {
+ OAuthServlet.handleException(response, e, "");
+ }
+
+ /**
+ * An {@link HttpServletRequest} that reports "OAuth" as the auth type and
+ * a specific given user as the userPrincipal making the request.
+ *
+ */
+ private static class AuthServletRequest extends HttpServletRequestWrapper {
+ private final Principal user;
+ private final String authType;
+
+ public AuthServletRequest(HttpServletRequest request, Principal user,
+ String authType) {
+ super(request);
+ this.user = user;
+ this.authType = authType;
+ }
+
+ @Override
+ public Principal getUserPrincipal() {
+ return user;
+ }
+
+ @Override
+ public String getAuthType() {
+ return authType;
+ }
}
}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStore.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStore.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStore.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStore.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthConsumer;
+import net.oauth.OAuthServiceProvider;
+
+/**
+ * Implementation of the {@link OAuthConsumerStore} based on a static
+ * configuration file which lists consumer keys and secrets, and is parsed
+ * at startup time.
+ */
+public class BasicOAuthConsumerStore implements OAuthConsumerStore {
+
+ public OAuthAccessor getAccessor(String consumerKey) {
+ // TODO: implement this further
+ return new OAuthAccessor(new OAuthConsumer("", "", "", new OAuthServiceProvider("", "", "")));
+ }
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapper.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapper.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapper.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapper.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthMessage;
+
+/**
+ * Implementation of the {@link OAuthTokenPrincipalMapper} based on a static
+ * configuration file which lists access tokens and users they belong to.
+ */
+public class BasicOAuthTokenPrincipalMapper implements OAuthTokenPrincipalMapper {
+
+ public OAuthPrincipal getPrincipalForToken(OAuthMessage message) {
+ // TODO: implement this further
+ return new OAuthPrincipal(message, "john.doe");
+ }
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenStore.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenStore.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenStore.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/BasicOAuthTokenStore.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthConsumer;
+import net.oauth.OAuthServiceProvider;
+
+
+/**
+ * Implementation of the {@link OAuthTokenStore} based on a static
+ * configuration file which lists access tokens and secrets, and is parsed
+ * at startup time.
+ */
+public class BasicOAuthTokenStore implements OAuthTokenStore {
+
+ public OAuthAccessor getAccessor(String accessToken, String consumerKey) {
+ // TODO: implement this further
+ return new OAuthAccessor(new OAuthConsumer("", "", "", new OAuthServiceProvider("", "", "")));
+ }
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/DelegatedPrincipal.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/DelegatedPrincipal.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/DelegatedPrincipal.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/DelegatedPrincipal.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import java.security.Principal;
+
+/**
+ * The principal class that is provided by OAuthServletFilter. Servlets can use
+ * HttpServletRequest.getUserPrincipal() and downcast the return value to
+ * DelegatedPrincipal in order to obtain an object representing the
+ * authenticated principal.
+ *
+ * A DelegatedPrincipal will only have a subset of the privileges compared to
+ * the privileges held by the original user.
+ *
+ * TODO: Implement appropriate methods that deal with the privileges held by
+ * this principal.
+ */
+public abstract class DelegatedPrincipal implements Principal {
+
+ public String getName() {
+ return new StringBuilder()
+ .append("DelegatedPrincipal[delegator: ")
+ .append(getDelegator())
+ .append(", delegatee: ")
+ .append(getDelegatee())
+ .append("]")
+ .toString();
+ }
+
+ /**
+ * Returns the user id, in a format native to the container, of the user that
+ * delegated some authority to this principal
+ */
+ public abstract String getDelegator();
+
+ /**
+ * Returns the name of the entity to which the user delegated some authority.
+ */
+ public abstract String getDelegatee();
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/GadgetSecurityTokenPrincipal.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/GadgetSecurityTokenPrincipal.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/GadgetSecurityTokenPrincipal.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/GadgetSecurityTokenPrincipal.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import org.apache.shindig.common.SecurityToken;
+
+public class GadgetSecurityTokenPrincipal extends DelegatedPrincipal {
+
+ private SecurityToken token;
+
+ public GadgetSecurityTokenPrincipal(SecurityToken token) {
+ this.token = token;
+ }
+
+ @Override
+ public String getDelegatee() {
+ return token.getDomain();
+ }
+
+ public String getDelegator() {
+ return token.getViewerId();
+ }
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthConsumerStore.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthConsumerStore.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthConsumerStore.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthConsumerStore.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthProblemException;
+
+/**
+ * Interface describing a service that can be used to look up keying material
+ * needed to verify incoming "two-legged" OAuth requests.
+ */
+public interface OAuthConsumerStore {
+
+ /**
+ * Returns an OAuthAccessor (which includes the consumer secret needed for
+ * request verification) for the given consumer key.
+ *
+ * @param consumerKey the consumer key (oauth_consumer_key) from the HTTP
+ * request.
+ * @return an OAuthAccessor that can be passed, along with an OAuthMessage,
+ * to an OAuthValidator to validate the message. The token secret
+ * in the OAuthAccessor will be set to the empty string.
+ * @throws OAuthProblemException if the information associated with the
+ * consumer. The reasons may include unknown or rejected consumer
+ * keys, etc.
+ */
+ OAuthAccessor getAccessor(String consumerKey) throws OAuthProblemException;
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthPrincipal.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthPrincipal.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthPrincipal.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthPrincipal.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthMessage;
+
+import java.io.IOException;
+import java.util.logging.Logger;
+
+public class OAuthPrincipal extends DelegatedPrincipal {
+
+ private static Logger log = Logger.getLogger(OAuthPrincipal.class.getName());
+
+ private OAuthMessage message;
+ private String delegator;
+
+ public OAuthPrincipal(OAuthMessage message, String delegator) {
+ this.message = message;
+ this.delegator = delegator;
+ }
+
+ @Override
+ public String getDelegatee() {
+ try {
+ return message.getConsumerKey();
+ } catch (IOException e) {
+ log.warning("no consumer key in OAuth message - returning null");
+ return null;
+ }
+ }
+
+ @Override
+ public String getDelegator() {
+ return delegator;
+ }
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenPrincipalMapper.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenPrincipalMapper.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenPrincipalMapper.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenPrincipalMapper.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthMessage;
+import net.oauth.OAuthProblemException;
+
+/**
+ * Interface describing a service that can be used to look up
+ * {@link DelegatedPrincipal}s associated with OAuth tokens.
+ */
+public interface OAuthTokenPrincipalMapper {
+
+ /**
+ * Returns the user for which the access token <code>accessToken</code> had
+ * been issued.
+ *
+ * @param message the OAuth message (including, among other things, the oauth
+ * token and consumer key needed to map to the principal represented
+ * by the token). Other information in the OAuth message may influence
+ * the privileges granted to the principal.
+ * @return the Principal that represents the (possibly partial) delegation of
+ * authority.
+ * @throws OAuthProblemException when the accessToken was not issued for the
+ * consumer identified in the <code>message</code>.
+ * @throws PrincipalMapperException when there was a problem retrieving the
+ * user associated with the principal.
+ */
+ OAuthPrincipal getPrincipalForToken(OAuthMessage message)
+ throws OAuthProblemException, PrincipalMapperException;
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenStore.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenStore.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenStore.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/OAuthTokenStore.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthProblemException;
+
+/**
+ * Interface describing a service that can be used to look up keying material
+ * needed to verify incoming OAuth requests.
+ */
+public interface OAuthTokenStore {
+
+ /**
+ * Returns an OAuthAccessor (which includes a consumer secret and token
+ * secret needed for request verification) for the given access token and
+ * consumer key.
+ *
+ * @param accessToken the access token (oauth_token) from the HTTP request
+ * @param consumerKey the consumer key (oauth_consumer_key) from the HTTP
+ * request.
+ * @return an OAuthAccessor that can be passed, along with an OAuthMessage,
+ * to an OAuthValidator to validate the message.
+ * @throws OAuthProblemException if the information associated with the
+ * consumer and/or token cannot be retrieved. The reasons may include
+ * expired access tokens, unknown consumer keys, etc.
+ */
+ OAuthAccessor getAccessor(String accessToken, String consumerKey)
+ throws OAuthProblemException;
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/PrincipalMapperException.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/PrincipalMapperException.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/PrincipalMapperException.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/PrincipalMapperException.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+/**
+ * Exception indicating that an OAuth message could not be mapped to a
+ * Principal, for reasons other than those enumerated in the OAuth
+ * problem reporting extension.
+ */
+public class PrincipalMapperException extends Exception {
+
+ private static final long serialVersionUID = -5280066863263541520L;
+
+ public PrincipalMapperException() {
+ super();
+ }
+
+ public PrincipalMapperException(String message) {
+ super(message);
+ }
+
+ public PrincipalMapperException(Throwable t) {
+ super(t);
+ }
+
+ public PrincipalMapperException(String message, Throwable t) {
+ super(message, t);
+ }
+
+}
Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/RequestorIdPrincipal.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/RequestorIdPrincipal.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/RequestorIdPrincipal.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/oauth/RequestorIdPrincipal.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+public class RequestorIdPrincipal extends DelegatedPrincipal {
+
+ private String container;
+ private String requestorId;
+
+ public RequestorIdPrincipal(String requestorId, String container) {
+ this.requestorId = requestorId;
+ this.container = container;
+ }
+
+ @Override
+ public String getDelegatee() {
+ return container;
+ }
+
+ @Override
+ public String getDelegator() {
+ return requestorId;
+ }
+}
Modified: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java?rev=678036&r1=678035&r2=678036&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java (original)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/EasyMockTestCase.java Fri Jul 18 13:59:06 2008
@@ -17,13 +17,13 @@
*/
package org.apache.shindig.social;
+import java.lang.reflect.Method;
+import java.util.List;
+
import junit.framework.TestCase;
import org.easymock.classextension.EasyMock;
-import java.util.ArrayList;
-import java.util.List;
-
import com.google.common.collect.Lists;
public abstract class EasyMockTestCase extends TestCase {
@@ -50,6 +50,31 @@
}
/**
+ * Creates a nice mock object for the given class, adds it to the internal
+ * list of all mocks, and returns it.
+ *
+ * @param clazz Class to be mocked.
+ * @return A mock instance of the given type.
+ **/
+ protected <T> T mock(Class<T> clazz, Method[] methods) {
+ return mock(clazz, methods, false);
+ }
+
+ /**
+ * Creates a strict mock object for the given class, adds it to the internal
+ * list of all mocks, and returns it.
+ *
+ * @param clazz Class to be mocked.
+ * @return A mock instance of the given type.
+ **/
+ protected <T> T mock(Class<T> clazz, Method[] methods, boolean strict) {
+ T m = strict ? EasyMock.createMock(clazz, methods)
+ : EasyMock.createNiceMock(clazz, methods);
+ mocks.add(m);
+ return m;
+ }
+
+ /**
* Creates a strict or nice mock object for the given class, adds it to the internal
* list of all mocks, and returns it.
*
Added: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStoreTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStoreTest.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStoreTest.java (added)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthConsumerStoreTest.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthConsumer;
+import net.oauth.OAuthServiceProvider;
+import junit.framework.TestCase;
+
+public class BasicOAuthConsumerStoreTest extends TestCase {
+
+ private BasicOAuthConsumerStore store;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ store = new BasicOAuthConsumerStore();
+ }
+
+ public void testGetAccessor() throws Exception {
+ OAuthAccessor accessor = store.getAccessor("");
+
+ assertNull(accessor.accessToken);
+ assertNull(accessor.requestToken);
+ assertNull(accessor.tokenSecret);
+
+ OAuthConsumer consumer = accessor.consumer;
+
+ assertEquals("", consumer.callbackURL);
+ assertEquals("", consumer.consumerKey);
+ assertEquals("", consumer.consumerSecret);
+
+ OAuthServiceProvider provider = consumer.serviceProvider;
+
+ assertEquals("", provider.accessTokenURL);
+ assertEquals("", provider.requestTokenURL);
+ assertEquals("", provider.userAuthorizationURL);
+ }
+}
Added: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapperTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapperTest.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapperTest.java (added)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenPrincipalMapperTest.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import junit.framework.TestCase;
+
+public class BasicOAuthTokenPrincipalMapperTest extends TestCase {
+
+ private BasicOAuthTokenPrincipalMapper mapper;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mapper = new BasicOAuthTokenPrincipalMapper();
+ }
+
+ public void testGetAccessor() throws Exception {
+ DelegatedPrincipal principal = mapper.getPrincipalForToken(null);
+
+ assertEquals("john.doe", principal.getDelegator());
+ }
+}
Added: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenStoreTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenStoreTest.java?rev=678036&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenStoreTest.java (added)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/BasicOAuthTokenStoreTest.java Fri Jul 18 13:59:06 2008
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. 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.
+ */
+package org.apache.shindig.social.oauth;
+
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthConsumer;
+import net.oauth.OAuthServiceProvider;
+import junit.framework.TestCase;
+
+public class BasicOAuthTokenStoreTest extends TestCase {
+
+ private BasicOAuthTokenStore store;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ store = new BasicOAuthTokenStore();
+ }
+
+ public void testGetAccessor() throws Exception {
+ OAuthAccessor accessor = store.getAccessor("", "");
+
+ assertNull(accessor.accessToken);
+ assertNull(accessor.requestToken);
+ assertNull(accessor.tokenSecret);
+
+ OAuthConsumer consumer = accessor.consumer;
+
+ assertEquals("", consumer.callbackURL);
+ assertEquals("", consumer.consumerKey);
+ assertEquals("", consumer.consumerSecret);
+
+ OAuthServiceProvider provider = consumer.serviceProvider;
+
+ assertEquals("", provider.accessTokenURL);
+ assertEquals("", provider.requestTokenURL);
+ assertEquals("", provider.userAuthorizationURL);
+ }
+
+}
Modified: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/OAuthServletFilterTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/OAuthServletFilterTest.java?rev=678036&r1=678035&r2=678036&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/OAuthServletFilterTest.java (original)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/oauth/OAuthServletFilterTest.java Fri Jul 18 13:59:06 2008
@@ -17,37 +17,431 @@
*/
package org.apache.shindig.social.oauth;
+import org.apache.shindig.common.SecurityToken;
+import org.apache.shindig.common.SecurityTokenDecoder;
+import org.apache.shindig.common.crypto.Crypto;
import org.apache.shindig.social.EasyMockTestCase;
-import org.apache.shindig.social.oauth.OAuthContextTest.FakeHttpServletRequest;
+
+import net.oauth.OAuth;
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthConsumer;
+import net.oauth.OAuthMessage;
+import net.oauth.OAuthProblemException;
+import net.oauth.OAuthServiceProvider;
+import net.oauth.OAuthValidator;
+import net.oauth.SimpleOAuthValidator;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
import javax.servlet.FilterChain;
+import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
public class OAuthServletFilterTest extends EasyMockTestCase {
- private FakeHttpServletRequest request;
- private HttpServletResponse response;
- private FilterChain chain;
-
- private OAuthServletFilter filter;
+ private AuthenticationServletFilter filter;
+ private String url;
+ private String consumerKey;
+ private String consumerSecret;
+ private String accessToken;
+ private String tokenSecret;
+
+ private OAuthValidator validator;
+ private OAuthTokenStore tokenStore;
+ private OAuthConsumerStore consumerStore;
+ private OAuthTokenPrincipalMapper tokenMapper;
@Override
protected void setUp() throws Exception {
super.setUp();
+ filter = new AuthenticationServletFilter();
+
+ validator = mock(OAuthValidator.class);
+ tokenStore = mock(OAuthTokenStore.class);
+ consumerStore = mock(OAuthConsumerStore.class);
+ tokenMapper = mock(OAuthTokenPrincipalMapper.class);
+
+ filter.setOAuthUtils(validator, tokenStore, consumerStore, tokenMapper);
+
+ url = "http://www.foo.com/bar.xml";
+ consumerKey = "consumerKey";
+ consumerSecret = "consumerSecret";
+
+ accessToken = "accessToken";
+ tokenSecret = "tokenSecret";
+ }
+
+ private OAuthAccessor createAccessor() throws Exception {
+ OAuthServiceProvider provider = new OAuthServiceProvider("", "", "");
+
+ OAuthConsumer consumer = new OAuthConsumer("", consumerKey, consumerSecret,
+ provider);
+
+ OAuthAccessor accessor = new OAuthAccessor(consumer);
+
+ accessor.accessToken = accessToken;
+ accessor.tokenSecret = tokenSecret;
+
+ return accessor;
+ }
+
+ private OAuthMessage createOAuthMessage(
+ Collection<? extends Map.Entry<String, String>> params) throws Exception {
+
+ if (params == null) {
+ params = new ArrayList<Map.Entry<String,String>>();
+ }
+
+ OAuthAccessor accessor = createAccessor();
- request = new FakeHttpServletRequest();
- response = mock(HttpServletResponse.class);
- chain = mock(FilterChain.class);
+ OAuthMessage message = new OAuthMessage("GET", url, params);
+ message.addParameter(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);
+ message.addParameter(OAuth.OAUTH_CONSUMER_KEY, consumerKey);
+ message.addParameter(OAuth.OAUTH_TOKEN, accessToken);
- filter = new OAuthServletFilter();
+ String nonce = Long.toHexString(Crypto.rand.nextLong());
+ message.addParameter(OAuth.OAUTH_NONCE, nonce);
+
+ String timestamp = Long.toString(System.currentTimeMillis()/1000L);
+ message.addParameter(OAuth.OAUTH_TIMESTAMP, timestamp);
+
+ message.sign(accessor);
+
+ return message;
+ }
+
+ private HttpServletRequest createServletRequest(
+ Collection<? extends Map.Entry<String, String>> params) throws Exception {
+
+ OAuthMessage message = createOAuthMessage(params);
+
+ HttpServletRequest request = mock(HttpServletRequest.class);
+
+ HashMap<String, String[]> map = new HashMap<String, String[]>();
+ for (Map.Entry<String, String> entry : message.getParameters()) {
+ map.put(entry.getKey(), new String[] { entry.getValue() });
+ }
+
+ expect(request.getRequestURL())
+ .andStubReturn(new StringBuffer(message.URL));
+ expect(request.getParameterMap()).andStubReturn(map);
+
+ return request;
+ }
+
+ private AuthenticationServletFilter createFilterForHandleRequest() throws Exception {
+
+ Method requestUsesSecurityToken = AuthenticationServletFilter.class
+ .getDeclaredMethod("requestUsesSecurityToken", HttpServletRequest.class);
+
+ Method handleSecurityTokenRequest = AuthenticationServletFilter.class
+ .getDeclaredMethod("handleSecurityTokenRequest", HttpServletRequest.class);
+
+ Method handleConsumerRequest = AuthenticationServletFilter.class
+ .getDeclaredMethod("handleConsumerRequest", HttpServletRequest.class,
+ OAuthMessage.class);
+
+ Method handleFullOAuth = AuthenticationServletFilter.class
+ .getDeclaredMethod("handleFullOAuth", HttpServletRequest.class,
+ OAuthMessage.class);
+
+ Method getOAuthMessageFromRequest = AuthenticationServletFilter.class
+ .getDeclaredMethod("getOAuthMessageFromRequest", HttpServletRequest.class);
+
+ return mock(AuthenticationServletFilter.class, new Method[] {
+ requestUsesSecurityToken,
+ handleSecurityTokenRequest,
+ handleConsumerRequest,
+ handleFullOAuth,
+ getOAuthMessageFromRequest
+ });
}
- public void testUnauthenticated() throws Exception {
+ private AuthenticationServletFilter createFilterForDoFilter() throws Exception {
+ Method handleRequest = AuthenticationServletFilter.class
+ .getDeclaredMethod("handleRequest", HttpServletRequest.class);
+
+ Method handleException = AuthenticationServletFilter.class
+ .getDeclaredMethod("handleException", HttpServletResponse.class,
+ OAuthProblemException.class);
+
+ return mock(AuthenticationServletFilter.class, new Method[] {
+ handleRequest,
+ handleException,
+ });
+ }
+
+ public void testDoFilter_authSucceeds() throws Exception {
+ filter = createFilterForDoFilter();
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ HttpServletRequest wrappedRequest = mock(HttpServletRequest.class);
+ HttpServletResponse response = mock(HttpServletResponse.class);
+ FilterChain chain = mock(FilterChain.class);
+
+ expect(filter.handleRequest(request)).andReturn(wrappedRequest);
+ chain.doFilter(wrappedRequest, response);
+ expectLastCall().once();
+
+ replay();
+
+ filter.doFilter(request, response, chain);
+
+ verify();
+ }
+
+ public void testDoFilter_authFails() throws Exception {
+ filter = createFilterForDoFilter();
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ OAuthProblemException problemException = new OAuthProblemException("foo");
+ HttpServletResponse response = mock(HttpServletResponse.class);
+ FilterChain chain = mock(FilterChain.class);
+
+ expect(filter.handleRequest(request)).andThrow(problemException);
+ filter.handleException(response, problemException);
+ expectLastCall().once();
+
+ replay();
+
filter.doFilter(request, response, chain);
- assertEquals(OAuthContext.AuthMethod.NONE,
- OAuthContext.fromRequest(request).getAuthMethod());
+ verify();
+ }
+
+ public void testHandleRequest_securityToken() throws Exception {
+ filter = createFilterForHandleRequest();
+
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ HttpServletRequest result = mock(HttpServletRequest.class);
+
+ expect(filter.requestUsesSecurityToken(request)).andReturn(true);
+ expect(filter.handleSecurityTokenRequest(request)).andReturn(result);
+
+ replay();
+
+ assertSame(result, filter.handleRequest(request));
+
+ verify();
+ }
+
+ public void testHandleRequest_consumerRequest() throws Exception {
+ filter = createFilterForHandleRequest();
+
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ HttpServletRequest result = mock(HttpServletRequest.class);
+ OAuthMessage message = mock(OAuthMessage.class);
+
+ expect(filter.requestUsesSecurityToken(request)).andReturn(false);
+ expect(filter.getOAuthMessageFromRequest(request)).andReturn(message);
+ message.requireParameters(OAuth.OAUTH_CONSUMER_KEY, OAuth.OAUTH_SIGNATURE);
+ expectLastCall();
+
+ expect(message.getToken()).andReturn(null);
+
+ expect(filter.handleConsumerRequest(request, message)).andReturn(result);
+
+ replay();
+
+ assertSame(result, filter.handleRequest(request));
+
+ verify();
+ }
+
+ public void testHandleRequest_fullOAuth() throws Exception {
+ filter = createFilterForHandleRequest();
+
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ HttpServletRequest result = mock(HttpServletRequest.class);
+ OAuthMessage message = mock(OAuthMessage.class);
+
+ expect(filter.requestUsesSecurityToken(request)).andReturn(false);
+ expect(filter.getOAuthMessageFromRequest(request)).andReturn(message);
+ message.requireParameters(OAuth.OAUTH_CONSUMER_KEY, OAuth.OAUTH_SIGNATURE);
+ expectLastCall();
+
+ expect(message.getToken()).andReturn("token");
+
+ expect(filter.handleFullOAuth(request, message)).andReturn(result);
+
+ replay();
+
+ assertSame(result, filter.handleRequest(request));
+
+ verify();
+ }
+
+ public void testGetRequestorId_present() throws Exception {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+
+ expect(request.getParameter(AuthenticationServletFilter.REQUESTOR_ID_PARAM))
+ .andReturn("abc");
+
+ replay();
+
+ assertSame("abc", filter.getRequestorId(request));
+
+ verify();
+ }
+
+ public void testGetRequestorId_absent() throws Exception {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+
+ expect(request.getParameter(AuthenticationServletFilter.REQUESTOR_ID_PARAM))
+ .andReturn(null);
+
+ replay();
+
+ assertNull(filter.getRequestorId(request));
+
+ verify();
+ }
+
+ public void testHandleRequestorIdRequest() throws Exception {
+ HttpServletRequest request = createServletRequest(null);
+ OAuthMessage message = createOAuthMessage(null);
+ OAuthAccessor accessor = createAccessor();
+ DelegatedPrincipal principal = new RequestorIdPrincipal("john doe", message.getConsumerKey());
+
+ // set a real validator
+ filter.setOAuthUtils(new SimpleOAuthValidator(), tokenStore, consumerStore,
+ tokenMapper);
+
+ expect(consumerStore.getAccessor(consumerKey)).andReturn(accessor);
+ expect(request.getParameter("xoauth_requestor_id")).andReturn("john doe");
+
+ replay();
+
+ HttpServletRequest result = filter.handleRequestorIdRequest(request, message);
+
+ verify();
+
+ assertEquals(principal.getName(), result.getUserPrincipal().getName());
+ assertEquals("OAuth-ConsumerRequest", result.getAuthType());
+ }
+
+ public void testGetSecurityToken_present() throws Exception {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+
+ SecurityToken token = mock(SecurityToken.class);
+ SecurityTokenDecoder decoder = mock(SecurityTokenDecoder.class);
+
+ filter.setSecurityTokenDecoder(decoder);
+
+ expect(request.getParameter(AuthenticationServletFilter.SECURITY_TOKEN_PARAM))
+ .andReturn("abc");
+ expect(decoder.createToken(
+ Collections.singletonMap(SecurityTokenDecoder.SECURITY_TOKEN_NAME,
+ "abc"))).andReturn(token);
+
+ replay();
+
+ assertSame(token, filter.getSecurityToken(request));
+
+ verify();
}
+ public void testGetSecurityToken_absent() throws Exception {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+
+ SecurityTokenDecoder decoder = mock(SecurityTokenDecoder.class);
+ filter.setSecurityTokenDecoder(decoder);
+
+ expect(request.getParameter(AuthenticationServletFilter.SECURITY_TOKEN_PARAM))
+ .andReturn(null);
+
+ replay();
+
+ assertNull(filter.getSecurityToken(request));
+
+ verify();
+ }
+
+ public void testHandleFullOAuth_success() throws Exception {
+ HttpServletRequest request = createServletRequest(null);
+ OAuthMessage message = createOAuthMessage(null);
+ OAuthAccessor accessor = createAccessor();
+
+ // set a real validator
+ filter.setOAuthUtils(new SimpleOAuthValidator(), tokenStore, consumerStore,
+ tokenMapper);
+
+ OAuthPrincipal principal = new OAuthPrincipal(message, "john.doe");
+
+ expect(tokenStore.getAccessor(accessToken, consumerKey))
+ .andReturn(accessor);
+ expect(tokenMapper.getPrincipalForToken(message))
+ .andReturn(principal);
+
+ replay();
+
+ HttpServletRequest result = filter.handleFullOAuth(request, message);
+
+ verify();
+
+ assertSame(principal, result.getUserPrincipal());
+ assertEquals("OAuth", result.getAuthType());
+ }
+
+ public void testHandleFullOAuth_failure() throws Exception {
+ HttpServletRequest request = createServletRequest(null);
+ OAuthMessage message = createOAuthMessage(null);
+ OAuthAccessor accessor = createAccessor();
+
+ // set a real validator
+ filter.setOAuthUtils(new SimpleOAuthValidator(), tokenStore, consumerStore,
+ tokenMapper);
+
+ // mess up the signature...
+ message.addParameter("oauth_some_other_parameter", "xyz");
+
+ expect(tokenStore.getAccessor(accessToken, consumerKey))
+ .andReturn(accessor);
+
+ replay();
+
+ try {
+ filter.handleFullOAuth(request, message);
+ fail("expected OAuthProblemException, but didn't get it");
+ } catch (OAuthProblemException e) {
+ assertEquals(OAuth.Problems.SIGNATURE_INVALID, e.getProblem());
+ }
+
+ verify();
+ }
+
+ public void testValidateMessage() throws Exception {
+
+ OAuthMessage message = mock(OAuthMessage.class);
+ OAuthAccessor accessor = mock(OAuthAccessor.class);
+
+ validator.validateMessage(message, accessor);
+ expectLastCall();
+
+ replay();
+
+ filter.validateMessage(message, accessor);
+
+ verify();
+ }
+
+ public void testGetOAuthMessageFromRequest() throws Exception {
+
+ HttpServletRequest request = createServletRequest(null);
+
+ replay();
+
+ OAuthMessage message = filter.getOAuthMessageFromRequest(request);
+
+ verify();
+
+ assertEquals(url, message.URL);
+ assertEquals(consumerKey, message.getConsumerKey());
+ assertEquals(accessToken, message.getToken());
+ }
}
Modified: incubator/shindig/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/pom.xml?rev=678036&r1=678035&r2=678036&view=diff
==============================================================================
--- incubator/shindig/trunk/pom.xml (original)
+++ incubator/shindig/trunk/pom.xml Fri Jul 18 13:59:06 2008
@@ -783,7 +783,7 @@
<dependency>
<groupId>net.oauth</groupId>
<artifactId>core</artifactId>
- <version>20080524</version>
+ <version>20080621</version>
<scope>compile</scope>
</dependency>
<dependency>