From 77ee491040e8b6385e5ada00c7145a46f98326c6 Mon Sep 17 00:00:00 2001 From: Richard Richter Date: Mon, 7 Sep 2020 00:43:39 +0200 Subject: [PATCH] MidpointAuthentication.java: cleanup/reformat, lang3, final fields... --- .../MidpointAuthentication.java | 100 ++++++++---------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git a/model/model-api/src/main/java/com/evolveum/midpoint/model/api/authentication/MidpointAuthentication.java b/model/model-api/src/main/java/com/evolveum/midpoint/model/api/authentication/MidpointAuthentication.java index 1bcc0d068e5..a64485e712f 100644 --- a/model/model-api/src/main/java/com/evolveum/midpoint/model/api/authentication/MidpointAuthentication.java +++ b/model/model-api/src/main/java/com/evolveum/midpoint/model/api/authentication/MidpointAuthentication.java @@ -1,25 +1,27 @@ /* - * Copyright (c) 2010-2019 Evolveum and contributors + * Copyright (C) 2010-2020 Evolveum and contributors * * This work is dual-licensed under the Apache License 2.0 * and European Union Public License. See LICENSE file for details. */ package com.evolveum.midpoint.model.api.authentication; -import com.evolveum.midpoint.schema.util.SecurityPolicyUtil; -import com.evolveum.midpoint.security.api.MidPointPrincipal; -import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceModuleNecessityType; -import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceModuleType; -import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceType; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import javax.servlet.http.HttpServletRequest; + import org.apache.commons.lang3.Validate; import org.springframework.security.authentication.AbstractAuthenticationToken; -import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; -import javax.servlet.ServletRequest; -import javax.servlet.http.HttpServletRequest; -import java.util.*; +import com.evolveum.midpoint.schema.util.SecurityPolicyUtil; +import com.evolveum.midpoint.security.api.MidPointPrincipal; +import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceModuleNecessityType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceModuleType; +import com.evolveum.midpoint.xml.ns._public.common.common_3.AuthenticationSequenceType; /** * @author skublik @@ -29,20 +31,14 @@ public class MidpointAuthentication extends AbstractAuthenticationToken { private final List modules; - private AuthenticationSequenceType sequence; + private final AuthenticationSequenceType sequence; + private final List authentications = new ArrayList<>(); private AuthenticationChannel authenticationChannel; - - private List authentications = new ArrayList(); - private List authModules; - private Object principal; - private Object credential; - private String sessionId; - private Collection authorities = AuthorityUtils.NO_AUTHORITIES; public MidpointAuthentication(AuthenticationSequenceType sequence) { @@ -81,6 +77,7 @@ public void addAuthentications(ModuleAuthentication authentication) { @Override public Collection getAuthorities() { + //noinspection unchecked return (Collection) authorities; } @@ -165,7 +162,7 @@ public boolean isProcessing() { return false; } - public int getIndexOfProcessingModule( boolean createEmptyAuthenticationIfNeeded){ + public int getIndexOfProcessingModule(boolean createEmptyAuthenticationIfNeeded) { if (getAuthentications().isEmpty()) { if (createEmptyAuthenticationIfNeeded) { addAuthentications(getAuthModules().get(0).getBaseModuleAuthentication()); @@ -187,11 +184,10 @@ public int getIndexOfProcessingModule( boolean createEmptyAuthenticationIfNeeded } return actualSize; } - return -1; -// throw new IllegalStateException("Couldn't find index of processing module"); + return -1; } - public int getIndexOfModule(ModuleAuthentication authentication){ + public int getIndexOfModule(ModuleAuthentication authentication) { Validate.notNull(authentication); for (int i = 0; i < getModules().size(); i++) { @@ -205,7 +201,7 @@ public int getIndexOfModule(ModuleAuthentication authentication){ public ModuleAuthentication getProcessingModuleAuthentication() { for (ModuleAuthentication authentication : getAuthentications()) { if (authentication.getState().equals(StateOfModule.LOGIN_PROCESSING) - || authentication.getState().equals(StateOfModule.LOGOUT_PROCESSING)) { + || authentication.getState().equals(StateOfModule.LOGOUT_PROCESSING)) { return authentication; } } @@ -213,11 +209,9 @@ public ModuleAuthentication getProcessingModuleAuthentication() { } public boolean isAuthenticationFailed() { - if (!isAuthenticated() && getProcessingModuleAuthentication() == null - && getAuthentications().size() == getAuthModules().size()) { - return true; - } - return false; + return !isAuthenticated() + && getProcessingModuleAuthentication() == null + && getAuthentications().size() == getAuthModules().size(); } @Override @@ -229,80 +223,78 @@ public String getName() { } public List getParallelProcessingModules() { - int indexOfProcessingModule = getIndexOfProcessingModule(false); + int indexOfProcessingModule = getIndexOfProcessingModule(false); if (indexOfProcessingModule == -2) { - return new ArrayList(); + return new ArrayList<>(); } return getParallelProcessingModules(indexOfProcessingModule); } private List getParallelProcessingModules(int actualIndex) { - List parallelProcesingModules = new ArrayList(); + List parallelProcessingModules = new ArrayList<>(); ModuleAuthentication processingModule = getAuthentications().get(actualIndex); AuthenticationSequenceModuleType processingModuleType = getModules().get(actualIndex); if (processingModule == null) { - return parallelProcesingModules; + return parallelProcessingModules; } if (actualIndex > 0) { for (int i = actualIndex - 1; i >= 0; i--) { if (getModules().get(i) != null - && processingModuleType.getOrder() == getModules().get(i).getOrder()) { - parallelProcesingModules.add(getAuthModules().get(i).getBaseModuleAuthentication()); + && processingModuleType.getOrder().equals(getModules().get(i).getOrder())) { + parallelProcessingModules.add(getAuthModules().get(i).getBaseModuleAuthentication()); } else { break; } } } - parallelProcesingModules.add(processingModule); + parallelProcessingModules.add(processingModule); for (int i = actualIndex + 1; i < getModules().size(); i++) { if (getModules().get(i) != null - && processingModuleType.getOrder() == getModules().get(i).getOrder()) { - parallelProcesingModules.add(getAuthModules().get(i).getBaseModuleAuthentication()); + && processingModuleType.getOrder().equals(getModules().get(i).getOrder())) { + parallelProcessingModules.add(getAuthModules().get(i).getBaseModuleAuthentication()); } } - - - return parallelProcesingModules; + return parallelProcessingModules; } public int resolveParallelModules(HttpServletRequest request, int actualIndex) { String header = request.getHeader("Authorization"); - if (header == null){ + if (header == null) { return actualIndex; } String type = header.split(" ")[0]; - List parallelProcesingModules = getParallelProcessingModules(actualIndex); + List parallelProcessingModules = getParallelProcessingModules(actualIndex); int resolvedIndex = -1; - for (ModuleAuthentication parallelProcesingModule : parallelProcesingModules) { - int usedIndex = getAuthentications().indexOf(parallelProcesingModule); - if (parallelProcesingModule.getNameOfModuleType().toLowerCase().equals(type.toLowerCase()) - && resolvedIndex == -1) { - parallelProcesingModule.setState(StateOfModule.LOGIN_PROCESSING); + for (ModuleAuthentication parallelProcessingModule : parallelProcessingModules) { + int usedIndex = getAuthentications().indexOf(parallelProcessingModule); + if (parallelProcessingModule.getNameOfModuleType().toLowerCase().equals(type.toLowerCase()) + && resolvedIndex == -1) { + parallelProcessingModule.setState(StateOfModule.LOGIN_PROCESSING); if (usedIndex != -1) { resolvedIndex = usedIndex; } else { resolvedIndex = getAuthentications().size(); } } else { - parallelProcesingModule.setState(StateOfModule.FAILURE); + parallelProcessingModule.setState(StateOfModule.FAILURE); } if (usedIndex == -1) { - getAuthentications().add(parallelProcesingModule); + getAuthentications().add(parallelProcessingModule); } else { - getAuthentications().set(usedIndex, parallelProcesingModule); + getAuthentications().set(usedIndex, parallelProcessingModule); } } - if (resolvedIndex == -1){ + if (resolvedIndex == -1) { throw new IllegalArgumentException("Couldn't find module with type '" + type + "' in sequence '" + getSequence().getName() + "'"); } return resolvedIndex; } - public boolean isLast(ModuleAuthentication moduleAuthentication){ + public boolean isLast(ModuleAuthentication moduleAuthentication) { if (getAuthentications().isEmpty()) { return false; } @@ -310,9 +302,9 @@ public boolean isLast(ModuleAuthentication moduleAuthentication){ if (index == -1) { return false; } - if (index == getModules().size()-1) { + if (index == getModules().size() - 1) { return true; } - return getModules().get(index).getOrder().equals(getModules().get(getModules().size()-1).getOrder()); + return getModules().get(index).getOrder().equals(getModules().get(getModules().size() - 1).getOrder()); } }