-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.ts
86 lines (74 loc) · 2.38 KB
/
handler.ts
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
import { Context } from "@azure/functions";
import {
EncryptedPayload,
toEncryptedPayload
} from "@pagopa/ts-commons/lib/encrypt";
import { IPString, NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import * as E from "fp-ts/lib/Either";
import { pipe } from "fp-ts/lib/function";
import * as t from "io-ts";
import { UTCISODateFromString } from "@pagopa/ts-commons/lib/dates";
import { readableReport } from "@pagopa/ts-commons/lib/reporters";
import { sequenceS } from "fp-ts/lib/Apply";
import { SpidMsgItem } from "./index";
/**
* Payload of the stored blob item
* (one for each SPID request or response).
*/
const SpidBlobItem = t.interface({
// Timestamp of Request/Response creation
createdAt: UTCISODateFromString,
// IP of the client that made a SPID login action
ip: IPString,
// XML payload of the SPID Request
// eslint-disable-next-line sort-keys
encryptedRequestPayload: EncryptedPayload,
// XML payload of the SPID Response
encryptedResponsePayload: EncryptedPayload,
// SPID request ID
spidRequestId: t.string
});
export type SpidBlobItem = t.TypeOf<typeof SpidBlobItem>;
export interface IOutputBinding {
readonly spidRequestResponse: SpidBlobItem;
}
export const encryptAndStore = async (
context: Context,
spidMsgItem: SpidMsgItem,
spidLogsPublicKey: NonEmptyString
): Promise<void | IOutputBinding> => {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const encrypt = (plainText: string) =>
toEncryptedPayload(spidLogsPublicKey, plainText);
return pipe(
sequenceS(E.Applicative)({
encryptedRequestPayload: encrypt(spidMsgItem.requestPayload),
encryptedResponsePayload: encrypt(spidMsgItem.responsePayload)
}),
E.map(item => ({
...spidMsgItem,
...item
})),
E.fold(
err =>
context.log.error(`StoreSpidLogs|ERROR=Cannot encrypt payload|${err}`),
(encryptedBlobItem: SpidBlobItem) =>
pipe(
t.exact(SpidBlobItem).decode(encryptedBlobItem),
E.fold(
errs => {
// unrecoverable error
context.log.error(
`StoreSpidLogs|ERROR=Cannot decode payload|ERROR_DETAILS=${readableReport(
errs
)}`
);
},
spidBlobItem => ({
spidRequestResponse: spidBlobItem
})
)
)
)
);
};