use of com.evolveum.midpoint.authentication.impl.module.authentication.NodeAuthenticationTokenImpl in project midpoint by Evolveum.
the class NodeAuthenticationEvaluatorImpl method authenticate.
public boolean authenticate(@Nullable String remoteName, String remoteAddress, @NotNull String credentials, String operation) {
LOGGER.debug("Checking if {} ({}) is a known node", remoteName, remoteAddress);
OperationResult result = new OperationResult(OPERATION_SEARCH_NODE);
ConnectionEnvironment connEnv = ConnectionEnvironment.create(SchemaConstants.CHANNEL_REST_URI);
try {
List<PrismObject<NodeType>> allNodes = repositoryService.searchObjects(NodeType.class, null, null, result);
List<PrismObject<NodeType>> matchingNodes = getMatchingNodes(allNodes, remoteName, remoteAddress);
if (matchingNodes.isEmpty()) {
LOGGER.debug("Authenticity cannot be established: No matching nodes for remote name '{}' and remote address '{}'", remoteName, remoteAddress);
} else if (matchingNodes.size() > 1 && !taskManager.isLocalNodeClusteringEnabled()) {
LOGGER.debug("Authenticity cannot be established: More than one matching node for remote name '{}' and " + "remote address '{}' with local-node clustering disabled: {}", remoteName, remoteAddress, matchingNodes);
} else {
assert matchingNodes.size() == 1 || matchingNodes.size() > 1 && taskManager.isLocalNodeClusteringEnabled();
LOGGER.trace("Matching result: Node(s) {} recognized as known (remote host name {} or IP address {} matched).", matchingNodes, remoteName, remoteAddress);
PrismObject<NodeType> actualNode = null;
for (PrismObject<NodeType> matchingNode : matchingNodes) {
ProtectedStringType encryptedSecret = matchingNode.asObjectable().getSecret();
if (encryptedSecret != null) {
String plainSecret;
try {
plainSecret = protector.decryptString(encryptedSecret);
} catch (EncryptionException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't decrypt node secret for {}", e, matchingNode);
continue;
}
if (credentials.equals(plainSecret)) {
LOGGER.debug("Node secret matches for {}", matchingNode);
actualNode = matchingNode;
break;
} else {
LOGGER.debug("Node secret does not match for {}", matchingNode);
}
} else {
LOGGER.debug("No secret known for node {}", matchingNode);
}
}
if (actualNode != null) {
LOGGER.trace("Established authenticity for remote {}", actualNode);
NodeAuthenticationTokenImpl authNtoken = new NodeAuthenticationTokenImpl(actualNode, remoteAddress, Collections.emptyList());
SecurityContextHolder.getContext().setAuthentication(authNtoken);
securityHelper.auditLoginSuccess(actualNode.asObjectable(), connEnv);
return true;
} else {
LOGGER.debug("Authenticity for {} couldn't be established: none of the secrets match", matchingNodes);
}
}
} catch (RuntimeException | SchemaException e) {
LOGGER.error("Unhandled exception when listing nodes");
LoggingUtils.logUnexpectedException(LOGGER, "Unhandled exception when listing nodes", e);
}
securityHelper.auditLoginFailure(remoteName != null ? remoteName : remoteAddress, null, connEnv, "Failed to authenticate node.");
return false;
}
Aggregations