-
-
Notifications
You must be signed in to change notification settings - Fork 489
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
JWT Headers security module #7899
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
199c54b
JWT Headers security module
david-blasby 8571521
changes from jose's review
david-blasby 4fe7ce6
make sure that token validation is being done
david-blasby ba3c2ff
allow two jwt-header auth filters at the same time + configuration
david-blasby 8133ef9
fix config problem, remove some unnecessary code
david-blasby 5caf519
merge with origin/main
david-blasby 23b1a5a
added support for easily turning on/off UpdateProfile and UpdateGroup…
david-blasby 7d0384e
update jwt shared library (gn+gs) dependency to 2.27 to include GS PR…
david-blasby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
core/src/main/java/org/fao/geonet/kernel/security/jwtheaders/JwtHeadersAuthFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (C) 2024 Food and Agriculture Organization of the | ||
* United Nations (FAO-UN), United Nations World Food Programme (WFP) | ||
* and United Nations Environment Programme (UNEP) | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA | ||
* | ||
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2, | ||
* Rome - Italy. email: [email protected] | ||
*/ | ||
|
||
package org.fao.geonet.kernel.security.jwtheaders; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.IOException; | ||
|
||
|
||
/** | ||
* This handles the JWT-Headers authentication filter. It's based on the Shibboleth filter. | ||
* | ||
*/ | ||
public class JwtHeadersAuthFilter extends GenericFilterBean { | ||
|
||
@Autowired | ||
public JwtHeadersUserUtil jwtHeadersUserUtil; | ||
|
||
JwtHeadersConfiguration jwtHeadersConfiguration; | ||
|
||
|
||
public JwtHeadersAuthFilter(JwtHeadersConfiguration jwtHeadersConfiguration) { | ||
this.jwtHeadersConfiguration = jwtHeadersConfiguration; | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) | ||
throws IOException, ServletException { | ||
var existingAuth = SecurityContextHolder.getContext().getAuthentication(); | ||
HttpServletRequest request = (HttpServletRequest) servletRequest; | ||
|
||
|
||
var config = jwtHeadersConfiguration.getJwtConfiguration(); | ||
|
||
var user = JwtHeadersTrivialUser.create(config, request); | ||
|
||
if (user == null && existingAuth != null) { | ||
if (existingAuth instanceof JwtHeadersUsernamePasswordAuthenticationToken) { | ||
//at this point, there isn't a JWT header, but there's an existing auth that was made by us (JWT header) | ||
// in this case, we need to log-off. They have a JSESSION auth that is no longer valid. | ||
logout(request); | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
return; | ||
} | ||
} | ||
|
||
|
||
if (user == null) { | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
return; // no valid user in header | ||
} | ||
|
||
//we have a valid user in the headers | ||
|
||
//existing user is the same user as the request | ||
if (existingAuth != null && existingAuth.getName().equals(user.getUsername())) { | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
return; // abort early - no need to do an expensive login. Use the existing one. | ||
} | ||
|
||
//existing user isnt the same user as the request | ||
if (existingAuth != null && !existingAuth.getName().equals(user.getUsername())) { | ||
//in this case there are two auth's - the existing one (likely from JSESSION) | ||
//and one coming in from the JWT headers. In this case, we kill the other login | ||
//and make a new one. | ||
logout(request); | ||
} | ||
|
||
var userDetails = jwtHeadersUserUtil.getUser(user, jwtHeadersConfiguration); | ||
if (userDetails != null) { | ||
UsernamePasswordAuthenticationToken auth = new JwtHeadersUsernamePasswordAuthenticationToken( | ||
userDetails, null, userDetails.getAuthorities()); | ||
auth.setDetails(userDetails); | ||
SecurityContextHolder.getContext().setAuthentication(auth); | ||
|
||
} | ||
|
||
filterChain.doFilter(servletRequest, servletResponse); | ||
} | ||
|
||
/** | ||
* handle a logout - clear out the security context, and invalidate the session | ||
* @param request | ||
* @throws ServletException | ||
*/ | ||
public void logout(HttpServletRequest request) throws ServletException { | ||
request.logout();//dont think this does anything in GN | ||
SecurityContextHolder.getContext().setAuthentication(null); | ||
request.getSession().invalidate(); | ||
} | ||
|
||
} | ||
|
||
|
114 changes: 114 additions & 0 deletions
114
core/src/main/java/org/fao/geonet/kernel/security/jwtheaders/JwtHeadersConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (C) 2024 Food and Agriculture Organization of the | ||
* United Nations (FAO-UN), United Nations World Food Programme (WFP) | ||
* and United Nations Environment Programme (UNEP) | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA | ||
* | ||
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2, | ||
* Rome - Italy. email: [email protected] | ||
*/ | ||
|
||
package org.fao.geonet.kernel.security.jwtheaders; | ||
|
||
import org.fao.geonet.kernel.security.SecurityProviderConfiguration; | ||
import org.geoserver.security.jwtheaders.JwtConfiguration; | ||
|
||
/** | ||
* configuration for the JWT Headers security filter. | ||
* See GN documentation. | ||
* This is based on GeoServer's JWT-Headers Module, so you can see there as well. | ||
* | ||
* This class handles the GN filter configuration details, and hands the actual configuration | ||
* for the filter to the JwtConfiguration class. This class is also used in Geoserver. | ||
* | ||
*/ | ||
public class JwtHeadersConfiguration implements SecurityProviderConfiguration { | ||
|
||
|
||
public LoginType loginType = LoginType.AUTOLOGIN; | ||
/** | ||
* true -> update the DB with the information from OIDC (don't allow user to edit profile in the UI) | ||
* false -> don't update the DB (user must edit profile in UI). | ||
*/ | ||
public boolean updateProfile = true; | ||
/** | ||
* true -> update the DB (user's group) with the information from OIDC (don't allow admin to edit user's groups in the UI) | ||
* false -> don't update the DB (admin must edit groups in UI). | ||
*/ | ||
public boolean updateGroup = true; | ||
protected JwtConfiguration jwtConfiguration; | ||
|
||
|
||
// getters/setters | ||
|
||
|
||
public JwtHeadersConfiguration() { | ||
jwtConfiguration = new JwtConfiguration(); | ||
} | ||
|
||
public boolean isUpdateProfile() { | ||
return updateProfile; | ||
} | ||
|
||
public void setUpdateProfile(boolean updateProfile) { | ||
this.updateProfile = updateProfile; | ||
} | ||
|
||
public boolean isUpdateGroup() { | ||
return updateGroup; | ||
} | ||
|
||
|
||
//---- abstract class methods | ||
|
||
public void setUpdateGroup(boolean updateGroup) { | ||
this.updateGroup = updateGroup; | ||
} | ||
|
||
@Override | ||
public String getLoginType() { | ||
return loginType.toString(); | ||
} | ||
|
||
@Override | ||
public String getSecurityProvider() { | ||
return "JWT-HEADERS"; | ||
} | ||
|
||
@Override | ||
public boolean isUserProfileUpdateEnabled() { | ||
// If updating profile from the security provider then disable the profile updates in the interface | ||
return !updateProfile; | ||
} | ||
|
||
//======================================================================== | ||
|
||
@Override | ||
public boolean isUserGroupUpdateEnabled() { | ||
// If updating group from the security provider then disable the group updates in the interface | ||
return !updateGroup; | ||
} | ||
|
||
public org.geoserver.security.jwtheaders.JwtConfiguration getJwtConfiguration() { | ||
return jwtConfiguration; | ||
} | ||
|
||
public void setJwtConfiguration( | ||
org.geoserver.security.jwtheaders.JwtConfiguration jwtConfiguration) { | ||
this.jwtConfiguration = jwtConfiguration; | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see
json-path
is used inservices
also. Please move the dependency with the version in both modules to the root pom and remove the version in the modules.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved