-2

What are some examples of "authHandler" in the "connect" client method of the ssh2 npm package?

im namely looking to re-order the methods and/or remove some.

  • By no means did I mean to offend you as an author. I was merely stating an observation. I mean, it will be easy to understand in your eyes since you've had most exposure to the question/issue. However, when I read question I don't believe its clear (my opinion). However, others may not. The documentation shows examples of `authHandler`. https://www.npmjs.com/package/ssh2#user-content-client-methods – Riddell Jun 11 '21 at 17:07
  • 2
    @Riddell not to worry, i take no offense at constructive criticism - only those that so casually disrespect another's effort without putting in any of their own. i have nothing but appreciation for the time you've taken here. as for the cited examples, those appear to only be an example of the object? as a fairly new js developer, i (and some others ive seen) need an example of the greater usage of the method in order to understand it. – Antioch Technologies Jun 11 '21 at 17:24

2 Answers2

2

Using the documentation, I'm going to try and provide a basic example which include usage of authHandler as mentioned in your question.

// Require Client Class from ssh2
const { Client } = require('ssh2');

// Create instance of Client (aka connection)
const conn = new Client();

// Create our ready event that's called after we connect
conn.on('ready', () => {
    console.log('Client :: ready');
});

// Connect with a config object passed in the parameters
conn.connect({
    host: '192.168.100.100',
    port: 22, // SSH

    // Authentication Handler
    authHandler: function (methodsLeft, partialSuccess, callback) {

        // Code e.g. get credentials from database
    
        // Once your logic is complete invoke the callback
        // http://npmjs.com/package/ssh2#client-examples
        callback({
            type: 'password',
            username: 'foo',
            password: 'bar',
        });
    }
});

The above should provide a working example if the credentials are changed. The code can be made slightly cleaner and the calls for conn class can be chained like so:

conn.on('ready', () => {
    console.log('Client :: ready');
}).connect({ // Chained
    host: '192.168.100.100',
    port: 22, // SSH

    // Authentication Handler
    authHandler: function (methodsLeft, partialSuccess, callback) {

        // Code e.g. get credentials from database
    
        // Once your logic is complete invoke the callback
        // http://npmjs.com/package/ssh2#client-examples
        callback({
            type: 'password',
            username: 'foo',
            password: 'bar',
        });
    }
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
Riddell
  • 1,429
  • 11
  • 22
1

Thanks for this @Riddell! Just adding to that answer. Using this approach, I was still getting an error that said 'Invalid username'.

Thing is, even when using authHandler() and returning the credentials in the callback, you still need to mention the 'username' in the base config object as well like this:

conn.on('ready', () => {
    console.log('Client :: ready');
}).connect({
    host: '192.168.100.100',
    port: 22,
    username: 'foo',

    authHandler: function (methodsLeft, partialSuccess, callback) {
        callback({
            type: 'password',
            username: 'foo',
            password: 'bar',
        });
    }
});

Also, if anyone is getting an error with the Auth Type when using this approach, you need to upgrade your ssh2 package version. Faced this issue when using ssh2@0.8.9 and got it resolved after upgrading to ssh2@1.5.0

yohaansunnie
  • 35
  • 1
  • 8