Skip to content

Commit

Permalink
Fix pac4j#73
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégoire Neuville committed Feb 26, 2020
1 parent 2fb9efd commit 8244518
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ See also [dropwizard-pac4j](https://github.com/pac4j/dropwizard-pac4j) for even
You need to add a dependency on:

- jax-rs-pac4j
1. for Jersey (>=2.26) : the `jersey-pac4j` library (<em>groupId</em>: **org.pac4j**, *version*: **3.0.0**)
1. for Jersey (>=2.29) : the `jersey-pac4j` library (<em>groupId</em>: **org.pac4j**, *version*: **3.0.2**)
1. for Jersey (>=2.26 && <2.29) : the `jersey228-pac4j` library (<em>groupId</em>: **org.pac4j**, *version*: **3.0.2**)
1. for Jersey (<2.26) : the `jersey225-pac4j` library (<em>groupId</em>: **org.pac4j**, *version*: **3.0.0**)
2. for Resteasy : the `resteasy-pac4j` library (<em>groupId</em>: **org.pac4j**, *version*: **3.0.0**) and `resteasy-cdi` for CDI support
- the appropriate `pac4j` [submodules](http://www.pac4j.org/docs/clients.html) (<em>groupId</em>: **org.pac4j**, *version*: **3.0.0**): `pac4j-oauth` for OAuth support (Facebook, Twitter...), `pac4j-cas` for CAS support, `pac4j-ldap` for LDAP authentication, etc.
Expand Down
6 changes: 3 additions & 3 deletions jersey/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<artifactId>parent</artifactId>
<version>3.0.2-SNAPSHOT</version>
</parent>
<groupId>org.pac4j</groupId>
<groupId>org.pac4j</groupId>
<artifactId>jersey-pac4j</artifactId>

<properties>
<jersey.version>2.26</jersey.version>
<jersey.version>2.30.1</jersey.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -47,7 +47,7 @@
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<!-- same version as in org.glassfish.jersey:project -->
<version>2.4.0</version>
<version>2.4.4</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected Pac4JProfileValueFactoryProvider(
ProfileFactoryBuilder profile,
Provider<MultivaluedParameterExtractorProvider> mpep
) {
super(mpep, Parameter.Source.UNKNOWN);
super(mpep, org.glassfish.jersey.model.Parameter.Source.UNKNOWN);
this.manager = manager;
this.optProfile = opt;
this.profile = profile;
Expand Down Expand Up @@ -261,4 +261,4 @@ public ProfileManagerFactory get() {
return new ProfileManagerValueFactory(providers);
}
}
}
}
87 changes: 87 additions & 0 deletions jersey228/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>parent</artifactId>
<groupId>org.pac4j.jax-rs</groupId>
<version>3.0.2-SNAPSHOT</version>
</parent>
<groupId>org.pac4j</groupId>
<artifactId>jersey228-pac4j</artifactId>

<properties>
<jersey.version>2.28</jersey.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
<scope>provided</scope>
</dependency>
<!-- optional dependencies -->
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
<!-- same version as in org.glassfish.jersey:project -->
<version>2.4.4</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>testing</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<exclusions>
<exclusion><!-- Conflicts with jersey-hk2 -->
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<scope>test</scope>
<exclusions>
<exclusion><!-- Conflicts with jersey-test-framework-provider-grizzly2 -->
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.pac4j.jax.rs.grizzly.features;

import org.glassfish.grizzly.http.server.Request;
import org.pac4j.jax.rs.features.JaxRsContextFactoryProvider;
import org.pac4j.jax.rs.grizzly.pac4j.GrizzlyJaxRsContext;

import javax.inject.Inject;
import javax.inject.Provider;

/**
*
* Extends {@link JaxRsContextFactoryProvider} to support the Grizzly container (without the need for servlet support)
* and its session manager (i.e., pac4j indirect clients will work, contrary than with
* {@link JaxRsContextFactoryProvider}).
*
* @see JaxRsContextFactoryProvider
* @author Victor Noel - Linagora
* @since 1.0.0
*
*/
public class GrizzlyJaxRsContextFactoryProvider extends JaxRsContextFactoryProvider {

@Inject
protected Provider<Request> requestProvider;

@Override
public JaxRsContextFactory getContext(Class<?> type) {
Request request = requestProvider.get();
assert request != null;
return context -> new GrizzlyJaxRsContext(getProviders(), context, getConfig().getSessionStore(), request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.pac4j.jax.rs.grizzly.pac4j;

import org.glassfish.grizzly.http.server.Request;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.jax.rs.pac4j.JaxRsContext;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.ext.Providers;

/**
*
* Notice: there is often chances that the JAX-RS implementation will read the input stream of the request when it
* arrives, and after that, it becomes impossible for Grizzly to read it. In particular this means that
* {@link Request#getParameter(String)} won't be able to return FORM parameters. This is why we don't override
* {@link JaxRsContext#getRequestParameter(String)} to use the Grizzly implementation.
*
* @author Victor Noel - Linagora
* @since 1.0.0
*
*/
public class GrizzlyJaxRsContext extends JaxRsContext {

private final Request request;

public GrizzlyJaxRsContext(Providers providers, ContainerRequestContext requestContext,
SessionStore sessionStore, Request request) {
super(providers, requestContext, sessionStore != null ? sessionStore : new GrizzlySessionStore());
this.request = request;
}

public Request getRequest() {
return request;
}

@Override
public String getRemoteAddr() {
return request.getRemoteAddr();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.pac4j.jax.rs.grizzly.pac4j;

import org.glassfish.grizzly.http.server.Session;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.jax.rs.pac4j.JaxRsContext;

import java.util.HashMap;
import java.util.Map;

/**
*
* @author Victor Noel - Linagora
* @since 1.0.0
*
*/
public class GrizzlySessionStore implements SessionStore<JaxRsContext> {

public Session getSession(final JaxRsContext context) {
assert context instanceof GrizzlyJaxRsContext;
return ((GrizzlyJaxRsContext) context).getRequest().getSession();
}

@Override
public String getOrCreateSessionId(JaxRsContext context) {
return getSession(context).getIdInternal();
}

@Override
public Object get(JaxRsContext context, String key) {
return getSession(context).getAttribute(key);
}

@Override
public void set(JaxRsContext context, String key, Object value) {
if (value == null) {
getSession(context).removeAttribute(key);
} else {
getSession(context).setAttribute(key, value);
}
}

@Override
public boolean destroySession(JaxRsContext context) {
final Session session = getSession(context);

session.setValid(false);

return true;
}

@Override
public Object getTrackableSession(JaxRsContext context) {
return getSession(context);
}

@Override
public boolean renewSession(JaxRsContext context) {
final Session session = getSession(context);
final Map<String, Object> attributes = new HashMap<>();
attributes.putAll(session.attributes());

session.setValid(false);

// let's recreate the session from zero
// (Grizzly reuse the same object, but that could change in the future...)
final Session newSession = getSession(context);
attributes.forEach(newSession::setAttribute);

return true;
}



@Override
public SessionStore<JaxRsContext> buildFromTrackableSession(JaxRsContext context, Object trackableSession) {
return new GrizzlySessionStore() {
@Override
public Session getSession(JaxRsContext context) {
return (Session) trackableSession;
}
};
}
}
Loading

0 comments on commit 8244518

Please sign in to comment.