-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
130 lines (115 loc) · 3.43 KB
/
index.js
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
127
128
129
130
const cookie = require('cookie');
const nonce = require('nonce')();
const request = require('request-promise');
const fs = require('fs');
const ShopifyToken = require('shopify-token');
const crypto = require('crypto');
const querystring = require('querystring');
const SHOPIFY_OAUTH_PATH = '/shopify';
const SHOPIFY_LOGIN_PATH = '/shopify/login'
const hmac256Validation = ({ req, res, shopifyApiSecret }) => {
const hmac = req.query.hmac || req.headers['x-shopify-hmac-sha256'];
const map = Object.assign({}, req.query);
delete map['signature'];
delete map['hmac'];
const message = querystring.stringify(map);
const providedHmac = Buffer.from(hmac, 'utf-8');
const generatedHash = Buffer.from(
crypto
.createHmac('sha256', shopifyApiSecret)
.update(message)
.digest('hex'),
'utf-8'
);
let hashEquals = false;
try {
hashEquals = crypto.timingSafeEqual(generatedHash, providedHmac)
} catch (e) {
hashEquals = false;
};
if (!hashEquals) {
return res.status(400).send('HMAC validation failed');
}
}
module.exports = {
bootstrap({
app,
shopifyAppScopes,
shopifyApiKey,
shopifyApiSecret,
shopifyAppUri,
successCallBack,
}) {
const redirectUri = shopifyAppUri + SHOPIFY_OAUTH_PATH + '/callback';
app.get(SHOPIFY_OAUTH_PATH, (req, res) => {
const shop = req.query.shop;
if (shop) {
const state = nonce();
const installUrl = 'https://' + shop +
'/admin/oauth/authorize?client_id=' + shopifyApiKey +
'&scope=' + shopifyAppScopes +
'&state=' + state +
'&redirect_uri=' + redirectUri
res.cookie('state', state);
res.redirect(installUrl);
} else {
return res.status(400).send('Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request');
}
});
app.get(`${SHOPIFY_OAUTH_PATH}/callback`, (req, res) => {
const { shop, hmac, code, state, host } = req.query;
if (req.headers.cookie) {
const stateCookie = cookie.parse(req.headers.cookie).state;
// if (state !== stateCookie) { // Required.
// return res.status(403).send('Request origin cannot be verified');
// }
}
if (shop && hmac && code) {
// DONE: Validate request is from Shopify
// hmac256Validation({ req, res, shopifyApiSecret });
// DONE: Exchange temporary code for a permanent access token
const accessTokenRequestUrl = 'https://' + shop + '/admin/oauth/access_token';
const accessTokenPayload = {
client_id: shopifyApiKey,
client_secret: shopifyApiSecret,
code,
};
request.post(accessTokenRequestUrl, { json: accessTokenPayload })
.then((accessTokenResponse) => {
const accessToken = accessTokenResponse.access_token;
successCallBack({
accessToken,
shop,
res,
req,
shop,
hmac,
code,
state,
host
});
})
.catch((error) => {
console.error(error)
res.status(error.statusCode).send(error.error);
});
} else {
res.status(400).send('Required parameters missing');
}
});
app.get(SHOPIFY_LOGIN_PATH, (req, res) => {
const { shop } = req.query;
if (!shop) {
res.status(400).send('Shop id such as mystore.myshopify.com is required')
} else {
const shopifyToken = new ShopifyToken({
apiKey: shopifyApiKey,
redirectUri,
sharedSecret: shopifyApiSecret,
});
const storeLoginURL = shopifyToken.generateAuthUrl(shop, shopifyAppScopes);
res.redirect(302, storeLoginURL);
}
})
}
}