-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.js
195 lines (173 loc) · 6.72 KB
/
strategy.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
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
191
192
193
194
195
/**
* Module dependencies.
*/
var util = require('util'),
url = require('url'),
uid = require('uid2'),
_ = require('lodash'),
OAuth2Strategy = require('passport-oauth2').Strategy,
AuthorizationError = require('passport-oauth2').AuthorizationError,
InternalOAuthError = require('passport-oauth2').InternalOAuthError;
function QuizletStrategy(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://quizlet.com/authorize';
options.tokenURL = options.tokenURL || 'https://api.quizlet.com/oauth/token';
options.scope = options.scope || ['read', 'write_set', 'write_group'];
options.scopeSeparator = options.scopeSeparator || ' ';
options.passReqToCallback = true;
options.state = true;
OAuth2Strategy.call(this, options, verify);
this.name = 'quizlet';
}
util.inherits(QuizletStrategy, OAuth2Strategy);
QuizletStrategy.prototype.authenticate = function (req, options) {
options = options || {};
var self = this;
if (req.query && req.query.error) {
if (req.query.error == 'access_denied') {
return this.fail({
message: req.query.error_description
});
} else {
return this.error(new AuthorizationError(req.query.error_description, req.query.error, req.query.error_uri));
}
}
var callbackURL = options.callbackURL || this._callbackURL;
if (callbackURL) {
var parsed = url.parse(callbackURL);
if (!parsed.protocol) {
// The callback URL is relative, resolve a fully qualified URL from the
// URL of the originating request.
callbackURL = url.resolve(utils.originalURL(req, {
proxy: this._trustProxy
}), callbackURL);
}
}
if (req.query && req.query.code) {
var code = req.query.code;
if (this._state) {
if (!req.session) {
return this.error(new Error('QuizletStrategy requires session support when using state. Did you forget app.use(express.session(...))?'));
}
var key = this._key;
if (!req.session[key]) {
return this.fail({
message: 'Unable to verify authorization request state.'
}, 403);
}
var state = req.session[key].state;
if (!state) {
return this.fail({
message: 'Unable to verify authorization request state.'
}, 403);
}
delete req.session[key].state;
if (Object.keys(req.session[key]).length === 0) {
delete req.session[key];
}
if (state !== req.query.state) {
return this.fail({
message: 'Invalid authorization request state.'
}, 403);
}
}
var params = this.tokenParams(options);
params.grant_type = 'authorization_code';
params.redirect_uri = callbackURL;
this._oauth2.getOAuthAccessToken(code, params,
function (err, accessToken, refreshToken, params) {
if (err) {
return self.error(self._createOAuthError('Failed to obtain access token', err));
}
self._params = params;
self._loadUserProfile(accessToken, function (err, profile) {
if (err) {
return self.error(err);
}
function verified(err, user, info) {
if (err) {
return self.error(err);
}
if (!user) {
return self.fail(info);
}
self.success(user, info);
}
try {
if (self._passReqToCallback) {
var arity = self._verify.length;
if (arity == 6) {
self._verify(req, accessToken, refreshToken, params, profile, verified);
} else { // arity == 5
self._verify(req, accessToken, refreshToken, profile, verified);
}
} else {
var arity = self._verify.length;
if (arity == 5) {
self._verify(accessToken, refreshToken, params, profile, verified);
} else { // arity == 4
self._verify(accessToken, refreshToken, profile, verified);
}
}
} catch (ex) {
return self.error(ex);
}
});
}
);
} else {
var params = this.authorizationParams(options);
params.response_type = 'code';
params.redirect_uri = callbackURL;
var scope = options.scope || this._scope;
if (scope) {
if (Array.isArray(scope)) {
scope = scope.join(this._scopeSeparator);
}
params.scope = scope;
}
var state = options.state;
if (state) {
params.state = state;
} else if (this._state) {
if (!req.session) {
return this.error(new Error('QuizletStrategy requires session support when using state. Did you forget app.use(express.session(...))?'));
}
var key = this._key;
state = uid(24);
if (!req.session[key]) {
req.session[key] = {};
}
req.session[key].state = state;
params.state = state;
}
var location = this._oauth2.getAuthorizeUrl(params);
this.redirect(location);
}
};
QuizletStrategy.prototype.userProfile = function (accessToken, done) {
var oauth2 = this._oauth2;
var userProfileURL = 'https://api.quizlet.com/2.0/users/' + this._params.user_id;
oauth2.get(userProfileURL, accessToken, function (err, body, res) {
if (err) {
return done(new InternalOAuthError('Failed to get user profile!', err));
}
try {
var json = JSON.parse(body);
delete json.studied;
delete json.favorite_sets;
delete json.sets;
var profile = _.clone(json);
profile.provider = 'quizlet';
profile._raw = body;
profile._json = json;
done(null, profile);
} catch (ex) {
done(ex);
}
});
};
/**
* Expose `QuizletStrategy`.
*/
module.exports = QuizletStrategy;