Skip to content
Open
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
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_

* 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**

* _3.0.1-git-09
Expand Down
2 changes: 1 addition & 1 deletion jspwiki-api/src/main/java/org/apache/wiki/api/Release.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public final class Release {
* <p>
* 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
Expand Down
54 changes: 54 additions & 0 deletions jspwiki-oidc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!--
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.
-->


# 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.
86 changes: 86 additions & 0 deletions jspwiki-oidc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<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/maven-v4_0_0.xsd">

<parent>
<groupId>org.apache.jspwiki</groupId>
<artifactId>jspwiki-builder</artifactId>
<version>3.0.1-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>jspwiki-oidc</artifactId>
<name>Apache JSPWiki OpenID Connect login</name>

<properties>
<pac4j.version>6.5.5</pac4j.version>
<jakartaee-pac4j.version>8.0.3</jakartaee-pac4j.version>
</properties>

<dependencies>
<dependency>
<groupId>org.pac4j</groupId>
<artifactId>jakartaee-pac4j</artifactId>
<version>${jakartaee-pac4j.version}</version>
</dependency>

<dependency>
<groupId>org.pac4j</groupId>
<artifactId>pac4j-oidc</artifactId>
<version>${pac4j.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jspwiki-main</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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 );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
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.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;

/**
* 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() );
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 ) {
final String value = props.getProperty( key );
if( value == null || value.isBlank() ) {
throw new IllegalStateException( CONFIG_FILE + " does not define " + key );
}
return value;
}

}
Original file line number Diff line number Diff line change
@@ -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 );
}
}

}
Loading
Loading