-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
120 lines (107 loc) · 2.86 KB
/
index.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
var
cors = require('cors'),
http = require('http'),
express = require('express'),
dotenv = require('dotenv'),
bodyParser = require('body-parser'),
request = require('request'),
CryptoJS = require('crypto-js');
var app = express();
dotenv.config();
const API_URL = "https://accounts.spotify.com/api/token";
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const CLIENT_CALLBACK_URL = process.env.CLIENT_CALLBACK_URL;
const ENCRYPTION_SECRET = process.env.ENCRYPTION_SECRET;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cors({
origin: true,
credentials: true
}));
const spotifyRequest = params => {
return new Promise((resolve, reject) => {
request.post(API_URL, {
form: params,
headers: {
"Authorization": "Basic " + new Buffer(CLIENT_ID + ":" + CLIENT_SECRET).toString('base64')
},
json: true
}, (err, resp) => err ? reject(err) : resolve(resp));
})
.then(resp => {
if (resp.statusCode != 200) {
return Promise.reject({
statusCode: resp.statusCode,
body: resp.body
});
}
return Promise.resolve(resp.body);
})
.catch(err => {
return Promise.reject({
statusCode: 500,
body: JSON.stringify({})
});
});
};
app.post('/exchange', (req, res) => {
const params = req.body;
if (!params.code) {
return res.json({
"error": "Parameter missing"
});
}
spotifyRequest({
grant_type: "authorization_code",
redirect_uri: CLIENT_CALLBACK_URL,
code: params.code
})
.then(session => {
let result = {
"access_token": session.access_token,
"expires_in": session.expires_in,
"refresh_token": encrypt(session.refresh_token)
};
return res.send(result);
})
.catch(response => {
return res.json(response);
});
});
// Get a new access token from a refresh token
app.post('/refresh', (req, res) => {
const params = req.body;
if (!params.refresh_token) {
return res.json({
"error": "Parameter missing"
});
}
spotifyRequest({
grant_type: "refresh_token",
refresh_token: decrypt(params.refresh_token)
})
.then(session => {
return res.send({
"access_token": session.access_token,
"expires_in": session.expires_in
});
})
.catch(response => {
return res.json(response);
});
});
function encrypt(text) {
return CryptoJS.AES.encrypt(text, ENCRYPTION_SECRET).toString();
};
function decrypt(text) {
var bytes = CryptoJS.AES.decrypt(text, ENCRYPTION_SECRET);
return bytes.toString(CryptoJS.enc.Utf8);
};
// Start the server
var server = http.createServer(app);
server.listen(process.env.PORT || 5000, function (err) {
console.info('listening in http://localhost:8080');
});