Skip to content

Commit

Permalink
some pattern matching for instanceof, removing unused authentication …
Browse files Browse the repository at this point in the history
…parameter from createEnvironment method
  • Loading branch information
katkav committed Aug 2, 2023
1 parent 4af8013 commit 813347c
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ public abstract class AbstractCredentialProvider<T extends AbstractAuthenticatio
public abstract Class<? extends CredentialPolicyType> getTypeOfCredential();

public boolean supports(Class<?> authenticationClass, Authentication authentication) {
if (!(authentication instanceof MidpointAuthentication)) {
if (!(authentication instanceof MidpointAuthentication mpAuthentication)) {
return supports(authenticationClass);
}
MidpointAuthentication mpAuthentication = (MidpointAuthentication) authentication;
ModuleAuthenticationImpl moduleAuthentication = (ModuleAuthenticationImpl) getProcessingModule(mpAuthentication);
if (moduleAuthentication == null || moduleAuthentication.getAuthentication() == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,4 @@ public boolean supports(Class<?> authentication) {
return ArchetypeSelectionAuthenticationToken.class.equals(authentication);
}

// @Override
// public Class<? extends CredentialPolicyType> getTypeOfCredential() {
// return null; //todo
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.evolveum.midpoint.authentication.api.AuthenticationChannel;
import com.evolveum.midpoint.authentication.impl.evaluator.AttributeVerificationEvaluatorImpl;
import com.evolveum.midpoint.authentication.impl.module.authentication.token.AttributeVerificationToken;
import com.evolveum.midpoint.authentication.impl.module.authentication.token.FocusVerificationToken;
import com.evolveum.midpoint.model.api.authentication.GuiProfiledPrincipal;
import com.evolveum.midpoint.model.api.context.AttributeVerificationAuthenticationContext;
import com.evolveum.midpoint.prism.path.ItemPath;
Expand Down Expand Up @@ -53,7 +52,7 @@ protected Authentication internalAuthentication(Authentication authentication, L
String enteredUsername = ((MidPointPrincipal) authentication.getPrincipal()).getUsername();
LOGGER.trace("Authenticating username '{}'", enteredUsername);

ConnectionEnvironment connEnv = createEnvironment(channel, authentication);
ConnectionEnvironment connEnv = createEnvironment(channel);

try {
Authentication token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected Authentication internalAuthentication(Authentication authentication, L
return authentication;
}

ConnectionEnvironment connEnv = createEnvironment(channel, authentication);
ConnectionEnvironment connEnv = createEnvironment(channel);

try {
Authentication token = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected Authentication internalAuthentication(Authentication authentication, L
return authentication;
}

ConnectionEnvironment connEnv = createEnvironment(channel, authentication);
ConnectionEnvironment connEnv = createEnvironment(channel);

try {
Authentication token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected Authentication internalAuthentication(Authentication authentication, L
return authentication;
}

ConnectionEnvironment connEnv = createEnvironment(channel, authentication);
ConnectionEnvironment connEnv = createEnvironment(channel);

try {
return new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected Authentication internalAuthentication(
String enteredUsername = (String) authentication.getPrincipal();
LOGGER.trace("Authenticating username '{}'", enteredUsername);

ConnectionEnvironment connEnv = createEnvironment(channel, authentication);
ConnectionEnvironment connEnv = createEnvironment(channel);
try {
Authentication token;
if (authentication instanceof MailNonceAuthenticationToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ public Authentication authenticate(Authentication originalAuthentication) throws
Authentication token = internalAuthentication(processingAuthentication, authRequirements.requireAssignment,
authRequirements.channel, authRequirements.focusType);

if (actualAuthentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) actualAuthentication;
if (actualAuthentication instanceof MidpointAuthentication mpAuthentication) {
ModuleAuthenticationImpl moduleAuthentication = (ModuleAuthenticationImpl) getProcessingModule(mpAuthentication);
if (token.getPrincipal() instanceof MidPointPrincipal) {
MidPointPrincipal principal = (MidPointPrincipal) token.getPrincipal();
if (token.getPrincipal() instanceof MidPointPrincipal principal) {
token = createNewAuthenticationToken(token,
mpAuthentication.getAuthenticationChannel().resolveAuthorities(principal.getAuthorities()));
} else {
Expand All @@ -96,8 +94,7 @@ public Authentication authenticate(Authentication originalAuthentication) throws
}

private boolean isAnonymous(Authentication originalAuthentication) {
if (originalAuthentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) originalAuthentication;
if (originalAuthentication instanceof MidpointAuthentication mpAuthentication) {
ModuleAuthentication moduleAuthentication = getProcessingModule(mpAuthentication);
return moduleAuthentication.getAuthentication() instanceof AnonymousAuthenticationToken;
}
Expand All @@ -106,40 +103,30 @@ private boolean isAnonymous(Authentication originalAuthentication) {

private Authentication initAuthRequirements(Authentication processingAuthentication, Authentication originalAuthentication,
Authentication actualAuthentication, AuthenticationRequirements authRequirements) {
if (originalAuthentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) originalAuthentication;
if (originalAuthentication instanceof MidpointAuthentication mpAuthentication) {
initAuthRequirements(mpAuthentication, authRequirements);
ModuleAuthentication moduleAuthentication = getProcessingModule(mpAuthentication);
if (moduleAuthentication.getFocusType() != null) {
authRequirements.focusType = PrismContext.get().getSchemaRegistry()
.determineCompileTimeClass(moduleAuthentication.getFocusType());
}
authRequirements.requireAssignment = mpAuthentication.getSequence().getRequireAssignmentTarget();
authRequirements.channel = mpAuthentication.getAuthenticationChannel();
return moduleAuthentication.getAuthentication();
} else if (actualAuthentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) actualAuthentication;
ModuleAuthentication moduleAuthentication = getProcessingModule(mpAuthentication);
if (moduleAuthentication != null && moduleAuthentication.getFocusType() != null) {
authRequirements.focusType = PrismContext.get().getSchemaRegistry()
.determineCompileTimeClass(moduleAuthentication.getFocusType());
}
authRequirements.requireAssignment = mpAuthentication.getSequence().getRequireAssignmentTarget();
authRequirements.channel = mpAuthentication.getAuthenticationChannel();
} else if (actualAuthentication instanceof MidpointAuthentication mpAuthentication) {
initAuthRequirements(mpAuthentication, authRequirements);
}
return processingAuthentication;
}

private void initAuthRequirements(MidpointAuthentication mpAuthentication, AuthenticationRequirements authRequirements) {
ModuleAuthentication moduleAuthentication = getProcessingModule(mpAuthentication);
if (moduleAuthentication != null && moduleAuthentication.getFocusType() != null) {
authRequirements.focusType = PrismContext.get().getSchemaRegistry()
.determineCompileTimeClass(moduleAuthentication.getFocusType());
}
authRequirements.requireAssignment = mpAuthentication.getSequence().getRequireAssignmentTarget();
authRequirements.channel = mpAuthentication.getAuthenticationChannel();
}

protected AuthenticationRequirements initAuthRequirements(Authentication actualAuthentication) {
AuthenticationRequirements authRequirements = new AuthenticationRequirements();
if (actualAuthentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) actualAuthentication;
ModuleAuthentication moduleAuthentication = getProcessingModule(mpAuthentication);
if (moduleAuthentication != null && moduleAuthentication.getFocusType() != null) {
authRequirements.focusType = PrismContext.get().getSchemaRegistry()
.determineCompileTimeClass(moduleAuthentication.getFocusType());
}
authRequirements.requireAssignment = mpAuthentication.getSequence().getRequireAssignmentTarget();
authRequirements.channel = mpAuthentication.getAuthenticationChannel();
if (actualAuthentication instanceof MidpointAuthentication mpAuthentication) {
initAuthRequirements(mpAuthentication, authRequirements);
}
return authRequirements;
}
Expand All @@ -164,7 +151,7 @@ protected ModuleAuthentication getProcessingModule(MidpointAuthentication mpAuth
return moduleAuthentication;
}

protected ConnectionEnvironment createEnvironment(AuthenticationChannel channel, Authentication authentication) {
protected ConnectionEnvironment createEnvironment(AuthenticationChannel channel) {
ConnectionEnvironment connEnv;
if (channel != null) {
connEnv = ConnectionEnvironment.create(channel.getChannelId());
Expand All @@ -173,8 +160,7 @@ protected ConnectionEnvironment createEnvironment(AuthenticationChannel channel,
}

Authentication processingAuthentication = SecurityUtil.getAuthentication();
if (processingAuthentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) processingAuthentication;
if (processingAuthentication instanceof MidpointAuthentication mpAuthentication) {
connEnv.setSessionIdOverride(mpAuthentication.getSessionId());
connEnv.setSequenceIdentifier(mpAuthentication.getSequenceIdentifier());
connEnv.setModuleIdentifier(mpAuthentication.getProcessingModuleAuthenticationIdentifier());
Expand All @@ -190,10 +176,9 @@ protected abstract Authentication createNewAuthenticationToken(
Authentication actualAuthentication, Collection<? extends GrantedAuthority> newAuthorities);

public boolean supports(Class<?> authenticationClass, Authentication authentication) {
if (!(authentication instanceof MidpointAuthentication)) {
if (!(authentication instanceof MidpointAuthentication mpAuthentication)) {
return supports(authenticationClass);
}
MidpointAuthentication mpAuthentication = (MidpointAuthentication) authentication;
ModuleAuthentication moduleAuthentication = getProcessingModule(mpAuthentication);
if (moduleAuthentication == null || moduleAuthentication.getAuthentication() == null) {
return false;
Expand Down Expand Up @@ -245,8 +230,7 @@ protected String getChannel() {
protected ConnectionEnvironment createConnectEnvironment(String channel) {
ConnectionEnvironment env = ConnectionEnvironment.create(channel);
Authentication actualAuthentication = SecurityContextHolder.getContext().getAuthentication();
if (actualAuthentication instanceof MidpointAuthentication) {
MidpointAuthentication mpAuthentication = (MidpointAuthentication) actualAuthentication;
if (actualAuthentication instanceof MidpointAuthentication mpAuthentication) {
if (mpAuthentication.getSessionId() != null) {
env.setSessionIdOverride(((MidpointAuthentication) actualAuthentication).getSessionId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected DirContextOperations doAuthentication(

mpDirContextAdapter.setChannel(authRequirements.channel);
mpDirContextAdapter.setRequireAssignment(authRequirements.requireAssignment);
mpDirContextAdapter.setConnectionEnvironment(createEnvironment(authRequirements.channel, authentication));
mpDirContextAdapter.setConnectionEnvironment(createEnvironment(authRequirements.channel));

return mpDirContextAdapter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public OidcResourceServerProvider(OpaqueTokenIntrospector introspector) {
protected Authentication internalAuthentication(Authentication authentication, List requireAssignment,
AuthenticationChannel channel, Class focusType) throws AuthenticationException {
Authentication token;
if (authentication instanceof BearerTokenAuthenticationToken) {
BearerTokenAuthenticationToken oidcAuthenticationToken = (BearerTokenAuthenticationToken) authentication;
if (authentication instanceof BearerTokenAuthenticationToken oidcAuthenticationToken) {
Authentication authenticationToken;
try {
authenticationToken = oidcProvider.authenticate(oidcAuthenticationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected Authentication internalAuthentication(Authentication authentication, L
String enteredUsername = getEnteredUsername(authentication);
LOGGER.trace("Authenticating username '{}'", enteredUsername);

ConnectionEnvironment connEnv = createEnvironment(channel, authentication);
ConnectionEnvironment connEnv = createEnvironment(channel);

try {
Authentication token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void writeAuthentication(Authentication originalAuthentication, Midpoi

protected PreAuthenticatedAuthenticationToken getPreAuthenticationToken(Authentication authentication, String enteredUsername, Class<? extends FocusType> focusType,
List<ObjectReferenceType> requireAssignment, AuthenticationChannel channel){
ConnectionEnvironment connEnv = createEnvironment(channel, authentication);
ConnectionEnvironment connEnv = createEnvironment(channel);
PreAuthenticationContext authContext = new PreAuthenticationContext(enteredUsername, focusType, requireAssignment);
if (channel != null) {
authContext.setSupportActivationByChannel(channel.isSupportActivationByChannel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected Authentication internalAuthentication(Authentication authentication, L
String enteredUsername = (String) authentication.getPrincipal();
LOGGER.trace("Authenticating username '{}'", enteredUsername);

ConnectionEnvironment connEnv = createEnvironment(channel, authentication);
ConnectionEnvironment connEnv = createEnvironment(channel);

try {
Authentication token;
Expand Down

0 comments on commit 813347c

Please sign in to comment.