Add transport-ssh
[netconf.git] / transport / transport-ssh / src / main / java / org / opendaylight / netconf / transport / ssh / UserAuthSessionListener.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech s.r.o. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.transport.ssh;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.IOException;
13 import java.util.Map;
14 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
15 import org.opendaylight.netconf.shaded.sshd.common.session.Session;
16 import org.opendaylight.netconf.shaded.sshd.common.session.SessionListener;
17
18 /**
19  * Session listener responsible for session authentication for both client and server.
20  *
21  * <P>Triggers authentication flow when after server key is accepted by client,
22  * invokes associated handler on authentication success/failure.
23  */
24 public class UserAuthSessionListener implements SessionListener {
25
26     final Map<Long, AuthHandler> sessionAuthHandlers;
27     final Map<Long, Session> sessions;
28
29     public UserAuthSessionListener(final Map<Long, AuthHandler> sessionAuthHandlers,
30             final Map<Long, Session> sessions) {
31         this.sessionAuthHandlers = sessionAuthHandlers;
32         this.sessions = sessions;
33     }
34
35     @Override
36     public void sessionCreated(Session session) {
37         sessions.put(session.getIoSession().getId(), session);
38     }
39
40     @Override
41     public void sessionException(final Session session, Throwable throwable) {
42         deleteSession(session);
43     }
44
45     @Override
46     public void sessionDisconnect(Session session, int reason, String msg, String language, boolean initiator) {
47         deleteSession(session);
48     }
49
50     @Override
51     public void sessionClosed(Session session) {
52         deleteSession(session);
53     }
54
55     private void deleteSession(final Session session) {
56         final Long id = idOf(session);
57         sessions.remove(id);
58         final var handler = sessionAuthHandlers.remove(id);
59         if (handler != null) {
60             handler.onFailure().run();
61         }
62     }
63
64     @Override
65     public void sessionEvent(Session session, Event event) {
66         if (Event.KeyEstablished == event && session instanceof ClientSession clientSession) {
67             // server key is accepted, trigger authentication flow
68             try {
69                 clientSession.auth().addListener(future -> {
70                     if (!future.isSuccess()) {
71                         deleteSession(session);
72                     }
73                 });
74             } catch (IOException e) {
75                 sessionException(session, e);
76             }
77         }
78         if (Event.Authenticated == event) {
79             final var handler = sessionAuthHandlers.remove(idOf(session));
80             if (handler != null) {
81                 handler.onSuccess().run();
82             }
83         }
84     }
85
86     private static Long idOf(final Session session) {
87         return session.getIoSession().getId();
88     }
89
90     public record AuthHandler(Runnable onSuccess, Runnable onFailure) {
91         public AuthHandler {
92             requireNonNull(onSuccess);
93             requireNonNull(onFailure);
94         }
95     }
96 }