Spaces:
Sleeping

File size: 3,408 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import crypto from "crypto";

const STATE_VERSION = "v1";

// ShopifyOauthState.ts
export interface ShopifyOauthStatePayload {
    userId: string;
    organizationId: string;
    shopDomain: string;
    clientId: string;
    clientSecret: string;  
    apiVersion: string;
    nonce: string;
    iat: number;
    exp: number;
}

/**
 * Signs a Shopify OAuth state payload into a compact string token.
 * Format: v1.base64url(payload).HMAC-SHA256(v1.base64url(payload), secret)
 * Note: clientSecret is intentionally excluded from the state payload for security.
 */
export function createSignedShopifyOauthState(
    payload: ShopifyOauthStatePayload,
    secret: string,
): string {
    const json = Buffer.from(JSON.stringify(payload)).toString("base64url");
    const sig = crypto
        .createHmac("sha256", secret)
        .update(`${STATE_VERSION}.${json}`)
        .digest("base64url");
    return `${STATE_VERSION}.${json}.${sig}`;
}

/**
 * Verifies and parses a signed Shopify OAuth state token.
 * Returns null if the signature is invalid or token is expired.
 */
export function verifySignedShopifyOauthState(
    token: string,
    secret: string,
): ShopifyOauthStatePayload | null {
    const parts = token.split(".");
    if (parts.length !== 3) return null;
    const [version, json, sig] = parts;
    if (version !== STATE_VERSION) return null;

    const expected = crypto
        .createHmac("sha256", secret)
        .update(`${version}.${json}`)
        .digest("base64url");

    if (
        !crypto.timingSafeEqual(
            Buffer.from(sig, "base64url"),
            Buffer.from(expected, "base64url"),
        )
    ) {
        return null;
    }

    try {
        const payload = JSON.parse(
            Buffer.from(json, "base64url").toString("utf8"),
        ) as ShopifyOauthStatePayload;

        if (Date.now() > payload.exp) {
            return null;
        }

        return payload;
    } catch {
        return null;
    }
}

/**
 * Verifies the HMAC on a Shopify OAuth callback query string.
 * Shopify signs the query params (excluding `hmac`) using the app's client secret.
 */
export function verifyShopifyCallbackHmac(input: {
    rawQuery: string;
    clientSecret: string;
}): boolean {
    const params = new URLSearchParams(input.rawQuery);
    const receivedHmac = params.get("hmac") ?? "";
    params.delete("hmac");

    const sortedQuery = Array.from(params.entries())
        .sort(([a], [b]) => a.localeCompare(b))
        .map(([k, v]) => `${k}=${v}`)
        .join("&");

    const computed = crypto
        .createHmac("sha256", input.clientSecret)
        .update(sortedQuery)
        .digest("hex");

    try {
        return crypto.timingSafeEqual(
            Buffer.from(computed, "hex"),
            Buffer.from(receivedHmac, "hex"),
        );
    } catch {
        return false;
    }
}

/**
 * Normalizes and validates a Shopify shop domain.
 * Returns the normalized domain (lowercase, no protocol/trailing slash).
 * Throws if the format is invalid.
 */
export function normalizeAndValidateShopDomain(shopDomain: string): string {
    const normalized = shopDomain
        .trim()
        .toLowerCase()
        .replace(/^https?:\/\//, "")
        .replace(/\/+$/, "");

    if (!/^[a-z0-9-]+\.myshopify\.com$/.test(normalized)) {
        throw new Error(`Invalid Shopify shop domain: ${shopDomain}`);
    }

    return normalized;
}