Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
Made changes to ObjectMapper to instantiate polymorphic Principals
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Apr 18, 2013
1 parent 69cd12f commit 3673319
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 118 deletions.
Expand Up @@ -22,6 +22,7 @@
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.core.spi.scanning.PackageNamesScanner;
import com.sun.jersey.core.spi.scanning.Scanner;
import org.surfnet.oaaas.auth.ObjectMapperProvider;
import org.surfnet.oaaas.resource.resourceserver.AccessTokenResource;
import org.surfnet.oaaas.resource.resourceserver.ClientResource;
import org.surfnet.oaaas.resource.resourceserver.ResourceServerResource;
Expand Down Expand Up @@ -50,10 +51,20 @@ public DefaultPackagesResourceConfig(String... packages) {
@Override
public void init(Scanner scanner) {
Set<Class<?>> classes = getClasses();
classes.add(ClientResource.class);
classes.add(ResourceServerResource.class);
/*
* The actual implementation of the OAuth spec
*/
classes.add(TokenResource.class);
/*
* Responsible for the (only) communication between Resource Servers and the Authorization Server for validation of Tokens
*/
classes.add(VerifyResource.class);
/*
* The Resource Server part of the Authorization Server used by the JavaScript admin client
*/
classes.add(ClientResource.class);
classes.add(ResourceServerResource.class);
classes.add(AccessTokenResource.class);

}
}

This file was deleted.

Expand Up @@ -86,7 +86,8 @@ protected Response buildViolationErrorResponse(Set<ConstraintViolation<?>> viola


protected String getUserId(HttpServletRequest request) {
return ((VerifyTokenResponse) request.getAttribute(AuthorizationServerFilter.VERIFY_TOKEN_RESPONSE)).getPrincipal().getName();
VerifyTokenResponse verifyTokenResponse = (VerifyTokenResponse) request.getAttribute(AuthorizationServerFilter.VERIFY_TOKEN_RESPONSE);
return verifyTokenResponse.getPrincipal().getName();
}

public String generateRandom() {
Expand Down
Expand Up @@ -29,15 +29,20 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.surfnet.oaaas.auth.ObjectMapperProvider;
import org.surfnet.oaaas.auth.principal.UserPassCredentials;
import org.surfnet.oaaas.model.AccessToken;
import org.surfnet.oaaas.model.ResourceServer;
import org.surfnet.oaaas.model.VerifyTokenResponse;
import org.surfnet.oaaas.repository.AccessTokenRepository;
import org.surfnet.oaaas.repository.ResourceServerRepository;

import java.io.IOException;

/**
* Resource for handling the call from resource servers to validate an access
* token. As this is not part of the oauth2 <a
Expand All @@ -54,6 +59,8 @@ public class VerifyResource {

private static final Logger LOG = LoggerFactory.getLogger(VerifyResource.class);

private static final ObjectMapper mapper = new ObjectMapperProvider().getContext(ObjectMapper.class);

@Inject
private AccessTokenRepository accessTokenRepository;

Expand All @@ -63,7 +70,7 @@ public class VerifyResource {
@GET
public Response verifyToken(@HeaderParam(HttpHeaders.AUTHORIZATION)
String authorization, @QueryParam("access_token")
String accessToken) {
String accessToken) throws IOException {

UserPassCredentials credentials = new UserPassCredentials(authorization);

Expand All @@ -87,7 +94,7 @@ public Response verifyToken(@HeaderParam(HttpHeaders.AUTHORIZATION)
token.getScopes(), token.getPrincipal(), token.getExpires());

LOG.debug("Responding with 200 in VerifyResource#verifyToken for user {}", credentials);
return Response.ok(verifyTokenResponse).build();
return Response.ok(mapper.writeValueAsString(verifyTokenResponse)).build();
}

private boolean tokenExpired(AccessToken token) {
Expand Down

This file was deleted.

Expand Up @@ -18,8 +18,11 @@
*/
package org.surfnet.oaaas.auth;

import javax.ws.rs.Produces;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.DeserializationConfig;
Expand All @@ -36,14 +39,16 @@
*/
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {

//http://stackoverflow.com/questions/4428109/jersey-jackson-json-date-serialization-format-problem-how-to-change-the-form

private ObjectMapper mapper;

public ObjectMapperProvider(){
mapper = new ObjectMapper().enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
mapper = new ObjectMapper().enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY).enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL)
.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL).setVisibility(JsonMethod.FIELD, Visibility.ANY);
mapper.registerModule(new MrBeanModule());

}

/* (non-Javadoc)
* @see javax.ws.rs.ext.ContextResolver#getContext(java.lang.Class)
*/
Expand Down
Expand Up @@ -28,7 +28,7 @@ public class AuthenticatedPrincipalTest {
@Test
public void testSerialization() {
AuthenticatedPrincipal principal = new AuthenticatedPrincipal("emma.blunt");
System.out.println(principal.serialize());
assertEquals("emma.blunt",AuthenticatedPrincipal.deserialize(principal.serialize()).getDisplayName());
}

}
14 changes: 14 additions & 0 deletions apis-surfconext-authn/README.md
Expand Up @@ -30,6 +30,20 @@ Enable this plugin by:
- ````spPrivateKey```` is the private key of Apis for the given setup. To be generated by the SP.
- ````spCertificate```` is the public key of Apis for the given setup. To be generated by the SP.

The latter two can be generated by the following commands:

openssl req -subj '/O=Organization, CN=APIS (TEST)/' -newkey rsa:2048 -new -x509 -days 3652 -nodes -out idp.crt -keyout idp.pem

Strip whitespace and the heading and footer from the files and use the result as the value.

In practice, use this command to obtain the value for the key:

cat idp.pem |head -n -1 |tail -n +2 | tr -d '\n'; echo

In practice, use this command to obtain the value for the certificate:

cat idp.crt |head -n -1 |tail -n +2 | tr -d '\n'; echo


## Modifying behaviour
To modify behaviour of this plugin, extend (one of) the following classes and wire them accordingly.
Expand Down

0 comments on commit 3673319

Please sign in to comment.