@coderoftime/fints 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +152 -0
- package/LICENSE +28 -0
- package/README.md +246 -2
- package/dist/client.d.ts +48 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +199 -0
- package/dist/client.js.map +1 -0
- package/dist/core/account.d.ts +168 -0
- package/dist/core/account.d.ts.map +1 -0
- package/dist/core/account.js +245 -0
- package/dist/core/account.js.map +1 -0
- package/dist/core/amount.d.ts +63 -0
- package/dist/core/amount.d.ts.map +1 -0
- package/dist/core/amount.js +192 -0
- package/dist/core/amount.js.map +1 -0
- package/dist/core/balance.d.ts +55 -0
- package/dist/core/balance.d.ts.map +1 -0
- package/dist/core/balance.js +54 -0
- package/dist/core/balance.js.map +1 -0
- package/dist/core/bank-info.d.ts +49 -0
- package/dist/core/bank-info.d.ts.map +1 -0
- package/dist/core/bank-info.js +52 -0
- package/dist/core/bank-info.js.map +1 -0
- package/dist/core/client.d.ts +161 -0
- package/dist/core/client.d.ts.map +1 -0
- package/dist/core/client.js +2 -0
- package/dist/core/client.js.map +1 -0
- package/dist/core/config.d.ts +80 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +153 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/errors.d.ts +153 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +247 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/feedback.d.ts +72 -0
- package/dist/core/feedback.d.ts.map +1 -0
- package/dist/core/feedback.js +127 -0
- package/dist/core/feedback.js.map +1 -0
- package/dist/core/transaction.d.ts +103 -0
- package/dist/core/transaction.d.ts.map +1 -0
- package/dist/core/transaction.js +96 -0
- package/dist/core/transaction.js.map +1 -0
- package/dist/core/transport.d.ts +64 -0
- package/dist/core/transport.d.ts.map +1 -0
- package/dist/core/transport.js +149 -0
- package/dist/core/transport.js.map +1 -0
- package/dist/formats/camt.d.ts +32 -0
- package/dist/formats/camt.d.ts.map +1 -0
- package/dist/formats/camt.js +242 -0
- package/dist/formats/camt.js.map +1 -0
- package/dist/formats/mt940.d.ts +54 -0
- package/dist/formats/mt940.d.ts.map +1 -0
- package/dist/formats/mt940.js +376 -0
- package/dist/formats/mt940.js.map +1 -0
- package/dist/formats/readers.d.ts +15 -0
- package/dist/formats/readers.d.ts.map +1 -0
- package/dist/formats/readers.js +42 -0
- package/dist/formats/readers.js.map +1 -0
- package/dist/formats/record-budget.d.ts +7 -0
- package/dist/formats/record-budget.d.ts.map +1 -0
- package/dist/formats/record-budget.js +17 -0
- package/dist/formats/record-budget.js.map +1 -0
- package/dist/formats/sepa-purpose.d.ts +28 -0
- package/dist/formats/sepa-purpose.d.ts.map +1 -0
- package/dist/formats/sepa-purpose.js +89 -0
- package/dist/formats/sepa-purpose.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/session/app-approval.d.ts +66 -0
- package/dist/session/app-approval.d.ts.map +1 -0
- package/dist/session/app-approval.js +226 -0
- package/dist/session/app-approval.js.map +1 -0
- package/dist/session/dialog-runner.d.ts +31 -0
- package/dist/session/dialog-runner.d.ts.map +1 -0
- package/dist/session/dialog-runner.js +111 -0
- package/dist/session/dialog-runner.js.map +1 -0
- package/dist/session/login-guard.d.ts +78 -0
- package/dist/session/login-guard.d.ts.map +1 -0
- package/dist/session/login-guard.js +181 -0
- package/dist/session/login-guard.js.map +1 -0
- package/dist/session/sepa-accounts.d.ts +40 -0
- package/dist/session/sepa-accounts.d.ts.map +1 -0
- package/dist/session/sepa-accounts.js +112 -0
- package/dist/session/sepa-accounts.js.map +1 -0
- package/dist/session/session-setup.d.ts +80 -0
- package/dist/session/session-setup.d.ts.map +1 -0
- package/dist/session/session-setup.js +182 -0
- package/dist/session/session-setup.js.map +1 -0
- package/dist/session/session-state.d.ts +50 -0
- package/dist/session/session-state.d.ts.map +1 -0
- package/dist/session/session-state.js +182 -0
- package/dist/session/session-state.js.map +1 -0
- package/dist/session/signed-dialog.d.ts +77 -0
- package/dist/session/signed-dialog.d.ts.map +1 -0
- package/dist/session/signed-dialog.js +170 -0
- package/dist/session/signed-dialog.js.map +1 -0
- package/dist/session/tan-selection.d.ts +96 -0
- package/dist/session/tan-selection.d.ts.map +1 -0
- package/dist/session/tan-selection.js +189 -0
- package/dist/session/tan-selection.js.map +1 -0
- package/dist/session/user-prompts.d.ts +65 -0
- package/dist/session/user-prompts.d.ts.map +1 -0
- package/dist/session/user-prompts.js +184 -0
- package/dist/session/user-prompts.js.map +1 -0
- package/dist/tan/challenge.d.ts +135 -0
- package/dist/tan/challenge.d.ts.map +1 -0
- package/dist/tan/challenge.js +184 -0
- package/dist/tan/challenge.js.map +1 -0
- package/dist/tan/flicker.d.ts +42 -0
- package/dist/tan/flicker.d.ts.map +1 -0
- package/dist/tan/flicker.js +296 -0
- package/dist/tan/flicker.js.map +1 -0
- package/dist/tan/tan-image.d.ts +22 -0
- package/dist/tan/tan-image.d.ts.map +1 -0
- package/dist/tan/tan-image.js +43 -0
- package/dist/tan/tan-image.js.map +1 -0
- package/dist/tan/tan-models.d.ts +149 -0
- package/dist/tan/tan-models.d.ts.map +1 -0
- package/dist/tan/tan-models.js +153 -0
- package/dist/tan/tan-models.js.map +1 -0
- package/dist/util/async.d.ts +22 -0
- package/dist/util/async.d.ts.map +1 -0
- package/dist/util/async.js +45 -0
- package/dist/util/async.js.map +1 -0
- package/dist/util/bytes.d.ts +22 -0
- package/dist/util/bytes.d.ts.map +1 -0
- package/dist/util/bytes.js +128 -0
- package/dist/util/bytes.js.map +1 -0
- package/dist/util/call-context.d.ts +12 -0
- package/dist/util/call-context.d.ts.map +1 -0
- package/dist/util/call-context.js +30 -0
- package/dist/util/call-context.js.map +1 -0
- package/dist/util/dates.d.ts +55 -0
- package/dist/util/dates.d.ts.map +1 -0
- package/dist/util/dates.js +189 -0
- package/dist/util/dates.js.map +1 -0
- package/dist/util/errors.d.ts +17 -0
- package/dist/util/errors.d.ts.map +1 -0
- package/dist/util/errors.js +24 -0
- package/dist/util/errors.js.map +1 -0
- package/dist/util/immutable-map.d.ts +16 -0
- package/dist/util/immutable-map.d.ts.map +1 -0
- package/dist/util/immutable-map.js +38 -0
- package/dist/util/immutable-map.js.map +1 -0
- package/dist/util/json.d.ts +17 -0
- package/dist/util/json.d.ts.map +1 -0
- package/dist/util/json.js +62 -0
- package/dist/util/json.js.map +1 -0
- package/dist/util/text.d.ts +37 -0
- package/dist/util/text.d.ts.map +1 -0
- package/dist/util/text.js +107 -0
- package/dist/util/text.js.map +1 -0
- package/dist/util/xml.d.ts +36 -0
- package/dist/util/xml.d.ts.map +1 -0
- package/dist/util/xml.js +194 -0
- package/dist/util/xml.js.map +1 -0
- package/dist/v3/account-extension.d.ts +25 -0
- package/dist/v3/account-extension.d.ts.map +1 -0
- package/dist/v3/account-extension.js +173 -0
- package/dist/v3/account-extension.js.map +1 -0
- package/dist/v3/bank-connection.d.ts +34 -0
- package/dist/v3/bank-connection.d.ts.map +1 -0
- package/dist/v3/bank-connection.js +58 -0
- package/dist/v3/bank-connection.js.map +1 -0
- package/dist/v3/dialog.d.ts +112 -0
- package/dist/v3/dialog.d.ts.map +1 -0
- package/dist/v3/dialog.js +241 -0
- package/dist/v3/dialog.js.map +1 -0
- package/dist/v3/message.d.ts +85 -0
- package/dist/v3/message.d.ts.map +1 -0
- package/dist/v3/message.js +303 -0
- package/dist/v3/message.js.map +1 -0
- package/dist/v3/parameter-store.d.ts +75 -0
- package/dist/v3/parameter-store.d.ts.map +1 -0
- package/dist/v3/parameter-store.js +143 -0
- package/dist/v3/parameter-store.js.map +1 -0
- package/dist/v3/parameters.d.ts +105 -0
- package/dist/v3/parameters.d.ts.map +1 -0
- package/dist/v3/parameters.js +450 -0
- package/dist/v3/parameters.js.map +1 -0
- package/dist/v3/protocol.d.ts +61 -0
- package/dist/v3/protocol.d.ts.map +1 -0
- package/dist/v3/protocol.js +113 -0
- package/dist/v3/protocol.js.map +1 -0
- package/dist/v3/queries.d.ts +51 -0
- package/dist/v3/queries.d.ts.map +1 -0
- package/dist/v3/queries.js +120 -0
- package/dist/v3/queries.js.map +1 -0
- package/dist/v3/segments.d.ts +106 -0
- package/dist/v3/segments.d.ts.map +1 -0
- package/dist/v3/segments.js +325 -0
- package/dist/v3/segments.js.map +1 -0
- package/dist/v3/syntax.d.ts +127 -0
- package/dist/v3/syntax.d.ts.map +1 -0
- package/dist/v3/syntax.js +367 -0
- package/dist/v3/syntax.js.map +1 -0
- package/package.json +67 -4
- package/src/client.ts +252 -0
- package/src/core/account.ts +323 -0
- package/src/core/amount.ts +211 -0
- package/src/core/balance.ts +85 -0
- package/src/core/bank-info.ts +66 -0
- package/src/core/client.ts +186 -0
- package/src/core/config.ts +182 -0
- package/src/core/errors.ts +277 -0
- package/src/core/feedback.ts +140 -0
- package/src/core/transaction.ts +161 -0
- package/src/core/transport.ts +189 -0
- package/src/formats/camt.ts +266 -0
- package/src/formats/mt940.ts +419 -0
- package/src/formats/readers.ts +49 -0
- package/src/formats/record-budget.ts +18 -0
- package/src/formats/sepa-purpose.ts +94 -0
- package/src/index.ts +70 -0
- package/src/session/app-approval.ts +278 -0
- package/src/session/dialog-runner.ts +122 -0
- package/src/session/login-guard.ts +221 -0
- package/src/session/sepa-accounts.ts +115 -0
- package/src/session/session-setup.ts +184 -0
- package/src/session/session-state.ts +204 -0
- package/src/session/signed-dialog.ts +192 -0
- package/src/session/tan-selection.ts +203 -0
- package/src/session/user-prompts.ts +193 -0
- package/src/tan/challenge.ts +261 -0
- package/src/tan/flicker.ts +308 -0
- package/src/tan/tan-image.ts +38 -0
- package/src/tan/tan-models.ts +252 -0
- package/src/util/async.ts +48 -0
- package/src/util/bytes.ts +129 -0
- package/src/util/call-context.ts +41 -0
- package/src/util/dates.ts +210 -0
- package/src/util/errors.ts +27 -0
- package/src/util/immutable-map.ts +47 -0
- package/src/util/json.ts +67 -0
- package/src/util/text.ts +112 -0
- package/src/util/xml.ts +183 -0
- package/src/v3/account-extension.ts +185 -0
- package/src/v3/bank-connection.ts +67 -0
- package/src/v3/dialog.ts +283 -0
- package/src/v3/message.ts +373 -0
- package/src/v3/parameter-store.ts +160 -0
- package/src/v3/parameters.ts +524 -0
- package/src/v3/protocol.ts +148 -0
- package/src/v3/queries.ts +190 -0
- package/src/v3/segments.ts +374 -0
- package/src/v3/syntax.ts +369 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { requireLatin1 } from '../core/config.js';
|
|
2
|
+
import {
|
|
3
|
+
bankErrorFor,
|
|
4
|
+
FinTsLoginDataChangedError,
|
|
5
|
+
FinTsProtocolError,
|
|
6
|
+
FinTsTanCancelledError,
|
|
7
|
+
} from '../core/errors.js';
|
|
8
|
+
import { allowedTanMethodsIn, FeedbackCodes, hasCode, hasErrors, userWarnings } from '../core/feedback.js';
|
|
9
|
+
import type { TanMethod } from '../tan/tan-models.js';
|
|
10
|
+
import { invalidArgument } from '../util/errors.js';
|
|
11
|
+
import type { Approval, FinTs3Dialog } from '../v3/dialog.js';
|
|
12
|
+
import { IncomingMessage3 } from '../v3/message.js';
|
|
13
|
+
import type { ParameterStore } from '../v3/parameter-store.js';
|
|
14
|
+
import type { SegmentSpec } from '../v3/syntax.js';
|
|
15
|
+
import { appApprovalWait } from './app-approval.js';
|
|
16
|
+
import type { LoginGuard, RequestKind } from './login-guard.js';
|
|
17
|
+
import type { SessionState } from './session-state.js';
|
|
18
|
+
import type { UserPrompts } from './user-prompts.js';
|
|
19
|
+
|
|
20
|
+
/** The parts of the session that every `SignedDialog` reports to. */
|
|
21
|
+
export interface SessionParts {
|
|
22
|
+
readonly parameters: ParameterStore;
|
|
23
|
+
readonly state: SessionState;
|
|
24
|
+
readonly prompts: UserPrompts;
|
|
25
|
+
readonly guard: LoginGuard;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A dialog signed with the PIN, which settles each answer before it hands it out: it keeps the parameter data of the
|
|
30
|
+
* dialog's opening, passes the notices on, has the `LoginGuard` judge the answer and throws its errors, and completes
|
|
31
|
+
* the approval the bank asks for. The session sends every message signed with the PIN through it, so that the guard
|
|
32
|
+
* judges every answer, that to the dialog's end included.
|
|
33
|
+
*/
|
|
34
|
+
export class SignedDialog {
|
|
35
|
+
constructor(
|
|
36
|
+
private readonly dialog: FinTs3Dialog,
|
|
37
|
+
private readonly session: SessionParts,
|
|
38
|
+
) {}
|
|
39
|
+
|
|
40
|
+
/** Whether the bank knows this dialog and it has not ended. */
|
|
41
|
+
get isActive(): boolean {
|
|
42
|
+
return this.dialog.isActive;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Logs in, which opens the dialog; with `synchronize` the bank also assigns a customer system ID. */
|
|
46
|
+
async login(synchronize = false): Promise<IncomingMessage3> {
|
|
47
|
+
return this.settle(await this.dialog.start(synchronize), 'login');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Logs in, asking strong authentication for `order`, then sends `order` in a message of its own, as for listing the
|
|
52
|
+
* TAN media while no medium is chosen yet (`FinTs3Dialog.startWith`); returns both answers.
|
|
53
|
+
*/
|
|
54
|
+
async loginWith(order: SegmentSpec): Promise<IncomingMessage3[]> {
|
|
55
|
+
const start = this.dialog.startWith(order);
|
|
56
|
+
const login = await this.settle(await start.login(), 'login');
|
|
57
|
+
return [login, await this.settle(await start.order(), 'order')];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Sends `order`, the first page of a query, and completes the approval the bank asks for. */
|
|
61
|
+
async send(order: SegmentSpec): Promise<IncomingMessage3> {
|
|
62
|
+
return this.settle(await this.dialog.send(order), 'order');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Sends `order` for a further page of a query (Formals B.6.3), whose answer must not ask for an approval: the client
|
|
67
|
+
* completes that of the first page only, so that a bank that asks anew for every page cannot prompt the user once per
|
|
68
|
+
* page. Throws `FinTsProtocolError` if it asks for one.
|
|
69
|
+
*/
|
|
70
|
+
async sendFurtherPage(order: SegmentSpec): Promise<IncomingMessage3> {
|
|
71
|
+
const response = await this.dialog.send(order);
|
|
72
|
+
this.receive(response, 'order', false);
|
|
73
|
+
if (this.dialog.approvalAskedIn(response) !== undefined) {
|
|
74
|
+
throw new FinTsProtocolError('The bank asks for an approval of a further page of the same query');
|
|
75
|
+
}
|
|
76
|
+
return response;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Ends the dialog at the bank; does nothing if it is not active. Of the answer only a rejected PIN counts, which the
|
|
81
|
+
* guard judges: the dialog is over either way.
|
|
82
|
+
*/
|
|
83
|
+
async end(): Promise<void> {
|
|
84
|
+
const response = await this.dialog.end();
|
|
85
|
+
if (response !== undefined && hasErrors(response.feedback)) {
|
|
86
|
+
this.session.guard.judge(bankErrorFor(response.feedback), 'order', this.dialog.method);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Receives `response`, the answer to a request of `kind`: keeps its parameter data where it answers the `opening` of
|
|
92
|
+
* the dialog, the login or its approval, even alongside errors, since the remedy for a refused login may need them;
|
|
93
|
+
* passes its notices on; and has the guard judge it, then throws its errors, as the refusal of the login where they
|
|
94
|
+
* amount to one.
|
|
95
|
+
*/
|
|
96
|
+
private receive(response: IncomingMessage3, kind: RequestKind, opening: boolean): void {
|
|
97
|
+
if (opening) this.session.parameters.keep(response);
|
|
98
|
+
this.readNotices(response);
|
|
99
|
+
const error = hasErrors(response.feedback) ? bankErrorFor(response.feedback, kind === 'approval') : undefined;
|
|
100
|
+
const refusal = this.session.guard.judge(error, kind, this.dialog.method);
|
|
101
|
+
// Every further message of a refused login's dialog would carry the refused PIN.
|
|
102
|
+
if (refusal !== undefined) this.dialog.discard();
|
|
103
|
+
if (error !== undefined) throw refusal ?? error;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Where `response` answers a login, a request of `kind` other than `order`, or its approval with new login data: ends
|
|
108
|
+
* the dialog, stops the client from logging in with the old ones, and fails the call with
|
|
109
|
+
* `FinTsLoginDataChangedError` (Rückmeldungscodes C.6). It runs before any approval, which would cost the user a TAN
|
|
110
|
+
* for a session that ends anyway. New login data next to an error are the login's refusal already
|
|
111
|
+
* (`loginRefusalFor`).
|
|
112
|
+
*/
|
|
113
|
+
private async retireOldLoginData(response: IncomingMessage3, kind: RequestKind): Promise<void> {
|
|
114
|
+
if (kind === 'order' || !hasCode(response.feedback, FeedbackCodes.newLoginData)) return;
|
|
115
|
+
await this.end();
|
|
116
|
+
const error = new FinTsLoginDataChangedError(response.feedback);
|
|
117
|
+
throw this.session.guard.judge(error, kind, this.dialog.method) ?? error;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Receives `response`, the answer to a request of `kind`, then completes the approval it asks for; returns it joined
|
|
122
|
+
* with the response that completes the approval.
|
|
123
|
+
*
|
|
124
|
+
* Throws `FinTsProtocolError` if that response asks for an approval once more: the bank has not executed the message
|
|
125
|
+
* then either, and read as done it would report an empty result.
|
|
126
|
+
*/
|
|
127
|
+
private async settle(response: IncomingMessage3, kind: RequestKind): Promise<IncomingMessage3> {
|
|
128
|
+
const opening = kind !== 'order';
|
|
129
|
+
this.receive(response, kind, opening);
|
|
130
|
+
await this.retireOldLoginData(response, kind);
|
|
131
|
+
const approval = this.dialog.approvalAskedIn(response);
|
|
132
|
+
if (approval === undefined) return response;
|
|
133
|
+
const approved = approval.challenge.isDecoupled
|
|
134
|
+
? await this.awaitAppApproval(approval, opening)
|
|
135
|
+
: await this.enterTan(approval, opening);
|
|
136
|
+
await this.retireOldLoginData(approved, kind);
|
|
137
|
+
if (this.dialog.approvalAskedIn(approved) !== undefined) {
|
|
138
|
+
throw new FinTsProtocolError('The bank asks for another approval in answer to the approval');
|
|
139
|
+
}
|
|
140
|
+
return IncomingMessage3.combine(response, approved);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Passes on the notices in `response`: the TAN methods the user may use (3920), the bank's messages and warnings. */
|
|
144
|
+
private readNotices(response: IncomingMessage3): void {
|
|
145
|
+
const allowed = allowedTanMethodsIn(response.feedback);
|
|
146
|
+
if (allowed.length > 0) this.session.state.rememberAllowedMethods(allowed);
|
|
147
|
+
for (const message of response.bankMessages) this.session.prompts.showBankMessage(message);
|
|
148
|
+
for (const warning of userWarnings(response.feedback)) this.session.prompts.showWarning(warning);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Asks the user for the TAN `approval` takes and sends it; `opening` tells whether it approves the login. */
|
|
152
|
+
private async enterTan(approval: Approval<IncomingMessage3>, opening: boolean): Promise<IncomingMessage3> {
|
|
153
|
+
const answer = await this.session.prompts.askTan(approval.challenge);
|
|
154
|
+
if (answer.kind === 'approved') {
|
|
155
|
+
throw invalidArgument('Answered a TAN challenge with an approval in the banking app', 'tanCallback');
|
|
156
|
+
}
|
|
157
|
+
// NFKC, so that digits of another width, such as those of a Japanese input method, go out as the digits they are.
|
|
158
|
+
const tan = answer.kind === 'tan' ? answer.tan.normalize('NFKC').trim() : '';
|
|
159
|
+
if (tan === '') throw new FinTsTanCancelledError();
|
|
160
|
+
const response = await approval.submitTan(requireTanOf(approval.challenge.method, tan));
|
|
161
|
+
this.receive(response, 'approval', opening);
|
|
162
|
+
return response;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Waits for the user to approve in the banking app; `opening` tells whether the approval is that of the login. */
|
|
166
|
+
private awaitAppApproval(approval: Approval<IncomingMessage3>, opening: boolean): Promise<IncomingMessage3> {
|
|
167
|
+
const prompts = this.session.prompts;
|
|
168
|
+
return appApprovalWait(approval, {
|
|
169
|
+
ask: (challenge) => prompts.askTan(challenge),
|
|
170
|
+
pause: (delayMs, interrupt) => prompts.pause(delayMs, interrupt),
|
|
171
|
+
receive: (response) => {
|
|
172
|
+
this.receive(response, 'approval', opening);
|
|
173
|
+
},
|
|
174
|
+
}).run();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Throws `TypeError` for a TAN that `method` rules out, which the bank would count as a failed attempt: one longer than
|
|
180
|
+
* `TanMethod.maxTanLength`, or with other characters than digits for a `TanMethod.numericTan` method; and for one that
|
|
181
|
+
* FinTS 3.0 cannot send. The message leaves out the TAN.
|
|
182
|
+
*/
|
|
183
|
+
function requireTanOf(method: TanMethod, tan: string): string {
|
|
184
|
+
const max = method.maxTanLength;
|
|
185
|
+
if (max !== undefined && max > 0 && tan.length > max) {
|
|
186
|
+
throw invalidArgument(`The TAN method ${method.name} takes TANs of at most ${max} characters`, 'tan');
|
|
187
|
+
}
|
|
188
|
+
if (method.numericTan && !/^[0-9]+$/.test(tan)) {
|
|
189
|
+
throw invalidArgument(`The TAN method ${method.name} takes digits only`, 'tan');
|
|
190
|
+
}
|
|
191
|
+
return requireLatin1(tan, 'tan');
|
|
192
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { FinTsTanSetupError } from '../core/errors.js';
|
|
2
|
+
import { TanMethod, type TanMedium } from '../tan/tan-models.js';
|
|
3
|
+
import { FinTs3Dialog } from '../v3/dialog.js';
|
|
4
|
+
import type { FinTs3Protocol } from '../v3/protocol.js';
|
|
5
|
+
import { noReference } from '../v3/segments.js';
|
|
6
|
+
import type { SessionState } from './session-state.js';
|
|
7
|
+
import type { UserPrompts } from './user-prompts.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The TAN method and medium the session's messages are signed with, chosen by the user among what the bank offers and
|
|
11
|
+
* the user may use, and kept in the session state.
|
|
12
|
+
*/
|
|
13
|
+
export class TanSelection {
|
|
14
|
+
/**
|
|
15
|
+
* The lookups of this session that found no medium to choose, by TAN method, with the list each gave; `undefined`
|
|
16
|
+
* where the bank gave none. Such a lookup is not repeated for its method until `select` or `forgetMedium` drops it.
|
|
17
|
+
*/
|
|
18
|
+
private readonly lookupsWithoutMedium = new Map<string, TanMedium[] | undefined>();
|
|
19
|
+
|
|
20
|
+
/** Whether the bank refused a login signed with the one-step method in this session. */
|
|
21
|
+
private oneStepRefused = false;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
private readonly protocol: FinTs3Protocol,
|
|
25
|
+
private readonly state: SessionState,
|
|
26
|
+
private readonly prompts: UserPrompts,
|
|
27
|
+
) {}
|
|
28
|
+
|
|
29
|
+
/** The TAN method in use; the one-step method until one is chosen. */
|
|
30
|
+
get method(): TanMethod {
|
|
31
|
+
const code = this.state.tanMethod;
|
|
32
|
+
if (code === undefined || code === TanMethod.oneStep.securityFunction) return TanMethod.oneStep;
|
|
33
|
+
const methods = this.protocol.parameters.bankParameters?.tanMethods ?? [];
|
|
34
|
+
return methods.find((m) => m.securityFunction === code) ?? TanMethod.oneStep;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Switches to `securityFunction` and `tanMedium` for the dialogs to come. */
|
|
38
|
+
select(securityFunction: string, tanMedium: string | undefined): void {
|
|
39
|
+
this.state.useTanMethod(securityFunction, tanMedium);
|
|
40
|
+
this.lookupsWithoutMedium.delete(securityFunction);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Forgets the medium in use and the lookups of this session, so that the next login looks the media up anew where the
|
|
45
|
+
* method needs one: the bank may have refused the medium, such as that of a phone the user has replaced.
|
|
46
|
+
*/
|
|
47
|
+
forgetMedium(): void {
|
|
48
|
+
this.state.useTanMedium(undefined);
|
|
49
|
+
this.lookupsWithoutMedium.clear();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A dialog signed with the TAN method in use and its medium.
|
|
54
|
+
*
|
|
55
|
+
* Throws `FinTsTanSetupError` if the method needs a medium the user has not chosen: the bank would refuse the
|
|
56
|
+
* login, and the client would not send the PIN again.
|
|
57
|
+
*/
|
|
58
|
+
newDialog(): FinTs3Dialog {
|
|
59
|
+
const method = this.method;
|
|
60
|
+
if (this.lacksMedium(method)) {
|
|
61
|
+
throw new FinTsTanSetupError(
|
|
62
|
+
`The TAN method ${method.name} needs a TAN medium, and the client could not learn one: the bank lists no ` +
|
|
63
|
+
'active medium for it, or does not offer or permit the list of TAN media, or rejected it. Pass a medium to ' +
|
|
64
|
+
'selectTanMethod, or choose another method',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return FinTs3Dialog.signed(this.protocol, method, this.state.tanMedium);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The dialog that looks up the TAN media, signed with the TAN method in use.
|
|
72
|
+
*
|
|
73
|
+
* It runs before the medium is known, so it names a placeholder where the method needs one; the bank ignores the
|
|
74
|
+
* medium in this dialog (PIN/TAN B.4.3.1.3).
|
|
75
|
+
*/
|
|
76
|
+
lookupDialog(): FinTs3Dialog {
|
|
77
|
+
const method = this.method;
|
|
78
|
+
return FinTs3Dialog.signed(this.protocol, method, this.lacksMedium(method) ? noReference : this.state.tanMedium);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The synchronization dialog. It runs before the TAN media are looked up, so its method must do without a medium the
|
|
83
|
+
* user has not chosen: the placeholder for one is meant for the lookup only (PIN/TAN B.4.3.1.3). The first method
|
|
84
|
+
* that does signs it:
|
|
85
|
+
* 1. the TAN method in use;
|
|
86
|
+
* 2. the one-step method, as for a user whose methods are not known yet (PIN/TAN B.4.3.1), unless the bank has refused
|
|
87
|
+
* a one-step login;
|
|
88
|
+
* 3. another two-step method the user may use.
|
|
89
|
+
*
|
|
90
|
+
* Throws `FinTsTanSetupError` if none does, before the bank could refuse the login.
|
|
91
|
+
*/
|
|
92
|
+
synchronizationDialog(): FinTs3Dialog {
|
|
93
|
+
const method = this.method;
|
|
94
|
+
if (!this.lacksMedium(method)) return FinTs3Dialog.signed(this.protocol, method, this.state.tanMedium);
|
|
95
|
+
if (!this.oneStepRefused) return FinTs3Dialog.signed(this.protocol, TanMethod.oneStep);
|
|
96
|
+
const other = this.available().find((m) => !m.isOneStep && !this.lacksMedium(m));
|
|
97
|
+
if (other !== undefined) return FinTs3Dialog.signed(this.protocol, other);
|
|
98
|
+
throw new FinTsTanSetupError(
|
|
99
|
+
`The bank refuses the one-step method, and the TAN method ${method.name} needs a TAN medium, which the client ` +
|
|
100
|
+
'can look up only after the synchronization. Pass the medium to selectTanMethod, or choose another method',
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Whether `method` needs a medium the user has not chosen. */
|
|
105
|
+
private lacksMedium(method: TanMethod): boolean {
|
|
106
|
+
return this.state.tanMedium === undefined && needsMediumChoice(method);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The two-step TAN methods the bank offers, the user may use and this client can run; the one-step method if there
|
|
111
|
+
* are none.
|
|
112
|
+
*
|
|
113
|
+
* Throws `FinTsTanSetupError` if the bank offers the user nothing but methods the client cannot run.
|
|
114
|
+
*/
|
|
115
|
+
available(): TanMethod[] {
|
|
116
|
+
const bpd = this.protocol.parameters.bankParameters;
|
|
117
|
+
if (bpd === undefined) return [TanMethod.oneStep];
|
|
118
|
+
const allowed = this.state.allowedMethods;
|
|
119
|
+
const isAllowed = (securityFunction: string): boolean =>
|
|
120
|
+
allowed === undefined || allowed.length === 0 || allowed.includes(securityFunction);
|
|
121
|
+
const runnable = bpd.tanMethods.filter((m) => isAllowed(m.securityFunction));
|
|
122
|
+
if (runnable.length === 0 && bpd.unsupportedTanMethods.some(isAllowed)) {
|
|
123
|
+
throw new FinTsTanSetupError(
|
|
124
|
+
'The bank offers only TAN methods this client cannot run: process variant 1, or methods that need an SMS ' +
|
|
125
|
+
'charge account or an HHD_UC answer',
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return runnable.length === 0 ? [TanMethod.oneStep] : runnable;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Stops using `rejected`, the method of a login the bank refused; returns whether another available method is left.
|
|
133
|
+
*
|
|
134
|
+
* The user chooses that method, unless the one in use is still available: a refused one-step synchronization does
|
|
135
|
+
* not rule out the method the user chose before.
|
|
136
|
+
*/
|
|
137
|
+
async replace(rejected: TanMethod): Promise<boolean> {
|
|
138
|
+
if (rejected.isOneStep) this.oneStepRefused = true;
|
|
139
|
+
const others = this.available().filter((m) => m.securityFunction !== rejected.securityFunction);
|
|
140
|
+
if (others.some((m) => m.securityFunction === this.state.tanMethod)) return true;
|
|
141
|
+
if (others.length === 0) return false;
|
|
142
|
+
this.state.useTanMethod((await this.prompts.chooseMethod(others)).securityFunction);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Keeps the chosen TAN method if it is still available, otherwise lets the user choose. */
|
|
147
|
+
async ensureMethodChosen(): Promise<void> {
|
|
148
|
+
const candidates = this.available();
|
|
149
|
+
const method =
|
|
150
|
+
candidates.find((m) => m.securityFunction === this.state.tanMethod) ??
|
|
151
|
+
(await this.prompts.chooseMethod(candidates));
|
|
152
|
+
if (method.securityFunction !== this.state.tanMethod) this.state.useTanMethod(method.securityFunction);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Whether the media of the method in use have to be looked up: it needs a medium the user has not chosen, and they
|
|
157
|
+
* were not looked up in vain for it in this session.
|
|
158
|
+
*/
|
|
159
|
+
get mediumToLookUp(): boolean {
|
|
160
|
+
const method = this.method;
|
|
161
|
+
return this.lacksMedium(method) && !this.lookupsWithoutMedium.has(method.securityFunction);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Lets the user choose the medium of the method in use among the active ones in `media`, the result of a lookup;
|
|
166
|
+
* none where the lookup gave no list or no active medium.
|
|
167
|
+
*/
|
|
168
|
+
async chooseMedium(media: TanMedium[] | undefined): Promise<void> {
|
|
169
|
+
const method = this.method;
|
|
170
|
+
const medium = media === undefined ? undefined : await this.chooseMediumOf(method, media);
|
|
171
|
+
this.state.useTanMedium(medium);
|
|
172
|
+
if (medium === undefined) this.lookupsWithoutMedium.set(method.securityFunction, media);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The list of the lookup that found no medium for the method in use; `undefined` where there was none, or the bank
|
|
177
|
+
* gave no list.
|
|
178
|
+
*
|
|
179
|
+
* It outlasts the call that failed for want of the medium, so that `getTanMedia` can show the user the list that tells
|
|
180
|
+
* why: the client neither looks the media up again for the method nor can list them in a dialog without a medium.
|
|
181
|
+
*/
|
|
182
|
+
get lookedUpMedia(): TanMedium[] | undefined {
|
|
183
|
+
return this.lookupsWithoutMedium.get(this.method.securityFunction);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The name of the medium the user chooses among the active ones in `media`; `undefined` if none is active. */
|
|
187
|
+
private async chooseMediumOf(method: TanMethod, media: readonly TanMedium[]): Promise<string | undefined> {
|
|
188
|
+
const active = media.filter((m) => m.isActive && m.name !== undefined);
|
|
189
|
+
// HITAB version 5 names the method each medium belongs to; a medium of another method would not do.
|
|
190
|
+
const ofMethod = active.filter((m) => m.securityFunction === method.securityFunction);
|
|
191
|
+
const candidates = ofMethod.length === 0 ? active : ofMethod;
|
|
192
|
+
if (candidates.length === 0) return undefined;
|
|
193
|
+
return (await this.prompts.chooseMedium(candidates)).name;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Whether messages signed with `method` must name the TAN medium: the method requires the name, and the user may have
|
|
199
|
+
* several media (PIN/TAN, HKTAN "Bezeichnung des TAN-Mediums").
|
|
200
|
+
*/
|
|
201
|
+
function needsMediumChoice(method: TanMethod): boolean {
|
|
202
|
+
return method.needsMediumName && (method.activeMediaCount ?? 0) > 1;
|
|
203
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { BankMessage } from '../core/bank-info.js';
|
|
2
|
+
import type { FinTsClientOptions, MessageLogger } from '../core/client.js';
|
|
3
|
+
import { clientClosedError } from '../core/errors.js';
|
|
4
|
+
import type { FinTsFeedback } from '../core/feedback.js';
|
|
5
|
+
import { TanAnswer, type TanChallenge } from '../tan/challenge.js';
|
|
6
|
+
import type { TanMedium, TanMethod } from '../tan/tan-models.js';
|
|
7
|
+
import { Deferred } from '../util/async.js';
|
|
8
|
+
import { invalidArgument } from '../util/errors.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Everything a session tells the app, asks of it or waits for: the bank's messages, warnings and log, the TAN callback,
|
|
12
|
+
* the selectors and the pauses between status requests.
|
|
13
|
+
*
|
|
14
|
+
* Once the client is closed nothing is told or asked, and every wait ends with `FinTsStateError`. Errors of the
|
|
15
|
+
* callbacks that only inform the app wait for `reportingCallbackErrors`, as `FinTsClientOptions` promises.
|
|
16
|
+
*/
|
|
17
|
+
export class UserPrompts {
|
|
18
|
+
private closed = false;
|
|
19
|
+
|
|
20
|
+
/** The first error an informing callback threw during the running call. */
|
|
21
|
+
private callbackError: { error: unknown } | undefined;
|
|
22
|
+
|
|
23
|
+
/** Aborts the waits under way; each wait removes its abort when it is over, or when nothing waits for it any more. */
|
|
24
|
+
private readonly aborts = new Set<() => void>();
|
|
25
|
+
|
|
26
|
+
constructor(private readonly options: FinTsClientOptions) {}
|
|
27
|
+
|
|
28
|
+
/** Ends every wait; the client calls it once. */
|
|
29
|
+
close(): void {
|
|
30
|
+
this.closed = true;
|
|
31
|
+
for (const abort of [...this.aborts]) abort();
|
|
32
|
+
this.aborts.clear();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Throws `FinTsStateError` once `close` was called. */
|
|
36
|
+
checkOpen(): void {
|
|
37
|
+
if (this.closed) throw clientClosedError();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Runs `call` and then fails it with the first error an informing callback threw meanwhile; an error of `call` itself
|
|
42
|
+
* takes precedence.
|
|
43
|
+
*/
|
|
44
|
+
async reportingCallbackErrors<T>(call: () => Promise<T>): Promise<T> {
|
|
45
|
+
this.callbackError = undefined;
|
|
46
|
+
const result = await call();
|
|
47
|
+
// Set by a callback while `call` ran, which the compiler cannot see.
|
|
48
|
+
const failure = this.callbackError as { error: unknown } | undefined;
|
|
49
|
+
if (failure !== undefined) throw failure.error;
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The app's logger, whose errors are kept for `reportingCallbackErrors`; `undefined` without one. */
|
|
54
|
+
get logger(): MessageLogger | undefined {
|
|
55
|
+
const logger = this.options.logger;
|
|
56
|
+
if (logger === undefined) return undefined;
|
|
57
|
+
return (direction, message) => {
|
|
58
|
+
this.inform(() => {
|
|
59
|
+
logger(direction, message);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Passes the bank's `message` to the app, unless the client is closed. */
|
|
65
|
+
showBankMessage(message: BankMessage): void {
|
|
66
|
+
const handler = this.options.onBankMessage;
|
|
67
|
+
if (handler !== undefined && !this.closed) {
|
|
68
|
+
this.inform(() => {
|
|
69
|
+
handler(message);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Passes the bank's `warning` to the app, unless the client is closed. */
|
|
75
|
+
showWarning(warning: FinTsFeedback): void {
|
|
76
|
+
const handler = this.options.onWarning;
|
|
77
|
+
if (handler !== undefined && !this.closed) {
|
|
78
|
+
this.inform(() => {
|
|
79
|
+
handler(warning);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private inform(callback: () => void): void {
|
|
85
|
+
try {
|
|
86
|
+
callback();
|
|
87
|
+
} catch (error) {
|
|
88
|
+
this.callbackError ??= { error };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Shows `challenge` through the TAN callback and returns its answer; rejects with `TypeError` for anything but a
|
|
94
|
+
* `TanAnswer`. Once `TanChallenge.resolved` settles, nothing waits for the answer any more.
|
|
95
|
+
*/
|
|
96
|
+
askTan(challenge: TanChallenge): Promise<TanAnswer> {
|
|
97
|
+
return this.unlessClosed(async () => tanAnswerOf(await this.options.tanCallback(challenge)), challenge.resolved);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Waits for `delayMs` milliseconds, or until `interrupt` settles. */
|
|
101
|
+
pause(delayMs: number, interrupt?: Promise<unknown>): Promise<void> {
|
|
102
|
+
if (this.closed) return Promise.reject(clientClosedError());
|
|
103
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
104
|
+
const elapsed = new Promise<void>((resolve) => {
|
|
105
|
+
timer = setTimeout(resolve, Math.max(0, delayMs));
|
|
106
|
+
});
|
|
107
|
+
// Neither an interrupted wait nor a closed client may leave a timer behind that keeps the process alive.
|
|
108
|
+
return this.unlessClosed(() =>
|
|
109
|
+
Promise.race([elapsed, ...(interrupt === undefined ? [] : [interrupt.then(() => undefined)])]),
|
|
110
|
+
).finally(() => {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The TAN method to use among `candidates`: the app's choice, or the first without a selector or an answer.
|
|
117
|
+
*
|
|
118
|
+
* Throws `TypeError` if the selector returns a method that is not among `candidates`.
|
|
119
|
+
*/
|
|
120
|
+
async chooseMethod(candidates: readonly TanMethod[]): Promise<TanMethod> {
|
|
121
|
+
const selector = this.options.tanMethodSelector;
|
|
122
|
+
if (candidates.length === 1 || selector === undefined) return candidates[0]!;
|
|
123
|
+
const chosen = await this.unlessClosed(() => selector(candidates));
|
|
124
|
+
if (chosen === undefined) return candidates[0]!;
|
|
125
|
+
const match = candidates.find((m) => m.securityFunction === chosen.securityFunction);
|
|
126
|
+
if (match === undefined) throw invalidArgument('Returned a TAN method the bank did not offer', 'tanMethodSelector');
|
|
127
|
+
return match;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The TAN medium to use among the active `media`: the app's choice, or the first without a selector or an answer.
|
|
132
|
+
*
|
|
133
|
+
* Throws `TypeError` if the selector returns a medium that is not among `media`.
|
|
134
|
+
*/
|
|
135
|
+
async chooseMedium(media: readonly TanMedium[]): Promise<TanMedium> {
|
|
136
|
+
const selector = this.options.tanMediumSelector;
|
|
137
|
+
if (media.length === 1 || selector === undefined) return media[0]!;
|
|
138
|
+
const chosen = await this.unlessClosed(() => selector(media));
|
|
139
|
+
if (chosen === undefined) return media[0]!;
|
|
140
|
+
const match = media.find((m) => m.name === chosen.name);
|
|
141
|
+
if (match === undefined) throw invalidArgument('Returned a TAN medium the bank did not list', 'tanMediumSelector');
|
|
142
|
+
return match;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Starts `wait` and ends it with `FinTsStateError` if `close` is called before it is over. Once `unwanted` settles,
|
|
147
|
+
* nothing waits for it any more: it is forgotten then, rather than kept until a callback that never answers does.
|
|
148
|
+
*
|
|
149
|
+
* Once the client is closed, the returned promise rejects rather than this function throwing: a caller that starts
|
|
150
|
+
* several waits at once would otherwise leave the failure of those started first unheard.
|
|
151
|
+
*/
|
|
152
|
+
private unlessClosed<T>(wait: () => T | Promise<T>, unwanted?: Promise<unknown>): Promise<T> {
|
|
153
|
+
if (this.closed) return Promise.reject(clientClosedError());
|
|
154
|
+
const result = new Deferred<T>();
|
|
155
|
+
const abort = (): void => {
|
|
156
|
+
result.reject(clientClosedError());
|
|
157
|
+
};
|
|
158
|
+
const forget = (): void => {
|
|
159
|
+
this.aborts.delete(abort);
|
|
160
|
+
};
|
|
161
|
+
this.aborts.add(abort);
|
|
162
|
+
// Runs `wait` at once, as far as its first `await`, and turns an error it throws into a rejection.
|
|
163
|
+
const started = (async (): Promise<T> => wait())();
|
|
164
|
+
started
|
|
165
|
+
.then(
|
|
166
|
+
(value) => {
|
|
167
|
+
result.resolve(value);
|
|
168
|
+
},
|
|
169
|
+
(error: unknown) => {
|
|
170
|
+
result.reject(error);
|
|
171
|
+
},
|
|
172
|
+
)
|
|
173
|
+
.finally(forget)
|
|
174
|
+
.catch(() => undefined);
|
|
175
|
+
void unwanted?.then(forget, forget);
|
|
176
|
+
return result.promise;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* `answer`, a result of the TAN callback, as a `TanAnswer`; throws `TypeError` for anything else, such as the
|
|
182
|
+
* `undefined` of a callback that returns nothing. The answer is read once, so that what was checked is what counts.
|
|
183
|
+
*/
|
|
184
|
+
function tanAnswerOf(answer: unknown): TanAnswer {
|
|
185
|
+
const { kind, tan } = (typeof answer === 'object' && answer !== null ? answer : {}) as {
|
|
186
|
+
kind?: unknown;
|
|
187
|
+
tan?: unknown;
|
|
188
|
+
};
|
|
189
|
+
if (kind === 'tan' && typeof tan === 'string') return TanAnswer.tan(tan);
|
|
190
|
+
if (kind === 'approved') return TanAnswer.approved;
|
|
191
|
+
if (kind === 'cancelled') return TanAnswer.cancelled;
|
|
192
|
+
throw invalidArgument('Returned no TanAnswer', 'tanCallback');
|
|
193
|
+
}
|