Skip to content

Commit 0b2dc12

Browse files
committed
Merge pull request #1226 from 1nput0utput/master
Spotify authentication
2 parents 213bfea + cbf3f3c commit 0b2dc12

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

src/authDataManager/index.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ let meetup = require("./meetup");
55
let google = require("./google");
66
let github = require("./github");
77
let twitter = require("./twitter");
8+
let spotify = require("./spotify");
89

910
let anonymous = {
1011
validateAuthData: () => {
@@ -23,6 +24,7 @@ let providers = {
2324
google,
2425
github,
2526
twitter,
27+
spotify,
2628
anonymous
2729
}
2830

@@ -33,14 +35,14 @@ module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) {
3335
}
3436
// To handle the test cases on configuration
3537
let getValidatorForProvider = function(provider) {
36-
38+
3739
if (provider === 'anonymous' && !_enableAnonymousUsers) {
3840
return;
3941
}
40-
42+
4143
let defaultProvider = providers[provider];
4244
let optionalProvider = oauthOptions[provider];
43-
45+
4446
if (!defaultProvider && !optionalProvider) {
4547
return;
4648
}
@@ -49,7 +51,7 @@ module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) {
4951
if (optionalProvider) {
5052
appIds = optionalProvider.appIds;
5153
}
52-
54+
5355
var validateAuthData;
5456
var validateAppId;
5557

@@ -72,11 +74,11 @@ module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) {
7274
validateAppId = optionalProvider.validateAppId;
7375
}
7476
}
75-
77+
7678
if (!validateAuthData || !validateAppId) {
7779
return;
7880
}
79-
81+
8082
return function(authData) {
8183
return validateAuthData(authData, optionalProvider).then(() => {
8284
if (appIds) {
@@ -89,6 +91,6 @@ module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) {
8991

9092
return Object.freeze({
9193
getValidatorForProvider,
92-
setEnableAnonymousUsers,
94+
setEnableAnonymousUsers,
9395
})
9496
}

src/authDataManager/spotify.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Helper functions for accessing the Spotify API.
2+
var https = require('https');
3+
var Parse = require('parse/node').Parse;
4+
5+
// Returns a promise that fulfills iff this user id is valid.
6+
function validateAuthData(authData) {
7+
return request('me', authData.access_token)
8+
.then((data) => {
9+
if (data && data.id == authData.id) {
10+
return;
11+
}
12+
throw new Parse.Error(
13+
Parse.Error.OBJECT_NOT_FOUND,
14+
'Spotify auth is invalid for this user.');
15+
});
16+
}
17+
18+
// Returns a promise that fulfills if this app id is valid.
19+
function validateAppId(appIds, authData) {
20+
var access_token = authData.access_token;
21+
if (!appIds.length) {
22+
throw new Parse.Error(
23+
Parse.Error.OBJECT_NOT_FOUND,
24+
'Spotify auth is not configured.');
25+
}
26+
return request('me', access_token)
27+
.then((data) => {
28+
if (data && appIds.indexOf(data.id) != -1) {
29+
return;
30+
}
31+
throw new Parse.Error(
32+
Parse.Error.OBJECT_NOT_FOUND,
33+
'Spotify auth is invalid for this user.');
34+
});
35+
}
36+
37+
// A promisey wrapper for Spotify API requests.
38+
function request(path, access_token) {
39+
return new Promise(function(resolve, reject) {
40+
https.get({
41+
host: 'api.spotify.com',
42+
path: '/v1/' + path,
43+
headers: {
44+
'Authorization': 'Bearer '+access_token
45+
}
46+
}, function(res) {
47+
var data = '';
48+
res.on('data', function(chunk) {
49+
data += chunk;
50+
});
51+
res.on('end', function() {
52+
data = JSON.parse(data);
53+
resolve(data);
54+
});
55+
}).on('error', function(e) {
56+
reject('Failed to validate this access token with Spotify.');
57+
});
58+
});
59+
}
60+
61+
module.exports = {
62+
validateAppId: validateAppId,
63+
validateAuthData: validateAuthData
64+
};

0 commit comments

Comments
 (0)