├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json ├── proxy.liquid └── server.coffee /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Michael Hewson 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Shopify App-Proxy Example 3 | 4 | Steps to get up and running: 5 | 6 | 1. Create a shopify app if you haven't already. 7 | 2. Add the file "secrets.json" to the directory above this one, filling it with your app's shared secret: 8 | ```json 9 | { 10 | "shopify_shared_secret": "your shared secret here" 11 | } 12 | ``` 13 | 4. `npm install` 14 | 3. Run the server with `node index.js`. Note: your server needs to be publicly accessible so shopify can send proxy requests to it. I recommend a [cloud9 IDE](https://c9.io) workspace. 15 | 4. Add the proxy url `"#{your_server_url}/proxy"` to your shopify app on the app settings page. 16 | 5. Install the app on your shopify store. 17 | 18 | Now you should be able to visit the proxy url on your store, and get a response like this: 19 | 20 | >This is the proxy page for #{your_store_name}. 21 | 22 | ## Signature Verification 23 | 24 | If you visit `#{your_server_url}/proxy` directy, you will get a signature validation error: 25 | 26 | >Signature validation for shopify proxy request failed 27 | 28 | ## License 29 | MIT (see LICENSE file) -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('coffee-script/register'); 2 | require('./server'); 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shopify-app-proxy-demo", 3 | "version": "0.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "dependencies": { 7 | "coffee": "^3.2.1", 8 | "express": "^4.13.4" 9 | }, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "author": "Michael Hewson", 14 | "license": "MIT" 15 | } 16 | -------------------------------------------------------------------------------- /proxy.liquid: -------------------------------------------------------------------------------- 1 | {% layout none %} 2 |
3 | This is the proxy page for {{ shop.name }}. 4 | {{ shop.name }} has {{ shop.products_count }} {{ shop.products_count | pluralize: 'product', 'products' }}. 5 | List of products in the store: 6 | {% for product in collections['all'].products %} - {{ product.title }}8 | -------------------------------------------------------------------------------- /server.coffee: -------------------------------------------------------------------------------- 1 | process = require 'process' 2 | express = require 'express' 3 | {inspect} = require 'util' 4 | crypto = require 'crypto' 5 | fs = require 'fs' 6 | querystring = require 'querystring' 7 | 8 | secrets = JSON.parse(fs.readFileSync('../secrets.json', 'utf-8')) 9 | 10 | app = express() 11 | 12 | app.get '/', (req, res) -> 13 | res.status(200).send('this is the home page') 14 | 15 | app.use '/proxy', (req, res, next) -> 16 | query_string = req.url.match(/\?(.*)/)?[1] ? '' 17 | query = querystring.parse(query_string) 18 | signature = query.signature ? '' 19 | delete query.signature 20 | input = 21 | Object.keys(query).sort() 22 | .map (key) -> 23 | value = query[key] 24 | value = [value] unless Array.isArray(value) 25 | "#{key}=#{value.join(',')}" 26 | .join('') 27 | hash = 28 | crypto.createHmac('sha256', secrets.shopify_shared_secret) 29 | .update(input) 30 | .digest('hex') 31 | if signature != hash 32 | res.status(403).send("Signature verification for shopify proxy request failed") 33 | else 34 | next() 35 | null 36 | 37 | app.get '/proxy', (req, res) -> 38 | res.set('Content-Type', 'application/liquid') 39 | .sendFile("proxy.liquid", root: '.') 40 | 41 | 42 | require('http').createServer(app).listen(process.env.PORT, process.env.IP) 43 | --------------------------------------------------------------------------------
{% endfor %} 7 |