-
Notifications
You must be signed in to change notification settings - Fork 21
/
session.js
358 lines (331 loc) Β· 10.2 KB
/
session.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* A generic class that lazy-loads User info
*/
define([
'backbone',
'js/components/api_request',
'js/components/api_targets',
'js/mixins/hardened',
'js/components/generic_module',
'js/mixins/dependon',
'js/components/api_query',
'js/components/user',
'js/components/api_feedback',
'js/mixins/api_access',
'utils',
], function(
Backbone,
ApiRequest,
ApiTargets,
Hardened,
GenericModule,
Dependon,
ApiQuery,
User,
ApiFeedback,
ApiAccess,
utils
) {
var SessionModel = Backbone.Model.extend({
defaults: function() {
return {
resetPasswordToken: undefined,
};
},
});
var Session = GenericModule.extend({
initialize: function(options) {
var options = options || {};
// right now, this will only be used if someone forgot their password
this.model = new SessionModel();
this.test = options.test ? true : undefined;
_.bindAll(this, [
'loginSuccess',
'loginFail',
'registerSuccess',
'registerFail',
'resetPassword1Success',
'resetPassword1Fail',
'resetPassword2Success',
'resetPassword2Fail',
]);
},
activate: function(beehive) {
this.setBeeHive(beehive);
},
/* methods that will be available to widgets */
login: function(data) {
var d = $.Deferred();
var that = this;
this.sendRequestWithNewCSRF(function(csrfToken) {
var request = new ApiRequest({
target: ApiTargets.LOGIN,
query: new ApiQuery({}),
options: {
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
headers: { 'X-CSRFToken': csrfToken },
done: function() {
// allow widgets to listen for success or failure
d.resolve.apply(d, arguments);
// session response to success
that.loginSuccess.apply(that, arguments);
},
fail: function() {
d.reject.apply(d, arguments);
that.loginFail.apply(that, arguments);
},
beforeSend: function(jqXHR, settings) {
jqXHR.session = this;
},
},
});
return this.getBeeHive()
.getService('Api')
.request(request);
});
return d.promise();
},
/*
* every time a csrf token is required, csrf manager will request a new token,
* and it allows you to attach callbacks to the promise it returns
* */
sendRequestWithNewCSRF: function(callback) {
callback = _.bind(callback, this);
this.getBeeHive()
.getObject('CSRFManager')
.getCSRF()
.done(callback);
},
logout: function() {
this.sendRequestWithNewCSRF(function(csrfToken) {
var request = new ApiRequest({
target: ApiTargets.LOGOUT,
query: new ApiQuery({}),
options: {
context: this,
type: 'POST',
headers: { 'X-CSRFToken': csrfToken },
contentType: 'application/json',
fail: this.logoutSuccess,
done: this.logoutSuccess,
},
});
return this.getBeeHive()
.getService('Api')
.request(request);
});
},
register: function(data) {
this.sendRequestWithNewCSRF(function(csrfToken) {
var request = new ApiRequest({
target: ApiTargets.USER,
query: new ApiQuery({}),
options: {
type: 'POST',
data: JSON.stringify(_.pick(data, 'given_name', 'family_name', 'email', 'password1', 'password2', 'g-recaptcha-response')),
contentType: 'application/json',
headers: { 'X-CSRFToken': csrfToken },
done: this.registerSuccess,
fail: this.registerFail,
},
});
return this.getBeeHive()
.getService('Api')
.request(request);
});
},
resetPassword1: function(data) {
var email = data.email;
var data = _.omit(data, 'email');
this.sendRequestWithNewCSRF(function(csrfToken) {
var request = new ApiRequest({
target: ApiTargets.RESET_PASSWORD + '/' + email,
query: new ApiQuery({}),
options: {
type: 'POST',
data: JSON.stringify(data),
headers: { 'X-CSRFToken': csrfToken },
contentType: 'application/json',
done: this.resetPassword1Success,
fail: this.resetPassword1Fail,
},
});
return this.getBeeHive()
.getService('Api')
.request(request);
});
},
resetPassword2: function(data) {
this.sendRequestWithNewCSRF(function(csrfToken) {
var request = new ApiRequest({
target:
ApiTargets.RESET_PASSWORD +
'/' +
this.model.get('resetPasswordToken'),
query: new ApiQuery({}),
options: {
type: 'PUT',
data: JSON.stringify(data),
contentType: 'application/json',
headers: { 'X-CSRFToken': csrfToken },
done: this.resetPassword2Success,
fail: this.resetPassword2Fail,
},
});
return this.getBeeHive()
.getService('Api')
.request(request);
});
},
setChangeToken: function(token) {
this.model.set('resetPasswordToken', token);
},
/* private methods */
loginSuccess: function(response, status, jqXHR) {
// reset auth token by contacting Bootstrap, which will log user in
var that = this;
this.getApiAccess({ reconnect: true }).done(function() {});
},
loginFail: function(xhr, status, errorThrown) {
var pubsub = this.getPubSub();
const error = utils.extractErrorMessageFromAjax(xhr, 'error unknown');
const message = `Login was unsuccessful (${error})`;
pubsub.publish(
pubsub.ALERT,
new ApiFeedback({
code: 0,
msg: message,
type: 'danger',
fade: true,
})
);
pubsub.publish(pubsub.USER_ANNOUNCEMENT, 'login_fail', message);
},
logoutSuccess: function(response, status, jqXHR) {
var that = this;
this.getApiAccess({ reconnect: true }).done(function() {
// set session state to logged out
that
.getBeeHive()
.getObject('User')
.completeLogOut();
});
},
registerSuccess: function(response, status, jqXHR) {
var pubsub = this.getPubSub();
// authentication widget will show a "success" view in response to this user announcement
pubsub.publish(pubsub.USER_ANNOUNCEMENT, 'register_success');
},
registerFail: function(xhr, status, errorThrown) {
var pubsub = this.getPubSub();
const error = utils.extractErrorMessageFromAjax(xhr, 'error unknown');
const message = `Registration was unsuccessful (${error})`;
pubsub.publish(
pubsub.ALERT,
new ApiFeedback({
code: 0,
msg: message,
type: 'danger',
fade: true,
})
);
pubsub.publish(pubsub.USER_ANNOUNCEMENT, 'register_fail', message);
},
resetPassword1Success: function(response, status, jqXHR) {
var pubsub = this.getPubSub();
pubsub.publish(pubsub.USER_ANNOUNCEMENT, 'reset_password_1_success');
},
resetPassword1Fail: function(xhr, status, errorThrown) {
var pubsub = this.getPubSub();
const error = utils.extractErrorMessageFromAjax(xhr, 'error unknown');
const message = `password reset step 1 was unsuccessful (${error})`;
pubsub.publish(
pubsub.ALERT,
new ApiFeedback({
code: 0,
msg: message,
type: 'danger',
fade: true,
})
);
pubsub.publish(
pubsub.USER_ANNOUNCEMENT,
'reset_password_1_fail',
message
);
},
resetPassword2Success: function(response, status, jqXHR) {
var promise;
var pubsub;
// reset auth token by contacting Bootstrap
// this will log the user in
promise = this.getApiAccess({ reconnect: true });
// notify interested widgets
pubsub = this.getPubSub();
promise.done(function() {
pubsub.publish(pubsub.USER_ANNOUNCEMENT, 'reset_password_2_success');
var message = 'Your password has been successfully reset!';
// navigate to home page
pubsub.publish(pubsub.NAVIGATE, 'index-page');
pubsub.publish(
pubsub.ALERT,
new ApiFeedback({
code: 0,
msg: message,
type: 'success',
modal: true,
})
);
});
promise.fail(function(xhr) {
const error = utils.extractErrorMessageFromAjax(xhr, 'error unknown');
const message = `Your password was not successfully reset. Please try to follow the link from the email you received again.\n\n(${error})`;
pubsub.publish(
pubsub.USER_ANNOUNCEMENT,
'reset_password_2_fail',
message
);
pubsub.publish(
pubsub.ALERT,
new ApiFeedback({
code: 0,
msg: message,
type: 'danger',
modal: true,
})
);
});
},
resetPassword2Fail: function(xhr, status, errorThrown) {
var pubsub = this.getPubSub();
const error = utils.extractErrorMessageFromAjax(xhr, 'error unknown');
const message = `password reset step 2 was unsuccessful (${error})`;
pubsub.publish(
pubsub.ALERT,
new ApiFeedback({
code: 0,
msg: message,
type: 'danger',
fade: true,
})
);
pubsub.publish(
pubsub.USER_ANNOUNCEMENT,
'reset_password_2_fail',
message
);
},
hardenedInterface: {
login: 'log the user in',
logout: 'log the user out',
register: 'registers a new user',
resetPassword1: 'sends an email to account',
resetPassword2: 'updates the password',
setChangeToken: 'the router stores the token to reset password here',
},
});
_.extend(Session.prototype, Dependon.BeeHive, Hardened, ApiAccess);
return Session;
});