From 1fea5166448bc5dc5209321c76f4e2c56276d31a Mon Sep 17 00:00:00 2001 From: Jackson Melcher Date: Wed, 1 Apr 2020 14:22:32 -0700 Subject: [PATCH 1/2] Changed Buffer() t0 Buffer.from() to avoid deprication. --- authorization_code/app.js | 209 ++++++++++++++++++++------------------ 1 file changed, 111 insertions(+), 98 deletions(-) diff --git a/authorization_code/app.js b/authorization_code/app.js index 9b8a6b55..dcc44196 100644 --- a/authorization_code/app.js +++ b/authorization_code/app.js @@ -23,13 +23,14 @@ var redirect_uri = 'REDIRECT_URI'; // Your redirect uri * @return {string} The generated string */ var generateRandomString = function(length) { - var text = ''; - var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + var text = ''; + var possible = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - for (var i = 0; i < length; i++) { - text += possible.charAt(Math.floor(Math.random() * possible.length)); - } - return text; + for (var i = 0; i < length; i++) { + text += possible.charAt(Math.floor(Math.random() * possible.length)); + } + return text; }; var stateKey = 'spotify_auth_state'; @@ -37,110 +38,122 @@ var stateKey = 'spotify_auth_state'; var app = express(); app.use(express.static(__dirname + '/public')) - .use(cors()) - .use(cookieParser()); + .use(cors()) + .use(cookieParser()); app.get('/login', function(req, res) { - - var state = generateRandomString(16); - res.cookie(stateKey, state); - - // your application requests authorization - var scope = 'user-read-private user-read-email'; - res.redirect('https://accounts.spotify.com/authorize?' + - querystring.stringify({ - response_type: 'code', - client_id: client_id, - scope: scope, - redirect_uri: redirect_uri, - state: state - })); + var state = generateRandomString(16); + res.cookie(stateKey, state); + + // your application requests authorization + var scope = 'user-read-private user-read-email'; + res.redirect( + 'https://accounts.spotify.com/authorize?' + + querystring.stringify({ + response_type: 'code', + client_id: client_id, + scope: scope, + redirect_uri: redirect_uri, + state: state + }) + ); }); app.get('/callback', function(req, res) { - - // your application requests refresh and access tokens - // after checking the state parameter - - var code = req.query.code || null; - var state = req.query.state || null; - var storedState = req.cookies ? req.cookies[stateKey] : null; - - if (state === null || state !== storedState) { - res.redirect('/#' + - querystring.stringify({ - error: 'state_mismatch' - })); - } else { - res.clearCookie(stateKey); - var authOptions = { - url: 'https://accounts.spotify.com/api/token', - form: { - code: code, - redirect_uri: redirect_uri, - grant_type: 'authorization_code' - }, - headers: { - 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) - }, - json: true - }; - - request.post(authOptions, function(error, response, body) { - if (!error && response.statusCode === 200) { - - var access_token = body.access_token, - refresh_token = body.refresh_token; - - var options = { - url: 'https://api.spotify.com/v1/me', - headers: { 'Authorization': 'Bearer ' + access_token }, - json: true + // your application requests refresh and access tokens + // after checking the state parameter + + var code = req.query.code || null; + var state = req.query.state || null; + var storedState = req.cookies ? req.cookies[stateKey] : null; + + if (state === null || state !== storedState) { + res.redirect( + '/#' + + querystring.stringify({ + error: 'state_mismatch' + }) + ); + } else { + res.clearCookie(stateKey); + var authOptions = { + url: 'https://accounts.spotify.com/api/token', + form: { + code: code, + redirect_uri: redirect_uri, + grant_type: 'authorization_code' + }, + headers: { + Authorization: + 'Basic ' + + new Buffer.from(client_id + ':' + client_secret).toString( + 'base64' + ) + }, + json: true }; - // use the access token to access the Spotify Web API - request.get(options, function(error, response, body) { - console.log(body); + request.post(authOptions, function(error, response, body) { + if (!error && response.statusCode === 200) { + var access_token = body.access_token, + refresh_token = body.refresh_token; + + var options = { + url: 'https://api.spotify.com/v1/me', + headers: { Authorization: 'Bearer ' + access_token }, + json: true + }; + + // use the access token to access the Spotify Web API + request.get(options, function(error, response, body) { + console.log(body); + }); + + // we can also pass the token to the browser to make requests from there + res.redirect( + '/#' + + querystring.stringify({ + access_token: access_token, + refresh_token: refresh_token + }) + ); + } else { + res.redirect( + '/#' + + querystring.stringify({ + error: 'invalid_token' + }) + ); + } }); - - // we can also pass the token to the browser to make requests from there - res.redirect('/#' + - querystring.stringify({ - access_token: access_token, - refresh_token: refresh_token - })); - } else { - res.redirect('/#' + - querystring.stringify({ - error: 'invalid_token' - })); - } - }); - } + } }); app.get('/refresh_token', function(req, res) { + // requesting access token from refresh token + var refresh_token = req.query.refresh_token; + var authOptions = { + url: 'https://accounts.spotify.com/api/token', + headers: { + Authorization: + 'Basic ' + + new Buffer(client_id + ':' + client_secret).toString('base64') + }, + form: { + grant_type: 'refresh_token', + refresh_token: refresh_token + }, + json: true + }; - // requesting access token from refresh token - var refresh_token = req.query.refresh_token; - var authOptions = { - url: 'https://accounts.spotify.com/api/token', - headers: { 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) }, - form: { - grant_type: 'refresh_token', - refresh_token: refresh_token - }, - json: true - }; - - request.post(authOptions, function(error, response, body) { - if (!error && response.statusCode === 200) { - var access_token = body.access_token; - res.send({ - 'access_token': access_token - }); - } - }); + request.post(authOptions, function(error, response, body) { + if (!error && response.statusCode === 200) { + var access_token = body.access_token; + res.send({ + access_token: access_token + }); + } + }); }); console.log('Listening on 8888'); From ca914134c6ccb22be799c7f8f5dfa07742dd31e8 Mon Sep 17 00:00:00 2001 From: Jackson Melcher Date: Wed, 1 Apr 2020 14:33:17 -0700 Subject: [PATCH 2/2] Formatted file correctly. --- authorization_code/app.js | 209 ++++++++++++++++++-------------------- 1 file changed, 98 insertions(+), 111 deletions(-) diff --git a/authorization_code/app.js b/authorization_code/app.js index dcc44196..ca44d917 100644 --- a/authorization_code/app.js +++ b/authorization_code/app.js @@ -23,14 +23,13 @@ var redirect_uri = 'REDIRECT_URI'; // Your redirect uri * @return {string} The generated string */ var generateRandomString = function(length) { - var text = ''; - var possible = - 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + var text = ''; + var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - for (var i = 0; i < length; i++) { - text += possible.charAt(Math.floor(Math.random() * possible.length)); - } - return text; + for (var i = 0; i < length; i++) { + text += possible.charAt(Math.floor(Math.random() * possible.length)); + } + return text; }; var stateKey = 'spotify_auth_state'; @@ -38,122 +37,110 @@ var stateKey = 'spotify_auth_state'; var app = express(); app.use(express.static(__dirname + '/public')) - .use(cors()) - .use(cookieParser()); + .use(cors()) + .use(cookieParser()); app.get('/login', function(req, res) { - var state = generateRandomString(16); - res.cookie(stateKey, state); - - // your application requests authorization - var scope = 'user-read-private user-read-email'; - res.redirect( - 'https://accounts.spotify.com/authorize?' + - querystring.stringify({ - response_type: 'code', - client_id: client_id, - scope: scope, - redirect_uri: redirect_uri, - state: state - }) - ); + + var state = generateRandomString(16); + res.cookie(stateKey, state); + + // your application requests authorization + var scope = 'user-read-private user-read-email'; + res.redirect('https://accounts.spotify.com/authorize?' + + querystring.stringify({ + response_type: 'code', + client_id: client_id, + scope: scope, + redirect_uri: redirect_uri, + state: state + })); }); app.get('/callback', function(req, res) { - // your application requests refresh and access tokens - // after checking the state parameter - - var code = req.query.code || null; - var state = req.query.state || null; - var storedState = req.cookies ? req.cookies[stateKey] : null; - - if (state === null || state !== storedState) { - res.redirect( - '/#' + - querystring.stringify({ - error: 'state_mismatch' - }) - ); - } else { - res.clearCookie(stateKey); - var authOptions = { - url: 'https://accounts.spotify.com/api/token', - form: { - code: code, - redirect_uri: redirect_uri, - grant_type: 'authorization_code' - }, - headers: { - Authorization: - 'Basic ' + - new Buffer.from(client_id + ':' + client_secret).toString( - 'base64' - ) - }, - json: true - }; - request.post(authOptions, function(error, response, body) { - if (!error && response.statusCode === 200) { - var access_token = body.access_token, - refresh_token = body.refresh_token; - - var options = { - url: 'https://api.spotify.com/v1/me', - headers: { Authorization: 'Bearer ' + access_token }, - json: true - }; - - // use the access token to access the Spotify Web API - request.get(options, function(error, response, body) { - console.log(body); - }); - - // we can also pass the token to the browser to make requests from there - res.redirect( - '/#' + - querystring.stringify({ - access_token: access_token, - refresh_token: refresh_token - }) - ); - } else { - res.redirect( - '/#' + - querystring.stringify({ - error: 'invalid_token' - }) - ); - } - }); - } -}); + // your application requests refresh and access tokens + // after checking the state parameter -app.get('/refresh_token', function(req, res) { - // requesting access token from refresh token - var refresh_token = req.query.refresh_token; + var code = req.query.code || null; + var state = req.query.state || null; + var storedState = req.cookies ? req.cookies[stateKey] : null; + + if (state === null || state !== storedState) { + res.redirect('/#' + + querystring.stringify({ + error: 'state_mismatch' + })); + } else { + res.clearCookie(stateKey); var authOptions = { - url: 'https://accounts.spotify.com/api/token', - headers: { - Authorization: - 'Basic ' + - new Buffer(client_id + ':' + client_secret).toString('base64') - }, - form: { - grant_type: 'refresh_token', - refresh_token: refresh_token - }, - json: true + url: 'https://accounts.spotify.com/api/token', + form: { + code: code, + redirect_uri: redirect_uri, + grant_type: 'authorization_code' + }, + headers: { + 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) + }, + json: true }; request.post(authOptions, function(error, response, body) { - if (!error && response.statusCode === 200) { - var access_token = body.access_token; - res.send({ - access_token: access_token - }); - } + if (!error && response.statusCode === 200) { + + var access_token = body.access_token, + refresh_token = body.refresh_token; + + var options = { + url: 'https://api.spotify.com/v1/me', + headers: { 'Authorization': 'Bearer ' + access_token }, + json: true + }; + + // use the access token to access the Spotify Web API + request.get(options, function(error, response, body) { + console.log(body); + }); + + // we can also pass the token to the browser to make requests from there + res.redirect('/#' + + querystring.stringify({ + access_token: access_token, + refresh_token: refresh_token + })); + } else { + res.redirect('/#' + + querystring.stringify({ + error: 'invalid_token' + })); + } }); + } +}); + +app.get('/refresh_token', function(req, res) { + + // requesting access token from refresh token + var refresh_token = req.query.refresh_token; + var authOptions = { + url: 'https://accounts.spotify.com/api/token', + headers: { 'Authorization': 'Basic ' + (new Buffer.from(client_id + ':' + client_secret).toString('base64')) }, + form: { + grant_type: 'refresh_token', + refresh_token: refresh_token + }, + json: true + }; + + request.post(authOptions, function(error, response, body) { + if (!error && response.statusCode === 200) { + var access_token = body.access_token; + res.send({ + 'access_token': access_token + }); + } + }); }); console.log('Listening on 8888');