Search in sources :

Example 11 with ConnectionEnvironment

use of com.evolveum.midpoint.security.api.ConnectionEnvironment in project midpoint by Evolveum.

the class SecurityQuestionProvider method internalAuthentication.

@Override
protected Authentication internalAuthentication(Authentication authentication, List<ObjectReferenceType> requireAssignment, AuthenticationChannel channel, Class<? extends FocusType> focusType) throws AuthenticationException {
    if (authentication.isAuthenticated() && authentication.getPrincipal() instanceof GuiProfiledPrincipal) {
        return authentication;
    }
    String enteredUsername = (String) authentication.getPrincipal();
    LOGGER.trace("Authenticating username '{}'", enteredUsername);
    ConnectionEnvironment connEnv = createEnvironment(channel);
    try {
        Authentication token;
        if (authentication instanceof SecurityQuestionsAuthenticationToken) {
            Map<String, String> answers = (Map<String, String>) authentication.getCredentials();
            SecurityQuestionsAuthenticationContext authContext = new SecurityQuestionsAuthenticationContext(enteredUsername, focusType, answers, requireAssignment);
            if (channel != null) {
                authContext.setSupportActivationByChannel(channel.isSupportActivationByChannel());
            }
            token = getEvaluator().authenticate(connEnv, authContext);
        } else {
            LOGGER.error("Unsupported authentication {}", authentication);
            throw new AuthenticationServiceException("web.security.provider.unavailable");
        }
        MidPointPrincipal principal = (MidPointPrincipal) token.getPrincipal();
        LOGGER.debug("User '{}' authenticated ({}), authorities: {}", authentication.getPrincipal(), authentication.getClass().getSimpleName(), principal.getAuthorities());
        return token;
    } catch (AuthenticationException e) {
        LOGGER.info("Authentication failed for {}: {}", enteredUsername, e.getMessage());
        throw e;
    }
}
Also used : SecurityQuestionsAuthenticationToken(com.evolveum.midpoint.authentication.impl.module.authentication.token.SecurityQuestionsAuthenticationToken) SecurityQuestionsAuthenticationContext(com.evolveum.midpoint.model.api.context.SecurityQuestionsAuthenticationContext) GuiProfiledPrincipal(com.evolveum.midpoint.model.api.authentication.GuiProfiledPrincipal) AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.springframework.security.core.Authentication) Map(java.util.Map) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) ConnectionEnvironment(com.evolveum.midpoint.security.api.ConnectionEnvironment) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Example 12 with ConnectionEnvironment

use of com.evolveum.midpoint.security.api.ConnectionEnvironment in project midpoint by Evolveum.

the class PasswordCallback method handle.

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    LOGGER.trace("Invoked PasswordCallback with {} callbacks: {}", callbacks.length, callbacks);
    WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
    String username = pc.getIdentifier();
    String wssPasswordType = pc.getType();
    LOGGER.trace("Username: '{}', Password type: {}", username, wssPasswordType);
    try {
        ConnectionEnvironment connEnv = ConnectionEnvironment.create(SchemaConstants.CHANNEL_WEB_SERVICE_URI);
        pc.setPassword(passwordAuthenticationEvaluatorImpl.getAndCheckUserPassword(connEnv, username));
    } catch (Exception e) {
        LOGGER.trace("Exception in password callback: {}: {}", e.getClass().getSimpleName(), e.getMessage(), e);
        throw new PasswordCallbackException("Authentication failed");
    }
}
Also used : WSPasswordCallback(org.apache.wss4j.common.ext.WSPasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) ConnectionEnvironment(com.evolveum.midpoint.security.api.ConnectionEnvironment)

Example 13 with ConnectionEnvironment

use of com.evolveum.midpoint.security.api.ConnectionEnvironment 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;
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) NodeAuthenticationTokenImpl(com.evolveum.midpoint.authentication.impl.module.authentication.NodeAuthenticationTokenImpl) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) ConnectionEnvironment(com.evolveum.midpoint.security.api.ConnectionEnvironment)

Example 14 with ConnectionEnvironment

use of com.evolveum.midpoint.security.api.ConnectionEnvironment in project midpoint by Evolveum.

the class MidPointAbstractAuthenticationProvider method createConnectEnvironment.

protected ConnectionEnvironment createConnectEnvironment(String channel) {
    ConnectionEnvironment env = ConnectionEnvironment.create(channel);
    Authentication actualAuthentication = SecurityContextHolder.getContext().getAuthentication();
    if (actualAuthentication instanceof MidpointAuthentication && ((MidpointAuthentication) actualAuthentication).getSessionId() != null) {
        env.setSessionIdOverride(((MidpointAuthentication) actualAuthentication).getSessionId());
    }
    return env;
}
Also used : ModuleAuthentication(com.evolveum.midpoint.authentication.api.config.ModuleAuthentication) MidpointAuthentication(com.evolveum.midpoint.authentication.api.config.MidpointAuthentication) Authentication(org.springframework.security.core.Authentication) MidpointAuthentication(com.evolveum.midpoint.authentication.api.config.MidpointAuthentication) ConnectionEnvironment(com.evolveum.midpoint.security.api.ConnectionEnvironment)

Example 15 with ConnectionEnvironment

use of com.evolveum.midpoint.security.api.ConnectionEnvironment in project midpoint by Evolveum.

the class PasswordProvider method internalAuthentication.

@Override
protected Authentication internalAuthentication(Authentication authentication, List<ObjectReferenceType> requireAssignment, AuthenticationChannel channel, Class<? extends FocusType> focusType) throws AuthenticationException {
    if (authentication.isAuthenticated() && authentication.getPrincipal() instanceof GuiProfiledPrincipal) {
        return authentication;
    }
    String enteredUsername = (String) authentication.getPrincipal();
    LOGGER.trace("Authenticating username '{}'", enteredUsername);
    ConnectionEnvironment connEnv = createEnvironment(channel);
    try {
        Authentication token;
        if (authentication instanceof UsernamePasswordAuthenticationToken) {
            String enteredPassword = (String) authentication.getCredentials();
            PasswordAuthenticationContext authContext = new PasswordAuthenticationContext(enteredUsername, enteredPassword, focusType, requireAssignment);
            if (channel != null) {
                authContext.setSupportActivationByChannel(channel.isSupportActivationByChannel());
            }
            token = getEvaluator().authenticate(connEnv, authContext);
        } else if (authentication instanceof PreAuthenticatedAuthenticationToken) {
            token = getEvaluator().authenticateUserPreAuthenticated(connEnv, new PreAuthenticationContext(enteredUsername, focusType, requireAssignment));
        } else {
            LOGGER.error("Unsupported authentication {}", authentication);
            throw new AuthenticationServiceException("web.security.provider.unavailable");
        }
        MidPointPrincipal principal = (MidPointPrincipal) token.getPrincipal();
        LOGGER.debug("User '{}' authenticated ({}), authorities: {}", authentication.getPrincipal(), authentication.getClass().getSimpleName(), principal.getAuthorities());
        return token;
    } catch (AuthenticationException e) {
        LOGGER.info("Authentication failed for {}: {}", enteredUsername, e.getMessage());
        throw e;
    }
}
Also used : PasswordAuthenticationContext(com.evolveum.midpoint.model.api.context.PasswordAuthenticationContext) GuiProfiledPrincipal(com.evolveum.midpoint.model.api.authentication.GuiProfiledPrincipal) AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.springframework.security.core.Authentication) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) PreAuthenticationContext(com.evolveum.midpoint.model.api.context.PreAuthenticationContext) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) ConnectionEnvironment(com.evolveum.midpoint.security.api.ConnectionEnvironment) MidPointPrincipal(com.evolveum.midpoint.security.api.MidPointPrincipal)

Aggregations

ConnectionEnvironment (com.evolveum.midpoint.security.api.ConnectionEnvironment)15 Authentication (org.springframework.security.core.Authentication)7 MidPointPrincipal (com.evolveum.midpoint.security.api.MidPointPrincipal)6 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)5 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)5 MidpointAuthentication (com.evolveum.midpoint.authentication.api.config.MidpointAuthentication)3 ModuleAuthentication (com.evolveum.midpoint.authentication.api.config.ModuleAuthentication)3 GuiProfiledPrincipal (com.evolveum.midpoint.model.api.authentication.GuiProfiledPrincipal)3 PasswordAuthenticationContext (com.evolveum.midpoint.model.api.context.PasswordAuthenticationContext)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 PreAuthenticationContext (com.evolveum.midpoint.model.api.context.PreAuthenticationContext)2 Task (com.evolveum.midpoint.task.api.Task)2 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)2 ProtectedStringType (com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType)2 IOException (java.io.IOException)2 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)2 SOAPMessage (javax.xml.soap.SOAPMessage)2 WSPasswordCallback (org.apache.wss4j.common.ext.WSPasswordCallback)2