-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in pac4j_update (pull request #669)
Pac4j update * NOJIRA Updates for Pac4J changes * NOJIRA: Pac4J libs update Updates for Pac4J updates to current release * NOJIRA: Pac4J libs update Updates for Pac4J updates to current release * NOJIRA: Pac4J libs update Updates for Pac4J updates to current release
- Loading branch information
Showing
16 changed files
with
323 additions
and
109 deletions.
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
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
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
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
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
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
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
45 changes: 45 additions & 0 deletions
45
pac4j-module/src/main/java/net/unicon/shibui/pac4j/Pac4JHttpServletRequestWrapper.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,45 @@ | ||
package net.unicon.shibui.pac4j; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import org.pac4j.core.profile.ProfileHelper; | ||
import org.pac4j.core.profile.UserProfile; | ||
|
||
import java.security.Principal; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
/** | ||
* FROM: https://github.com/pac4j/jee-pac4j/blob/master/jakartaee-pac4j/src/main/java/org/pac4j/jee/util/Pac4JHttpServletRequestWrapper.java | ||
*/ | ||
public class Pac4JHttpServletRequestWrapper extends HttpServletRequestWrapper { | ||
private Collection<UserProfile> profiles; | ||
|
||
public Pac4JHttpServletRequestWrapper(final HttpServletRequest request, final Collection<UserProfile> profiles) { | ||
super(request); | ||
this.profiles = profiles; | ||
} | ||
|
||
@Override | ||
public String getRemoteUser() { | ||
return getPrincipal().map(p -> p.getName()).orElse(null); | ||
} | ||
|
||
private Optional<UserProfile> getProfile() { | ||
return ProfileHelper.flatIntoOneProfile(profiles); | ||
} | ||
|
||
private Optional<Principal> getPrincipal() { | ||
return getProfile().map(UserProfile::asPrincipal); | ||
} | ||
|
||
@Override | ||
public Principal getUserPrincipal() { | ||
return getPrincipal().orElse(null); | ||
} | ||
|
||
@Override | ||
public boolean isUserInRole(String role) { | ||
return this.profiles.stream().anyMatch(p -> p.getRoles().contains(role)); | ||
} | ||
} |
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
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
78 changes: 78 additions & 0 deletions
78
pac4j-module/src/main/java/net/unicon/shibui/pac4j/SecurityFilter.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,78 @@ | ||
package net.unicon.shibui.pac4j; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.FilterConfig; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.pac4j.core.adapter.FrameworkAdapter; | ||
import org.pac4j.core.config.Config; | ||
import org.pac4j.core.profile.ProfileHelper; | ||
import org.pac4j.core.profile.UserProfile; | ||
import org.pac4j.core.util.Pac4jConstants; | ||
import org.pac4j.core.util.security.SecurityEndpoint; | ||
import org.pac4j.core.util.security.SecurityEndpointBuilder; | ||
import org.pac4j.jee.config.AbstractConfigFilter; | ||
import org.pac4j.jee.context.JEEFrameworkParameters; | ||
|
||
import java.io.IOException; | ||
import java.security.Principal; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
@Getter | ||
@Setter | ||
public class SecurityFilter extends AbstractConfigFilter implements SecurityEndpoint { | ||
private String clients; | ||
private String authorizers; | ||
private String matchers; | ||
public SecurityFilter() {} | ||
|
||
public SecurityFilter(final Config config) { | ||
setConfig(config); | ||
} | ||
|
||
public SecurityFilter(final Config config, final String clients) { | ||
this(config); | ||
this.clients = clients; | ||
} | ||
|
||
public SecurityFilter(final Config config, final String clients, final String authorizers) { | ||
this(config, clients); | ||
this.authorizers = authorizers; | ||
} | ||
|
||
public SecurityFilter(final Config config, final String clients, final String authorizers, final String matchers) { | ||
this(config, clients, authorizers); | ||
this.matchers = matchers; | ||
} | ||
|
||
public static SecurityFilter build(final Object... parameters) { | ||
final SecurityFilter securityFilter = new SecurityFilter(); | ||
SecurityEndpointBuilder.buildConfig(securityFilter, parameters); | ||
return securityFilter; | ||
} | ||
|
||
@Override | ||
public void init(final FilterConfig filterConfig) throws ServletException { | ||
super.init(filterConfig); | ||
|
||
this.clients = getStringParam(filterConfig, Pac4jConstants.CLIENTS, this.clients); | ||
this.authorizers = getStringParam(filterConfig, Pac4jConstants.AUTHORIZERS, this.authorizers); | ||
this.matchers = getStringParam(filterConfig, Pac4jConstants.MATCHERS, this.matchers); | ||
} | ||
|
||
@Override | ||
protected final void internalFilter(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws IOException, ServletException { | ||
Config config = getSharedConfig(); | ||
FrameworkAdapter.INSTANCE.applyDefaultSettingsIfUndefined(config); | ||
config.getSecurityLogic().perform(config, (ctx, session, profiles) -> { | ||
// if no profiles are loaded, pac4j is not concerned with this request | ||
filterChain.doFilter(profiles.isEmpty() ? request : new Pac4JHttpServletRequestWrapper(request, profiles), response); | ||
return null; | ||
}, clients, authorizers, matchers, new JEEFrameworkParameters(request, response)); | ||
} | ||
} |
Oops, something went wrong.