2 * Copyright (c) 2023 PANTHEON.tech s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.callhome.server.ssh;
10 import static java.util.Objects.requireNonNull;
12 import java.security.KeyPair;
13 import java.util.Collection;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
19 * Authorization context for incoming call home sessions.
21 * @see CallHomeSshAuthProvider
23 public interface CallHomeSshAuthSettings {
26 * Unique identifier of a client this auth settings belongs to.
33 * Applies auth settings on {@link ClientSession} instance for subsequent {@link ClientSession#auth()} invocation.
35 * @param session client session object
37 void applyTo(ClientSession session);
40 * Default implementation of {@link CallHomeSshAuthSettings}. Serves SSH authentication by password(s) and/or
43 * @param id unique client identifier
44 * @param username username
45 * @param passwords collection of passwords, optional if keyPairs defined
46 * @param keyPairs collection of {@link KeyPair} objects, optional if passwords defined
48 record DefaultAuthSettings(@NonNull String id, @NonNull String username, @Nullable Collection<String> passwords,
49 @Nullable Collection<KeyPair> keyPairs) implements CallHomeSshAuthSettings {
51 public DefaultAuthSettings {
53 requireNonNull(username);
54 if ((passwords == null || passwords.isEmpty()) && (keyPairs == null || keyPairs.isEmpty())) {
55 throw new IllegalArgumentException("Neither passwords nor keyPairs is defined");
60 public void applyTo(final ClientSession session) {
61 session.setUsername(username);
62 if (keyPairs != null) {
63 for (KeyPair keyPair : keyPairs) {
64 session.addPublicKeyIdentity(keyPair);
67 if (passwords != null) {
68 for (String password : passwords) {
69 session.addPasswordIdentity(password);