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

pac4j: fix incompatible dependencies + authorization regression #15753

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 extensions-core/druid-pac4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
<properties>
<pac4j.version>4.5.7</pac4j.version>

<!-- Following must be updated along with any updates to pac4j version -->
<!-- Following must be updated along with any updates to pac4j version. One can find the compatible version of nimbus libraries in org.pac4j:pac4j-oidc dependencies-->
<nimbus.lang.tag.version>1.7</nimbus.lang.tag.version>
<nimbus.jose.jwt.version>7.9</nimbus.jose.jwt.version>
<oauth2.oidc.sdk.version>6.5</oauth2.oidc.sdk.version>
<nimbus.jose.jwt.version>8.22.1</nimbus.jose.jwt.version>
<oauth2.oidc.sdk.version>8.22</oauth2.oidc.sdk.version>
Pankaj260100 marked this conversation as resolved.
Show resolved Hide resolved
</properties>

<dependencies>
Expand Down Expand Up @@ -145,6 +145,11 @@
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import org.pac4j.core.engine.DefaultCallbackLogic;
import org.pac4j.core.engine.DefaultSecurityLogic;
import org.pac4j.core.engine.SecurityLogic;
import org.pac4j.core.exception.http.HttpAction;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.http.adapter.JEEHttpActionAdapter;
import org.pac4j.core.profile.UserProfile;

import javax.servlet.Filter;
Expand All @@ -48,11 +47,9 @@ public class Pac4jFilter implements Filter
{
private static final Logger LOGGER = new Logger(Pac4jFilter.class);

private static final HttpActionAdapter<String, JEEContext> NOOP_HTTP_ACTION_ADAPTER = (HttpAction code, JEEContext ctx) -> null;

private final Config pac4jConfig;
private final SecurityLogic<String, JEEContext> securityLogic;
private final CallbackLogic<String, JEEContext> callbackLogic;
private final SecurityLogic<Object, JEEContext> securityLogic;
private final CallbackLogic<Object, JEEContext> callbackLogic;
private final SessionStore<JEEContext> sessionStore;

private final String name;
Expand Down Expand Up @@ -95,11 +92,11 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
callbackLogic.perform(
context,
pac4jConfig,
NOOP_HTTP_ACTION_ADAPTER,
JEEHttpActionAdapter.INSTANCE,
"/",
true, false, false, null);
} else {
String uid = securityLogic.perform(
Object uid = securityLogic.perform(
context,
pac4jConfig,
(JEEContext ctx, Collection<UserProfile> profiles, Object... parameters) -> {
Expand All @@ -110,11 +107,13 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
return profiles.iterator().next().getId();
}
},
NOOP_HTTP_ACTION_ADAPTER,
null, null, null, null);
Copy link
Contributor Author

@Pankaj260100 Pankaj260100 Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the Authorizer from null to "none". In the older version, if it is null, it simply returns authenticated and authorized -> grant access. But in the 4.5.7 pac4j version, it uses CsrfAuthorizer as default, And because of this, I was getting 403 in API calls. So, I have set it to "none".


JEEHttpActionAdapter.INSTANCE,
null, "none", null, null);
Pankaj260100 marked this conversation as resolved.
Show resolved Hide resolved
// Changed the Authorizer from null to "none".
// In the older version, if it is null, it simply grant access and returns authorized.
// But in the newer pac4j version, it uses CsrfAuthorizer as default, And because of this, It was returning 403 in API calls.
if (uid != null) {
AuthenticationResult authenticationResult = new AuthenticationResult(uid, authorizerName, name, null);
AuthenticationResult authenticationResult = new AuthenticationResult(uid.toString(), authorizerName, name, null);
servletRequest.setAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT, authenticationResult);
filterChain.doFilter(servletRequest, servletResponse);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.druid.security.pac4j;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.pac4j.core.context.JEEContext;
import org.pac4j.core.exception.http.ForbiddenAction;
import org.pac4j.core.exception.http.FoundAction;
import org.pac4j.core.exception.http.HttpAction;
import org.pac4j.core.exception.http.WithLocationAction;
import org.pac4j.core.http.adapter.JEEHttpActionAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.mockito.ArgumentMatchers.any;

@RunWith(MockitoJUnitRunner.class)
public class Pac4jFilterTest
{

@Mock
private HttpServletRequest request;
@Mock
private HttpServletResponse response;
private JEEContext context;

@Before
public void setUp()
{
context = new JEEContext(request, response);
}

@Test
public void testActionAdapterForRedirection()
{
HttpAction httpAction = new FoundAction("testUrl");
Mockito.doReturn(httpAction.getCode()).when(response).getStatus();
Mockito.doReturn(((WithLocationAction) httpAction).getLocation()).when(response).getHeader(any());
JEEHttpActionAdapter.INSTANCE.adapt(httpAction, context);
Assert.assertEquals(response.getStatus(), 302);
Assert.assertEquals(response.getHeader("Location"), "testUrl");
}

@Test
public void testActionAdapterForForbidden()
{
HttpAction httpAction = ForbiddenAction.INSTANCE;
Mockito.doReturn(httpAction.getCode()).when(response).getStatus();
JEEHttpActionAdapter.INSTANCE.adapt(httpAction, context);
Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_FORBIDDEN);
}

}
14 changes: 12 additions & 2 deletions licenses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -809,17 +809,27 @@ name: com.nimbusds nimbus-jose-jwt
license_category: binary
module: extensions/druid-pac4j
license_name: Apache License version 2.0
version: 7.9
version: 8.22.1
libraries:
- com.nimbusds: nimbus-jose-jwt

---

name: com.nimbusds content-type
license_category: binary
module: extensions/druid-pac4j
license_name: Apache License version 2.0
version: 2.1
libraries:
- com.nimbusds: content-type

---

name: com.nimbusds oauth2-oidc-sdk
license_category: binary
module: extensions/druid-pac4j
license_name: Apache License version 2.0
version: 6.5
version: 8.22
libraries:
- com.nimbusds: oauth2-oidc-sdk

Expand Down
Loading