This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
service-mixin.js
235 lines (198 loc) · 7.11 KB
/
service-mixin.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// The service-mixin is used in views that know about services, which is mostly
// OAuth services but also Sync.
'use strict';
define([
'lib/promise',
'views/base',
'views/decorators/button_progress_indicator',
'lib/url',
'lib/oauth-client',
'lib/assertion',
'lib/oauth-errors',
'lib/config-loader',
'lib/session',
'lib/service-name',
'lib/channels'
], function (p, BaseView, buttonProgressIndicator, Url, OAuthClient, Assertion, OAuthErrors,
ConfigLoader, Session, ServiceName, Channels) {
/* jshint camelcase: false */
// If the user completes an OAuth flow using a different browser than
// they started with, we redirect them back to the RP with this code
// in the `error_code` query param.
var RP_DIFFERENT_BROWSER_ERROR_CODE = 3005;
var SYNC_SERVICE = 'sync';
var EXPECT_CHANNEL_RESPONSE_TIMEOUT = 5000;
function shouldSetupOAuthLinksOnError () {
/*jshint validthis: true*/
var hasServiceView = this.className && this.className.match('oauth');
return hasServiceView || this.isOAuthSameBrowser();
}
function notifyChannel(message, data) {
/*jshint validthis: true*/
var self = this;
// Assume the receiver of the channel's notification will either
// respond or shut the FxA window.
// If it doesn't, assume there was an error and show a generic
// error to the user
if (data && data.timeout) {
self._expectResponseTimeout = self.setTimeout(function () {
self.displayError(OAuthErrors.toError('TRY_AGAIN'), OAuthErrors);
}, EXPECT_CHANNEL_RESPONSE_TIMEOUT);
}
return Channels.sendExpectResponse(message, data, {
window: self.window,
channel: self.channel
}).then(null, function (err) {
self.clearTimeout(self._expectResponseTimeout);
throw err;
});
}
/**
* Apply additional result data depending on the current channel environment
*
* @param {Object} result
* @param {Object} options
* @returns {Object}
*/
function decorateOAuthResult(result, options) {
options = options || {};
// if specific to the WebChannel flow
if (Channels.getWebChannelId(options.context)) {
// set closeWindow
result.closeWindow = options.viewOptions && options.viewOptions.source === 'signin';
// if the source is "signin" then set a timeout for a successful WebChannel signin
if (options.viewOptions.source === 'signin') {
result.timeout = 3000;
}
}
return p(result);
}
/**
* Formats the OAuth "result.redirect" url into a {code, state} object
*
* @param {Object} result
* @returns {Object}
*/
function formatOAuthResult(result) {
// get code and state from redirect params
var redirectParams = result.redirect.split('?')[1];
result.state = Url.searchParam('state', redirectParams);
result.code = Url.searchParam('code', redirectParams);
return p(result);
}
return {
setupOAuth: function (params) {
if (! this._configLoader) {
this._configLoader = new ConfigLoader();
}
this._oAuthClient = new OAuthClient();
if (! params) {
// params listed in:
// https://github.com/mozilla/fxa-oauth-server/blob/master/docs/api.md#post-v1authorization
params = Url.searchParams(this.window.location.search,
['client_id', 'redirect_uri', 'state', 'scope', 'action', 'webChannelId']);
}
this._oAuthParams = params;
// FxA auth server API expects a 'service' parameter to include in
// verification emails. Oauth uses 'client_id', so we set 'service'
// to the 'client_id'.
this.service = params.client_id || Session.service;
Session.set('service', this.service);
// A hint that allows Session to determine whether the user
// is in the OAuth flow.
Session.set('client_id', params.client_id);
},
persistOAuthParams: function () {
Session.set('oauth', this._oAuthParams);
},
setServiceInfo: function () {
var self = this;
if (this.isSync()) {
self.serviceName = new ServiceName(this.translator).get(this.service);
return p();
}
return this._oAuthClient.getClientInfo(this.service)
.then(function(clientInfo) {
self.serviceName = clientInfo.name;
self.serviceRedirectURI = clientInfo.redirect_uri;
});
},
finishOAuthFlowDifferentBrowser: function () {
return notifyChannel.call(this, 'oauth_complete', {
redirect: this.serviceRedirectURI,
error: RP_DIFFERENT_BROWSER_ERROR_CODE
});
},
finishOAuthFlow: buttonProgressIndicator(function (viewOptions) {
var self = this;
return this._configLoader.fetch().then(function(config) {
return Assertion.generate(config.oauthUrl);
})
.then(function(assertion) {
self._oAuthParams.assertion = assertion;
return self._oAuthClient.getCode(self._oAuthParams);
})
.then(function(result) {
return formatOAuthResult(result);
})
.then(function(result) {
return decorateOAuthResult(result, {
context: self.window,
viewOptions: viewOptions
});
})
.then(function(result) {
return notifyChannel.call(self, 'oauth_complete', result);
})
.then(function() {
Session.clear('oauth');
// on success, keep the button progress indicator going until the
// window closes.
return { pageNavigation: true };
})
.fail(function(err) {
Session.clear('oauth');
self.displayError(err, OAuthErrors);
});
}),
hasService: function () {
return !!Session.service;
},
/**
* Check whether the user is in the OAuth flow.
*/
isOAuth: function () {
return Session.isOAuth();
},
isOAuthSameBrowser: function () {
// The signup/signin flow sets Session.oauth with the
// Oauth parameters. If the user opens the verification
// link in the same browser, then we check to make sure
// the service listed in the link is the same as the client_id
// in the previously saved Oauth params.
/* jshint camelcase: false */
return !!Session.oauth && Session.oauth.client_id === Session.service;
},
isOAuthDifferentBrowser: function () {
return this.isOAuth() && ! this.isOAuthSameBrowser();
},
setupOAuthLinks: function () {
this.$('a[href~="/signin"]').attr('href', '/oauth/signin');
this.$('a[href~="/signup"]').attr('href', '/oauth/signup');
},
// override this method so we can fix signup/signin links in errors
displayErrorUnsafe: function (err, errors) {
var result = BaseView.prototype.displayErrorUnsafe.call(this, err, errors);
if (shouldSetupOAuthLinksOnError.call(this)) {
this.setupOAuthLinks();
}
return result;
},
isSync: function () {
return Session.service === SYNC_SERVICE;
}
};
});