ember_simple_auth-rails 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,342 @@
1
+ (function(global) {
2
+
3
+ var define, requireModule;
4
+
5
+ (function() {
6
+ var registry = {}, seen = {};
7
+
8
+ define = function(name, deps, callback) {
9
+ registry[name] = { deps: deps, callback: callback };
10
+ };
11
+
12
+ requireModule = function(name) {
13
+ if (seen.hasOwnProperty(name)) { return seen[name]; }
14
+ seen[name] = {};
15
+
16
+ if (!registry[name]) {
17
+ throw new Error("Could not find module " + name);
18
+ }
19
+
20
+ var mod = registry[name],
21
+ deps = mod.deps,
22
+ callback = mod.callback,
23
+ reified = [],
24
+ exports;
25
+
26
+ for (var i=0, l=deps.length; i<l; i++) {
27
+ if (deps[i] === 'exports') {
28
+ reified.push(exports = {});
29
+ } else {
30
+ reified.push(requireModule(resolve(deps[i])));
31
+ }
32
+ }
33
+
34
+ var value = callback.apply(this, reified);
35
+ return seen[name] = exports || value;
36
+
37
+ function resolve(child) {
38
+ if (child.charAt(0) !== '.') { return child; }
39
+ var parts = child.split("/");
40
+ var parentBase = name.split("/").slice(0, -1);
41
+
42
+ for (var i=0, l=parts.length; i<l; i++) {
43
+ var part = parts[i];
44
+
45
+ if (part === '..') { parentBase.pop(); }
46
+ else if (part === '.') { continue; }
47
+ else { parentBase.push(part); }
48
+ }
49
+
50
+ return parentBase.join("/");
51
+ }
52
+ };
53
+
54
+ requireModule.registry = registry;
55
+ })();
56
+
57
+ define("ember-simple-auth-oauth2",
58
+ ["./ember-simple-auth-oauth2/authenticators/oauth2","./ember-simple-auth-oauth2/authorizers/oauth2","exports"],
59
+ function(__dependency1__, __dependency2__, __exports__) {
60
+ "use strict";
61
+ var Authenticator = __dependency1__.OAuth2;
62
+ var Authorizer = __dependency2__.OAuth2;
63
+
64
+ __exports__.Authenticator = Authenticator;
65
+ __exports__.Authorizer = Authorizer;
66
+ });
67
+ define("ember-simple-auth-oauth2/authenticators/oauth2",
68
+ ["exports"],
69
+ function(__exports__) {
70
+ "use strict";
71
+ var global = (typeof window !== 'undefined') ? window : {},
72
+ Ember = global.Ember;
73
+
74
+ /**
75
+ Authenticator that conforms to OAuth 2
76
+ ([RFC 6749](http://tools.ietf.org/html/rfc6749)), specifically the _"Resource
77
+ Owner Password Credentials Grant Type"_.
78
+
79
+ This authenticator supports refreshing the access token automatically and
80
+ will trigger the `'updated'` event each time the token was refreshed.
81
+
82
+ _The factory for this authenticator is registered as
83
+ `'authenticator:oauth2-password-grant'` in Ember's container._
84
+
85
+ @class OAuth2
86
+ @namespace Authenticators
87
+ @extends Base
88
+ */
89
+ var OAuth2 = Ember.SimpleAuth.Authenticators.Base.extend({
90
+ /**
91
+ The endpoint on the server the authenticator acquires the access token
92
+ from.
93
+
94
+ @property serverTokenEndpoint
95
+ @type String
96
+ @default '/token'
97
+ */
98
+ serverTokenEndpoint: '/token',
99
+
100
+ /**
101
+ Sets whether the authenticator automatically refreshes access tokens.
102
+
103
+ @property refreshAccessTokens
104
+ @type Boolean
105
+ @default true
106
+ */
107
+ refreshAccessTokens: true,
108
+
109
+ /**
110
+ @property _refreshTokenTimeout
111
+ @private
112
+ */
113
+ _refreshTokenTimeout: null,
114
+
115
+ /**
116
+ Restores the session from a set of session properties; __will return a
117
+ resolving promise when there's a non-empty `access_token` in the `data`__
118
+ and a rejecting promise otherwise.
119
+
120
+ This method also schedules automatic token refreshing when there are values
121
+ for `refresh_token` and `expires_in` in the `data` and automatic token
122
+ refreshing is not disabled (see
123
+ [Ember.SimpleAuth.Authenticators.OAuth2#refreshAccessTokens](#Ember-SimpleAuth-Authenticators-OAuth2-refreshAccessTokens)).
124
+
125
+ @method restore
126
+ @param {Object} data The data to restore the session from
127
+ @return {Ember.RSVP.Promise} A promise that when it resolves results in the session being authenticated
128
+ */
129
+ restore: function(data) {
130
+ var _this = this;
131
+ return new Ember.RSVP.Promise(function(resolve, reject) {
132
+ if (!Ember.isEmpty(data.access_token)) {
133
+ var now = (new Date()).getTime();
134
+ if (!Ember.isEmpty(data.expires_at) && data.expires_at < now) {
135
+ if (_this.refreshAccessTokens) {
136
+ _this.refreshAccessToken(data.expires_in, data.refresh_token).then(function(data) {
137
+ resolve(data);
138
+ }, reject);
139
+ } else {
140
+ reject();
141
+ }
142
+ } else {
143
+ _this.scheduleAccessTokenRefresh(data.expires_in, data.expires_at, data.refresh_token);
144
+ resolve(data);
145
+ }
146
+ } else {
147
+ reject();
148
+ }
149
+ });
150
+ },
151
+
152
+ /**
153
+ Authenticates the session with the specified `credentials`; the credentials
154
+ are `POST`ed to the `serverTokenEndpoint` (see
155
+ [Ember.SimpleAuth.Authenticators.OAuth2#serverTokenEndpoint](#Ember-SimpleAuth-Authenticators-OAuth2-serverTokenEndpoint))
156
+ and if they are valid the server returns an access token in response (see
157
+ http://tools.ietf.org/html/rfc6749#section-4.3). __If the credentials are
158
+ valid and authentication succeeds, a promise that resolves with the
159
+ server's response is returned__, otherwise a promise that rejects with the
160
+ error is returned.
161
+
162
+ This method also schedules automatic token refreshing when there are values
163
+ for `refresh_token` and `expires_in` in the server response and automatic
164
+ token refreshing is not disabled (see
165
+ [Ember.SimpleAuth.Authenticators.OAuth2#refreshAccessTokens](#Ember-SimpleAuth-Authenticators-OAuth2-refreshAccessTokens)).
166
+
167
+ @method authenticate
168
+ @param {Object} credentials The credentials to authenticate the session with
169
+ @return {Ember.RSVP.Promise} A promise that resolves when an access token is successfully acquired from the server and rejects otherwise
170
+ */
171
+ authenticate: function(credentials) {
172
+ var _this = this;
173
+ return new Ember.RSVP.Promise(function(resolve, reject) {
174
+ var data = { grant_type: 'password', username: credentials.identification, password: credentials.password };
175
+ _this.makeRequest(data).then(function(response) {
176
+ Ember.run(function() {
177
+ var expiresAt = _this.absolutizeExpirationTime(response.expires_in);
178
+ _this.scheduleAccessTokenRefresh(response.expires_in, expiresAt, response.refresh_token);
179
+ resolve(Ember.$.extend(response, { expires_at: expiresAt }));
180
+ });
181
+ }, function(xhr, status, error) {
182
+ Ember.run(function() {
183
+ reject(xhr.responseJSON || xhr.responseText);
184
+ });
185
+ });
186
+ });
187
+ },
188
+
189
+ /**
190
+ Cancels any outstanding automatic token refreshes and returns a resolving
191
+ promise.
192
+
193
+ @method invalidate
194
+ @return {Ember.RSVP.Promise} A resolving promise
195
+ */
196
+ invalidate: function() {
197
+ Ember.run.cancel(this._refreshTokenTimeout);
198
+ delete this._refreshTokenTimeout;
199
+ return new Ember.RSVP.resolve();
200
+ },
201
+
202
+ /**
203
+ Sends an `AJAX` request to the `serverTokenEndpoint`. This will always be a
204
+ _"POST_" request with content type _"application/x-www-form-urlencoded"_ as
205
+ specified in [RFC 6749](http://tools.ietf.org/html/rfc6749).
206
+
207
+ This method is not meant to be used directly but serves as an extension
208
+ point to e.g. add _"Client Credentials"_ (see
209
+ [RFC 6749, section 2.3](http://tools.ietf.org/html/rfc6749#section-2.3)).
210
+
211
+ @method makeRequest
212
+ @param {Object} data The data to send with the request, e.g. username and password or the refresh token
213
+ @return {Deferred object} A Deferred object (see [the jQuery docs](http://api.jquery.com/category/deferred-object/)) that is compatible to Ember.RSVP.Promise; will resolve if the request succeeds, reject otherwise
214
+ @protected
215
+ */
216
+ makeRequest: function(data) {
217
+ if (!Ember.SimpleAuth.Utils.isSecureUrl(this.serverTokenEndpoint)) {
218
+ Ember.Logger.warn('Credentials are transmitted via an insecure connection - use HTTPS to keep them secure.');
219
+ }
220
+ return Ember.$.ajax({
221
+ url: this.serverTokenEndpoint,
222
+ type: 'POST',
223
+ data: data,
224
+ dataType: 'json',
225
+ contentType: 'application/x-www-form-urlencoded'
226
+ });
227
+ },
228
+
229
+ /**
230
+ @method scheduleAccessTokenRefresh
231
+ @private
232
+ */
233
+ scheduleAccessTokenRefresh: function(expiresIn, expiresAt, refreshToken) {
234
+ var _this = this;
235
+ if (this.refreshAccessTokens) {
236
+ var now = (new Date()).getTime();
237
+ if (Ember.isEmpty(expiresAt) && !Ember.isEmpty(expiresIn)) {
238
+ expiresAt = new Date(now + expiresIn * 1000).getTime();
239
+ }
240
+ var offset = (Math.floor(Math.random() * 15) + 5) * 1000;
241
+ if (!Ember.isEmpty(refreshToken) && !Ember.isEmpty(expiresAt) && expiresAt > now - offset) {
242
+ Ember.run.cancel(this._refreshTokenTimeout);
243
+ delete this._refreshTokenTimeout;
244
+ this._refreshTokenTimeout = Ember.run.later(this, this.refreshAccessToken, expiresIn, refreshToken, expiresAt - now - offset);
245
+ }
246
+ }
247
+ },
248
+
249
+ /**
250
+ @method refreshAccessToken
251
+ @private
252
+ */
253
+ refreshAccessToken: function(expiresIn, refreshToken) {
254
+ var _this = this;
255
+ var data = { grant_type: 'refresh_token', refresh_token: refreshToken };
256
+ return new Ember.RSVP.Promise(function(resolve, reject) {
257
+ _this.makeRequest(data).then(function(response) {
258
+ Ember.run(function() {
259
+ expiresIn = response.expires_in || expiresIn;
260
+ refreshToken = response.refresh_token || refreshToken;
261
+ var expiresAt = _this.absolutizeExpirationTime(expiresIn);
262
+ var data = Ember.$.extend(response, { expires_in: expiresIn, expires_at: expiresAt, refresh_token: refreshToken });
263
+ _this.scheduleAccessTokenRefresh(expiresIn, null, refreshToken);
264
+ _this.trigger('updated', data);
265
+ resolve(data);
266
+ });
267
+ }, function(xhr, status, error) {
268
+ Ember.Logger.warn('Access token could not be refreshed - server responded with ' + error + '.');
269
+ reject();
270
+ });
271
+ });
272
+ },
273
+
274
+ /**
275
+ @method absolutizeExpirationTime
276
+ @private
277
+ */
278
+ absolutizeExpirationTime: function(expiresIn) {
279
+ if (!Ember.isEmpty(expiresIn)) {
280
+ return new Date((new Date().getTime()) + (expiresIn - 5) * 1000).getTime();
281
+ }
282
+ }
283
+ });
284
+
285
+ __exports__.OAuth2 = OAuth2;
286
+ });
287
+ define("ember-simple-auth-oauth2/authorizers/oauth2",
288
+ ["exports"],
289
+ function(__exports__) {
290
+ "use strict";
291
+ var global = (typeof window !== 'undefined') ? window : {},
292
+ Ember = global.Ember;
293
+
294
+ /**
295
+ Authorizer that conforms to OAuth 2
296
+ ([RFC 6749](http://tools.ietf.org/html/rfc6749)) by sending a bearer token
297
+ ([RFC 6749](http://tools.ietf.org/html/rfc6750)) in the request's
298
+ `Authorization` header.
299
+
300
+ _The factory for this authorizer is registered as
301
+ `'authorizer:oauth2-bearer'` in Ember's container._
302
+
303
+ @class OAuth2
304
+ @namespace Authorizers
305
+ @extends Base
306
+ */
307
+ var OAuth2 = Ember.SimpleAuth.Authorizers.Base.extend({
308
+ /**
309
+ Authorizes an XHR request by sending the `access_token` property from the
310
+ session as a bearer token in the `Authorization` header:
311
+
312
+ ```
313
+ Authorization: Bearer <access_token>
314
+ ```
315
+
316
+ @method authorize
317
+ @param {jqXHR} jqXHR The XHR request to authorize (see http://api.jquery.com/jQuery.ajax/#jqXHR)
318
+ @param {Object} requestOptions The options as provided to the `$.ajax` method (see http://api.jquery.com/jQuery.ajaxPrefilter/)
319
+ */
320
+ authorize: function(jqXHR, requestOptions) {
321
+ var accessToken = this.get('session.access_token');
322
+ if (this.get('session.isAuthenticated') && !Ember.isEmpty(accessToken)) {
323
+ if (!Ember.SimpleAuth.Utils.isSecureUrl(requestOptions.url)) {
324
+ Ember.Logger.warn('Credentials are transmitted via an insecure connection - use HTTPS to keep them secure.');
325
+ }
326
+ jqXHR.setRequestHeader('Authorization', 'Bearer ' + accessToken);
327
+ }
328
+ }
329
+ });
330
+
331
+ __exports__.OAuth2 = OAuth2;
332
+ });
333
+ var oauth2 = requireModule('ember-simple-auth-oauth2');
334
+
335
+ global.Ember.SimpleAuth.Authenticators.OAuth2 = oauth2.Authenticator;
336
+ global.Ember.SimpleAuth.Authorizers.OAuth2 = oauth2.Authorizer;
337
+
338
+ global.Ember.SimpleAuth.initializeExtension(function(container, application, options) {
339
+ container.register('authorizer:oauth2-bearer', global.Ember.SimpleAuth.Authorizers.OAuth2);
340
+ container.register('authenticator:oauth2-password-grant', global.Ember.SimpleAuth.Authenticators.OAuth2);
341
+ });
342
+ })((typeof global !== 'undefined') ? global : window);