@whoa-chat/connector 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/dist/src/contract.d.ts +21 -0
- package/dist/src/index.d.ts +41 -0
- package/dist/src/index.js +1 -0
- package/dist/src/merchant.d.ts +24 -0
- package/package.json +32 -4
- package/README.md +0 -3
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const CONNECTOR_VERSION = 1;
|
|
2
|
+
/** Every op is optional: an app answers the ones it supports and 400s the rest. */
|
|
3
|
+
export type ConnectorOp = "merchant" | "reference" | "search" | "actions" | "composer" | "pricing" | "storePanel" | "otherApps" | "markReview" | "reviewStore" | "cardData" | "find" | "storeApps" | "live" | "installs" | "uninstallReasons" | "availability" | "dashboard" | "presence" | "hours" | "gifs" | "gif" | "storeStatus" | "reviewQueue" | "reviewTemplate";
|
|
4
|
+
/** The request the chat sends: the body to post and its token. */
|
|
5
|
+
export declare const signConnectorRequest: (secret: string, req: {
|
|
6
|
+
ws: string;
|
|
7
|
+
op: ConnectorOp;
|
|
8
|
+
shop?: string | null;
|
|
9
|
+
args?: Record<string, unknown>;
|
|
10
|
+
}, nowMs?: number) => {
|
|
11
|
+
body: string;
|
|
12
|
+
token: string;
|
|
13
|
+
};
|
|
14
|
+
export type ConnectorRequest = {
|
|
15
|
+
ws: string;
|
|
16
|
+
op: ConnectorOp;
|
|
17
|
+
shop: string | null;
|
|
18
|
+
body: Record<string, unknown>;
|
|
19
|
+
};
|
|
20
|
+
/** On the app's server: the request when the token is ours, fresh, and matches this exact body; else null. */
|
|
21
|
+
export declare const verifyConnectorRequest: (secret: string | undefined, token: string, rawBody: string, nowMs?: number) => ConnectorRequest | null;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export declare const purposeKey: (secret: string, purpose: string) => NonSharedBuffer;
|
|
2
|
+
export declare const supportChatApi: (_shop?: string) => string | null;
|
|
3
|
+
export declare const contactToken: (shop: string, contact: {
|
|
4
|
+
email: string | null;
|
|
5
|
+
owner: string | null;
|
|
6
|
+
named?: boolean;
|
|
7
|
+
timezone: string | null;
|
|
8
|
+
country?: string | null;
|
|
9
|
+
locale?: string | null;
|
|
10
|
+
}, info?: unknown) => string | null;
|
|
11
|
+
export declare const inboxUrlFor: (shop: string) => string;
|
|
12
|
+
export declare const postChatEvent: (shop: string, event: {
|
|
13
|
+
type: string;
|
|
14
|
+
title: string;
|
|
15
|
+
fields: {
|
|
16
|
+
label: string;
|
|
17
|
+
value: string;
|
|
18
|
+
url?: string;
|
|
19
|
+
}[];
|
|
20
|
+
activity?: boolean;
|
|
21
|
+
}, name?: string, contact?: string | null) => Promise<void>;
|
|
22
|
+
export type FeatureCardInfo = {
|
|
23
|
+
title: string;
|
|
24
|
+
description?: string;
|
|
25
|
+
requestId?: string;
|
|
26
|
+
addedAt?: string;
|
|
27
|
+
};
|
|
28
|
+
export declare const featureCardCall: (shop: string, feature: {
|
|
29
|
+
op: "get";
|
|
30
|
+
id: string;
|
|
31
|
+
} | {
|
|
32
|
+
op: "added";
|
|
33
|
+
id: string;
|
|
34
|
+
requestId: string;
|
|
35
|
+
}) => Promise<{
|
|
36
|
+
card?: FeatureCardInfo;
|
|
37
|
+
added?: boolean;
|
|
38
|
+
} | null>;
|
|
39
|
+
export declare const verifyTeamToken: (token: string) => string | null;
|
|
40
|
+
export * from "./contract";
|
|
41
|
+
export * from "./merchant";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createHmac as d,timingSafeEqual as Y}from"node:crypto";import{createHash as z,createHmac as b,timingSafeEqual as J}from"node:crypto";var v=1,$="suprchat-connector",M=/^[a-z0-9][a-z0-9-]*\.myshopify\.com$/,P=e=>b("sha256",e).update(`suprchat:${$}`).digest(),R=e=>z("sha256").update(e).digest("base64url"),A=e=>Buffer.from(JSON.stringify(e)).toString("base64url"),K=(e,t,r=Date.now())=>{let a=t.shop??null,i=JSON.stringify({...t.args,op:t.op,...a?{shop:a}:{}}),s=Math.floor(r/1e3),l=`${A({alg:"HS256",typ:"JWT"})}.${A({purpose:$,v,ws:t.ws,op:t.op,shop:a,bh:R(i),iat:s,exp:s+60})}`,o=`${l}.${b("sha256",P(e)).update(l).digest("base64url")}`;return{body:i,token:o}},W=(e,t,r,a=Date.now())=>{let[i,s,l]=t.split(".");if(!e||!i||!s||!l)return null;let o=b("sha256",P(e)).update(`${i}.${s}`).digest(),p=Buffer.from(l,"base64url");if(p.length!==o.length||!J(p,o))return null;try{let n=JSON.parse(Buffer.from(s,"base64url").toString("utf8")),c=a/1e3;if(n.purpose!==$||n.v!==v||typeof n.ws!="string"||typeof n.op!="string"||typeof n.exp!="number"||n.exp<c||n.exp>c+300||n.bh!==R(r))return null;let u=JSON.parse(r),g=n.shop===null?null:typeof n.shop=="string"&&M.test(n.shop)?n.shop:void 0;return g===void 0||u.op!==n.op||(u.shop??null)!==g?null:{ws:n.ws,op:n.op,shop:g,body:u}}catch{return null}};import{createHmac as x,timingSafeEqual as B}from"node:crypto";var y="whoa-merchant",I=1,O=3600,F=60,T=6e3,C=/^[a-z0-9][a-z0-9-]*\.myshopify\.com$/,U=/^[^\s@<>",;]{1,64}@[^\s@<>",;]{1,190}\.[a-z]{2,}$/i,H=e=>x("sha256",e).update(`whoa:${y}`).digest(),E=e=>Buffer.from(JSON.stringify(e)).toString("base64url"),Z=(e,t,r={})=>{if(!e)throw new Error("No chat secret");if(!C.test(t.shop))throw new Error("Not a myshopify.com domain");let a=Math.floor((r.nowMs??Date.now())/1e3),i=a+Math.min(Math.max(r.ttlS??O,60),O),s=t.store&&JSON.stringify(t.store).length<=T?t.store:void 0,l={purpose:y,v:I,app:t.app,shop:t.shop,email:t.email,name:t.name??null,locale:t.locale??null,tz:t.timezone??null,country:t.country??null,...s?{store:s}:{},iat:a,exp:i},o=`${E({alg:"HS256",typ:"JWT"})}.${E(l)}`;return`${o}.${x("sha256",H(e)).update(o).digest("base64url")}`},N=e=>{let t=e.split(".")[1];if(!t)return null;try{let r=JSON.parse(Buffer.from(t,"base64url").toString("utf8"));return r&&typeof r=="object"&&!Array.isArray(r)?r:null}catch{return null}},X=e=>{let t=N(e);return t?.purpose===y&&typeof t.app=="string"&&/^[A-Za-z0-9_-]{1,100}$/.test(t.app)?t.app:null},V=(e,t,r,a=Date.now())=>{let[i,s,l]=t.split(".");if(!i||!s||!l||t.split(".").length!==3)return null;let o=Buffer.from(l,"base64url");if(!e.filter(Boolean).some(f=>{let h=x("sha256",H(f)).update(`${i}.${s}`).digest();return o.length===h.length&&B(o,h)}))return null;try{if(JSON.parse(Buffer.from(i,"base64url").toString("utf8")).alg!=="HS256")return null}catch{return null}let n=N(t),c=a/1e3;if(!n||n.purpose!==y||n.v!==I||n.app!==r||typeof n.iat!="number"||typeof n.exp!="number"||n.exp<c||n.iat>c+F||n.exp-n.iat>O||typeof n.shop!="string"||!C.test(n.shop)||typeof n.email!="string"||!U.test(n.email))return null;let u=(f,h)=>typeof f=="string"&&f.trim()?f.trim().slice(0,h):null,g=u(n.tz,64),S=u(n.locale,20),w=u(n.country,2),k=n.store&&typeof n.store=="object"&&!Array.isArray(n.store)&&JSON.stringify(n.store).length<=T?n.store:null;return{app:r,shop:n.shop,email:n.email,name:u(n.name,120),timezone:g&&/^[A-Za-z_]+(\/[A-Za-z0-9_+-]+){0,2}$/.test(g)?g:null,locale:S&&/^[a-z]{2,3}(-[A-Za-z0-9]{2,8})?$/.test(S)?S:null,country:w&&/^[A-Z]{2}$/.test(w)?w:null,store:k}};var m=(e,t)=>d("sha256",e).update(`supr-chat:${t}`).digest(),_=()=>process.env.WHOA_CHAT_SECRET||process.env.SHOPIFY_API_SECRET,D=e=>process.env.SUPPORT_CHAT_API_URL?.replace(/\/$/,"")||null,tt=(e,t,r)=>{let a=_();if(!a||!t.email)return null;let i=o=>Buffer.from(JSON.stringify(o)).toString("base64url"),s=Math.floor(Date.now()/1e3),l=`${i({alg:"HS256",typ:"JWT"})}.${i({aud:process.env.SHOPIFY_API_KEY,dest:`https://${e}`,purpose:"support-contact",email:t.email,owner:t.owner,named:t.named===!0,tz:t.timezone,country:t.country??null,locale:t.locale??null,app:process.env.SHOPIFY_APP_URL,...r?{info:r}:{},nbf:s-5,exp:s+6*3600})}`;return`${l}.${d("sha256",m(a,"support-contact")).update(l).digest("base64url")}`},nt=e=>`${(process.env.SUPPORT_CHAT_INBOX_URL??"https://app.whoa.chat").replace(/\/$/,"")}/?shop=${encodeURIComponent(e)}`,et=async(e,t,r,a)=>{let i=process.env.SUPPORT_CHAT_API_URL?.replace(/\/$/,""),s=_();if(!i||!s)return;let l=u=>Buffer.from(JSON.stringify(u)).toString("base64url"),o=Math.floor(Date.now()/1e3),p=`${l({alg:"HS256",typ:"JWT"})}.${l({aud:process.env.SHOPIFY_API_KEY,dest:`https://${e}`,purpose:"support-event",event:t,name:r,nbf:o-5,exp:o+120})}`,n=`${p}.${d("sha256",m(s,"support-event")).update(p).digest("base64url")}`,c=await fetch(`${i}/v1/app/events`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({token:n,...a?{contact:a}:{}}),signal:AbortSignal.timeout(8e3)});if(!c.ok)throw new Error(`chat event ${c.status}`)},rt=async(e,t)=>{let r=D(e),a=_();if(!r||!a)return null;let i=n=>Buffer.from(JSON.stringify(n)).toString("base64url"),s=Math.floor(Date.now()/1e3),l=`${i({alg:"HS256",typ:"JWT"})}.${i({aud:process.env.SHOPIFY_API_KEY,dest:`https://${e}`,purpose:"support-feature",feature:t,nbf:s-5,exp:s+120})}`,o=`${l}.${d("sha256",m(a,"support-feature")).update(l).digest("base64url")}`,p=await fetch(`${r}/v1/app/feature-card`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({token:o}),signal:AbortSignal.timeout(8e3)});if(p.status===404)return null;if(!p.ok)throw new Error(`chat feature card ${p.status}`);return await p.json()},st=e=>{let t=process.env.SHOPIFY_API_SECRET,[r,a,i]=e.split(".");if(!t||!r||!a||!i)return null;let s=d("sha256",m(t,"support-chat-team")).update(`${r}.${a}`).digest(),l=Buffer.from(i,"base64url");if(l.length!==s.length||!Y(l,s))return null;try{let o=JSON.parse(Buffer.from(a,"base64url").toString("utf8")),p=Date.now()/1e3;if(o.purpose!=="support-chat-team"||o.aud!==process.env.SHOPIFY_API_KEY||typeof o.exp!="number"||o.exp<p||o.exp>p+300)return null;let n=new URL(String(o.dest)).hostname.toLowerCase();return/^[a-z0-9][a-z0-9-]*\.myshopify\.com$/.test(n)?n:null}catch{return null}};export{v as CONNECTOR_VERSION,y as MERCHANT_PURPOSE,I as MERCHANT_TOKEN_VERSION,tt as contactToken,rt as featureCardCall,nt as inboxUrlFor,X as merchantTokenApp,et as postChatEvent,m as purposeKey,K as signConnectorRequest,Z as signMerchant,D as supportChatApi,W as verifyConnectorRequest,V as verifyMerchant,st as verifyTeamToken};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const MERCHANT_PURPOSE = "whoa-merchant";
|
|
2
|
+
export declare const MERCHANT_TOKEN_VERSION = 1;
|
|
3
|
+
/** What an app says about the merchant in front of it. store: optional details for the team's store panel. */
|
|
4
|
+
export type MerchantIdentity = {
|
|
5
|
+
app: string;
|
|
6
|
+
shop: string;
|
|
7
|
+
email: string;
|
|
8
|
+
name?: string | null;
|
|
9
|
+
locale?: string | null;
|
|
10
|
+
timezone?: string | null;
|
|
11
|
+
country?: string | null;
|
|
12
|
+
store?: Record<string, unknown> | null;
|
|
13
|
+
};
|
|
14
|
+
/** On the app's server: the token the chat package sends for this merchant. ttlS at most an hour. */
|
|
15
|
+
export declare const signMerchant: (chatSecret: string, m: MerchantIdentity, opts?: {
|
|
16
|
+
ttlS?: number;
|
|
17
|
+
nowMs?: number;
|
|
18
|
+
}) => string;
|
|
19
|
+
/** Unverified: which app a merchant token says it is from, only to find that app's secret. null: not one. */
|
|
20
|
+
export declare const merchantTokenApp: (token: string) => string | null;
|
|
21
|
+
/** In the chat: the merchant when one of the secrets signed this token for that app and it is still valid; else null. */
|
|
22
|
+
export declare const verifyMerchant: (secrets: string[], token: string, app: string, nowMs?: number) => (MerchantIdentity & {
|
|
23
|
+
name: string | null;
|
|
24
|
+
}) | null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whoa-chat/connector",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Whoa Chat for your Shopify app's server: sign who the merchant is, post app events, verify the inbox's connector requests.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"homepage": "https://whoa.chat/docs/",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Super-Simple-Co/whoa-chat.git",
|
|
10
|
+
"directory": "packages/connector"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/src/index.d.ts",
|
|
19
|
+
"default": "./dist/src/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
24
|
+
"prepack": "node ../core/pack.mjs build --bundle",
|
|
25
|
+
"postpack": "node ../core/pack.mjs restore"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md"
|
|
30
|
+
],
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=18"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/README.md
DELETED