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

Resolves#327 Use ServiceLoader discovery to discover the CallerDetailsResolver and ResourceAccessResolver. #363

Merged
merged 1 commit into from
Dec 7, 2023
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
19 changes: 9 additions & 10 deletions impl/src/main/java/org/glassfish/soteria/SecurityContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static jakarta.security.enterprise.AuthenticationStatus.SEND_FAILURE;
import static jakarta.security.enterprise.AuthenticationStatus.SUCCESS;
import static org.glassfish.soteria.SoteriaServiceProviders.getServiceProvider;
import static org.glassfish.soteria.mechanisms.jaspic.Jaspic.getLastAuthenticationStatus;

import java.io.Serializable;
Expand All @@ -33,21 +34,19 @@

import org.glassfish.soteria.authorization.spi.CallerDetailsResolver;
import org.glassfish.soteria.authorization.spi.ResourceAccessResolver;
import org.glassfish.soteria.authorization.spi.impl.JaccResourceAccessResolver;
import org.glassfish.soteria.authorization.spi.impl.ReflectionAndJaccCallerDetailsResolver;
import org.glassfish.soteria.mechanisms.jaspic.Jaspic;

public class SecurityContextImpl implements SecurityContext, Serializable {

private static final long serialVersionUID = 1L;

private CallerDetailsResolver callerDetailsResolver;
private ResourceAccessResolver resourceAccessResolver;

@PostConstruct
public void init() {
callerDetailsResolver = new ReflectionAndJaccCallerDetailsResolver();
resourceAccessResolver = new JaccResourceAccessResolver();
callerDetailsResolver = getServiceProvider(CallerDetailsResolver.class);
resourceAccessResolver = getServiceProvider(ResourceAccessResolver.class);
}

@Override
Expand Down Expand Up @@ -77,15 +76,15 @@ public boolean hasAccessToWebResource(String resource, String... methods) {

@Override
public AuthenticationStatus authenticate(HttpServletRequest request, HttpServletResponse response, AuthenticationParameters parameters) {

try {
if (Jaspic.authenticate(request, response, parameters)) {
// All servers return true when authentication actually took place
// All servers return true when authentication actually took place
return SUCCESS;
}

// GlassFish returns false when either authentication is in progress or authentication
// failed (or was not done at all).
// failed (or was not done at all).
// Therefore we need to rely on the status we saved as a request attribute
return getLastAuthenticationStatus(request);
} catch (IllegalArgumentException e) { // TODO: exception type not ideal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package org.glassfish.soteria.authorization.spi.impl;

import org.glassfish.soteria.DefaultService;
import org.glassfish.soteria.authorization.JACC;
import org.glassfish.soteria.authorization.spi.ResourceAccessResolver;

public class JaccResourceAccessResolver implements ResourceAccessResolver {
public class JaccResourceAccessResolver implements ResourceAccessResolver, DefaultService {

@Override
public boolean hasAccessToWebResource(String resource, String... methods) {
return JACC.hasAccessToWebResource(resource, methods);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.glassfish.soteria.authorization.spi.impl;

import org.glassfish.soteria.DefaultService;
import org.glassfish.soteria.authorization.JACC;
import org.glassfish.soteria.authorization.spi.CallerDetailsResolver;

Expand All @@ -26,7 +27,7 @@
import static java.util.Collections.emptyList;
import static jakarta.security.jacc.PolicyContext.getContextID;

public class ReflectionAndJaccCallerDetailsResolver implements CallerDetailsResolver {
public class ReflectionAndJaccCallerDetailsResolver implements CallerDetailsResolver, DefaultService {

@Override
public Principal getCallerPrincipal() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.soteria.authorization.spi.impl.ReflectionAndJaccCallerDetailsResolver
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.soteria.authorization.spi.impl.JaccResourceAccessResolver