Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option which will try "keyboard-interactive" user authentication #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ Options are an object with following properties:
* `bastionHost` (optional): You can specify a bastion host if you want
* `passphrase` (optional): You can specify the passphrase when you have an encrypted private key. If you don't specify the passphrase and you use an encrypted private key, you get prompted in the command line.
* `noReadline` (optional): Don't prompt for private key passphrases using readline (eg if this is not run in an interactive session)
* `tryKeyboardInteractive` (optional): Try keyboard-interactive user authentication if primary authentication method fails, and prompts provided by SSH server (defaults to `false`)


#### `connection.executeCommand(command: string): Promise<void>`

Expand Down
55 changes: 48 additions & 7 deletions src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ interface Options {
endHost: string
agentSocket?: string,
skipAutoPrivateKey?: boolean
noReadline?: boolean
noReadline?: boolean,
tryKeyboardInteractive?:boolean
}

interface ForwardingOptions {
Expand All @@ -59,15 +60,18 @@ class SSHConnection {
this.options.endPort = 22
}
if (!options.privateKey && !options.agentForward && !options.skipAutoPrivateKey) {
const defaultFilePath = path.join(os.homedir(), '.ssh', 'id_rsa' )
const defaultFilePath = path.join(os.homedir(), '.ssh', 'id_rsa')
if (fs.existsSync(defaultFilePath)) {
this.options.privateKey = fs.readFileSync(defaultFilePath)
}
}
if (!options.tryKeyboardInteractive) {
this.options.tryKeyboardInteractive = false
}
}

public async shutdown() {
this.debug("Shutdown connections")
this.debug('Shutdown connections')
for (const connection of this.connections) {
connection.removeAllListeners()
connection.end()
Expand All @@ -82,7 +86,7 @@ class SSHConnection {

public async tty() {
const connection = await this.establish()
this.debug("Opening tty")
this.debug('Opening tty')
await this.shell(connection)
}

Expand Down Expand Up @@ -153,7 +157,8 @@ class SSHConnection {
port: this.options.endPort,
username: this.options.username,
password: this.options.password,
privateKey: this.options.privateKey
privateKey: this.options.privateKey,
tryKeyboard: this.options.tryKeyboardInteractive
}
if (this.options.agentForward) {
options['agentForward'] = true
Expand Down Expand Up @@ -181,7 +186,7 @@ class SSHConnection {
// in fact they always contain a `encryption` header, so we can't do a simple check
options['passphrase'] = this.options.passphrase
const looksEncrypted: boolean = this.options.privateKey ? this.options.privateKey.toString().toLowerCase().includes('encrypted') : false
if (looksEncrypted && !options['passphrase'] && !this.options.noReadline ) {
if (looksEncrypted && !options['passphrase'] && !this.options.noReadline) {
options['passphrase'] = await this.getPassphrase()
}
connection.on('ready', () => {
Expand All @@ -192,13 +197,17 @@ class SSHConnection {
connection.on('error', (error) => {
reject(error)
})

connection.on('keyboard-interactive', (_name:string, _instructions:string, _instructionsLang:string, prompts:{prompt:string}[], finish):void => {
this.presentAllPrompts(prompts, finish)
})

try {
connection.connect(options)
} catch (error) {
reject(error)
}


})
}

Expand All @@ -214,6 +223,38 @@ class SSHConnection {
})
}

private presentAllPrompts(prompts:{prompt:string}[], finish) {
// prompt answers collected so far
const promptAnswers:string[] = []
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// when readline closed, we are finished with prompts
rl.on('close', () => {
// send back prompt answers
finish(promptAnswers)
})
// function to present prompt at index specified
const presentPrompt = (prompts, promptIndex):void => {
rl.question(prompts[promptIndex].prompt, (answer:string) => {
// add answer to prompt array
promptAnswers.push(answer)
const nextPromptNum = promptIndex + 1
// if no next prompt, close read line
if (nextPromptNum >= prompts.length) {
rl.close()
} else {
presentPrompt(prompts, nextPromptNum)
}
})
}
// if there are prompts, present prompt at index 0
if (prompts.length > 0) {
presentPrompt(prompts, 0)
}
}

async forward(options: ForwardingOptions) {
const connection = await this.establish()
return new Promise((resolve, reject) => {
Expand Down