forked from MONEI/Shopify-api-node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
220 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
Session = require './session-oauth' | ||
|
||
|
||
exports.createSession = (url, token, apiKey, secret, params) -> | ||
new Session(url, token, apiKey, secret, params) | ||
exports.createSession = (shopname, permanent_token) -> | ||
new (require __dirname + "/session")(shopname, permanent_token) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,42 @@ | ||
crypto = require 'crypto' | ||
Blog = require './resources/blog' | ||
Product = require './resources/product' | ||
Order = require './resources/order' | ||
|
||
trim = (string) -> | ||
string.replace(/^\s\s*/, '').replace(/\s\s*$/, '') | ||
|
||
empty = (string)-> | ||
string = trim(string) | ||
string.length is 0 | ||
|
||
sortObj = (o) -> | ||
sorted = {} | ||
a = [] | ||
|
||
for key of o | ||
if o.hasOwnProperty key | ||
a.push key | ||
|
||
a.sort() | ||
|
||
for key in [0..a.length] | ||
sorted[a[key]] = o[a[key]] | ||
|
||
return sorted | ||
|
||
isNumeric = (n) -> | ||
!isNaN(parseFloat(n)) and isFinite(n) | ||
|
||
Resource = require './resource' | ||
|
||
class Session | ||
|
||
protocol: "https" | ||
|
||
constructor: (@url, @token = '', @apiKey, @secret, @params = {}) -> | ||
@token = @url if empty(@token) | ||
if @params['signature']? | ||
timestamp = (new Date(@params['timestamp'])).getTime() | ||
expireTime = (new Date).getTime() - (24 * 84600) | ||
if not @validateSignature(@params) and expireTime > timestamp | ||
throw new Error 'Invalid signature: Possible malicious login.' | ||
|
||
@url = @prepareUrl(@url) | ||
|
||
if @valid | ||
@blog = new Blog(@site()) | ||
@product = new Product(@site()) | ||
@order = new Order(@site()) | ||
|
||
createPermissionUrl: -> | ||
"http://#{@url}/admin/api/auth?api_key=#{@apiKey}" if not empty(@url) and not empty(@apiKey) | ||
|
||
site: -> | ||
"#{@protocol}://#{@apiKey}:#{@computedPassword()}@#{@url}/admin" | ||
|
||
valid: -> | ||
not empty(@url) and not empty(@token) | ||
|
||
computedPassword: -> | ||
crypto.createHash('md5').update("#{@secret}#{@token}").digest("hex") | ||
|
||
prepareUrl: (url) -> | ||
return '' if empty(url) | ||
url.replace /https?:\/\//, '' | ||
url += '.myshopify.com' unless url.indexOf(".") isnt -1 | ||
return url | ||
|
||
validateSignature: (params) -> | ||
@signature = params['signature'] | ||
generatedSignature = @secret | ||
params = sortObj(params) | ||
for k, v of params | ||
if k isnt "signature" and k isnt "action" and k isnt "controller" and not isNumeric(k) and k? | ||
generatedSignature += "#{k}=#{v}" | ||
|
||
generatedSignature = generatedSignature.replace(new RegExp("undefined=undefined"), '') | ||
generatedSignature = crypto.createHash('md5').update("#{generatedSignature}").digest("hex") | ||
generatedSignature is @signature | ||
|
||
|
||
|
||
|
||
|
||
|
||
constructor:(@store_name, @persistent_token)-> | ||
@protocol = 'https' | ||
@registerOAuthToken() | ||
@blog = new Blog(@site()) | ||
@product = new Product(@site()) | ||
@order = new Order(@site()) | ||
|
||
|
||
onRedirectUrl:(url, cb)-> | ||
url.replace /\?code=[\w\d]+/, (code)=> | ||
temp_token = code.split('=')[1] | ||
@requestPermanentAccessToken temp_token, (@persistent_token)=> | ||
@registerOAuthToken() | ||
process.nextTick -> | ||
do cb | ||
|
||
requestPermanentAccessToken:(temp_token, cb)-> | ||
params = "client_id=#{@api_key}&client_secret=#{@secret}&code=#{temp_token}" | ||
Resource.post "#{@site()}/oauth/access_token", 'oauth', params, (err, response)=> | ||
if err? | ||
throw err | ||
return | ||
response = JSON.parse response | ||
process.nextTick -> | ||
cb response.access_token | ||
|
||
|
||
site:()-> | ||
"#{@protocol}://#{@store_name}.myshopify.com/admin" | ||
|
||
registerOAuthToken:()-> | ||
if @persistent_token isnt null | ||
Resource.setOAuthToken @persistent_token | ||
|
||
module.exports = Session |