forked from mainmatter/ember-simple-auth-component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-auth-devise.amd.js
376 lines (321 loc) · 12.5 KB
/
simple-auth-devise.amd.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
(function(global) {
var define = global.define;
var require = global.require;
var Ember = global.Ember;
if (typeof Ember === 'undefined' && typeof require !== 'undefined') {
Ember = require('ember');
}
Ember.libraries.register('Ember Simple Auth Devise', '0.8.0');
define("simple-auth-devise/authenticators/devise",
["simple-auth/authenticators/base","./../configuration","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var Base = __dependency1__["default"];
var Configuration = __dependency2__["default"];
/**
Authenticator that works with the Ruby gem
[Devise](https://github.com/plataformatec/devise).
__As token authentication is not actually part of devise anymore, the server
needs to implement some customizations__ to work with this authenticator -
see the README and
[discussion here](https://gist.github.com/josevalim/fb706b1e933ef01e4fb6).
_The factory for this authenticator is registered as
`'simple-auth-authenticator:devise'` in Ember's container._
@class Devise
@namespace SimpleAuth.Authenticators
@module simple-auth-devise/authenticators/devise
@extends Base
*/
__exports__["default"] = Base.extend({
/**
The endpoint on the server the authenticator acquires the auth token
and email from.
This value can be configured via
[`SimpleAuth.Configuration.Devise#serverTokenEndpoint`](#SimpleAuth-Configuration-Devise-serverTokenEndpoint).
@property serverTokenEndpoint
@type String
@default '/users/sign_in'
*/
serverTokenEndpoint: '/users/sign_in',
/**
The devise resource name
This value can be configured via
[`SimpleAuth.Configuration.Devise#resourceName`](#SimpleAuth-Configuration-Devise-resourceName).
@property resourceName
@type String
@default 'user'
*/
resourceName: 'user',
/**
The token attribute name.
This value can be configured via
[`SimpleAuth.Configuration.Devise#tokenAttributeName`](#SimpleAuth-Configuration-Devise-tokenAttributeName).
@property tokenAttributeName
@type String
@default 'token'
*/
tokenAttributeName: 'token',
/**
The identification attribute name.
This value can be configured via
[`SimpleAuth.Configuration.Devise#identificationAttributeName`](#SimpleAuth-Configuration-Devise-identificationAttributeName).
@property identificationAttributeName
@type String
@default 'email'
*/
identificationAttributeName: 'email',
/**
@method init
@private
*/
init: function() {
this.serverTokenEndpoint = Configuration.serverTokenEndpoint;
this.resourceName = Configuration.resourceName;
this.tokenAttributeName = Configuration.tokenAttributeName;
this.identificationAttributeName = Configuration.identificationAttributeName;
},
/**
Restores the session from a set of session properties; __will return a
resolving promise when there's a non-empty `token` and a non-empty
`email` in the `properties`__ and a rejecting promise otherwise.
@method restore
@param {Object} properties The properties to restore the session from
@return {Ember.RSVP.Promise} A promise that when it resolves results in the session being authenticated
*/
restore: function(properties) {
var _this = this;
var propertiesObject = Ember.Object.create(properties);
return new Ember.RSVP.Promise(function(resolve, reject) {
if (!Ember.isEmpty(propertiesObject.get(_this.tokenAttributeName)) && !Ember.isEmpty(propertiesObject.get(_this.identificationAttributeName))) {
resolve(properties);
} else {
reject();
}
});
},
/**
Authenticates the session with the specified `credentials`; the credentials
are `POST`ed to the
[`Authenticators.Devise#serverTokenEndpoint`](#SimpleAuth-Authenticators-Devise-serverTokenEndpoint)
and if they are valid the server returns an auth token and email in
response. __If the credentials are valid and authentication succeeds, a
promise that resolves with the server's response is returned__, otherwise a
promise that rejects with the server error is returned.
@method authenticate
@param {Object} options The credentials to authenticate the session with
@return {Ember.RSVP.Promise} A promise that resolves when an auth token and email is successfully acquired from the server and rejects otherwise
*/
authenticate: function(credentials) {
var _this = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
var data = {};
data[_this.resourceName] = {
password: credentials.password
};
data[_this.resourceName][_this.identificationAttributeName] = credentials.identification;
_this.makeRequest(data).then(function(response) {
Ember.run(function() {
resolve(response);
});
}, function(xhr, status, error) {
Ember.run(function() {
reject(xhr.responseJSON || xhr.responseText);
});
});
});
},
/**
Does nothing
@method invalidate
@return {Ember.RSVP.Promise} A resolving promise
*/
invalidate: function() {
return Ember.RSVP.resolve();
},
/**
@method makeRequest
@private
*/
makeRequest: function(data, resolve, reject) {
return Ember.$.ajax({
url: this.serverTokenEndpoint,
type: 'POST',
data: data,
dataType: 'json',
beforeSend: function(xhr, settings) {
xhr.setRequestHeader('Accept', settings.accepts.json);
}
});
}
});
});
define("simple-auth-devise/authorizers/devise",
["simple-auth/authorizers/base","./../configuration","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var Base = __dependency1__["default"];
var Configuration = __dependency2__["default"];
/**
Authorizer that works with the Ruby gem
[Devise](https://github.com/plataformatec/devise) by sending the `token` and
`email` properties from the session in the `Authorization` header.
__As token authentication is not actually part of devise anymore, the server
needs to implement some customizations__ to work with this authenticator -
see the README for more information.
_The factory for this authorizer is registered as
`'simple-auth-authorizer:devise'` in Ember's container._
@class Devise
@namespace SimpleAuth.Authorizers
@module simple-auth-devise/authorizers/devise
@extends Base
*/
__exports__["default"] = Base.extend({
/**
The token attribute name.
This value can be configured via
[`SimpleAuth.Configuration.Devise#tokenAttributeName`](#SimpleAuth-Configuration-Devise-tokenAttributeName).
@property tokenAttributeName
@type String
@default 'token'
*/
tokenAttributeName: 'token',
/**
The identification attribute name.
This value can be configured via
[`SimpleAuth.Configuration.Devise#identificationAttributeName`](#SimpleAuth-Configuration-Devise-identificationAttributeName).
@property identificationAttributeName
@type String
@default 'email'
*/
identificationAttributeName: 'email',
/**
Authorizes an XHR request by sending the `token` and `email`
properties from the session in the `Authorization` header:
```
Authorization: Token <tokenAttributeName>="<token>", <identificationAttributeName>="<user identification>"
```
@method authorize
@param {jqXHR} jqXHR The XHR request to authorize (see http://api.jquery.com/jQuery.ajax/#jqXHR)
@param {Object} requestOptions The options as provided to the `$.ajax` method (see http://api.jquery.com/jQuery.ajaxPrefilter/)
*/
/**
@method init
@private
*/
init: function() {
this.tokenAttributeName = Configuration.tokenAttributeName;
this.identificationAttributeName = Configuration.identificationAttributeName;
},
authorize: function(jqXHR, requestOptions) {
var secureData = this.get('session.secure');
var userToken = secureData[this.tokenAttributeName];
var userIdentification = secureData[this.identificationAttributeName];
if (this.get('session.isAuthenticated') && !Ember.isEmpty(userToken) && !Ember.isEmpty(userIdentification)) {
var authData = this.tokenAttributeName + '="' + userToken + '", ' + this.identificationAttributeName + '="' + userIdentification + '"';
jqXHR.setRequestHeader('Authorization', 'Token ' + authData);
}
}
});
});
define("simple-auth-devise/configuration",
["simple-auth/utils/load-config","exports"],
function(__dependency1__, __exports__) {
"use strict";
var loadConfig = __dependency1__["default"];
var defaults = {
serverTokenEndpoint: '/users/sign_in',
resourceName: 'user',
tokenAttributeName: 'token',
identificationAttributeName: 'email'
};
/**
Ember Simple Auth Device's configuration object.
To change any of these values, set them on the application's environment
object:
```js
ENV['simple-auth-devise'] = {
serverTokenEndpoint: '/some/other/endpoint'
}
```
@class Devise
@namespace SimpleAuth.Configuration
@module simple-auth/configuration
*/
__exports__["default"] = {
/**
The endpoint on the server the authenticator acquires the auth token
and email from.
@property serverTokenEndpoint
@readOnly
@static
@type String
@default '/users/sign_in'
*/
serverTokenEndpoint: defaults.serverTokenEndpoint,
/**
The devise resource name.
@property resourceName
@readOnly
@static
@type String
@default 'user'
*/
resourceName: defaults.resourceName,
/**
The token attribute name.
@property tokenAttributeName
@readOnly
@static
@type String
@default 'token'
*/
tokenAttributeName: defaults.tokenAttributeName,
/**
The identification attribute name. This is the parameter that is sent to
[serverTokenEndpoint](#SimpleAuth-Configuration-Devise-serverTokenEndpoint)
during the authentication process, is expected in the response and is used
by the
[Devise authorizer](#SimpleAuth-Authorizers-Devise).
@property identificationAttributeName
@readOnly
@static
@type String
@default 'email'
*/
identificationAttributeName: defaults.identificationAttributeName,
/**
@method load
@private
*/
load: loadConfig(defaults)
};
});
define("simple-auth-devise/ember",
["./initializer"],
function(__dependency1__) {
"use strict";
var initializer = __dependency1__["default"];
Ember.onLoad('Ember.Application', function(Application) {
Application.initializer(initializer);
});
});
define("simple-auth-devise/initializer",
["./configuration","simple-auth/utils/get-global-config","simple-auth-devise/authenticators/devise","simple-auth-devise/authorizers/devise","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __exports__) {
"use strict";
var Configuration = __dependency1__["default"];
var getGlobalConfig = __dependency2__["default"];
var Authenticator = __dependency3__["default"];
var Authorizer = __dependency4__["default"];
__exports__["default"] = {
name: 'simple-auth-devise',
before: 'simple-auth',
initialize: function(container, application) {
var config = getGlobalConfig('simple-auth-devise');
Configuration.load(container, config);
application.register('simple-auth-authorizer:devise', Authorizer);
application.register('simple-auth-authenticator:devise', Authenticator);
}
};
});
})(this);