Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pushing code for ben
  • Loading branch information
ajs6f committed Jun 4, 2013
1 parent 6c93394 commit 96023ad
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -45,7 +45,7 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>test</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.grizzly</groupId>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/fcrepo/auth/oauth/Constants.java
@@ -0,0 +1,11 @@

package org.fcrepo.auth.oauth;

public interface Constants {

public static final String OAUTH_WORKSPACE = "oauth";

public static final String CLIENT_PROPERTY = "oauth:client";

public static final String PRINCIPAL_PROPERTY = "oauth:principal";
}
55 changes: 55 additions & 0 deletions src/main/java/org/fcrepo/auth/oauth/Decision.java
@@ -0,0 +1,55 @@

package org.fcrepo.auth.oauth;

import java.security.Principal;

import org.apache.oltu.oauth2.rsfilter.OAuthClient;
import org.apache.oltu.oauth2.rsfilter.OAuthDecision;

public class Decision implements OAuthDecision {

private OAuthClient oAuthClient;

private Principal principal;

private boolean isAuthorized;

public Decision(final String client, final String principal) {
this.oAuthClient = new OAuthClient() {

@Override
public String getClientId() {
return client;
}

};
this.principal = new Principal() {

@Override
public String getName() {
return principal;
}

};
}

@Override
public OAuthClient getOAuthClient() {
return oAuthClient;
}

@Override
public Principal getPrincipal() {
return principal;
}

@Override
public boolean isAuthorized() {
return isAuthorized;
}

public void setAuthorized(final boolean isAuthorized) {
this.isAuthorized = isAuthorized;
}

}
@@ -0,0 +1,67 @@

package org.fcrepo.auth.oauth;

import static com.google.common.base.Throwables.propagate;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;

import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthRuntimeException;
import org.apache.oltu.oauth2.rsfilter.OAuthDecision;
import org.apache.oltu.oauth2.rsfilter.OAuthRSProvider;
import org.fcrepo.session.SessionFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static org.fcrepo.auth.oauth.Constants.*;

@Component
public class DefaultOAuthResourceProvider implements OAuthRSProvider {

@Autowired
private SessionFactory sessionFactory;

private static final Logger LOGGER =
getLogger(DefaultOAuthResourceProvider.class);

@Override
public OAuthDecision validateRequest(final String rsId, final String token,
final HttpServletRequest req) throws OAuthProblemException {
// first check validity of token
try {
final Session session = sessionFactory.getSession(OAUTH_WORKSPACE);
try {
if (!session.itemExists("/tokens/" + token)) {
throw new OAuthRuntimeException("Invalid token!");
} else {
final Node tokenNode = session.getNode("/tokens/" + token);
LOGGER.debug("Retrieved token from: {}", tokenNode
.getPath());
final String client =
tokenNode.getProperty(CLIENT_PROPERTY).getString();
LOGGER.debug("Retrieved client: {}", client);
final String principal =
tokenNode.getProperty(PRINCIPAL_PROPERTY)
.getString();
LOGGER.debug("Retrieved principal: {}", principal);
return new Decision(client, principal);
}
} finally {
session.logout();
}
} catch (final RepositoryException e) {
propagate(e);
}

return null;
}

public void setSessionFactory(final SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
21 changes: 20 additions & 1 deletion src/main/java/org/fcrepo/auth/oauth/api/AuthzEndpoint.java
@@ -1,6 +1,7 @@

package org.fcrepo.auth.oauth.api;

import static com.google.common.collect.ImmutableSet.copyOf;
import static javax.servlet.http.HttpServletResponse.SC_FOUND;
import static javax.ws.rs.core.Response.status;
import static org.apache.oltu.oauth2.as.response.OAuthASResponse.authorizationResponse;
Expand All @@ -18,6 +19,9 @@
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.annotation.PostConstruct;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletRequest;
import org.apache.oltu.oauth2.as.issuer.MD5Generator;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
Expand All @@ -26,13 +30,15 @@
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.fcrepo.AbstractResource;
import org.springframework.stereotype.Component;

import static org.apache.oltu.oauth2.common.message.OAuthResponse.errorResponse;
import static org.fcrepo.auth.oauth.Constants.OAUTH_WORKSPACE;

@Component
@Path("/authorization")
public class AuthzEndpoint {
public class AuthzEndpoint extends AbstractResource {

@GET
public Response authorize(@Context
Expand Down Expand Up @@ -90,4 +96,17 @@ public Response authorize(@Context
}
}

@PostConstruct
public void init() throws RepositoryException {
final Session session = sessions.getSession();
try {
if (!copyOf(session.getWorkspace().getAccessibleWorkspaceNames())
.contains(OAUTH_WORKSPACE)) {
session.getWorkspace().createWorkspace(OAUTH_WORKSPACE);
}
} finally {
session.logout();
}
}

}
34 changes: 25 additions & 9 deletions src/main/java/org/fcrepo/auth/oauth/api/TokenEndpoint.java
@@ -1,13 +1,24 @@

package org.fcrepo.auth.oauth.api;

import static com.google.common.collect.ImmutableSet.copyOf;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.status;
import static org.apache.oltu.oauth2.as.response.OAuthASResponse.tokenResponse;
import static org.apache.oltu.oauth2.common.OAuth.OAUTH_GRANT_TYPE;
import static org.apache.oltu.oauth2.common.error.OAuthError.TokenResponse.INVALID_CLIENT;
import static org.apache.oltu.oauth2.common.error.OAuthError.TokenResponse.INVALID_GRANT;
import static org.apache.oltu.oauth2.common.error.OAuthError.TokenResponse.UNAUTHORIZED_CLIENT;
import static org.apache.oltu.oauth2.common.message.OAuthResponse.errorResponse;
import static org.fcrepo.auth.oauth.Constants.CLIENT_PROPERTY;
import static org.fcrepo.auth.oauth.Constants.OAUTH_WORKSPACE;
import static org.fcrepo.auth.oauth.Constants.PRINCIPAL_PROPERTY;

import javax.annotation.PostConstruct;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand All @@ -19,12 +30,6 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import static org.apache.oltu.oauth2.as.response.OAuthASResponse.tokenResponse;
import static org.apache.oltu.oauth2.common.message.OAuthResponse.errorResponse;
import static org.apache.oltu.oauth2.common.OAuth.OAUTH_GRANT_TYPE;
import static org.apache.oltu.oauth2.common.error.OAuthError.TokenResponse.INVALID_GRANT;
import static org.apache.oltu.oauth2.common.error.OAuthError.TokenResponse.UNAUTHORIZED_CLIENT;
import static org.apache.oltu.oauth2.common.error.OAuthError.TokenResponse.INVALID_CLIENT;
import org.apache.oltu.oauth2.as.issuer.MD5Generator;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuer;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
Expand All @@ -36,8 +41,6 @@
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import org.fcrepo.AbstractResource;
import static org.fcrepo.auth.oauth.filter.Constants.CLIENT_PROPERTY;
import static org.fcrepo.auth.oauth.filter.Constants.PRINCIPAL_PROPERTY;
import org.springframework.stereotype.Component;

@Component
Expand Down Expand Up @@ -140,7 +143,7 @@ public Response authorize(@Context

private void saveToken(final String token, final String client,
final String username) throws RepositoryException {
final Session session = sessions.getSession();
final Session session = sessions.getSession(OAUTH_WORKSPACE);
try {
final Node tokenNode =
jcrTools.findOrCreateNode(session, "/tokens/" + token);
Expand All @@ -158,4 +161,17 @@ private boolean isValid() {
return false;
}

@PostConstruct
public void init() throws RepositoryException {
final Session session = sessions.getSession();
try {
if (!copyOf(session.getWorkspace().getAccessibleWorkspaceNames())
.contains(OAUTH_WORKSPACE)) {
session.getWorkspace().createWorkspace(OAUTH_WORKSPACE);
}
} finally {
session.logout();
}
}

}

0 comments on commit 96023ad

Please sign in to comment.