Skip to content

Commit

Permalink
Revert "Bug 2515 - Jersey to support JAX-RS 2.0"
Browse files Browse the repository at this point in the history
This reverts commit b1c7e09.

Change-Id: I4f1be95f9cab2f25a8309c305f8991a0faaccea2
Signed-off-by: Ryan Goulding <[email protected]>
  • Loading branch information
ryandgoulding committed Oct 29, 2015
1 parent b1c7e09 commit aec82e9
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 203 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ Add the AAA `TokeAuthFilter` filter to your REST resource (RESTconf example):

<servlet>
<servlet-name>JAXRSRestconf</servlet-name>
<servlet-class>org.glassfish.jersey.core.spi.container.servlet.ServletContainer</servlet-class>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.opendaylight.controller.sal.rest.impl.RestconfApplication</param-value>
</init-param>
<!-- Token Auth Filter -->
<init-param>
<param-name>org.glassfish.jersey.core.spi.container.ContainerRequestFilters</param-name>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>
org.opendaylight.aaa.sts.TokenAuthFilter
</param-value>
Expand Down
6 changes: 1 addition & 5 deletions aaa-authn-basic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.List;
import java.util.Map;

import org.glassfish.jersey.internal.util.Base64;
import org.opendaylight.aaa.AuthenticationBuilder;
import org.opendaylight.aaa.PasswordCredentialBuilder;
import org.opendaylight.aaa.api.Authentication;
Expand All @@ -23,6 +22,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.core.util.Base64;

/**
* An HTTP Basic authenticator. Note that this is provided as a Hydrogen
* backward compatible authenticator, but usage of this authenticator or
Expand Down Expand Up @@ -76,8 +77,8 @@ private static String extractAuthHeader(final Map<String, List<String>> headers)
}

private static String [] extractCredentialArray(final String authHeader) {
return new String(Base64.decode(authHeader
.substring(BASIC_PREFIX.length()).getBytes())).split(AUTH_SEP);
return new String(Base64.base64Decode(authHeader
.substring(BASIC_PREFIX.length()))).split(AUTH_SEP);
}

private static boolean verifyCredentialArray(final String [] creds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.List;
import java.util.Map;

import org.glassfish.jersey.internal.util.Base64;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.aaa.ClaimBuilder;
Expand All @@ -28,6 +27,8 @@
import org.opendaylight.aaa.api.Claim;
import org.opendaylight.aaa.api.CredentialAuth;

import com.sun.jersey.core.util.Base64;

public class HttpBasicAuthTest {
private static final String USERNAME = "admin";
private static final String PASSWORD = "admin";
Expand Down
10 changes: 6 additions & 4 deletions aaa-authn-federation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</parent>

<artifactId>aaa-authn-federation</artifactId>
<version>0.3.0-SNAPSHOT</version>
<packaging>bundle</packaging>

<dependencies>
Expand All @@ -26,13 +27,13 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -62,8 +63,8 @@
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -96,6 +97,7 @@
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*,com.sun.jersey.spi.container.servlet</Import-Package>
<Web-ContextPath>/oauth2/federation</Web-ContextPath>
<Web-Connectors>federationConn</Web-Connectors>
<Bundle-Activator>org.opendaylight.aaa.federation.Activator</Bundle-Activator>
Expand Down
8 changes: 4 additions & 4 deletions aaa-authn-keystone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -73,8 +73,8 @@
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
8 changes: 4 additions & 4 deletions aaa-authn-sssd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -55,8 +55,8 @@
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
9 changes: 5 additions & 4 deletions aaa-authn-sts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -63,8 +63,8 @@
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -97,6 +97,7 @@
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*,com.sun.jersey.spi.container.servlet</Import-Package>
<Web-ContextPath>/oauth2</Web-ContextPath>
<Bundle-Activator>org.opendaylight.aaa.sts.Activator</Bundle-Activator>
<manifestLocation>${project.basedir}/META-INF</manifestLocation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@

package org.opendaylight.aaa.sts;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand All @@ -29,11 +26,14 @@
import org.opendaylight.aaa.api.AuthenticationException;
import org.opendaylight.aaa.api.TokenAuth;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

/**
* A token-based authentication filter for resource providers.
*
* @author liemmn
* @see javax.ws.rs.ContainerRequestFilter
*
*/
public class TokenAuthFilter implements ContainerRequestFilter {

Expand All @@ -44,6 +44,55 @@ public class TokenAuthFilter implements ContainerRequestFilter {
@Context
private HttpServletRequest httpRequest;

@Override
public ContainerRequest filter(ContainerRequest request) {

// Do the CORS check first
if(checkCORSOptionRequest(request)) {
return request;
}

// Are we up yet?
if (ServiceLocator.getInstance().getAuthenticationService() == null) {
throw new WebApplicationException(
Response.status(Status.SERVICE_UNAVAILABLE)
.type(MediaType.APPLICATION_JSON)
.entity("{\"error\":\"Authentication service unavailable\"}")
.build());
}

// Are we doing authentication or not?
if (ServiceLocator.getInstance().getAuthenticationService().isAuthEnabled()) {
Map<String, List<String>> headers = request.getRequestHeaders();

// Go through and invoke other TokenAuth first...
List<TokenAuth> tokenAuthCollection =
ServiceLocator.getInstance().getTokenAuthCollection();
for (TokenAuth ta : tokenAuthCollection) {
try {
Authentication auth = ta.validate(headers);
if (auth != null) {
ServiceLocator.getInstance().getAuthenticationService().set(auth);
return request;
}
} catch (AuthenticationException ae) {
throw unauthorized();
}
}

// OK, last chance to validate token...
try {
OAuthAccessResourceRequest or = new OAuthAccessResourceRequest(
httpRequest, ParameterStyle.HEADER);
validate(or.getAccessToken());
} catch (OAuthSystemException | OAuthProblemException e) {
throw unauthorized();
}
}

return request;
}

/**
* CORS access control : when browser sends cross-origin request, it first sends the OPTIONS method
* with a list of access control request headers, which has a list of custom headers and access control method
Expand All @@ -52,10 +101,16 @@ public class TokenAuthFilter implements ContainerRequestFilter {
* We should not do any authorization against such request.
* for more details : https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
*/
private boolean checkCORSOptionRequest(ContainerRequestContext request) {

private boolean checkCORSOptionRequest(ContainerRequest request) {
if(OPTIONS.equals(request.getMethod())) {
String headerString = request.getHeaderString(ACCESS_CONTROL_REQUEST_HEADERS);
return headerString.toLowerCase().contains(AUTHORIZATION);
List<String> headerList = request.getRequestHeader(ACCESS_CONTROL_REQUEST_HEADERS);
if(headerList != null && !headerList.isEmpty()) {
String header = headerList.get(0);
if (header != null && header.toLowerCase().contains(AUTHORIZATION)) {
return true;
}
}
}
return false;
}
Expand Down Expand Up @@ -90,48 +145,4 @@ public UnauthorizedException() {
super(response);
}
}

@Override
public void filter(ContainerRequestContext request) throws IOException {
// Do the CORS check first
if(checkCORSOptionRequest(request)) {
return;
}
// Are we up yet?
if (ServiceLocator.getInstance().getAuthenticationService() == null) {
throw new WebApplicationException(
Response.status(Status.SERVICE_UNAVAILABLE)
.type(MediaType.APPLICATION_JSON)
.entity("{\"error\":\"Authentication service unavailable\"}")
.build());
}

// Are we doing authentication or not?
if (ServiceLocator.getInstance().getAuthenticationService().isAuthEnabled()) {
Map<String, List<String>> headers = request.getHeaders();
// Go through and invoke other TokenAuth first...
for (TokenAuth ta : ServiceLocator.getInstance().getTokenAuthCollection()) {
try {
Authentication auth = ta.validate(headers);
if (auth != null) {
ServiceLocator.getInstance().getAuthenticationService().set(auth);
return;
}
} catch (AuthenticationException ae) {
request.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
}
}

// OK, last chance to validate token...
try {
OAuthAccessResourceRequest or = new OAuthAccessResourceRequest(
httpRequest, ParameterStyle.HEADER);
validate(or.getAccessToken());
return;
} catch (OAuthSystemException | OAuthProblemException e) {
request.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
}
request.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
}
}
}
Loading

0 comments on commit aec82e9

Please sign in to comment.