Attempt to handle SSH prompt to add known host

This commit is contained in:
Sergio Padrino 2021-08-03 18:49:22 +02:00
parent b957e535cd
commit 8abd7b0708
6 changed files with 84 additions and 13 deletions

View file

@ -1,6 +1,7 @@
import { getKeyForEndpoint } from '../auth'
import { TokenStore } from '../stores'
import { TrampolineCommandHandler } from './trampoline-command'
import { trampolineUIHelper } from './trampoline-ui-helper'
export const askpassTrampolineHandler: TrampolineCommandHandler = async command => {
if (command.parameters.length !== 1) {
@ -8,8 +9,10 @@ export const askpassTrampolineHandler: TrampolineCommandHandler = async command
}
if (command.parameters[0].startsWith('The authenticity of host ')) {
// FIXME: actually ask the user what to do with the host
return 'yes'
const addHost = await trampolineUIHelper.promptAddingSSHHost(
command.parameters[0]
)
return addHost ? 'yes' : 'no'
}
const username = command.environmentVariables.get('DESKTOP_USERNAME')

View file

@ -1,11 +1,6 @@
import { PopupType } from '../../models/popup'
import { Dispatcher } from '../../ui/dispatcher'
enum AddSSHHostPreference {
YES = 'yes',
NO = 'no',
PERMANENT = 'permanent',
}
class TrampolineUIHelper {
// The dispatcher must be set before this helper can do anything
private dispatcher!: Dispatcher
@ -14,13 +9,12 @@ class TrampolineUIHelper {
this.dispatcher = dispatcher
}
public promptAddingSSHHost(message: string): Promise<AddSSHHostPreference> {
return new Promise((resolve, reject) => {
public promptAddingSSHHost(message: string): Promise<boolean> {
return new Promise(resolve => {
this.dispatcher.showPopup({
type: 'input',
title: 'Add SSH Host',
type: PopupType.AddSSHHost,
message,
input: '',
onSubmit: resolve,
})
})
}

View file

@ -74,6 +74,7 @@ export enum PopupType {
WarnLocalChangesBeforeUndo,
WarningBeforeReset,
InvalidatedToken,
AddSSHHost,
}
export type Popup =
@ -296,3 +297,8 @@ export type Popup =
type: PopupType.InvalidatedToken
account: Account
}
| {
type: PopupType.AddSSHHost
message: string
onSubmit: (addHost: boolean) => void
}

View file

@ -146,6 +146,7 @@ import {
MultiCommitOperationKind,
MultiCommitOperationStepKind,
} from '../models/multi-commit-operation'
import { AddSSHHost } from './ssh/add-ssh-host'
const MinuteInMilliseconds = 1000 * 60
const HourInMilliseconds = MinuteInMilliseconds * 60
@ -2084,6 +2085,16 @@ export class App extends React.Component<IAppProps, IAppState> {
/>
)
}
case PopupType.AddSSHHost: {
return (
<AddSSHHost
key="add-ssh-host"
message={popup.message}
onSubmit={popup.onSubmit}
onDismissed={onPopupDismissedFn}
/>
)
}
default:
return assertNever(popup, `Unknown popup type: ${popup}`)
}

View file

@ -82,6 +82,7 @@ import {
ApplicationTheme,
supportsSystemThemeChanges,
} from './lib/application-theme'
import { trampolineUIHelper } from '../lib/trampoline/trampoline-ui-helper'
if (__DEV__) {
installDevGlobals()
@ -301,6 +302,9 @@ document.body.classList.add(`platform-${process.platform}`)
dispatcher.setAppFocusState(remote.getCurrentWindow().isFocused())
// The trampoline UI helper needs a reference to the dispatcher before it's used
trampolineUIHelper.setDispatcher(dispatcher)
ipcRenderer.on('focus', () => {
const { selectedState } = appStore.getState()

View file

@ -0,0 +1,53 @@
import * as React from 'react'
import { Dialog, DialogContent, DialogFooter } from '../dialog'
import { Row } from '../lib/row'
import { OkCancelButtonGroup } from '../dialog/ok-cancel-button-group'
interface IAddSSHHostProps {
readonly message: string
readonly onSubmit: (addHost: boolean) => void
readonly onDismissed: () => void
}
/**
* Dialog prompts the user to add a new SSH host as known.
*/
export class AddSSHHost extends React.Component<IAddSSHHostProps> {
public render() {
return (
<Dialog
id="add-ssh-host"
type="normal"
title="SSH Host"
onSubmit={this.onSubmit}
onDismissed={this.props.onDismissed}
>
<DialogContent>
<Row>{this.props.message}</Row>
</DialogContent>
<DialogFooter>
<OkCancelButtonGroup
okButtonText="Yes"
cancelButtonText="No"
onCancelButtonClick={this.onCancel}
/>
</DialogFooter>
</Dialog>
)
}
private submit(addHost: boolean) {
const { onSubmit, onDismissed } = this.props
onSubmit(addHost)
onDismissed()
}
private onSubmit = () => {
this.submit(true)
}
private onCancel = () => {
this.submit(false)
}
}