forked from mikermcneil/node-deezer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.js
More file actions
190 lines (151 loc) · 5.42 KB
/
Copy pathoauth.js
File metadata and controls
190 lines (151 loc) · 5.42 KB
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
/**
* Module dependencies
*/
var request = require('request'),
_ = require('lodash'),
querystring = require('querystring'),
Err = require('./errors'),
handleApiError = require('./util/apiError'),
toCSV = require('./util/toCSV');
/**
* OAuth logic
*/
module.exports = {
/**
* Get the authentication url where your user should be redirected
*
* @param {String} appId (your Deezer application id from the Deezer developer portal)
* @param {String} redirectUrl (URL which will handle the user's code from Deezer)
* @param {Array|undefined} perms (requested permissions [optional])
*
* NOTE: `redirectUrl` must be within the 'Application domain' specified for this app
* in your Deezer developer portal at: http://developers.deezer.com/myapps
*/
getLoginUrl: function (appId, redirectUrl, perms) {
if ( typeof appId !== 'string' && typeof appId !== 'number' ) {
throw Err.invalidArgument('appId', appId, ['string', 'number']);
}
if ( typeof redirectUrl !== 'string' ) {
throw Err.invalidArgument('redirectUrl', redirectUrl, ['string']);
}
if ( !_.isArray(perms) && typeof perms !== 'undefined' ) {
throw Err.invalidArgument('perms', perms, ['Array', 'undefined']);
}
// If unspecified, basic_access is used by default
// (Deezer does this under the covers anyway)
if (!perms) perms = ['basic_access'];
// Reduce `perms` into a comma-separated-value string
perms = toCSV(perms);
// Build set of parameters to send to the authentication endpoint
// to verify that the user whose account you'd like to access
// is on board and cool w/ it and everything
// Then return the ready-to-go URL:
return this.endpoints.userAuth +
'?' + querystring.stringify({
app_id : appId,
redirect_uri : redirectUrl,
perms : perms
});
},
/**
* Generate an access token to access a user's Deezer account
*
* > NOTE: You must first have a valid `code` from the user proving that they're OK with this!!
* > You can get a code by redirecting the user to the url generated by calling `DZ.getLoginUrl(appId, callbackUrl)`
* > You'll probably want to call `DZ.createSession()` from the handler for the `callbackUrl` you specified
* > in `getLoginUrl(appId, callbackUrl)`, since that's where you'll have access to the `code`
*
* @param {String} appId (your Deezer application id from the Deezer developer portal)
* @param {String} code (the OAuth `code` generated by Deezer and sent to the `callbackUrl`)
* @param {String} secret (your Deezer app's secret from the developer portal)
* @param {Function} cb
* @param {Error|null} err
*/
createSession: function (appId, secret, code, cb) {
if ( typeof appId !== 'string' && typeof appId !== 'number' ) {
throw Err.invalidArgument('appId', appId, ['string', 'number']);
}
if ( typeof code !== 'string' ) {
throw Err.invalidArgument('code', code, ['string']);
}
if ( typeof secret !== 'string' ) {
throw Err.invalidArgument('secret', secret, ['string']);
}
if ( !_.isFunction(cb) ) {
throw Err.invalidArgument('cb', cb, ['Function']);
}
// Communicate w/ Deezer
request.get({
url : this.endpoints.accessToken,
qs : {
app_id : appId,
secret : secret,
code : code
}
}, function createSessionResponse (err, r, body) {
// Catch API errors in a standardized way
err = handleApiError(err, r, body);
if (err) return cb(err);
// Attempt to parse response body as form values
// (see example here: http://developers.deezer.com/api/oauth)
var parsedResponse = querystring.parse(body);
if (!parsedResponse.access_token) return cb(body);
// Cast `expires` result to either `0` or a natural number ( > 0 )
// i.e. we'll allow the `expires` value to be missing from the response,
// but we assume that means the token *never* expires!
if (!parsedResponse.expires) parsedResponse.expires = 0;
// Trim whitespace
if (typeof parsedResponse.expires === 'string') {
parsedResponse.expires.replace(/\s*/g, '');
}
// Cast to number
parsedResponse.expires = +parsedResponse.expires;
// NOTE: `expires` represents the number of seconds remaining before
// the access token expires
// Send back parsed response
cb(null, {
accessToken : parsedResponse.access_token,
expires : parsedResponse.expires
});
});
},
/**
* Lookup the validity of a Deezer user session for your app (i.e. access token)
*
* @param {String} accessToken (the OAuth token representing a user's session)
* @param {Function} cb
* @param {Error|null} err
*/
checkSession: function (accessToken, cb) {
// Doesn't appear to be a way to do this,
// at least in the current API doc here:
// http://developers.deezer.com/api
throw errors.notYetSupported('checkSession');
},
/**
* Invalidate/destroy the specified user's session
*
* @param {String} accessToken (the OAuth token representing a user's session)
* @param {Function} cb
* @param {Error|null} err
*/
destroySession: function (accessToken, cb) {
// Doesn't appear to be a way to do this,
// at least in the current API doc here:
// http://developers.deezer.com/api
throw errors.notYetSupported('destroySession');
}
};
/**
* Available Permissions:
* (as of Saturday, Sep 7, 2013)
* (from http://developers.deezer.com/api/permissions)
*
* basic_access
* email
* offline_access
* manage_library
* manage_community
* delete_library
* listening_history
*/