forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinentry.js
113 lines (100 loc) · 3.18 KB
/
pinentry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// @flow
import logger from '../logger'
import * as ConfigGen from '../actions/config-gen'
import * as PinentryGen from '../actions/pinentry-gen'
import * as Saga from '../util/saga'
import * as I from 'immutable'
import * as RPCTypes from '../constants/types/rpc-gen'
import engine from '../engine'
// We keep track of sessionID to response objects since this is initiated by the daemon
type IncomingErrorCallback = (?{code?: number, desc?: string}) => void
const sessionIDToResponse: {
[key: string]: {
error: IncomingErrorCallback,
result: ({+passphrase: string, +storeSecret: boolean}) => void,
},
} = {}
function setupEngineListeners() {
engine().actionOnConnect('registerSecretUI', () => {
RPCTypes.delegateUiCtlRegisterSecretUIRpcPromise()
.then(response => {
logger.info('Registered secret ui')
})
.catch(error => {
logger.warn('error in registering secret ui: ', error)
})
})
engine().setCustomResponseIncomingCallMap({
'keybase.1.secretUi.getPassphrase': (param, response) => {
logger.info('Asked for passphrase')
const {prompt, submitLabel, cancelLabel, windowTitle, retryLabel, features, type} = param.pinentry
const {sessionID} = param
// Stash response
sessionIDToResponse[String(sessionID)] = response
return Saga.put(
PinentryGen.createNewPinentry({
cancelLabel,
prompt,
retryLabel,
sessionID,
showTyping: features.showTyping,
submitLabel,
type,
windowTitle,
})
)
},
})
}
function _onNewPinentry(action: PinentryGen.NewPinentryPayload) {
return Saga.put(
PinentryGen.createReplaceEntity({
entities: I.Map([[action.payload.sessionID, action.payload]]),
keyPath: ['sessionIDToPinentry'],
})
)
}
function _onSubmit(action: PinentryGen.OnSubmitPayload) {
const {sessionID, passphrase} = action.payload
_respond(sessionID, {passphrase})
return Saga.put(
PinentryGen.createDeleteEntity({
ids: [action.payload.sessionID],
keyPath: ['sessionIDToPinentry'],
})
)
}
function _onCancel(action: PinentryGen.OnCancelPayload) {
const {sessionID} = action.payload
_respond(sessionID, null, {
code: RPCTypes.constantsStatusCode.scinputcanceled,
desc: 'Input canceled',
})
return Saga.put(
PinentryGen.createDeleteEntity({
ids: [action.payload.sessionID],
keyPath: ['sessionIDToPinentry'],
})
)
}
function _respond(sessionID: number, result: any, err: ?any): void {
const sessionKey = String(sessionID)
const response = sessionIDToResponse[sessionKey]
if (response == null) {
logger.info('lost response reference')
return
}
if (err != null) {
response.error(err)
} else {
response.result(result)
}
delete sessionIDToResponse[sessionKey]
}
function* pinentrySaga(): Saga.SagaGenerator<any, any> {
yield Saga.safeTakeEveryPure(PinentryGen.onSubmit, _onSubmit)
yield Saga.safeTakeEveryPure(PinentryGen.onCancel, _onCancel)
yield Saga.safeTakeEveryPure(PinentryGen.newPinentry, _onNewPinentry)
yield Saga.actionToAction(ConfigGen.setupEngineListeners, setupEngineListeners)
}
export default pinentrySaga