@theokit/gateway-sms 0.4.0 → 0.6.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 +12 -0
- package/README.md +39 -0
- package/dist/index.cjs +163 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +53 -1
- package/dist/index.d.ts +53 -1
- package/dist/index.js +163 -15
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -616,6 +616,24 @@ var SMSAdapter = class extends BasePlatformAdapter {
|
|
|
616
616
|
async deliver(event) {
|
|
617
617
|
return this.runHandler(this.inboundHandler, event);
|
|
618
618
|
}
|
|
619
|
+
/**
|
|
620
|
+
* Deliver like {@link deliver}, but a throwing handler is logged by name only: its message can
|
|
621
|
+
* carry the inbound text, which the webhook route must not write to a log line.
|
|
622
|
+
*
|
|
623
|
+
* @internal
|
|
624
|
+
*/
|
|
625
|
+
async deliverQuietly(event) {
|
|
626
|
+
const handler = this.inboundHandler;
|
|
627
|
+
if (handler === void 0) return "no_handler";
|
|
628
|
+
try {
|
|
629
|
+
await handler(event);
|
|
630
|
+
} catch {
|
|
631
|
+
process.stderr.write(`[sms] handler threw
|
|
632
|
+
`);
|
|
633
|
+
return "handler_threw";
|
|
634
|
+
}
|
|
635
|
+
return "ok";
|
|
636
|
+
}
|
|
619
637
|
};
|
|
620
638
|
function secretFromOpts(opts) {
|
|
621
639
|
switch (opts.backend) {
|
|
@@ -632,8 +650,8 @@ function secretFromOpts(opts) {
|
|
|
632
650
|
}
|
|
633
651
|
|
|
634
652
|
// src/parse-inbound.ts
|
|
635
|
-
function parseInbound(
|
|
636
|
-
const backend = createBackend(
|
|
653
|
+
function parseInbound(options2, ctx) {
|
|
654
|
+
const backend = createBackend(options2);
|
|
637
655
|
let inbound;
|
|
638
656
|
try {
|
|
639
657
|
inbound = backend.parseInbound(ctx);
|
|
@@ -641,7 +659,7 @@ function parseInbound(options, ctx) {
|
|
|
641
659
|
return null;
|
|
642
660
|
}
|
|
643
661
|
if (inbound.messageId === "") return null;
|
|
644
|
-
return inboundToMessageEvent(inbound,
|
|
662
|
+
return inboundToMessageEvent(inbound, options2.backend);
|
|
645
663
|
}
|
|
646
664
|
function decode(s) {
|
|
647
665
|
return decodeURIComponent(s.replace(/\+/g, " "));
|
|
@@ -665,27 +683,157 @@ function sign(authToken, url, pairs) {
|
|
|
665
683
|
for (const [k, v] of sorted) data += k + v;
|
|
666
684
|
return createHmac("sha1", authToken).update(data, "utf8").digest("base64");
|
|
667
685
|
}
|
|
686
|
+
function verifyTwilioSignature(authToken, url, rawBody, signature) {
|
|
687
|
+
if (signature === void 0 || signature.length === 0) return false;
|
|
688
|
+
let pairs;
|
|
689
|
+
try {
|
|
690
|
+
pairs = parseForm(rawBody);
|
|
691
|
+
} catch {
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
const expected = Buffer.from(sign(authToken, url, pairs));
|
|
695
|
+
const actual = Buffer.from(signature);
|
|
696
|
+
return expected.length === actual.length && timingSafeEqual(expected, actual);
|
|
697
|
+
}
|
|
668
698
|
function twilioRequestVerifier(opts) {
|
|
669
699
|
return async (request) => {
|
|
670
700
|
const header = request.headers.get("x-twilio-signature");
|
|
671
701
|
if (header === null || header.length === 0) {
|
|
672
702
|
return { ok: false, reason: "missing X-Twilio-Signature header" };
|
|
673
703
|
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
const actual = Buffer.from(header);
|
|
682
|
-
if (expected.length !== actual.length || !timingSafeEqual(expected, actual)) {
|
|
683
|
-
return { ok: false, reason: "signature mismatch" };
|
|
684
|
-
}
|
|
704
|
+
const ok = verifyTwilioSignature(
|
|
705
|
+
opts.authToken,
|
|
706
|
+
opts.publicUrl ?? request.url,
|
|
707
|
+
await request.text(),
|
|
708
|
+
header
|
|
709
|
+
);
|
|
710
|
+
if (!ok) return { ok: false, reason: "signature mismatch" };
|
|
685
711
|
return { ok: true };
|
|
686
712
|
};
|
|
687
713
|
}
|
|
688
714
|
|
|
715
|
+
// src/fetch-webhook.ts
|
|
716
|
+
var options = /* @__PURE__ */ new WeakMap();
|
|
717
|
+
function rememberSmsOptions(adapter, opts) {
|
|
718
|
+
options.set(adapter, opts);
|
|
719
|
+
}
|
|
720
|
+
function statusFor(outcomes) {
|
|
721
|
+
let status = 200;
|
|
722
|
+
for (const outcome of outcomes) {
|
|
723
|
+
if (outcome === "handler_threw") status = 500;
|
|
724
|
+
else if (outcome === "no_handler" && status === 200) status = 503;
|
|
725
|
+
}
|
|
726
|
+
return status;
|
|
727
|
+
}
|
|
728
|
+
function lowerHeaders(headers) {
|
|
729
|
+
const out = {};
|
|
730
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
731
|
+
if (value !== void 0) out[key.toLowerCase()] = value;
|
|
732
|
+
}
|
|
733
|
+
return out;
|
|
734
|
+
}
|
|
735
|
+
function verified(opts, request) {
|
|
736
|
+
if (opts === void 0 || opts.authToken.trim().length === 0) return false;
|
|
737
|
+
return verifyTwilioSignature(
|
|
738
|
+
opts.authToken,
|
|
739
|
+
opts.publicUrl,
|
|
740
|
+
request.rawBody,
|
|
741
|
+
lowerHeaders(request.headers)["x-twilio-signature"]
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
function toEvent(opts, rawBody, headers) {
|
|
745
|
+
return parseInbound(opts, { headers, rawBody, url: opts.publicUrl });
|
|
746
|
+
}
|
|
747
|
+
var twilioWebhook = {
|
|
748
|
+
methods: ["POST"],
|
|
749
|
+
/** The same check `handle` makes; needs no connection. */
|
|
750
|
+
verify(adapter, request) {
|
|
751
|
+
return verified(options.get(adapter), request);
|
|
752
|
+
},
|
|
753
|
+
/** A payload of form fields (as an object) is parsed like the form body and delivered. */
|
|
754
|
+
async ingest(adapter, payload) {
|
|
755
|
+
const opts = options.get(adapter);
|
|
756
|
+
if (opts === void 0 || payload === null || typeof payload !== "object") return "malformed";
|
|
757
|
+
const form = new URLSearchParams();
|
|
758
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
759
|
+
if (typeof value === "string") form.set(key, value);
|
|
760
|
+
}
|
|
761
|
+
const event = toEvent(opts, form.toString(), {});
|
|
762
|
+
if (event === null) return "malformed";
|
|
763
|
+
return statusFor([await adapter.deliverQuietly(event)]) === 200 ? "delivered" : "no_handler";
|
|
764
|
+
},
|
|
765
|
+
async handle(adapter, request) {
|
|
766
|
+
const opts = options.get(adapter);
|
|
767
|
+
if (opts === void 0 || !verified(opts, request)) return { status: 401 };
|
|
768
|
+
const event = toEvent(opts, request.rawBody, lowerHeaders(request.headers));
|
|
769
|
+
if (event === null) return { status: 400 };
|
|
770
|
+
return { status: statusFor([await adapter.deliverQuietly(event)]) };
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
|
|
774
|
+
// src/channel.ts
|
|
775
|
+
var KEYS = ["accountSid", "authToken", "fromNumber", "publicUrl"];
|
|
776
|
+
function readString(config, key) {
|
|
777
|
+
const value = config[key];
|
|
778
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
779
|
+
throw new ConfigurationError({
|
|
780
|
+
code: "invalid_config",
|
|
781
|
+
message: `gateway-sms: config.${key} must be a non-empty string`,
|
|
782
|
+
detail: key
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
return value;
|
|
786
|
+
}
|
|
787
|
+
var CONFIG = [
|
|
788
|
+
{
|
|
789
|
+
key: "accountSid",
|
|
790
|
+
required: true,
|
|
791
|
+
secret: false,
|
|
792
|
+
env: "SMS_ACCOUNT_SID",
|
|
793
|
+
description: "The Twilio account SID."
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
key: "authToken",
|
|
797
|
+
required: true,
|
|
798
|
+
secret: true,
|
|
799
|
+
env: "SMS_AUTH_TOKEN",
|
|
800
|
+
description: "The Twilio auth token; also verifies inbound signatures."
|
|
801
|
+
},
|
|
802
|
+
{
|
|
803
|
+
key: "fromNumber",
|
|
804
|
+
required: true,
|
|
805
|
+
secret: false,
|
|
806
|
+
env: "SMS_FROM_NUMBER",
|
|
807
|
+
description: "The bot's own E.164 number, used as the sender."
|
|
808
|
+
},
|
|
809
|
+
{
|
|
810
|
+
key: "publicUrl",
|
|
811
|
+
required: true,
|
|
812
|
+
secret: false,
|
|
813
|
+
env: "SMS_PUBLIC_URL",
|
|
814
|
+
description: "The public URL Twilio posts inbound messages to."
|
|
815
|
+
}
|
|
816
|
+
];
|
|
817
|
+
var smsChannel = {
|
|
818
|
+
platform: "sms",
|
|
819
|
+
transport: "webhook",
|
|
820
|
+
webhook: twilioWebhook,
|
|
821
|
+
config: CONFIG,
|
|
822
|
+
create(config) {
|
|
823
|
+
const [accountSid, authToken, fromNumber, publicUrl] = KEYS.map((k) => readString(config, k));
|
|
824
|
+
const opts = {
|
|
825
|
+
backend: "twilio",
|
|
826
|
+
accountSid,
|
|
827
|
+
authToken,
|
|
828
|
+
fromNumber,
|
|
829
|
+
publicUrl
|
|
830
|
+
};
|
|
831
|
+
const adapter = new SMSAdapter(opts);
|
|
832
|
+
rememberSmsOptions(adapter, opts);
|
|
833
|
+
return adapter;
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
|
|
689
837
|
// src/twiml.ts
|
|
690
838
|
var ILLEGAL = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;
|
|
691
839
|
function esc(s) {
|
|
@@ -797,6 +945,6 @@ function smsWebhookVerifier(adapter, opts = {}) {
|
|
|
797
945
|
};
|
|
798
946
|
}
|
|
799
947
|
|
|
800
|
-
export { BackendNotInstalledError, ConfigurationError, SMSAdapter, createWebhookServer, normalizeE164, parseInbound, smsWebhookVerifier, splitForSMS, twilioRequestVerifier, twiml };
|
|
948
|
+
export { BackendNotInstalledError, ConfigurationError, SMSAdapter, createWebhookServer, normalizeE164, parseInbound, smsChannel, smsWebhookVerifier, splitForSMS, twilioRequestVerifier, twiml };
|
|
801
949
|
//# sourceMappingURL=index.js.map
|
|
802
950
|
//# sourceMappingURL=index.js.map
|