All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sshd.client.auth.keyboard.UserInteraction Maven / Gradle / Ivy

There is a newer version: 2.12.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.sshd.client.auth.keyboard;

import java.util.List;

import org.apache.sshd.client.session.ClientSession;

/**
 * Interface used by the ssh client to communicate with the end user.
 *
 * @author Apache MINA SSHD Project
 * @see RFC 4256
 */
public interface UserInteraction {
    /**
     * A useful "placeholder" that indicates that no interaction is expected.
     * Note: throws {@link IllegalStateException} is any of the interaction
     * methods is called
     */
    UserInteraction NONE = new UserInteraction() {
        @Override
        public boolean isInteractionAllowed(ClientSession session) {
            return false;
        }

        @Override
        public String[] interactive(ClientSession session, String name, String instruction, String lang, String[] prompt, boolean[] echo) {
            throw new IllegalStateException("interactive(" + session + ")[" + name + "] unexpected call");
        }

        @Override
        public String getUpdatedPassword(ClientSession session, String prompt, String lang) {
            throw new IllegalStateException("getUpdatedPassword(" + session + ")[" + prompt + "] unexpected call");
        }

        @Override
        public String toString() {
            return "NONE";
        }
    };

    /**
     *
     * @param session The {@link ClientSession}
     * @return {@code true} if user interaction allowed for this session (default)
     */
    default boolean isInteractionAllowed(ClientSession session) {
        return true;
    }

    /**
     * Called if the server sent any extra information beyond the standard
     * version line
     *
     * @param session The {@link ClientSession} through which this information
     * was received
     * @param lines The sent extra lines - without the server version
     * @see RFC 4253 - section 4.2
     */
    default void serverVersionInfo(ClientSession session, List lines) {
        // do nothing
    }

    /**
     * Displays the welcome banner to the user.
     *
     * @param session The {@link ClientSession} through which the banner was received
     * @param banner  The welcome banner
     * @param lang    The banner language code - may be empty
     */
    default void welcome(ClientSession session, String banner, String lang) {
        // do nothing
    }

    /**
     * Invoked when "keyboard-interactive" authentication mechanism
     * is used in order to provide responses for the server's challenges
     * (a.k.a. prompts)
     *
     * @param session     The {@link ClientSession} through which the request was received
     * @param name        The interaction name (may be empty)
     * @param instruction The instruction (may be empty)
     * @param lang        The language for the data (may be empty)
     * @param prompt      The prompts to be displayed (may be empty)
     * @param echo        For each prompt whether to echo the user's response
     * @return The replies - Note: the protocol states that the number
     * of replies should be exactly the same as the number of prompts,
     * however we do not enforce it since it is defined as the server's
     * job to check and manage this violation.
     */
    String[] interactive(ClientSession session, String name, String instruction, String lang, String[] prompt, boolean[] echo);

    /**
     * Invoked when the server returns an {@code SSH_MSG_USERAUTH_PASSWD_CHANGEREQ}
     * response indicating that the password should be changed - e.g., expired or
     * not strong enough (as per the server's policy).
     *
     * @param session The {@link ClientSession} through which the request was received
     * @param prompt The server's prompt (may be empty)
     * @param lang The prompt's language (may be empty)
     * @return The password to use - if {@code null}/empty then no updated
     * password was provided - thus failing the authentication via passwords
     * (Note: authentication might still succeed via some other means -
     * be it other passwords, public keys, etc...)
     */
    String getUpdatedPassword(ClientSession session, String prompt, String lang);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy