From cbf3f3c1be7a1054aa1eb83fe1c9b6cc4ef4fb7a Mon Sep 17 00:00:00 2001 From: Rajinder Ramgarhia Date: Mon, 28 Mar 2016 02:07:10 -0400 Subject: [PATCH] Spotify authentication --- src/authDataManager/index.js | 16 +++++---- src/authDataManager/spotify.js | 64 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 src/authDataManager/spotify.js diff --git a/src/authDataManager/index.js b/src/authDataManager/index.js index 77ee7473ea..06873f7e4e 100644 --- a/src/authDataManager/index.js +++ b/src/authDataManager/index.js @@ -5,6 +5,7 @@ let meetup = require("./meetup"); let google = require("./google"); let github = require("./github"); let twitter = require("./twitter"); +let spotify = require("./spotify"); let anonymous = { validateAuthData: () => { @@ -23,6 +24,7 @@ let providers = { google, github, twitter, + spotify, anonymous } @@ -33,14 +35,14 @@ module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) { } // To handle the test cases on configuration let getValidatorForProvider = function(provider) { - + if (provider === 'anonymous' && !_enableAnonymousUsers) { return; } - + let defaultProvider = providers[provider]; let optionalProvider = oauthOptions[provider]; - + if (!defaultProvider && !optionalProvider) { return; } @@ -49,7 +51,7 @@ module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) { if (optionalProvider) { appIds = optionalProvider.appIds; } - + var validateAuthData; var validateAppId; @@ -72,11 +74,11 @@ module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) { validateAppId = optionalProvider.validateAppId; } } - + if (!validateAuthData || !validateAppId) { return; } - + return function(authData) { return validateAuthData(authData, optionalProvider).then(() => { if (appIds) { @@ -89,6 +91,6 @@ module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) { return Object.freeze({ getValidatorForProvider, - setEnableAnonymousUsers, + setEnableAnonymousUsers, }) } diff --git a/src/authDataManager/spotify.js b/src/authDataManager/spotify.js new file mode 100644 index 0000000000..89c72b8472 --- /dev/null +++ b/src/authDataManager/spotify.js @@ -0,0 +1,64 @@ +// Helper functions for accessing the Spotify API. +var https = require('https'); +var Parse = require('parse/node').Parse; + +// Returns a promise that fulfills iff this user id is valid. +function validateAuthData(authData) { + return request('me', authData.access_token) + .then((data) => { + if (data && data.id == authData.id) { + return; + } + throw new Parse.Error( + Parse.Error.OBJECT_NOT_FOUND, + 'Spotify auth is invalid for this user.'); + }); +} + +// Returns a promise that fulfills if this app id is valid. +function validateAppId(appIds, authData) { + var access_token = authData.access_token; + if (!appIds.length) { + throw new Parse.Error( + Parse.Error.OBJECT_NOT_FOUND, + 'Spotify auth is not configured.'); + } + return request('me', access_token) + .then((data) => { + if (data && appIds.indexOf(data.id) != -1) { + return; + } + throw new Parse.Error( + Parse.Error.OBJECT_NOT_FOUND, + 'Spotify auth is invalid for this user.'); + }); +} + +// A promisey wrapper for Spotify API requests. +function request(path, access_token) { + return new Promise(function(resolve, reject) { + https.get({ + host: 'api.spotify.com', + path: '/v1/' + path, + headers: { + 'Authorization': 'Bearer '+access_token + } + }, function(res) { + var data = ''; + res.on('data', function(chunk) { + data += chunk; + }); + res.on('end', function() { + data = JSON.parse(data); + resolve(data); + }); + }).on('error', function(e) { + reject('Failed to validate this access token with Spotify.'); + }); + }); +} + +module.exports = { + validateAppId: validateAppId, + validateAuthData: validateAuthData +};