Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pac4j to 5.2.0 #257

Merged
merged 8 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ under the License.
</scm>

<properties>
<revision>3</revision>
<revision>4</revision>
<changelist>999999-SNAPSHOT</changelist>
<jenkins.version>2.361</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
Expand Down Expand Up @@ -98,8 +98,7 @@ under the License.
<dependency>
<groupId>org.pac4j</groupId>
<artifactId>pac4j-saml</artifactId>
<!-- versions 4.x.x require JDK 11 -->
<version>3.9.0</version>
<version>5.2.0</version>
Riliane marked this conversation as resolved.
Show resolved Hide resolved
<optional>false</optional>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -196,6 +195,12 @@ under the License.
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.pac4j</groupId>
<artifactId>pac4j-jee</artifactId>
<version>5.2.0</version>
<optional>false</optional>
</dependency>
Riliane marked this conversation as resolved.
Show resolved Hide resolved
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/jenkinsci/plugins/saml/OpenSAMLWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
package org.jenkinsci.plugins.saml;

import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.opensaml.core.config.InitializationException;
import org.opensaml.core.config.InitializationService;
import org.pac4j.core.context.J2EContext;
import org.pac4j.core.context.JEEContext;
Riliane marked this conversation as resolved.
Show resolved Hide resolved
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.TechnicalException;
import org.pac4j.core.http.callback.NoParameterCallbackUrlResolver;
import org.pac4j.saml.client.SAML2Client;
import org.pac4j.saml.config.SAML2Configuration;
Expand Down Expand Up @@ -86,7 +88,7 @@ public T get() {
* @return J2E Context from the current HTTP request and response.
*/
protected WebContext createWebContext() {
return new J2EContext(request, response);
return new JEEContext(request, response);
}

/**
Expand Down Expand Up @@ -140,8 +142,7 @@ protected SAML2Client createSAML2Client() {
// reference) is set, include it in the request to the IdP, and request
// that the IdP uses exact matching for authentication types
if (samlPluginConfig.getAuthnContextClassRef() != null) {
//noinspection deprecation
config.setAuthnContextClassRef(samlPluginConfig.getAuthnContextClassRef());
config.setAuthnContextClassRefs(Arrays.asList(samlPluginConfig.getAuthnContextClassRef()));
config.setComparisonType("exact");
}

Expand All @@ -160,7 +161,7 @@ protected SAML2Client createSAML2Client() {
if (LOG.isLoggable(FINE)) {
try {
LOG.fine(saml2Client.getServiceProviderMetadataResolver().getMetadata());
} catch (IOException e) {
} catch (TechnicalException e) {
LOG.fine("Is not possible to show the metadata : " + e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.HttpAction;
import org.pac4j.core.context.session.JEESessionStore;
Riliane marked this conversation as resolved.
Show resolved Hide resolved
import org.pac4j.core.exception.http.HttpAction;
import org.pac4j.saml.client.SAML2Client;
import org.pac4j.saml.credentials.SAML2Credentials;
import org.pac4j.saml.exceptions.SAMLException;
Expand Down Expand Up @@ -52,8 +53,8 @@ protected SAML2Profile process() {
try {
SAML2Client client = createSAML2Client();
WebContext context = createWebContext();
credentials = client.getCredentials(context);
saml2Profile = client.getUserProfile(credentials, context);
credentials = (SAML2Credentials) client.getCredentials(context, JEESessionStore.INSTANCE).orElse(null);
saml2Profile = (SAML2Profile) client.getUserProfile(credentials, context, JEESessionStore.INSTANCE).orElse(null);
client.destroy();
} catch (HttpAction|SAMLException e) {
//if the SAMLResponse is not valid we send the user again to the IdP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@

import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.pac4j.core.context.session.JEESessionStore;
Riliane marked this conversation as resolved.
Show resolved Hide resolved
import org.pac4j.core.exception.http.HttpAction;
import org.pac4j.core.exception.http.RedirectionAction;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.HttpAction;
import org.pac4j.core.redirect.RedirectAction;
import org.pac4j.saml.client.SAML2Client;

/**
* Process the current configuration and request to prepare a Redirection to the IdP.
*/
public class SamlRedirectActionWrapper extends OpenSAMLWrapper<RedirectAction> {
public class SamlRedirectActionWrapper extends OpenSAMLWrapper<RedirectionAction> {

public SamlRedirectActionWrapper(SamlPluginConfig samlPluginConfig, StaplerRequest request, StaplerResponse response) {
this.request = request;
Expand All @@ -41,11 +42,11 @@ public SamlRedirectActionWrapper(SamlPluginConfig samlPluginConfig, StaplerReque
*/
@SuppressWarnings("unused")
@Override
protected RedirectAction process() throws IllegalStateException {
protected RedirectionAction process() throws IllegalStateException {
try {
SAML2Client client = createSAML2Client();
WebContext context = createWebContext();
RedirectAction redirection = client.getRedirectAction(context);
RedirectionAction redirection = client.getRedirectionAction(context, JEESessionStore.INSTANCE).orElse(null);
client.destroy();
return redirection;
} catch (HttpAction e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.pac4j.core.exception.TechnicalException;
import org.pac4j.saml.client.SAML2Client;

/**
Expand All @@ -46,7 +47,7 @@ protected HttpResponse process() throws IllegalStateException {
try {
metadata = client.getServiceProviderMetadataResolver().getMetadata();
client.destroy();
} catch (IOException e) {
} catch (TechnicalException e) {
throw new IllegalStateException(e);
}
return HttpResponses.text(metadata);
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/org/jenkinsci/plugins/saml/SamlSecurityRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.pac4j.core.redirect.RedirectAction;
import org.pac4j.core.redirect.RedirectAction.RedirectType;
import org.pac4j.core.exception.http.OkAction;
Riliane marked this conversation as resolved.
Show resolved Hide resolved
import org.pac4j.core.exception.http.RedirectionAction;
import org.pac4j.core.exception.http.SeeOtherAction;
import org.springframework.dao.DataAccessException;
import org.pac4j.saml.profile.SAML2Profile;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.BadCredentialsException;
Expand Down Expand Up @@ -255,15 +257,15 @@ public HttpResponse doCommenceLogin(final StaplerRequest request, final StaplerR
String redirectOnFinish = calculateSafeRedirect(from, referer);
request.getSession().setAttribute(REFERER_ATTRIBUTE, redirectOnFinish);

RedirectAction action = new SamlRedirectActionWrapper(getSamlPluginConfig(), request, response).get();
if (action.getType() == RedirectType.REDIRECT) {
LOG.fine("REDIRECT : " + action.getLocation());
return HttpResponses.redirectTo(action.getLocation());
} else if (action.getType() == RedirectType.SUCCESS) {
LOG.fine("SUCCESS : " + action.getContent());
return HttpResponses.literalHtml(action.getContent());
RedirectionAction action = new SamlRedirectActionWrapper(getSamlPluginConfig(), request, response).get();
if (action instanceof SeeOtherAction) {
kuisathaverat marked this conversation as resolved.
Show resolved Hide resolved
Riliane marked this conversation as resolved.
Show resolved Hide resolved
LOG.fine("REDIRECT : " + ((SeeOtherAction)action).getLocation());
return HttpResponses.redirectTo(((SeeOtherAction)action).getLocation());
Riliane marked this conversation as resolved.
Show resolved Hide resolved
} else if (action instanceof OkAction) {
LOG.fine("SUCCESS : " + ((OkAction) action).getContent());
return HttpResponses.literalHtml(((OkAction) action).getContent());
} else {
throw new IllegalStateException("Received unexpected response type " + action.getType());
throw new IllegalStateException("Received unexpected response type " + action.getCode());
}
}

Expand Down