Skip to content

Commit

Permalink
MidpointAuthentication.java: cleanup/reformat, lang3, final fields...
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Sep 6, 2020
1 parent afcc35f commit 77ee491
Showing 1 changed file with 46 additions and 54 deletions.
@@ -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
Expand All @@ -29,20 +31,14 @@ public class MidpointAuthentication extends AbstractAuthenticationToken {

private final List<AuthenticationSequenceModuleType> modules;

private AuthenticationSequenceType sequence;
private final AuthenticationSequenceType sequence;
private final List<ModuleAuthentication> authentications = new ArrayList<>();

private AuthenticationChannel authenticationChannel;

private List<ModuleAuthentication> authentications = new ArrayList<ModuleAuthentication>();

private List<AuthModule> authModules;

private Object principal;

private Object credential;

private String sessionId;

private Collection<? extends GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES;

public MidpointAuthentication(AuthenticationSequenceType sequence) {
Expand Down Expand Up @@ -81,6 +77,7 @@ public void addAuthentications(ModuleAuthentication authentication) {

@Override
public Collection<GrantedAuthority> getAuthorities() {
//noinspection unchecked
return (Collection<GrantedAuthority>) authorities;
}

Expand Down Expand Up @@ -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());
Expand All @@ -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++) {
Expand All @@ -205,19 +201,17 @@ 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;
}
}
return null;
}

public boolean isAuthenticationFailed() {
if (!isAuthenticated() && getProcessingModuleAuthentication() == null
&& getAuthentications().size() == getAuthModules().size()) {
return true;
}
return false;
return !isAuthenticated()
&& getProcessingModuleAuthentication() == null
&& getAuthentications().size() == getAuthModules().size();
}

@Override
Expand All @@ -229,90 +223,88 @@ public String getName() {
}

public List<ModuleAuthentication> getParallelProcessingModules() {
int indexOfProcessingModule = getIndexOfProcessingModule(false);
int indexOfProcessingModule = getIndexOfProcessingModule(false);
if (indexOfProcessingModule == -2) {
return new ArrayList<ModuleAuthentication>();
return new ArrayList<>();
}
return getParallelProcessingModules(indexOfProcessingModule);
}

private List<ModuleAuthentication> getParallelProcessingModules(int actualIndex) {
List<ModuleAuthentication> parallelProcesingModules = new ArrayList<ModuleAuthentication>();
List<ModuleAuthentication> 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<ModuleAuthentication> parallelProcesingModules = getParallelProcessingModules(actualIndex);
List<ModuleAuthentication> 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;
}
int index = getIndexOfModule(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());
}
}

0 comments on commit 77ee491

Please sign in to comment.