Merge "Do not use protobuf serialization for FindPrimary and it's responses"
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / ssh / authentication / LoginPassword.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  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
9 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication;
10
11 import java.io.IOException;
12
13 import org.apache.sshd.ClientSession;
14 import org.apache.sshd.client.future.AuthFuture;
15
16 import ch.ethz.ssh2.Connection;
17
18 /**
19  * Class Providing username/password authentication option to
20  * {@link org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshHandler}
21  */
22 public class LoginPassword extends AuthenticationHandler {
23     private final String username;
24     private final String password;
25
26     public LoginPassword(String username, String password) {
27         this.username = username;
28         this.password = password;
29     }
30
31     @Override
32     public void authenticate(Connection connection) throws IOException {
33         final boolean isAuthenticated = connection.authenticateWithPassword(username, password);
34
35         if (!isAuthenticated) {
36             throw new IOException("Authentication failed.");
37         }
38     }
39
40     @Override
41     public String getUsername() {
42         return username;
43     }
44
45     @Override
46     public AuthFuture authenticate(final ClientSession session) throws IOException {
47         session.addPasswordIdentity(password);
48         return session.auth();
49     }
50 }