From 95640562b973842121004201fc38d6a6b61f0cef Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Wed, 18 Nov 2026 06:29:47 +0100 Subject: [PATCH 1/3] JSPWIKI-94 Proof of concept: log in with OpenID Connect through Keycloak A new jspwiki-oidc module uses the pac4j filters to send the user to the identity provider and a filter that presents the logged in user to the wiki as a container login. The wiki recognizes the user as authenticated without changes in its authentication code. Work in progress, not ready for review. --- jspwiki-oidc/pom.xml | 79 +++++++++++++++++++ .../apache/wiki/oidc/OidcConfigFactory.java | 74 +++++++++++++++++ .../apache/wiki/oidc/OidcIdentityFilter.java | 64 +++++++++++++++ .../apache/wiki/oidc/OidcRequestWrapper.java | 59 ++++++++++++++ jspwiki-war/pom.xml | 6 ++ jspwiki-war/src/main/webapp/WEB-INF/web.xml | 44 +++++++++++ jspwiki-war/src/main/webapp/oidc/login.jsp | 22 ++++++ pom.xml | 1 + 8 files changed, 349 insertions(+) create mode 100644 jspwiki-oidc/pom.xml create mode 100644 jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcConfigFactory.java create mode 100644 jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcIdentityFilter.java create mode 100644 jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcRequestWrapper.java create mode 100644 jspwiki-war/src/main/webapp/oidc/login.jsp diff --git a/jspwiki-oidc/pom.xml b/jspwiki-oidc/pom.xml new file mode 100644 index 0000000000..ca044170b8 --- /dev/null +++ b/jspwiki-oidc/pom.xml @@ -0,0 +1,79 @@ + + + + + + org.apache.jspwiki + jspwiki-builder + 3.0.1-SNAPSHOT + + + 4.0.0 + jspwiki-oidc + Apache JSPWiki OpenID Connect login + + + 6.5.5 + 8.0.3 + + + + + org.pac4j + jakartaee-pac4j + ${jakartaee-pac4j.version} + + + + org.pac4j + pac4j-oidc + ${pac4j.version} + + + + org.slf4j + slf4j-api + + + + jakarta.servlet + jakarta.servlet-api + provided + + + + org.junit.jupiter + junit-jupiter-api + test + + + + org.junit.jupiter + junit-jupiter-engine + test + + + + org.mockito + mockito-core + test + + + diff --git a/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcConfigFactory.java b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcConfigFactory.java new file mode 100644 index 0000000000..2ae58b0330 --- /dev/null +++ b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcConfigFactory.java @@ -0,0 +1,74 @@ +/* + 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.wiki.oidc; + +import java.io.InputStream; +import java.util.Properties; + +import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod; +import org.pac4j.core.client.Clients; +import org.pac4j.core.config.Config; +import org.pac4j.core.config.ConfigFactory; +import org.pac4j.core.http.url.DefaultUrlResolver; +import org.pac4j.oidc.authorization.generator.KeycloakRolesAuthorizationGenerator; +import org.pac4j.oidc.client.KeycloakOidcClient; +import org.pac4j.oidc.config.KeycloakOidcConfiguration; + +/** + * Builds the pac4j configuration from {@code jspwiki-oidc.properties}, which has to be in the classpath of the web application. + */ +public class OidcConfigFactory implements ConfigFactory { + + static final String CONFIG_FILE = "/jspwiki-oidc.properties"; + + /** {@inheritDoc} */ + @Override + public Config build( final Object... parameters ) { + final Properties props = new Properties(); + try( InputStream in = OidcConfigFactory.class.getResourceAsStream( CONFIG_FILE ) ) { + if( in == null ) { + throw new IllegalStateException( CONFIG_FILE + " not found in the classpath" ); + } + props.load( in ); + } catch( final java.io.IOException e ) { + throw new IllegalStateException( "Could not read " + CONFIG_FILE, e ); + } + final KeycloakOidcConfiguration oidc = new KeycloakOidcConfiguration(); + oidc.setClientId( required( props, "oidc.clientId" ) ); + oidc.setSecret( required( props, "oidc.secret" ) ); + oidc.setScope( props.getProperty( "oidc.scope", "openid email profile" ) ); + oidc.setClientAuthenticationMethod( ClientAuthenticationMethod.CLIENT_SECRET_BASIC ); + oidc.setRealm( required( props, "oidc.realm" ) ); + oidc.setBaseUri( required( props, "oidc.baseUri" ) ); + oidc.setDiscoveryURI( required( props, "oidc.discoveryUri" ) ); + final KeycloakOidcClient client = new KeycloakOidcClient( oidc ); + client.addAuthorizationGenerator( new KeycloakRolesAuthorizationGenerator() ); + client.setUrlResolver( new DefaultUrlResolver( true ) ); + return new Config( new Clients( required( props, "oidc.callbackUrl" ), client ) ); + } + + private static String required( final Properties props, final String key ) { + final String value = props.getProperty( key ); + if( value == null || value.isBlank() ) { + throw new IllegalStateException( CONFIG_FILE + " does not define " + key ); + } + return value; + } + +} diff --git a/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcIdentityFilter.java b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcIdentityFilter.java new file mode 100644 index 0000000000..f40dde999a --- /dev/null +++ b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcIdentityFilter.java @@ -0,0 +1,64 @@ +/* + 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.wiki.oidc; + +import java.io.IOException; +import java.util.Optional; +import java.util.Set; + +import org.pac4j.core.profile.ProfileManager; +import org.pac4j.core.profile.UserProfile; +import org.pac4j.jee.context.JEEContext; +import org.pac4j.jee.context.session.JEESessionStore; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +/** + * Hands the user logged in with OpenID Connect to the wiki. It has to run before {@code WikiServletFilter}, which takes + * the user from {@code getRemoteUser()} and {@code getUserPrincipal()} of the request, as it does with a container login. + */ +public class OidcIdentityFilter implements Filter { + + /** {@inheritDoc} */ + @Override + public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain ) throws IOException, ServletException { + final HttpServletRequest req = ( HttpServletRequest )request; + if( req.getSession( false ) == null ) { + chain.doFilter( request, response ); + return; + } + final JEEContext context = new JEEContext( req, ( HttpServletResponse )response ); + final ProfileManager profiles = new ProfileManager( context, new JEESessionStore() ); + final Optional< UserProfile > profile = profiles.getProfile(); + if( profile.isPresent() ) { + final UserProfile user = profile.get(); + final String name = user.getUsername() != null ? user.getUsername() : user.getId(); + chain.doFilter( new OidcRequestWrapper( req, name, Set.copyOf( user.getRoles() ) ), response ); + } else { + chain.doFilter( request, response ); + } + } + +} diff --git a/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcRequestWrapper.java b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcRequestWrapper.java new file mode 100644 index 0000000000..527ba11417 --- /dev/null +++ b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcRequestWrapper.java @@ -0,0 +1,59 @@ +/* + 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.wiki.oidc; + +import java.security.Principal; +import java.util.Set; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletRequestWrapper; + +/** + * Presents the identity of an OpenID Connect login to the wiki as if the container had authenticated the user. + */ +class OidcRequestWrapper extends HttpServletRequestWrapper { + + private final Principal principal; + private final Set< String > roles; + + OidcRequestWrapper( final HttpServletRequest request, final String name, final Set< String > roles ) { + super( request ); + this.principal = () -> name; + this.roles = roles; + } + + /** {@inheritDoc} */ + @Override + public String getRemoteUser() { + return principal.getName(); + } + + /** {@inheritDoc} */ + @Override + public Principal getUserPrincipal() { + return principal; + } + + /** {@inheritDoc} */ + @Override + public boolean isUserInRole( final String role ) { + return roles.contains( role ) || super.isUserInRole( role ); + } + +} diff --git a/jspwiki-war/pom.xml b/jspwiki-war/pom.xml index 2bfaa2ccea..bca7ec5d80 100644 --- a/jspwiki-war/pom.xml +++ b/jspwiki-war/pom.xml @@ -43,6 +43,12 @@ ${project.version} + + ${project.groupId} + jspwiki-oidc + ${project.version} + + ${project.groupId} jspwiki-markdown diff --git a/jspwiki-war/src/main/webapp/WEB-INF/web.xml b/jspwiki-war/src/main/webapp/WEB-INF/web.xml index afce534d0d..3ca88ef7b2 100644 --- a/jspwiki-war/src/main/webapp/WEB-INF/web.xml +++ b/jspwiki-war/src/main/webapp/WEB-INF/web.xml @@ -74,6 +74,50 @@ org.apache.wiki.ui.WikiJSPFilter + + + OidcCallbackFilter + org.pac4j.jee.filter.CallbackFilter + + defaultUrl + /oidc/login.jsp + + + renewSession + true + + + + OidcCallbackFilter + /oidc/callback + + + + OidcSecurityFilter + org.pac4j.jee.filter.SecurityFilter + + configFactory + org.apache.wiki.oidc.OidcConfigFactory + + + clients + KeycloakOidcClient + + + + OidcSecurityFilter + /oidc/login.jsp + + + + OidcIdentityFilter + org.apache.wiki.oidc.OidcIdentityFilter + + + OidcIdentityFilter + /* + + WikiServletFilter /attach/* diff --git a/jspwiki-war/src/main/webapp/oidc/login.jsp b/jspwiki-war/src/main/webapp/oidc/login.jsp new file mode 100644 index 0000000000..0fb97825a2 --- /dev/null +++ b/jspwiki-war/src/main/webapp/oidc/login.jsp @@ -0,0 +1,22 @@ +<%-- + 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. +--%> +<% + // The OpenID Connect filters have already logged the user in when the request gets here. + response.sendRedirect( request.getContextPath() + "/Wiki.jsp" ); +%> diff --git a/pom.xml b/pom.xml index e4f24c55df..997fec5399 100644 --- a/pom.xml +++ b/pom.xml @@ -146,6 +146,7 @@ jspwiki-http jspwiki-wysiwyg jspwiki-main + jspwiki-oidc jspwiki-markdown jspwiki-tika-searchprovider jspwiki-kendra-searchprovider From 072399da408a5f4ee430b14da039048d1465ee42 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Wed, 18 Nov 2026 06:39:12 +0100 Subject: [PATCH 2/3] JSPWIKI-94 Keep the OpenID Connect filters disabled by default and add a test The filters of the proof of concept are commented out in web.xml, as the container managed authentication section is, and the war does not include the module. Adds the ChangeLog entry and a test of the request wrapper. --- ChangeLog.md | 6 +++ .../java/org/apache/wiki/api/Release.java | 2 +- .../wiki/oidc/OidcRequestWrapperTest.java | 52 +++++++++++++++++++ jspwiki-war/pom.xml | 6 --- jspwiki-war/src/main/webapp/WEB-INF/web.xml | 8 ++- 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 jspwiki-oidc/src/test/java/org/apache/wiki/oidc/OidcRequestWrapperTest.java diff --git a/ChangeLog.md b/ChangeLog.md index 880da8f4df..f91f814c7d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -17,6 +17,12 @@ specific language governing permissions and limitations under the License. --> +**2026-11-18 Arturo Bernal (abernal AT apache DOT org)** + +* _3.0.1-git-10_ + +* Proof of concept for [JSPWIKI-94](https://issues.apache.org/jira/browse/JSPWIKI-94): the new module jspwiki-oidc logs users in with OpenID Connect, with the pac4j filters, and presents them to the wiki as users authenticated by the container. The filters are disabled in web.xml + **2026-11-14 Multiple authors** * _3.0.1-git-09 diff --git a/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java b/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java index 7128b9933e..a588475922 100644 --- a/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java +++ b/jspwiki-api/src/main/java/org/apache/wiki/api/Release.java @@ -69,7 +69,7 @@ public final class Release { *

* If the build identifier is empty, it is not added. */ - public static final String BUILD = "9"; + public static final String BUILD = "10"; /** * This is the generic version string you should use when printing out the version. It is of diff --git a/jspwiki-oidc/src/test/java/org/apache/wiki/oidc/OidcRequestWrapperTest.java b/jspwiki-oidc/src/test/java/org/apache/wiki/oidc/OidcRequestWrapperTest.java new file mode 100644 index 0000000000..76b2149107 --- /dev/null +++ b/jspwiki-oidc/src/test/java/org/apache/wiki/oidc/OidcRequestWrapperTest.java @@ -0,0 +1,52 @@ +/* + 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.wiki.oidc; + +import java.util.Set; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import jakarta.servlet.http.HttpServletRequest; + +class OidcRequestWrapperTest { + + private final HttpServletRequest request = Mockito.mock( HttpServletRequest.class ); + private final OidcRequestWrapper wrapper = new OidcRequestWrapper( request, "kcuser", Set.of( "Authenticated" ) ); + + @Test + void presentsTheLoggedInUserAsContainerUser() { + Assertions.assertEquals( "kcuser", wrapper.getRemoteUser() ); + Assertions.assertEquals( "kcuser", wrapper.getUserPrincipal().getName() ); + } + + @Test + void grantsTheRolesOfTheIdentityProvider() { + Assertions.assertTrue( wrapper.isUserInRole( "Authenticated" ) ); + Assertions.assertFalse( wrapper.isUserInRole( "Admin" ) ); + } + + @Test + void keepsTheRolesOfTheWrappedRequest() { + Mockito.when( request.isUserInRole( "Admin" ) ).thenReturn( true ); + Assertions.assertTrue( wrapper.isUserInRole( "Admin" ) ); + } + +} diff --git a/jspwiki-war/pom.xml b/jspwiki-war/pom.xml index bca7ec5d80..2bfaa2ccea 100644 --- a/jspwiki-war/pom.xml +++ b/jspwiki-war/pom.xml @@ -43,12 +43,6 @@ ${project.version} - - ${project.groupId} - jspwiki-oidc - ${project.version} - - ${project.groupId} jspwiki-markdown diff --git a/jspwiki-war/src/main/webapp/WEB-INF/web.xml b/jspwiki-war/src/main/webapp/WEB-INF/web.xml index 3ca88ef7b2..9d83b5a4f3 100644 --- a/jspwiki-war/src/main/webapp/WEB-INF/web.xml +++ b/jspwiki-war/src/main/webapp/WEB-INF/web.xml @@ -74,7 +74,12 @@ org.apache.wiki.ui.WikiJSPFilter - + + WikiServletFilter From 028aee1d52cb4e3555ecc8e85e53012b137a9ccd Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Wed, 18 Nov 2026 07:06:03 +0100 Subject: [PATCH 3/3] JSPWIKI-94 Add roles, logout and the wiki login links to the OpenID Connect module OidcAuthorizer declares that the container handles the login and takes the roles from the property jspwiki.oidc.roles, so Login.jsp and the roles of the identity provider work as with a container login. The logout filter ends the session of the identity provider and returns to the wiki. The war includes the module with the profile oidc. Adds the tests of the authorizer and a description of how to enable it. --- ChangeLog.md | 2 +- jspwiki-oidc/README.md | 54 ++++++++++++++++++ jspwiki-oidc/pom.xml | 7 +++ .../org/apache/wiki/oidc/OidcAuthorizer.java | 52 +++++++++++++++++ .../apache/wiki/oidc/OidcConfigFactory.java | 17 +++++- .../apache/wiki/oidc/OidcAuthorizerTest.java | 57 +++++++++++++++++++ .../src/test/resources/WEB-INF/web.xml | 24 ++++++++ jspwiki-war/pom.xml | 14 +++++ jspwiki-war/src/main/webapp/WEB-INF/web.xml | 38 +++++++++++-- jspwiki-war/src/main/webapp/oidc/login.jsp | 22 ------- 10 files changed, 258 insertions(+), 29 deletions(-) create mode 100644 jspwiki-oidc/README.md create mode 100644 jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcAuthorizer.java create mode 100644 jspwiki-oidc/src/test/java/org/apache/wiki/oidc/OidcAuthorizerTest.java create mode 100644 jspwiki-oidc/src/test/resources/WEB-INF/web.xml delete mode 100644 jspwiki-war/src/main/webapp/oidc/login.jsp diff --git a/ChangeLog.md b/ChangeLog.md index f91f814c7d..595a53b3ee 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -21,7 +21,7 @@ under the License. * _3.0.1-git-10_ -* Proof of concept for [JSPWIKI-94](https://issues.apache.org/jira/browse/JSPWIKI-94): the new module jspwiki-oidc logs users in with OpenID Connect, with the pac4j filters, and presents them to the wiki as users authenticated by the container. The filters are disabled in web.xml +* Fix for [JSPWIKI-94](https://issues.apache.org/jira/browse/JSPWIKI-94): the new module jspwiki-oidc logs users in with OpenID Connect, with the pac4j filters, and presents them to the wiki as users authenticated by the container, including their roles and the logout. It is not included in the war unless it is built with the profile oidc, and its filters are disabled in web.xml **2026-11-14 Multiple authors** diff --git a/jspwiki-oidc/README.md b/jspwiki-oidc/README.md new file mode 100644 index 0000000000..5b10f2bf37 --- /dev/null +++ b/jspwiki-oidc/README.md @@ -0,0 +1,54 @@ + + + +# jspwiki-oidc + +Logs users in with OpenID Connect, using the pac4j filters, and presents them to the wiki as users authenticated by the +container. It has been tried with Keycloak, see [JSPWIKI-94](https://issues.apache.org/jira/browse/JSPWIKI-94). + +## Enabling it + +1. Build the war with `mvn package -Poidc`, or add this module and its dependencies to `WEB-INF/lib`. +2. Create `WEB-INF/classes/jspwiki-oidc.properties`: + + ``` + oidc.clientId=jspwiki-client + oidc.secret=the client secret + oidc.scope=openid email profile + oidc.realm=jspwiki + oidc.baseUri=https://keycloak.example.org + oidc.discoveryUri=https://keycloak.example.org/realms/jspwiki/.well-known/openid-configuration + oidc.callbackUrl=/oidc/callback + ``` + +3. Add to `WEB-INF/classes/jspwiki-custom.properties`: + + ``` + jspwiki.authorizer = org.apache.wiki.oidc.OidcAuthorizer + ``` + + The roles the wiki takes from the identity provider are `Admin` and `Authenticated`. Change them with the property + `jspwiki.oidc.roles`, a comma separated list. +4. In `WEB-INF/web.xml`, remove the comment marks around the OpenID Connect filters. +5. In Keycloak, create a confidential client whose valid redirect URIs and post logout redirect URIs include the address of the wiki, + and the realm roles `Admin` and `Authenticated`, which are assigned to the users. + +The links to log in and out of the wiki then use the identity provider: `Login.jsp` sends the user to it, and `Logout.jsp` ends the +session of the wiki and of the identity provider. The wiki is not asked for passwords, and its own user profiles are not used. diff --git a/jspwiki-oidc/pom.xml b/jspwiki-oidc/pom.xml index ca044170b8..a49b56d242 100644 --- a/jspwiki-oidc/pom.xml +++ b/jspwiki-oidc/pom.xml @@ -47,6 +47,13 @@ ${pac4j.version} + + ${project.groupId} + jspwiki-main + ${project.version} + provided + + org.slf4j slf4j-api diff --git a/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcAuthorizer.java b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcAuthorizer.java new file mode 100644 index 0000000000..985fe9dca2 --- /dev/null +++ b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcAuthorizer.java @@ -0,0 +1,52 @@ +/* + 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.wiki.oidc; + +import java.util.Arrays; +import java.util.Properties; + +import org.apache.wiki.api.core.Engine; +import org.apache.wiki.auth.authorize.Role; +import org.apache.wiki.auth.authorize.WebContainerAuthorizer; + +/** + * Authorizer for wikis whose users log in with OpenID Connect. The login is done by filters and not by constraints of the web + * application descriptor, so this authorizer declares that the container handles the login, and takes the names of the roles + * from the property {@code jspwiki.oidc.roles}, a comma separated list that defaults to {@code Admin,Authenticated}. + */ +public class OidcAuthorizer extends WebContainerAuthorizer { + + /** Property with the comma separated names of the roles the identity provider may grant. */ + public static final String PROP_ROLES = "jspwiki.oidc.roles"; + + private static final String DEFAULT_ROLES = "Admin,Authenticated"; + + /** {@inheritDoc} */ + @Override + public void initialize( final Engine engine, final Properties props ) { + super.initialize( engine, props ); + m_containerAuthorized = true; + m_containerRoles = Arrays.stream( props.getProperty( PROP_ROLES, DEFAULT_ROLES ).split( "," ) ) + .map( String::trim ) + .filter( name -> !name.isEmpty() ) + .map( Role::new ) + .toArray( Role[]::new ); + } + +} diff --git a/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcConfigFactory.java b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcConfigFactory.java index 2ae58b0330..ff031472f7 100644 --- a/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcConfigFactory.java +++ b/jspwiki-oidc/src/main/java/org/apache/wiki/oidc/OidcConfigFactory.java @@ -24,8 +24,12 @@ Licensed to the Apache Software Foundation (ASF) under one import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod; import org.pac4j.core.client.Clients; import org.pac4j.core.config.Config; +import org.pac4j.core.client.Client; import org.pac4j.core.config.ConfigFactory; +import org.pac4j.core.context.CallContext; +import org.pac4j.core.engine.DefaultLogoutLogic; import org.pac4j.core.http.url.DefaultUrlResolver; +import org.pac4j.core.http.url.UrlResolver; import org.pac4j.oidc.authorization.generator.KeycloakRolesAuthorizationGenerator; import org.pac4j.oidc.client.KeycloakOidcClient; import org.pac4j.oidc.config.KeycloakOidcConfiguration; @@ -59,8 +63,17 @@ public Config build( final Object... parameters ) { oidc.setDiscoveryURI( required( props, "oidc.discoveryUri" ) ); final KeycloakOidcClient client = new KeycloakOidcClient( oidc ); client.addAuthorizationGenerator( new KeycloakRolesAuthorizationGenerator() ); - client.setUrlResolver( new DefaultUrlResolver( true ) ); - return new Config( new Clients( required( props, "oidc.callbackUrl" ), client ) ); + final UrlResolver urlResolver = new DefaultUrlResolver( true ); + client.setUrlResolver( urlResolver ); + final Config config = new Config( new Clients( required( props, "oidc.callbackUrl" ), client ) ); + // the identity provider needs an absolute URL to return to after the logout + config.setLogoutLogic( new DefaultLogoutLogic() { + @Override + protected String enhanceRedirectUrl( final CallContext ctx, final Config config, final Client client, final String redirectUrl ) { + return urlResolver.compute( redirectUrl, ctx.webContext() ); + } + } ); + return config; } private static String required( final Properties props, final String key ) { diff --git a/jspwiki-oidc/src/test/java/org/apache/wiki/oidc/OidcAuthorizerTest.java b/jspwiki-oidc/src/test/java/org/apache/wiki/oidc/OidcAuthorizerTest.java new file mode 100644 index 0000000000..3fd7eb0732 --- /dev/null +++ b/jspwiki-oidc/src/test/java/org/apache/wiki/oidc/OidcAuthorizerTest.java @@ -0,0 +1,57 @@ +/* + 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.wiki.oidc; + +import java.util.Arrays; +import java.util.Properties; +import java.util.stream.Collectors; + +import org.apache.wiki.api.core.Engine; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +class OidcAuthorizerTest { + + private OidcAuthorizer initialized( final Properties props ) { + final OidcAuthorizer authorizer = new OidcAuthorizer(); + authorizer.initialize( Mockito.mock( Engine.class ), props ); + return authorizer; + } + + @Test + void declaresThatTheContainerHandlesTheLogin() { + Assertions.assertTrue( initialized( new Properties() ).isContainerAuthorized() ); + } + + @Test + void knowsAdminAndAuthenticatedByDefault() { + final String names = Arrays.stream( initialized( new Properties() ).getRoles() ).map( r -> r.getName() ).sorted().collect( Collectors.joining( "," ) ); + Assertions.assertEquals( "Admin,Authenticated", names ); + } + + @Test + void takesTheRolesFromTheProperty() { + final Properties props = new Properties(); + props.setProperty( OidcAuthorizer.PROP_ROLES, " Editors , Admin ,," ); + final String names = Arrays.stream( initialized( props ).getRoles() ).map( r -> r.getName() ).sorted().collect( Collectors.joining( "," ) ); + Assertions.assertEquals( "Admin,Editors", names ); + } + +} diff --git a/jspwiki-oidc/src/test/resources/WEB-INF/web.xml b/jspwiki-oidc/src/test/resources/WEB-INF/web.xml new file mode 100644 index 0000000000..703b4cb02b --- /dev/null +++ b/jspwiki-oidc/src/test/resources/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + jspwiki-oidc test + diff --git a/jspwiki-war/pom.xml b/jspwiki-war/pom.xml index 2bfaa2ccea..9007912542 100644 --- a/jspwiki-war/pom.xml +++ b/jspwiki-war/pom.xml @@ -270,4 +270,18 @@ + + + + + oidc + + + ${project.groupId} + jspwiki-oidc + ${project.version} + + + + diff --git a/jspwiki-war/src/main/webapp/WEB-INF/web.xml b/jspwiki-war/src/main/webapp/WEB-INF/web.xml index 9d83b5a4f3..ff5cea2ffb 100644 --- a/jspwiki-war/src/main/webapp/WEB-INF/web.xml +++ b/jspwiki-war/src/main/webapp/WEB-INF/web.xml @@ -76,8 +76,9 @@