-
Notifications
You must be signed in to change notification settings - Fork 4
/
fatsecret.js
174 lines (155 loc) · 5.29 KB
/
fatsecret.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* MIT License
*
* Copyright (c) 2017 Joey Jan
*
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*/
'use strict';
const crypto = require('crypto');
const request = require('request');
const API_BASE = 'http://platform.fatsecret.com/rest/server.api';
const OAUTH_REQUEST_TOKEN = 'http://www.fatsecret.com/oauth/request_token';
const OAUTH_ACESS_TOKEN = 'http://www.fatsecret.com/oauth/access_token';
const DEFAULT_PARAMS = {
format : 'json',
oauth_version : '1.0',
oauth_signature_method: 'HMAC-SHA1'
};
class FatSecret {
constructor(key, secret) {
this.key = 'cd2baf077bbb4c3394c91f742376e111';
this.secret = '1aa545ed08c54854b817cb05315f9972';
}
/**
* Use this function to call any fatsecret api method. Refer to their api docs on what params to pass
* @param methodToCall
* @param args
* @returns {Promise}
*/
method(methodToCall, args) {
args.method = methodToCall;
return this._doRequest(args)
}
/**
* Set the users token and secret to use for future calls.
* @param token
* @param secret
* @returns {FatSecret}
*/
setUserAuth(token, secret) {
this.user_token = token;
this.user_secret = secret;
return this;
}
/**
* Generate an oauth permission request url. Uses oob option instead of a redirect.
* @returns {string}
*/
getOauthUrl() {
let params = {
oauth_nonce : crypto.randomBytes(10).toString('HEX'),
oauth_version : '1.0',
oauth_callback : 'oob',
oauth_timestamp : Math.floor((new Date()).getTime() / 1000),
oauth_consumer_key : this.key,
oauth_signature_method: 'HMAC-SHA1'
};
return this._signRequest(OAUTH_REQUEST_TOKEN, params);
}
/**
* Get a token and secret for a user given an oob code
* @param token
* @param secret
* @param code
* @returns {string}
*/
getAccessToken(token, secret, code) {
let params = {
oauth_token : token,
oauth_nonce : crypto.randomBytes(10).toString('HEX'),
user_secret : secret,
oauth_version : '1.0',
oauth_verifier : code,
oauth_timestamp : Math.floor((new Date()).getTime() / 1000),
oauth_consumer_key : this.key,
oauth_signature_method: 'HMAC-SHA1'
};
return this._signRequest(OAUTH_ACESS_TOKEN, params);
}
/**
* Perform the request to fatsecret with default params merged in.
* @param params
* @returns {Promise}
* @private
*/
_doRequest(params) {
return new Promise((resolve, reject) => {
request({
uri : this._signRequest(API_BASE, Object.assign({}, DEFAULT_PARAMS, params)),
json: true
}, function (err, response, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
});
});
}
/**
* Calculates and appends the signature hash to the query params
* @param baseUrl
* @param params
* @returns {string}
* @private
*/
_signRequest(baseUrl, params) {
let qs = '';
let secret = this.secret + '&';
params['oauth_nonce'] = crypto.randomBytes(10).toString('HEX');
params['oauth_timestamp'] = Math.floor((new Date()).getTime() / 1000);
params['oauth_consumer_key'] = this.key;
// if we have user auth info, add it to the request
if (this.user_token) {
params['oauth_token'] = this.user_token;
}
// if a user secret was passed in or set, add it to the secret for the hmac
if (params.user_secret) {
secret += params.user_secret;
delete params.user_secret;
} else if (this.user_secret) {
secret += this.user_secret;
}
// build the sorted key value pair string that will be used for the hmac and request
Object
.keys(params)
.sort()
.forEach(param => qs += '&' + param + '=' + encodeURIComponent(params[param]));
//remove first &
qs = qs.substr(1);
// generate the hmac
let mac = crypto.createHmac('sha1', secret);
mac.update('GET&' + encodeURIComponent(baseUrl) + '&' + encodeURIComponent(qs));
// add the generated signature to the request params and return it
qs = baseUrl + '?' + qs + '&oauth_signature=' + encodeURIComponent(mac.digest('base64'));
return qs;
}
}
module.exports = FatSecret;