-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (36 loc) · 1.66 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
var util = require('util');
var OAuth2Strategy = require('passport-oauth').OAuth2Strategy,
InternalOAuthError = require('passport-oauth').InternalOAuthError;
var Strategy = function(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://www.dailymotion.com/oauth/authorize';
options.tokenURL = options.tokenURL || 'https://api.dailymotion.com/oauth/token';
options.scopeSeparator = options.scopeSeparator || ',';
OAuth2Strategy.call(this, options, verify);
this.name = 'dailymotion';
this._profileURL = options.profileURL || 'https://api.dailymotion.com/me';
};
util.inherits(Strategy, OAuth2Strategy);
Strategy.prototype.userProfile = function(accessToken, done) {
var url = this._profileURL;
this._oauth2.getProtectedResource(url, accessToken, function(err, body, res) {
if (!!err)
return done(new InternalOAuthError('failed to fetch user profile', err));
try { var json = JSON.parse(body); }
catch(e) { done(e); }
var dmProfile = { provider: 'dailymotion' };
dmProfile.id = json.id;
if (!!json.email)
dmProfile.emails = [{ value: json.email }];
dmProfile.displayName = json.screenname;
var nameComponents = dmProfile.displayName.split(' ');
var pName = { familyName: '', givenName: nameComponents[0] };
if (nameComponents.length > 1)
pName.familyName = nameComponents[nameComponents.length - 1];
dmProfile.name = pName;
dmProfile._raw = body;
dmProfile._json = json;
done(null, dmProfile);
});
};
module.exports.Strategy = Strategy;