-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.js
294 lines (253 loc) · 9.34 KB
/
twitter.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import express from 'express'
import fetch from 'node-fetch'
import crypto from 'crypto'
const twitter = express.Router()
const consumer_key = process.env.TWITTER_CONSUMER_KEY
const consumer_secret = process.env.TWITTER_CONSUMER_SECRET
const bearer_token = process.env.TWITTER_BEARER_TOKEN
const access_token = process.env.TWITTER_ACCESS_TOKEN
const access_token_secret = process.env.TWITTER_ACCESS_TOKEN_SECRET
console.log("Twitter", {consumer_key, consumer_secret, bearer_token, access_token, access_token_secret})
// For compliance with RFC 3986 (which reserves !, ', (, ), and *)
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}
function collectParameters(params) {
// 1. Percent encode every key and value that will be signed.
var new_params = {}
for (let key in params) {
let new_key = fixedEncodeURIComponent(key)
let new_value = fixedEncodeURIComponent(params[key])
new_params[new_key] = new_value
}
// 2. Sort the list of parameters alphabetically by encoded key .
new_params = Object.keys(new_params).sort().reduce(
(result, key) => {
result[key] = new_params[key];
return result;
}, {}
)
// 3. For each key/value pair:
let param_string = ""
for (let key in new_params) {
// 3.1 Append the encoded key to the output string.
param_string += key
// 3.2 Append the ‘=’ character to the output string.
param_string += '='
// 3.3 Append the encoded value to the output string.
param_string += new_params[key]
// 3.4 If there are more key/value pairs remaining, append a ‘&’ character to the output string.
param_string += '&'
}
param_string = param_string.slice(0,-1) // Remove the last '&'
// console.log("Parameter String", param_string)
return param_string
}
function createSignatureBaseString(param_string, method, url) {
// 4. Creating the signature base string
let base_string = ''
// 4.1 Convert the HTTP Method to uppercase and add to base_string
base_string += method.toUpperCase()
// 4.2 Append the ‘&’ character to the output string.
base_string += '&'
// 4.3 Percent encode the URL and append it to the output string.
base_string += fixedEncodeURIComponent(url)
// 4.4 Append the ‘&’ character to the output string.
base_string += '&'
// 4.5 Percent encode the parameter string and append it to the output string.
base_string += fixedEncodeURIComponent(param_string)
// console.log("Signature Base String", base_string)
return base_string
}
function getSigningKey(con_secret, tok_secret) {
let signing_key = fixedEncodeURIComponent(con_secret) + '&'
if( tok_secret)
signing_key += fixedEncodeURIComponent(tok_secret)
// console.log(signing_key)
return signing_key
}
function calculateSignature(base_string, signing_key) {
let oauth_sign = crypto
.createHmac('sha1', signing_key)
.update(base_string)
.digest('base64')
// console.log(oauth_sign)
return oauth_sign
}
function makeAuthSign(params, method, url, con_secret, tok_secret) {
console.log("Creating_Auth_Signature_for_Twitter")
/* Steps from https://developer.twitter.com/en/docs/authentication/oauth-1-0a/creating-a-signature */
// Collecting Paramters
const param_string = collectParameters(params)
// Creating the signature base string
const base_string = createSignatureBaseString(param_string, method, url)
// 5. Getting a signing key
const signing_key = getSigningKey(con_secret, tok_secret)
// 6. Calculate the signature
const oauth_sign = calculateSignature(base_string, signing_key)
return oauth_sign
}
function buildAuthHeader(params) {
// OAuth requires parameters to be sorted
params = Object.keys(params).sort().reduce(
(result, key) => {
result[key] = params[key];
return result;
}, {}
)
let dst = ""
// 1. Append the string “OAuth ” (including the space at the end) to DST.
dst += "OAuth "
// 2. For each key/value pair of the 7 parameters listed above:
for (let key in params) {
// 2.1 Percent encode the key and append it to DST.
dst += fixedEncodeURIComponent(key)
// 2.2 Append the equals character ‘=’ to DST.
dst += "="
// 2.3 Append a double quote ‘”’ to DST.
dst += '"'
// 2.4 Percent encode the value and append it to DST.
dst += fixedEncodeURIComponent(params[key])
// 2.5 Append a double quote ‘”’ to DST.
dst += '"'
// 2.6 If there are key/value pairs remaining, append a ', ' to DST.
dst += ', '
}
// Remove the trailing ', ' from dst
dst = dst.slice(0, -2)
// console.log(dst)
return dst
}
async function getRequestToken() {
const method = "POST"
const endpoint_url = "https://api.twitter.com/oauth/request_token"
const callback_url = 'https://oauth3.jademaveric.repl.co/login/twitter/callback'
const params = {
'oauth_callback': callback_url,
'oauth_consumer_key': consumer_key,
'oauth_nonce': Math.random().toString(16).slice(2),
'oauth_signature_method': 'HMAC-SHA1',
'oauth_timestamp': Date.now()/1000 | 0,
'oauth_version': '1.0'
}
params.oauth_signature = makeAuthSign(params, method, endpoint_url, consumer_secret)
// console.log(params)
const header = {
'Authorization': buildAuthHeader(params)
}
// console.log(header)
const res = await fetch('https://api.twitter.com/oauth/request_token', {
method: 'POST',
headers: header
})
const data = await res.text()
console.log("Got response from twitter /oauth/request_token", data)
const res_params = new URLSearchParams(data)
if (res_params.get('oauth_callback_confirmed') === 'true') {
console.log(res_params)
return {
'oauth_token': res_params.get('oauth_token'),
'oauth_token_secret': res_params.get('oauth_token_secret'),
'oauth_callback_confirmed': true
}
}
else {
console.log("Error: OAuth Callback Confirmed is false")
return {'oauth_callback_confirmed': false, 'error': data}
}
}
twitter.get('/', async (req, res) => {
console.log("Logging into twitter")
const {oauth_token, oauth_token_secret, oauth_callback_confirmed, error} = await getRequestToken()
req.session.oauth_token = oauth_token
if (oauth_callback_confirmed === false) {
res.send(error)
console.log('There was an error')
} else {
console.log("Got Request token, redirecting user to twitter")
const url = `https://api.twitter.com/oauth/authenticate?oauth_token=${oauth_token}`
res.redirect(url)
}
})
async function getAccessToken (oauth_token, oauth_verifier) {
console.log("Requsting Access Token")
const method = 'POST'
const url = `https://api.twitter.com/oauth/access_token`
const params = {
'oauth_consumer_key': consumer_key,
'oauth_nonce': Math.random().toString(16).slice(2),
'oauth_signature_method': 'HMAC-SHA1',
'oauth_timestamp': Date.now()/1000 | 0,
'oauth_token': oauth_token,
'oauth_version': '1.0'
}
params.oauth_signature = makeAuthSign(params, method, url, consumer_secret)
console.log(params)
const res = await fetch(url, {
method: 'POST',
headers: {
'Authorization': buildAuthHeader(params),
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `oauth_verifier=${oauth_verifier}`
})
const data = await res.text()
// console.log(data)
const res_params = new URLSearchParams(data)
// console.log(res_params)
return {
'user_token': res_params.get('oauth_token'),
'user_token_secret': res_params.get('oauth_token_secret'),
'user_id': res_params.get('user_id'),
'screen_name': res_params.get('screen_name')
}
}
async function getTwitterUser(user_token, user_secret, user_id, screen_name) {
const method = 'GET'
const url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
const params = {
'oauth_consumer_key': consumer_key,
'oauth_nonce': Math.random().toString(16).slice(2),
'oauth_signature_method': 'HMAC-SHA1',
'oauth_timestamp': Date.now()/1000 | 0,
'oauth_token': user_token,
'oauth_version': '1.0'
}
params.oauth_signature = makeAuthSign(params, method, url, consumer_secret, user_secret)
const res = await fetch(url, {
method: 'GET',
headers: {
'Authorization': buildAuthHeader(params)
}
})
const data = await res.json()
console.log(data)
return data
}
twitter.get('/callback', async (req, res) => {
console.log("Got /callback from twitter")
const oauth_token = req.query.oauth_token
const oauth_verifier = req.query.oauth_verifier
if (oauth_token == req.session.oauth_token) {
const {user_token, user_token_secret, user_id, screen_name} = await getAccessToken(oauth_token, oauth_verifier)
const twitterData = await getTwitterUser(user_token, user_token_secret, user_id, screen_name)
if (twitterData) {
req.session.provider = 'twitter'
req.session.id = twitterData.id
req.session.token = user_token
req.session.secret = user_token_secret
req.session.avatar_url = twitterData.profile_image_url_https
req.session.name = twitterData.name
req.session.email = undefined
res.redirect('/')
}
} else {
req.session = null
res.send("Authentication error, token did not match<br>\
<a href='/'>Home</a>")
}
})
export default twitter
export {makeAuthSign, collectParameters, createSignatureBaseString, getSigningKey, calculateSignature, buildAuthHeader}