@coderoftime/fints 0.0.0-stage → 0.1.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 +11 -0
- package/LICENSE +28 -0
- package/README.md +156 -2
- package/dist/core/account.d.ts +140 -0
- package/dist/core/account.d.ts.map +1 -0
- package/dist/core/account.js +174 -0
- package/dist/core/account.js.map +1 -0
- package/dist/core/amount.d.ts +62 -0
- package/dist/core/amount.d.ts.map +1 -0
- package/dist/core/amount.js +189 -0
- package/dist/core/amount.js.map +1 -0
- package/dist/core/balance.d.ts +54 -0
- package/dist/core/balance.d.ts.map +1 -0
- package/dist/core/balance.js +53 -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 +126 -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 +68 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +113 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/errors.d.ts +91 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +182 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/feedback.d.ts +69 -0
- package/dist/core/feedback.d.ts.map +1 -0
- package/dist/core/feedback.js +124 -0
- package/dist/core/feedback.js.map +1 -0
- package/dist/core/transaction.d.ts +98 -0
- package/dist/core/transaction.d.ts.map +1 -0
- package/dist/core/transaction.js +91 -0
- package/dist/core/transaction.js.map +1 -0
- package/dist/core/transport.d.ts +37 -0
- package/dist/core/transport.d.ts.map +1 -0
- package/dist/core/transport.js +92 -0
- package/dist/core/transport.js.map +1 -0
- package/dist/formats/camt.d.ts +12 -0
- package/dist/formats/camt.d.ts.map +1 -0
- package/dist/formats/camt.js +159 -0
- package/dist/formats/camt.js.map +1 -0
- package/dist/formats/mt940.d.ts +42 -0
- package/dist/formats/mt940.d.ts.map +1 -0
- package/dist/formats/mt940.js +297 -0
- package/dist/formats/mt940.js.map +1 -0
- package/dist/formats/readers.d.ts +11 -0
- package/dist/formats/readers.d.ts.map +1 -0
- package/dist/formats/readers.js +47 -0
- package/dist/formats/readers.js.map +1 -0
- package/dist/formats/sepa-purpose.d.ts +22 -0
- package/dist/formats/sepa-purpose.d.ts.map +1 -0
- package/dist/formats/sepa-purpose.js +68 -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 +60 -0
- package/dist/session/app-approval.d.ts.map +1 -0
- package/dist/session/app-approval.js +215 -0
- package/dist/session/app-approval.js.map +1 -0
- package/dist/session/bank-connection.d.ts +28 -0
- package/dist/session/bank-connection.d.ts.map +1 -0
- package/dist/session/bank-connection.js +48 -0
- package/dist/session/bank-connection.js.map +1 -0
- package/dist/session/dialog-runner.d.ts +34 -0
- package/dist/session/dialog-runner.d.ts.map +1 -0
- package/dist/session/dialog-runner.js +112 -0
- package/dist/session/dialog-runner.js.map +1 -0
- package/dist/session/login-guard.d.ts +73 -0
- package/dist/session/login-guard.d.ts.map +1 -0
- package/dist/session/login-guard.js +109 -0
- package/dist/session/login-guard.js.map +1 -0
- package/dist/session/parameter-data.d.ts +83 -0
- package/dist/session/parameter-data.d.ts.map +1 -0
- package/dist/session/parameter-data.js +68 -0
- package/dist/session/parameter-data.js.map +1 -0
- package/dist/session/pin-tan-client.d.ts +54 -0
- package/dist/session/pin-tan-client.d.ts.map +1 -0
- package/dist/session/pin-tan-client.js +153 -0
- package/dist/session/pin-tan-client.js.map +1 -0
- package/dist/session/pin-tan-dialog.d.ts +71 -0
- package/dist/session/pin-tan-dialog.d.ts.map +1 -0
- package/dist/session/pin-tan-dialog.js +124 -0
- package/dist/session/pin-tan-dialog.js.map +1 -0
- package/dist/session/protocol.d.ts +209 -0
- package/dist/session/protocol.d.ts.map +1 -0
- package/dist/session/protocol.js +75 -0
- package/dist/session/protocol.js.map +1 -0
- package/dist/session/response-handler.d.ts +48 -0
- package/dist/session/response-handler.d.ts.map +1 -0
- package/dist/session/response-handler.js +121 -0
- package/dist/session/response-handler.js.map +1 -0
- package/dist/session/sepa-accounts.d.ts +34 -0
- package/dist/session/sepa-accounts.d.ts.map +1 -0
- package/dist/session/sepa-accounts.js +96 -0
- package/dist/session/sepa-accounts.js.map +1 -0
- package/dist/session/session-setup.d.ts +72 -0
- package/dist/session/session-setup.d.ts.map +1 -0
- package/dist/session/session-setup.js +160 -0
- package/dist/session/session-setup.js.map +1 -0
- package/dist/session/session-state.d.ts +47 -0
- package/dist/session/session-state.d.ts.map +1 -0
- package/dist/session/session-state.js +147 -0
- package/dist/session/session-state.js.map +1 -0
- package/dist/session/tan-selection.d.ts +88 -0
- package/dist/session/tan-selection.d.ts.map +1 -0
- package/dist/session/tan-selection.js +178 -0
- package/dist/session/tan-selection.js.map +1 -0
- package/dist/session/user-prompts.d.ts +61 -0
- package/dist/session/user-prompts.d.ts.map +1 -0
- package/dist/session/user-prompts.js +164 -0
- package/dist/session/user-prompts.js.map +1 -0
- package/dist/tan/challenge.d.ts +114 -0
- package/dist/tan/challenge.d.ts.map +1 -0
- package/dist/tan/challenge.js +185 -0
- package/dist/tan/challenge.js.map +1 -0
- package/dist/tan/flicker.d.ts +36 -0
- package/dist/tan/flicker.d.ts.map +1 -0
- package/dist/tan/flicker.js +262 -0
- package/dist/tan/flicker.js.map +1 -0
- package/dist/tan/tan-image.d.ts +21 -0
- package/dist/tan/tan-image.d.ts.map +1 -0
- package/dist/tan/tan-image.js +41 -0
- package/dist/tan/tan-image.js.map +1 -0
- package/dist/tan/tan-models.d.ts +145 -0
- package/dist/tan/tan-models.d.ts.map +1 -0
- package/dist/tan/tan-models.js +148 -0
- package/dist/tan/tan-models.js.map +1 -0
- package/dist/util/async.d.ts +24 -0
- package/dist/util/async.d.ts.map +1 -0
- package/dist/util/async.js +49 -0
- package/dist/util/async.js.map +1 -0
- package/dist/util/bytes.d.ts +23 -0
- package/dist/util/bytes.d.ts.map +1 -0
- package/dist/util/bytes.js +136 -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 +29 -0
- package/dist/util/call-context.js.map +1 -0
- package/dist/util/dates.d.ts +62 -0
- package/dist/util/dates.d.ts.map +1 -0
- package/dist/util/dates.js +169 -0
- package/dist/util/dates.js.map +1 -0
- package/dist/util/errors.d.ts +9 -0
- package/dist/util/errors.d.ts.map +1 -0
- package/dist/util/errors.js +15 -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 +13 -0
- package/dist/util/json.d.ts.map +1 -0
- package/dist/util/json.js +44 -0
- package/dist/util/json.js.map +1 -0
- package/dist/util/text.d.ts +20 -0
- package/dist/util/text.d.ts.map +1 -0
- package/dist/util/text.js +78 -0
- package/dist/util/text.js.map +1 -0
- package/dist/util/xml.d.ts +35 -0
- package/dist/util/xml.d.ts.map +1 -0
- package/dist/util/xml.js +183 -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 +176 -0
- package/dist/v3/account-extension.js.map +1 -0
- package/dist/v3/client.d.ts +24 -0
- package/dist/v3/client.d.ts.map +1 -0
- package/dist/v3/client.js +16 -0
- package/dist/v3/client.js.map +1 -0
- package/dist/v3/dialog.d.ts +78 -0
- package/dist/v3/dialog.d.ts.map +1 -0
- package/dist/v3/dialog.js +138 -0
- package/dist/v3/dialog.js.map +1 -0
- package/dist/v3/message.d.ts +72 -0
- package/dist/v3/message.d.ts.map +1 -0
- package/dist/v3/message.js +259 -0
- package/dist/v3/message.js.map +1 -0
- package/dist/v3/parameters.d.ts +79 -0
- package/dist/v3/parameters.d.ts.map +1 -0
- package/dist/v3/parameters.js +396 -0
- package/dist/v3/parameters.js.map +1 -0
- package/dist/v3/protocol.d.ts +68 -0
- package/dist/v3/protocol.d.ts.map +1 -0
- package/dist/v3/protocol.js +263 -0
- package/dist/v3/protocol.js.map +1 -0
- package/dist/v3/segments.d.ts +89 -0
- package/dist/v3/segments.d.ts.map +1 -0
- package/dist/v3/segments.js +283 -0
- package/dist/v3/segments.js.map +1 -0
- package/dist/v3/syntax.d.ts +76 -0
- package/dist/v3/syntax.d.ts.map +1 -0
- package/dist/v3/syntax.js +301 -0
- package/dist/v3/syntax.js.map +1 -0
- package/package.json +63 -4
- package/src/core/account.ts +250 -0
- package/src/core/amount.ts +209 -0
- package/src/core/balance.ts +84 -0
- package/src/core/bank-info.ts +66 -0
- package/src/core/client.ts +149 -0
- package/src/core/config.ts +141 -0
- package/src/core/errors.ts +203 -0
- package/src/core/feedback.ts +137 -0
- package/src/core/transaction.ts +156 -0
- package/src/core/transport.ts +114 -0
- package/src/formats/camt.ts +169 -0
- package/src/formats/mt940.ts +333 -0
- package/src/formats/readers.ts +56 -0
- package/src/formats/sepa-purpose.ts +75 -0
- package/src/index.ts +68 -0
- package/src/session/app-approval.ts +265 -0
- package/src/session/bank-connection.ts +51 -0
- package/src/session/dialog-runner.ts +119 -0
- package/src/session/login-guard.ts +135 -0
- package/src/session/parameter-data.ts +137 -0
- package/src/session/pin-tan-client.ts +186 -0
- package/src/session/pin-tan-dialog.ts +160 -0
- package/src/session/protocol.ts +311 -0
- package/src/session/response-handler.ts +130 -0
- package/src/session/sepa-accounts.ts +97 -0
- package/src/session/session-setup.ts +165 -0
- package/src/session/session-state.ts +170 -0
- package/src/session/tan-selection.ts +192 -0
- package/src/session/user-prompts.ts +174 -0
- package/src/tan/challenge.ts +252 -0
- package/src/tan/flicker.ts +270 -0
- package/src/tan/tan-image.ts +36 -0
- package/src/tan/tan-models.ts +247 -0
- package/src/util/async.ts +53 -0
- package/src/util/bytes.ts +135 -0
- package/src/util/call-context.ts +40 -0
- package/src/util/dates.ts +193 -0
- package/src/util/errors.ts +16 -0
- package/src/util/immutable-map.ts +47 -0
- package/src/util/json.ts +51 -0
- package/src/util/text.ts +82 -0
- package/src/util/xml.ts +172 -0
- package/src/v3/account-extension.ts +190 -0
- package/src/v3/client.ts +29 -0
- package/src/v3/dialog.ts +199 -0
- package/src/v3/message.ts +321 -0
- package/src/v3/parameters.ts +451 -0
- package/src/v3/protocol.ts +331 -0
- package/src/v3/segments.ts +331 -0
- package/src/v3/syntax.ts +307 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// Internal to the package, and named after the FinTS specification. Documentation is where the meaning is not evident.
|
|
2
|
+
|
|
3
|
+
import { BankMessage } from '../core/bank-info.js';
|
|
4
|
+
import { FinTsFeedback } from '../core/feedback.js';
|
|
5
|
+
import type { BankResponse } from '../session/protocol.js';
|
|
6
|
+
import { TanMethod } from '../tan/tan-models.js';
|
|
7
|
+
import { base64Encode, BytesBuilder, latin1Decode, latin1Encode } from '../util/bytes.js';
|
|
8
|
+
import { formatFintsDate, formatFintsTime } from '../util/dates.js';
|
|
9
|
+
import { FormatError } from '../util/errors.js';
|
|
10
|
+
import { decodeBankText, decodeBase64Lines } from '../util/text.js';
|
|
11
|
+
import { Binary, parseSegments, type Segment, SegmentSpec, type SegmentValue } from './syntax.js';
|
|
12
|
+
|
|
13
|
+
/** Signature data of a PIN/TAN message. */
|
|
14
|
+
export interface PinTanCredentials {
|
|
15
|
+
readonly countryCode: string;
|
|
16
|
+
readonly bankCode: string;
|
|
17
|
+
readonly userId: string;
|
|
18
|
+
readonly systemId: string;
|
|
19
|
+
readonly pin: string;
|
|
20
|
+
readonly securityFunction: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isTwoStep(c: PinTanCredentials): boolean {
|
|
24
|
+
return c.securityFunction !== TanMethod.oneStep.securityFunction;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Codes of the PIN/TAN security envelope, from the FinTS security specification. PIN/TAN neither hashes, signs nor
|
|
28
|
+
// encrypts, but the envelope must name the algorithms of the chip card procedures all the same.
|
|
29
|
+
|
|
30
|
+
/** Area the signature covers: signature head and data (SHM). */
|
|
31
|
+
const signedArea = 1;
|
|
32
|
+
|
|
33
|
+
/** Role of the security supplier: issuer (ISS). */
|
|
34
|
+
const issuer = 1;
|
|
35
|
+
|
|
36
|
+
/** Security identification: message sender (MS), empty CID, then the customer system ID. */
|
|
37
|
+
function sender(systemId: string): SegmentValue {
|
|
38
|
+
return [1, null, systemId];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Security reference number, the same in every message. */
|
|
42
|
+
const securityReference = 1;
|
|
43
|
+
|
|
44
|
+
/** Security time stamp (STS). */
|
|
45
|
+
function timestamp(time: Date): SegmentValue {
|
|
46
|
+
return [1, formatFintsDate(time), formatFintsTime(time)];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Hash algorithm: owner hashing (OHA), mutually agreed algorithm (ZZZ), initialisation value in clear (IVC). */
|
|
50
|
+
const hashAlgorithm = [1, 999, 1];
|
|
51
|
+
|
|
52
|
+
/** Signature algorithm: owner signing (OSG), RSA, ISO 9796-1. */
|
|
53
|
+
const signatureAlgorithm = [6, 10, 16];
|
|
54
|
+
|
|
55
|
+
/** Security function of the encryption head: plain text, since PIN/TAN does not encrypt. */
|
|
56
|
+
const plainText = 998;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Encryption algorithm: owner symmetric (OSY), CBC, 2-key triple DES, a key of zeros, symmetric key (KYE),
|
|
60
|
+
* initialisation value in clear (IVC).
|
|
61
|
+
*/
|
|
62
|
+
const encryptionAlgorithm = [2, 2, 13, new Binary(new Uint8Array(8)), 5, 1];
|
|
63
|
+
|
|
64
|
+
/** Compression function: none. */
|
|
65
|
+
const noCompression = 0;
|
|
66
|
+
|
|
67
|
+
/** Key name: bank, user, key type `S` (signature) or `V` (encryption), key number and version 0. */
|
|
68
|
+
function keyName(c: PinTanCredentials, keyType: string): SegmentValue {
|
|
69
|
+
return [c.countryCode, c.bankCode, c.userId, keyType, 0, 0];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A random integer from 0 below `bound`, from a cryptographically secure source. */
|
|
73
|
+
function secureRandom(bound: number): number {
|
|
74
|
+
const values = new Uint32Array(1);
|
|
75
|
+
crypto.getRandomValues(values);
|
|
76
|
+
return values[0]! % bound;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A random 14-digit reference that ties the signature head (HNSHK) to the signature end (HNSHA). */
|
|
80
|
+
function securityControlReference(): string {
|
|
81
|
+
return `${secureRandom(900000000) + 100000000}${secureRandom(100000).toString().padStart(5, '0')}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A message to send, and the same message with PIN and TAN masked for logging. */
|
|
85
|
+
export class OutgoingMessage {
|
|
86
|
+
constructor(
|
|
87
|
+
readonly bytes: Uint8Array,
|
|
88
|
+
/** The parts of the masked message, each decoded on its own as the bank's text is. */
|
|
89
|
+
private readonly maskedParts: readonly Uint8Array[],
|
|
90
|
+
) {}
|
|
91
|
+
|
|
92
|
+
/** The message with PIN and TAN masked; decoded only when a logger asks for it. */
|
|
93
|
+
get masked(): string {
|
|
94
|
+
return this.maskedParts.map(decodeBankText).join('');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The segments between header and trailer, as sent and with PIN and TAN masked, and the number for the trailer. */
|
|
99
|
+
interface Body {
|
|
100
|
+
readonly bytes: Uint8Array;
|
|
101
|
+
readonly masked: readonly Uint8Array[];
|
|
102
|
+
readonly nextNumber: number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Builds a message: header (HNHBK), the `segments`, trailer (HNHBS).
|
|
107
|
+
*
|
|
108
|
+
* With `security` the segments are signed with PIN and optional `tan` (HNSHK … HNSHA) and wrapped in the encryption
|
|
109
|
+
* envelope (HNVSK, HNVSD) that PIN/TAN uses without actual encryption.
|
|
110
|
+
*
|
|
111
|
+
* The masked message states the sizes of its masked content, so that the log does not tell how long PIN and TAN are.
|
|
112
|
+
*/
|
|
113
|
+
export function buildMessage(parts: {
|
|
114
|
+
dialogId: string;
|
|
115
|
+
messageNumber: number;
|
|
116
|
+
segments: readonly SegmentSpec[];
|
|
117
|
+
security?: PinTanCredentials | undefined;
|
|
118
|
+
tan?: string | undefined;
|
|
119
|
+
now?: Date;
|
|
120
|
+
}): OutgoingMessage {
|
|
121
|
+
const body =
|
|
122
|
+
parts.security === undefined
|
|
123
|
+
? plainBody(parts.segments)
|
|
124
|
+
: signedBody(parts.segments, parts.security, parts.tan, parts.now ?? new Date());
|
|
125
|
+
const trailer = new SegmentSpec('HNHBS', 1, [parts.messageNumber]).build(body.nextNumber);
|
|
126
|
+
// The header states the size of the whole message, itself included; its own size does not depend on it.
|
|
127
|
+
const header = (size: number): Uint8Array =>
|
|
128
|
+
new SegmentSpec('HNHBK', 3, [size.toString().padStart(12, '0'), 300, parts.dialogId, parts.messageNumber]).build(1);
|
|
129
|
+
const sizeOf = (sections: readonly Uint8Array[]): number => header(0).length + lengthOf(sections) + trailer.length;
|
|
130
|
+
const result = new BytesBuilder();
|
|
131
|
+
result.add(header(sizeOf([body.bytes])));
|
|
132
|
+
result.add(body.bytes);
|
|
133
|
+
result.add(trailer);
|
|
134
|
+
return new OutgoingMessage(result.takeBytes(), [header(sizeOf(body.masked)), ...body.masked, trailer]);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The number of bytes in `parts` together. */
|
|
138
|
+
function lengthOf(parts: readonly Uint8Array[]): number {
|
|
139
|
+
return parts.reduce((sum, part) => sum + part.length, 0);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The segments of a message without security, as the anonymous dialog sends them. */
|
|
143
|
+
function plainBody(segments: readonly SegmentSpec[]): Body {
|
|
144
|
+
const built = segments.map((segment, i) => segment.build(i + 2));
|
|
145
|
+
const bytes = new BytesBuilder();
|
|
146
|
+
built.forEach((b) => {
|
|
147
|
+
bytes.add(b);
|
|
148
|
+
});
|
|
149
|
+
return { bytes: bytes.takeBytes(), masked: built, nextNumber: built.length + 2 };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The segments signed with PIN and optional `tan`, inside the encryption envelope. */
|
|
153
|
+
function signedBody(
|
|
154
|
+
segments: readonly SegmentSpec[],
|
|
155
|
+
security: PinTanCredentials,
|
|
156
|
+
tan: string | undefined,
|
|
157
|
+
time: Date,
|
|
158
|
+
): Body {
|
|
159
|
+
const profile = ['PIN', isTwoStep(security) ? 2 : 1];
|
|
160
|
+
const controlReference = securityControlReference();
|
|
161
|
+
const inner = new BytesBuilder();
|
|
162
|
+
const maskedInner: Uint8Array[] = [];
|
|
163
|
+
const addInner = (built: Uint8Array, masked?: Uint8Array): void => {
|
|
164
|
+
inner.add(built);
|
|
165
|
+
maskedInner.push(masked ?? built);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
addInner(
|
|
169
|
+
new SegmentSpec('HNSHK', 4, [
|
|
170
|
+
profile,
|
|
171
|
+
security.securityFunction,
|
|
172
|
+
controlReference,
|
|
173
|
+
signedArea,
|
|
174
|
+
issuer,
|
|
175
|
+
sender(security.systemId),
|
|
176
|
+
securityReference,
|
|
177
|
+
timestamp(time),
|
|
178
|
+
hashAlgorithm,
|
|
179
|
+
signatureAlgorithm,
|
|
180
|
+
keyName(security, 'S'),
|
|
181
|
+
]).build(2),
|
|
182
|
+
);
|
|
183
|
+
let number = 3;
|
|
184
|
+
for (const segment of segments) addInner(segment.build(number++));
|
|
185
|
+
const signatureEnd = (pin: string, tanValue: string | undefined): SegmentSpec =>
|
|
186
|
+
new SegmentSpec('HNSHA', 2, [controlReference, null, [pin, tanValue]]);
|
|
187
|
+
addInner(
|
|
188
|
+
signatureEnd(security.pin, tan).build(number),
|
|
189
|
+
signatureEnd('***', tan === undefined ? undefined : '***').build(number),
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const innerBytes = inner.takeBytes();
|
|
193
|
+
const encryptionHead = new SegmentSpec('HNVSK', 3, [
|
|
194
|
+
profile,
|
|
195
|
+
plainText,
|
|
196
|
+
issuer,
|
|
197
|
+
sender(security.systemId),
|
|
198
|
+
timestamp(time),
|
|
199
|
+
encryptionAlgorithm,
|
|
200
|
+
keyName(security, 'V'),
|
|
201
|
+
noCompression,
|
|
202
|
+
]).build(998);
|
|
203
|
+
const bytes = new BytesBuilder();
|
|
204
|
+
bytes.add(encryptionHead);
|
|
205
|
+
bytes.add(new SegmentSpec('HNVSD', 1, [new Binary(innerBytes)]).build(999));
|
|
206
|
+
const masked = [
|
|
207
|
+
encryptionHead,
|
|
208
|
+
latin1Encode(`HNVSD:999:1+@${lengthOf(maskedInner)}@`),
|
|
209
|
+
...maskedInner,
|
|
210
|
+
latin1Encode("'"),
|
|
211
|
+
];
|
|
212
|
+
return { bytes: bytes.takeBytes(), masked, nextNumber: number + 1 };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** A received message, with the segments of the encryption envelope unpacked. */
|
|
216
|
+
export class IncomingMessage3 implements BankResponse {
|
|
217
|
+
/**
|
|
218
|
+
* Return codes for the whole message (HIRMG), then for single request segments (HIRMS).
|
|
219
|
+
*
|
|
220
|
+
* The segment a HIRMS refers to is not kept: each message carries one order, and the answer that completes an
|
|
221
|
+
* approval reports on the order of an earlier message (PIN/TAN B.4.2.1).
|
|
222
|
+
*/
|
|
223
|
+
readonly feedback: readonly FinTsFeedback[];
|
|
224
|
+
|
|
225
|
+
readonly messageRejected: boolean;
|
|
226
|
+
|
|
227
|
+
private constructor(
|
|
228
|
+
readonly segments: readonly Segment[],
|
|
229
|
+
readonly size: number,
|
|
230
|
+
) {
|
|
231
|
+
this.feedback = Object.freeze(
|
|
232
|
+
['HIRMG', 'HIRMS'].flatMap((type) => segments.filter((s) => s.type === type).flatMap(feedbackOf)),
|
|
233
|
+
);
|
|
234
|
+
this.messageRejected = segments
|
|
235
|
+
.filter((s) => s.type === 'HIRMG')
|
|
236
|
+
.flatMap(feedbackOf)
|
|
237
|
+
.some((f) => f.isError);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Parses a message; throws `FormatError` if `data` is not a FinTS message, such as the HTML page of a proxy or a
|
|
242
|
+
* maintenance notice.
|
|
243
|
+
*/
|
|
244
|
+
static parse(data: Uint8Array): IncomingMessage3 {
|
|
245
|
+
const outer = parseSegments(data);
|
|
246
|
+
if (outer[0]?.type !== 'HNHBK') throw new FormatError('Not a FinTS message: no message header');
|
|
247
|
+
const segments: Segment[] = [];
|
|
248
|
+
for (const s of outer) {
|
|
249
|
+
if (s.type === 'HNVSD') {
|
|
250
|
+
const payload = s.binary(1);
|
|
251
|
+
if (payload !== undefined) segments.push(...parseSegments(payload));
|
|
252
|
+
} else {
|
|
253
|
+
segments.push(s);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return new IncomingMessage3(segments, data.length);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
static combine(first: IncomingMessage3, second: IncomingMessage3): IncomingMessage3 {
|
|
260
|
+
return new IncomingMessage3([...first.segments, ...second.segments], first.size + second.size);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
get bankMessages(): BankMessage[] {
|
|
264
|
+
return this.findAll('HIKIM').map((m) => new BankMessage(m.text(1) ?? '', m.text(2) ?? ''));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
get dialogId(): string | undefined {
|
|
268
|
+
return this.find('HNHBK')?.text(3);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
find(type: string): Segment | undefined {
|
|
272
|
+
return this.segments.find((s) => s.type === type);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
findAll(type: string): Segment[] {
|
|
276
|
+
return this.segments.filter((s) => s.type === type);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function feedbackOf(s: Segment): FinTsFeedback[] {
|
|
281
|
+
const feedback: FinTsFeedback[] = [];
|
|
282
|
+
for (let i = 1; i < s.length; i++) {
|
|
283
|
+
const code = s.text(i);
|
|
284
|
+
if (code === undefined) continue;
|
|
285
|
+
const parameters: string[] = [];
|
|
286
|
+
// Empty parameters stay, as empty text, so that each keeps its position.
|
|
287
|
+
for (let j = 3; j < s.group(i).length; j++) parameters.push(s.text(i, j) ?? '');
|
|
288
|
+
feedback.push(new FinTsFeedback({ code, reference: s.text(i, 1), text: s.text(i, 2) ?? '', parameters }));
|
|
289
|
+
}
|
|
290
|
+
return feedback;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Unpacks the transport encoding: PIN/TAN servers answer in base64, a few with the raw message.
|
|
295
|
+
*
|
|
296
|
+
* Returns anything else unchanged, for `IncomingMessage3.parse` to reject.
|
|
297
|
+
*/
|
|
298
|
+
export function decodeTransport(body: Uint8Array): Uint8Array {
|
|
299
|
+
if (startsWithMessageHeader(body)) return body;
|
|
300
|
+
try {
|
|
301
|
+
return decodeBase64Lines(latin1Decode(body));
|
|
302
|
+
} catch (error) {
|
|
303
|
+
if (error instanceof FormatError) return body;
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const messageHeader = latin1Encode('HNHBK');
|
|
309
|
+
const whitespaceBytes = new Set([0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x20]);
|
|
310
|
+
|
|
311
|
+
/** Whether `body` is a raw message: its header segment comes first, after any white space. */
|
|
312
|
+
function startsWithMessageHeader(body: Uint8Array): boolean {
|
|
313
|
+
let start = 0;
|
|
314
|
+
while (start < body.length && whitespaceBytes.has(body[start]!)) start++;
|
|
315
|
+
if (body.length - start < messageHeader.length) return false;
|
|
316
|
+
return messageHeader.every((byte, i) => body[start + i] === byte);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function encodeTransport(message: Uint8Array): Uint8Array {
|
|
320
|
+
return latin1Encode(base64Encode(message));
|
|
321
|
+
}
|