All files / core/blong-gogo/src/adapter/server/slack slackInIdentity.ts

30.55% Statements 22/72
100% Branches 0/0
0% Functions 0/1
30.55% Lines 22/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x                                                                                                    
import type {IChatAccountMap, VocabularyProxy} from '@feasibleone/blong/types';
import {handler, type IMeta} from '@feasibleone/blong/types';
 
import {chatAccountFind} from '../../../chatAccounts.ts';
 
/**
 * `slackIn.identity` — is this request Slack's, and which account is it for?
 *
 * Runs before the payload is read: the signature is checked over the raw bytes, the account is
 * resolved from the two path parameters, and the platform's own event id is handed back so the
 * receiver can drop a retry without processing it twice (`@@D-676`).
 */
interface IHookRequest {
    params?: {appId?: string; clientId?: string; action?: string};
    headers: Record<string, string | string[] | undefined>;
    rawBody?: Buffer;
    body?: unknown;
}
 
const header = (value: string | string[] | undefined): string | undefined =>
    typeof value === 'string' ? value : Array.isArray(value) ? value[0] : undefined;
 
export default handler(({lib: {error, slackSignatureMatches}}) => {
    const errors = error({
        'slack.requestAccountMissing': {
            message: 'No Slack account is configured for app {appId} and client {clientId}',
            statusCode: 404,
        },
        'slack.signatureInvalid': {
            message: 'The Slack signature does not match the request body',
            statusCode: 401,
        },
    });

    return async function slackInIdentity(request: IHookRequest, $meta: IMeta) {
        const port = this as unknown as {
            config?: {accounts?: IChatAccountMap};
            log?: {warn?: (entry: unknown) => void};
            /** The framework vocabulary, which is where the missing account is reported. */
            signal?: VocabularyProxy;
        };
        const {appId, clientId} = request.params ?? {};
        const found = chatAccountFind(port.config?.accounts, appId, clientId);
        if (!found) {
            // Said before the throw, because the 404 reaches the platform and nothing else: an app
            // whose Request URL carries ids no account declares looks exactly like a message that was
            // never sent, and the two ids are the whole difference.
            port.signal?.frameworkSlackRequestAccountMissing?.({
                accounts: Object.keys(port.config?.accounts ?? {}).join(', '),
                appId,
                clientId,
            });
            throw errors['slack.requestAccountMissing']({params: {appId, clientId}}, $meta);
        }
        if (
            !slackSignatureMatches({
                secret: found.account.signingSecret,
                timestamp: header(request.headers['x-slack-request-timestamp']),
                signature: header(request.headers['x-slack-signature']),
                rawBody: request.rawBody,
            })
        )
            throw errors['slack.signatureInvalid']({}, $meta);
        return {
            account: found.name,
            appId,
            clientId,
            // `event_id` is Slack's id for a delivery; a retry carries the same one.
            requestId: (request.body as {event_id?: string} | undefined)?.event_id,
        };
    };
});