Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Code cleanup, beginning to impl token authorization
  • Loading branch information
ajs6f committed Jun 11, 2013
1 parent 1c7cee9 commit 42b2a87
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 34 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/fcrepo/auth/oauth/Constants.java
Expand Up @@ -10,4 +10,7 @@ public interface Constants {

//TODO get namespaced properties to work
public static final String PRINCIPAL_PROPERTY = "oauth-principal";

//TODO get namespaced properties to work
public static final String SCOPES_PROPERTY = "oauth-scopes";
}
46 changes: 34 additions & 12 deletions src/main/java/org/fcrepo/auth/oauth/api/AuthzEndpoint.java
@@ -1,7 +1,6 @@

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 @@ -13,37 +12,46 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.annotation.PostConstruct;
import javax.jcr.Node;
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;
import org.apache.oltu.oauth2.as.request.OAuthAuthzRequest;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.as.response.OAuthASResponse.OAuthAuthorizationResponseBuilder;
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.fcrepo.auth.oauth.Constants;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

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.api.Util.createOauthWorkspace;
import static org.slf4j.LoggerFactory.getLogger;

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

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

@GET
public Response authorize(@Context
final HttpServletRequest request) throws URISyntaxException,
OAuthSystemException {
OAuthSystemException, RepositoryException {

OAuthAuthzRequest oauthRequest = null;

Expand All @@ -57,11 +65,16 @@ public Response authorize(@Context
final String responseType =
oauthRequest.getParam(OAUTH_RESPONSE_TYPE);

final OAuthASResponse.OAuthAuthorizationResponseBuilder builder =
final OAuthAuthorizationResponseBuilder builder =
authorizationResponse(request, SC_FOUND);

if (responseType.equals(CODE.toString())) {
builder.setCode(oauthIssuerImpl.authorizationCode());
final String authCode = oauthIssuerImpl.authorizationCode();
LOGGER.debug("Created authorization code: {}", authCode);
final String client = oauthRequest.getClientId();
final Set<String> scopes = oauthRequest.getScopes();
saveAuthCode(authCode, scopes, client);
builder.setCode(authCode);
}
if (responseType.equals(TOKEN.toString())) {
builder.setAccessToken(oauthIssuerImpl.accessToken());
Expand Down Expand Up @@ -96,17 +109,26 @@ public Response authorize(@Context
}
}

@PostConstruct
public void init() throws RepositoryException {
final Session session = sessions.getSession();
private void saveAuthCode(final String authCode, final Set<String> scopes,
final String client) throws RepositoryException {
final Session session = sessions.getSession(OAUTH_WORKSPACE);
try {
if (!copyOf(session.getWorkspace().getAccessibleWorkspaceNames())
.contains(OAUTH_WORKSPACE)) {
session.getWorkspace().createWorkspace(OAUTH_WORKSPACE);
}
final Node codeNode =
jcrTools.findOrCreateNode(session, "/authorization-codes/" +
authCode);
codeNode.setProperty(CLIENT_PROPERTY, client);
codeNode.setProperty(Constants.SCOPES_PROPERTY, scopes
.toArray(new String[0]));
session.save();
} finally {
session.logout();
}

}

@PostConstruct
public void init() throws RepositoryException {
createOauthWorkspace(sessions);
}

}
22 changes: 10 additions & 12 deletions src/main/java/org/fcrepo/auth/oauth/api/TokenEndpoint.java
@@ -1,7 +1,6 @@

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;
Expand All @@ -17,6 +16,8 @@
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 static org.fcrepo.auth.oauth.api.Util.createOauthWorkspace;
import static org.slf4j.LoggerFactory.getLogger;

import javax.annotation.PostConstruct;
import javax.jcr.Node;
Expand All @@ -41,6 +42,7 @@
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import org.fcrepo.AbstractResource;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -50,13 +52,16 @@ public class TokenEndpoint extends AbstractResource {
public static final String INVALID_CLIENT_DESCRIPTION =
"Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method).";

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

@POST
@Consumes(APPLICATION_FORM_URLENCODED)
@Produces(APPLICATION_JSON)
public Response authorize(@Context
public Response getToken(@Context
final HttpServletRequest request) throws OAuthSystemException,
RepositoryException {

LOGGER.debug("Received request for token carried on request: {}",
request);
OAuthTokenRequest oauthRequest = null;

final OAuthIssuer oauthIssuerImpl =
Expand Down Expand Up @@ -125,6 +130,7 @@ public Response authorize(@Context
}

final String token = oauthIssuerImpl.accessToken();
LOGGER.debug("Created token: {}", token);
saveToken(token, oauthRequest.getClientId(), oauthRequest
.getUsername());
final OAuthResponse response =
Expand Down Expand Up @@ -163,15 +169,7 @@ private boolean isValid() {

@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();
}
createOauthWorkspace(sessions);
}

}
28 changes: 28 additions & 0 deletions src/main/java/org/fcrepo/auth/oauth/api/Util.java
@@ -0,0 +1,28 @@

package org.fcrepo.auth.oauth.api;

import static com.google.common.collect.ImmutableSet.copyOf;
import static org.fcrepo.auth.oauth.Constants.OAUTH_WORKSPACE;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.session.SessionFactory;

public class Util {

public static void
createOauthWorkspace(final SessionFactory sessionFactory)
throws RepositoryException {
final Session session = sessionFactory.getSession();
try {
if (!copyOf(session.getWorkspace().getAccessibleWorkspaceNames())
.contains(OAUTH_WORKSPACE)) {
session.getWorkspace().createWorkspace(OAUTH_WORKSPACE);
}
} finally {
session.logout();
}
}

}
9 changes: 0 additions & 9 deletions src/main/java/org/fcrepo/auth/oauth/filter/Constants.java

This file was deleted.

Expand Up @@ -2,6 +2,8 @@
package org.fcrepo.auth.oauth.filter;

import static com.google.common.base.Throwables.propagate;
import static org.fcrepo.auth.oauth.Constants.CLIENT_PROPERTY;
import static org.fcrepo.auth.oauth.Constants.PRINCIPAL_PROPERTY;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.Node;
Expand All @@ -19,7 +21,7 @@
import org.springframework.stereotype.Component;

@Component
public class DefaultOAuthResourceProvider implements OAuthRSProvider, Constants {
public class DefaultOAuthResourceProvider implements OAuthRSProvider {

@Autowired
SessionFactory sessionFactory;
Expand Down

0 comments on commit 42b2a87

Please sign in to comment.