forked from mainmatter/ember-simple-auth-component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-auth.js
1761 lines (1536 loc) · 64.6 KB
/
simple-auth.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(global) {
Ember.libraries.register('Ember Simple Auth', '0.8.0');
var define, requireModule;
(function() {
var registry = {}, seen = {};
define = function(name, deps, callback) {
registry[name] = { deps: deps, callback: callback };
};
requireModule = function(name) {
if (seen.hasOwnProperty(name)) { return seen[name]; }
seen[name] = {};
if (!registry[name]) {
throw new Error("Could not find module " + name);
}
var mod = registry[name],
deps = mod.deps,
callback = mod.callback,
reified = [],
exports;
for (var i=0, l=deps.length; i<l; i++) {
if (deps[i] === 'exports') {
reified.push(exports = {});
} else {
reified.push(requireModule(resolve(deps[i])));
}
}
var value = callback.apply(this, reified);
return seen[name] = exports || value;
function resolve(child) {
if (child.charAt(0) !== '.') { return child; }
var parts = child.split("/");
var parentBase = name.split("/").slice(0, -1);
for (var i=0, l=parts.length; i<l; i++) {
var part = parts[i];
if (part === '..') { parentBase.pop(); }
else if (part === '.') { continue; }
else { parentBase.push(part); }
}
return parentBase.join("/");
}
};
requireModule.registry = registry;
})();
define("simple-auth/authenticators/base",
["exports"],
function(__exports__) {
"use strict";
/**
The base for all authenticators. __This serves as a starting point for
implementing custom authenticators and must not be used directly.__
The authenticator authenticates the session. The actual mechanism used to do
this might e.g. be posting a set of credentials to a server and in exchange
retrieving an access token, initiating authentication against an external
provider like Facebook etc. and depends on the specific authenticator. Any
data that the authenticator receives upon successful authentication and
resolves with from the
[`Authenticators.Base#authenticate`](#SimpleAuth-Authenticators-Base-authenticate)
method is stored in the session and can then be used by the authorizer (see
[`Authorizers.Base`](#SimpleAuth-Authorizers-Base)).
The authenticator also decides whether a set of data that was restored from
the session store (see
[`Stores.Base`](#SimpleAuth-Stores-Base)) is sufficient for the session to be
authenticated or not.
__Custom authenticators have to be registered with Ember's dependency
injection container__ so that the session can retrieve an instance, e.g.:
```js
import Base from 'simple-auth/authenticators/base';
var CustomAuthenticator = Base.extend({
...
});
Ember.Application.initializer({
name: 'authentication',
initialize: function(container, application) {
application.register('authenticator:custom', CustomAuthenticator);
}
});
```
```js
// app/controllers/login.js
export default Ember.Controller.extend({
actions: {
authenticate: function() {
this.get('session').authenticate('authenticator:custom');
}
}
});
```
@class Base
@namespace SimpleAuth.Authenticators
@module simple-auth/authenticators/base
@extends Ember.Object
@uses Ember.Evented
*/
__exports__["default"] = Ember.Object.extend(Ember.Evented, {
/**
__Triggered when the data that constitutes the session is updated by the
authenticator__. This might happen e.g. because the authenticator refreshes
it or an event is triggered from an external authentication provider. The
session automatically catches that event, passes the updated data back to
the authenticator's
[SimpleAuth.Authenticators.Base#restore](#SimpleAuth-Authenticators-Base-restore)
method and handles the result of that invocation accordingly.
@event sessionDataUpdated
@param {Object} data The updated session data
*/
/**
__Triggered when the data that constitutes the session is invalidated by
the authenticator__. This might happen e.g. because the date expires or an
event is triggered from an external authentication provider. The session
automatically catches that event and invalidates itself.
@event sessionDataInvalidated
*/
/**
Restores the session from a set of properties. __This method is invoked by
the session either after the application starts up and session data was
restored from the store__ or when properties in the store have changed due
to external events (e.g. in another tab) and the new session data needs to
be re-checked for whether it still constitutes an authenticated session.
__This method returns a promise. A resolving promise will result in the
session being authenticated.__ Any data the promise resolves with will be
saved in and accessible via the session's `secure` property. In most cases,
`data` will simply be forwarded through the promise. A rejecting promise
indicates that `data` does not constitute a valid session and will result
in the session being invalidated.
`SimpleAuth.Authenticators.Base`'s implementation always returns a
rejecting promise.
@method restore
@param {Object} data The data to restore the session from
@return {Ember.RSVP.Promise} A promise that when it resolves results in the session being authenticated
*/
restore: function(data) {
return Ember.RSVP.reject();
},
/**
Authenticates the session with the specified `options`. These options vary
depending on the actual authentication mechanism the authenticator
implements (e.g. a set of credentials or a Facebook account id etc.). __The
session will invoke this method when it is being authenticated__ (see
[SimpleAuth.Session#authenticate](#SimpleAuth-Session-authenticate)).
__This method returns a promise. A resolving promise will result in the
session being authenticated.__ Any properties the promise resolves with
will be saved in and accessible via the session's `secure` property. A
rejecting promise indicates that authentication failed and the session will
remain unchanged.
`SimpleAuth.Authenticators.Base`'s implementation always returns a
rejecting promise and thus never authenticates the session.
@method authenticate
@param {Any} [...options] The arguments that the authenticator requires to authenticate the session
@return {Ember.RSVP.Promise} A promise that when it resolves results in the session being authenticated
*/
authenticate: function(options) {
return Ember.RSVP.reject();
},
/**
This callback is invoked when the session is invalidated. While the session
will invalidate itself and clear all session properties, it might be
necessary for some authenticators to perform additional tasks (e.g.
invalidating an access token on the server), which should be done in this
method.
__This method returns a promise. A resolving promise will result in the
session being invalidated.__ A rejecting promise will result in the session
invalidation being intercepted and the session being left authenticated.
`SimpleAuth.Authenticators.Base`'s implementation always returns a
resolving promise and thus never intercepts session invalidation.
@method invalidate
@param {Object} data The data that the session currently holds
@return {Ember.RSVP.Promise} A promise that when it resolves results in the session being invalidated
*/
invalidate: function(data) {
return Ember.RSVP.resolve();
}
});
});
define("simple-auth/authorizers/base",
["exports"],
function(__exports__) {
"use strict";
/**
The base for all authorizers. __This serves as a starting point for
implementing custom authorizers and must not be used directly.__
__The authorizer preprocesses all XHR requests__ (except ones to 3rd party
origins, see
[Configuration.crossOriginWhitelist](#SimpleAuth-Configuration-crossOriginWhitelist))
and makes sure they have the required data attached that allows the server to
identify the user making the request. This data might be an HTTP header,
query string parameters in the URL, cookies etc. __The authorizer has to fit
the authenticator__ (see
[SimpleAuth.Authenticators.Base](#SimpleAuth-Authenticators-Base))
as it relies on data that the authenticator acquires during authentication.
@class Base
@namespace SimpleAuth.Authorizers
@module simple-auth/authorizers/base
@extends Ember.Object
*/
__exports__["default"] = Ember.Object.extend({
/**
The session the authorizer gets the data it needs to authorize requests
from.
@property session
@readOnly
@type SimpleAuth.Session
@default the session instance
*/
session: null,
/**
Authorizes an XHR request by adding some sort of secret information that
allows the server to identify the user making the request (e.g. a token in
the `Authorization` header or some other secret in the query string etc.).
`SimpleAuth.Authorizers.Base`'s implementation does nothing.
@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/)
*/
authorize: function(jqXHR, requestOptions) {
}
});
});
define("simple-auth/configuration",
["simple-auth/utils/load-config","exports"],
function(__dependency1__, __exports__) {
"use strict";
var loadConfig = __dependency1__["default"];
var defaults = {
authenticationRoute: 'login',
routeAfterAuthentication: 'index',
routeIfAlreadyAuthenticated: 'index',
sessionPropertyName: 'session',
authorizer: null,
session: 'simple-auth-session:main',
store: 'simple-auth-session-store:local-storage',
localStorageKey: 'ember_simple_auth:session',
crossOriginWhitelist: [],
applicationRootUrl: null
};
/**
Ember Simple Auth's configuration object.
To change any of these values, set them on the application's environment
object:
```js
ENV['simple-auth'] = {
authenticationRoute: 'sign-in'
};
```
@class Configuration
@namespace SimpleAuth
@module simple-auth/configuration
*/
__exports__["default"] = {
/**
The route to transition to for authentication.
@property authenticationRoute
@readOnly
@static
@type String
@default 'login'
*/
authenticationRoute: defaults.authenticationRoute,
/**
The route to transition to after successful authentication.
@property routeAfterAuthentication
@readOnly
@static
@type String
@default 'index'
*/
routeAfterAuthentication: defaults.routeAfterAuthentication,
/**
The route to transition to if a route that implements
[`UnauthenticatedRouteMixin`](#SimpleAuth-UnauthenticatedRouteMixin) is
accessed when the session is authenticated.
@property routeIfAlreadyAuthenticated
@readOnly
@static
@type String
@default 'index'
*/
routeIfAlreadyAuthenticated: defaults.routeIfAlreadyAuthenticated,
/**
The name of the property that the session is injected with into routes and
controllers.
@property sessionPropertyName
@readOnly
@static
@type String
@default 'session'
*/
sessionPropertyName: defaults.sessionPropertyName,
/**
The authorizer factory to use as it is registered with Ember's container,
see
[Ember's API docs](http://emberjs.com/api/classes/Ember.Application.html#method_register);
when the application does not interact with a server that requires
authorized requests, no authorizer is needed.
@property authorizer
@readOnly
@static
@type String
@default null
*/
authorizer: defaults.authorizer,
/**
The session factory to use as it is registered with Ember's container,
see
[Ember's API docs](http://emberjs.com/api/classes/Ember.Application.html#method_register).
@property session
@readOnly
@static
@type String
@default 'simple-auth-session:main'
*/
session: defaults.session,
/**
The store factory to use as it is registered with Ember's container, see
[Ember's API docs](http://emberjs.com/api/classes/Ember.Application.html#method_register).
@property store
@readOnly
@static
@type String
@default simple-auth-session-store:local-storage
*/
store: defaults.store,
/**
The key the store stores the data in.
@property localStorageKey
@type String
@default 'ember_simple_auth:session'
*/
localStorageKey: defaults.localStorageKey,
/**
Ember Simple Auth will never authorize requests going to a different origin
than the one the Ember.js application was loaded from; to explicitely
enable authorization for additional origins, whitelist those origins with
this setting. _Beware that origins consist of protocol, host and port (port
can be left out when it is 80 for HTTP or 443 for HTTPS)_, e.g.
`http://domain.com:1234`, `https://external.net`. You can also whitelist
all subdomains for a specific domain using wildcard expressions e.g.
`http://*.domain.com:1234`, `https://*.external.net` or whitelist all
external origins by specifying `['*']`.
@property crossOriginWhitelist
@readOnly
@static
@type Array
@default []
*/
crossOriginWhitelist: defaults.crossOriginWhitelist,
/**
@property applicationRootUrl
@private
*/
applicationRootUrl: defaults.applicationRootUrl,
/**
@method load
@private
*/
load: loadConfig(defaults, function(container, config) {
this.applicationRootUrl = container.lookup('router:main').get('rootURL') || '/';
})
};
});
define("simple-auth/ember",
["./initializer"],
function(__dependency1__) {
"use strict";
var initializer = __dependency1__["default"];
Ember.onLoad('Ember.Application', function(Application) {
Application.initializer(initializer);
});
});
define("simple-auth/initializer",
["./configuration","./utils/get-global-config","./setup","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __exports__) {
"use strict";
var Configuration = __dependency1__["default"];
var getGlobalConfig = __dependency2__["default"];
var setup = __dependency3__["default"];
__exports__["default"] = {
name: 'simple-auth',
initialize: function(container, application) {
var config = getGlobalConfig('simple-auth');
Configuration.load(container, config);
setup(container, application);
}
};
});
define("simple-auth/mixins/application-route-mixin",
["./../configuration","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Configuration = __dependency1__["default"];
/**
The mixin for the application route; defines actions that are triggered
when authentication is required, when the session has successfully been
authenticated or invalidated or when authentication or invalidation fails or
authorization is rejected by the server. These actions provide a good
starting point for adding custom behavior to these events.
__When this mixin is used and the application's `ApplicationRoute` defines
the `beforeModel` method, that method has to call `_super`.__
Using this mixin is optional. Without using it, the session's events will not
be automatically translated into route actions but would have to be handled
inidivially, e.g. in an initializer:
```js
Ember.Application.initializer({
name: 'authentication',
after: 'simple-auth',
initialize: function(container, application) {
var applicationRoute = container.lookup('route:application');
var session = container.lookup('simple-auth-session:main');
// handle the session events
session.on('sessionAuthenticationSucceeded', function() {
applicationRoute.transitionTo('index');
});
}
});
```
@class ApplicationRouteMixin
@namespace SimpleAuth
@module simple-auth/mixins/application-route-mixin
@extends Ember.Mixin
@static
*/
__exports__["default"] = Ember.Mixin.create({
/**
@method activate
@private
*/
activate: function () {
/*
Used to detect the first time the application route is entered so that
the transition can be used as the target of send before entering the
application route and the route can be used once it has been entered.
*/
this.set('_authRouteEntryComplete', true);
this._super();
},
/**
@method beforeModel
@private
*/
beforeModel: function(transition) {
this._super(transition);
if (!this.get('_authEventListenersAssigned')) {
this.set('_authEventListenersAssigned', true);
var _this = this;
Ember.A([
'sessionAuthenticationSucceeded',
'sessionAuthenticationFailed',
'sessionInvalidationSucceeded',
'sessionInvalidationFailed',
'authorizationFailed'
]).forEach(function(event) {
_this.get(Configuration.sessionPropertyName).on(event, function(error) {
Array.prototype.unshift.call(arguments, event);
var target = _this.get('_authRouteEntryComplete') ? _this : transition;
target.send.apply(target, arguments);
});
});
}
},
actions: {
/**
This action triggers a transition to the
[`Configuration.authenticationRoute`](#SimpleAuth-Configuration-authenticationRoute).
It is triggered automatically by the
[`AuthenticatedRouteMixin`](#SimpleAuth-AuthenticatedRouteMixin) whenever
a route that requires authentication is accessed but the session is not
currently authenticated.
__For an application that works without an authentication route (e.g.
because it opens a new window to handle authentication there), this is
the action to override, e.g.:__
```js
App.ApplicationRoute = Ember.Route.extend(SimpleAuth.ApplicationRouteMixin, {
actions: {
sessionRequiresAuthentication: function() {
this.get('session').authenticate('authenticator:custom', {});
}
}
});
```
@method actions.sessionRequiresAuthentication
*/
sessionRequiresAuthentication: function() {
this.transitionTo(Configuration.authenticationRoute);
},
/**
This action triggers a transition to the
[`Configuration.authenticationRoute`](#SimpleAuth-Configuration-authenticationRoute).
It can be used in templates as shown above. It is also triggered
automatically by the
[`AuthenticatedRouteMixin`](#SimpleAuth-AuthenticatedRouteMixin) whenever
a route that requries authentication is accessed but the session is not
currently authenticated.
__For an application that works without an authentication route (e.g.
because it opens a new window to handle authentication there), this is
the action to override, e.g.:__
```js
App.ApplicationRoute = Ember.Route.extend(SimpleAuth.ApplicationRouteMixin, {
actions: {
authenticateSession: function() {
this.get('session').authenticate('authenticator:custom', {});
}
}
});
```
@method actions.authenticateSession
@deprecated use [`ApplicationRouteMixin#sessionRequiresAuthentication`](#SimpleAuth-ApplicationRouteMixin-sessionRequiresAuthentication) instead
*/
authenticateSession: function() {
Ember.deprecate('The authenticateSession action is deprecated. Use sessionRequiresAuthentication instead.');
this.send('sessionRequiresAuthentication');
},
/**
This action is triggered whenever the session is successfully
authenticated. If there is a transition that was previously intercepted
by
[`AuthenticatedRouteMixin#beforeModel`](#SimpleAuth-AuthenticatedRouteMixin-beforeModel)
it will retry it. If there is no such transition, this action transitions
to the
[`Configuration.routeAfterAuthentication`](#SimpleAuth-Configuration-routeAfterAuthentication).
@method actions.sessionAuthenticationSucceeded
*/
sessionAuthenticationSucceeded: function() {
var attemptedTransition = this.get(Configuration.sessionPropertyName).get('attemptedTransition');
if (attemptedTransition) {
attemptedTransition.retry();
this.get(Configuration.sessionPropertyName).set('attemptedTransition', null);
} else {
this.transitionTo(Configuration.routeAfterAuthentication);
}
},
/**
This action is triggered whenever session authentication fails. The
`error` argument is the error object that the promise the authenticator
returns rejects with. (see
[`Authenticators.Base#authenticate`](#SimpleAuth-Authenticators-Base-authenticate)).
It can be overridden to display error messages etc.:
```js
App.ApplicationRoute = Ember.Route.extend(SimpleAuth.ApplicationRouteMixin, {
actions: {
sessionAuthenticationFailed: function(error) {
this.controllerFor('application').set('loginErrorMessage', error.message);
}
}
});
```
@method actions.sessionAuthenticationFailed
@param {any} error The error the promise returned by the authenticator rejects with, see [`Authenticators.Base#authenticate`](#SimpleAuth-Authenticators-Base-authenticate)
*/
sessionAuthenticationFailed: function(error) {
},
/**
This action invalidates the session (see
[`Session#invalidate`](#SimpleAuth-Session-invalidate)).
If invalidation succeeds, it reloads the application (see
[`ApplicationRouteMixin#sessionInvalidationSucceeded`](#SimpleAuth-ApplicationRouteMixin-sessionInvalidationSucceeded)).
@method actions.invalidateSession
@deprecated use [`Session#invalidate`](#SimpleAuth-Session-invalidate) instead
*/
invalidateSession: function() {
Ember.deprecate("The invalidateSession action is deprecated. Use the session's invalidate method directly instead.");
this.get(Configuration.sessionPropertyName).invalidate();
},
/**
This action is invoked whenever the session is successfully invalidated.
It reloads the Ember.js application by redirecting the browser to the
application's root URL so that all in-memory data (such as Ember Data
stores etc.) gets cleared. The root URL is automatically retrieved from
the Ember.js application's router (see
http://emberjs.com/guides/routing/#toc_specifying-a-root-url).
If your Ember.js application will be used in an environment where the
users don't have direct access to any data stored on the client (e.g.
[cordova](http://cordova.apache.org)) this action can be overridden to
simply transition to the `'index'` route.
@method actions.sessionInvalidationSucceeded
*/
sessionInvalidationSucceeded: function() {
if (!Ember.testing) {
window.location.replace(Configuration.applicationRootUrl);
}
},
/**
This action is invoked whenever session invalidation fails. This mainly
serves as an extension point to add custom behavior and does nothing by
default.
@method actions.sessionInvalidationFailed
@param {any} error The error the promise returned by the authenticator rejects with, see [`Authenticators.Base#invalidate`](#SimpleAuth-Authenticators-Base-invalidate)
*/
sessionInvalidationFailed: function(error) {
},
/**
This action is invoked when an authorization error occurs (which is
the case __when the server responds with HTTP status 401__). It
invalidates the session and reloads the application (see
[`ApplicationRouteMixin#sessionInvalidationSucceeded`](#SimpleAuth-ApplicationRouteMixin-sessionInvalidationSucceeded)).
@method actions.authorizationFailed
*/
authorizationFailed: function() {
if (this.get(Configuration.sessionPropertyName).get('isAuthenticated')) {
this.get(Configuration.sessionPropertyName).invalidate();
}
}
}
});
});
define("simple-auth/mixins/authenticated-route-mixin",
["./../configuration","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Configuration = __dependency1__["default"];
/**
This mixin is for routes that require the session to be authenticated to be
accessible. Including this mixin in a route automatically adds a hook that
enforces the session to be authenticated and redirects to the
[`Configuration.authenticationRoute`](#SimpleAuth-Configuration-authenticationRoute)
if it is not.
```js
// app/routes/protected.js
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin);
```
`AuthenticatedRouteMixin` performs the redirect in the `beforeModel` method
so that in all methods executed after that the session is guaranteed to be
authenticated. __If `beforeModel` is overridden, ensure that the custom
implementation calls `this._super(transition)`__ so that the session
enforcement code is actually executed.
@class AuthenticatedRouteMixin
@namespace SimpleAuth
@module simple-auth/mixins/authenticated-route-mixin
@extends Ember.Mixin
@static
*/
__exports__["default"] = Ember.Mixin.create({
/**
This method implements the enforcement of the session being authenticated.
If the session is not authenticated, the current transition will be aborted
and a redirect will be triggered to the
[`Configuration.authenticationRoute`](#SimpleAuth-Configuration-authenticationRoute).
The method also saves the intercepted transition so that it can be retried
after the session has been authenticated (see
[`ApplicationRouteMixin#sessionAuthenticationSucceeded`](#SimpleAuth-ApplicationRouteMixin-sessionAuthenticationSucceeded)).
@method beforeModel
@param {Transition} transition The transition that lead to this route
*/
beforeModel: function(transition) {
var superResult = this._super(transition);
if (!this.get(Configuration.sessionPropertyName).get('isAuthenticated')) {
transition.abort();
this.get(Configuration.sessionPropertyName).set('attemptedTransition', transition);
Ember.assert('The route configured as Configuration.authenticationRoute cannot implement the AuthenticatedRouteMixin mixin as that leads to an infinite transitioning loop!', this.get('routeName') !== Configuration.authenticationRoute);
transition.send('sessionRequiresAuthentication');
}
return superResult;
}
});
});
define("simple-auth/mixins/authentication-controller-mixin",
["./../configuration","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Configuration = __dependency1__["default"];
/**
This mixin is for the controller that handles the
[`Configuration.authenticationRoute`](#SimpleAuth-Configuration-authenticationRoute).
It provides the `authenticate` action that will authenticate the session with
the configured authenticator (see
[`AuthenticationControllerMixin#authenticator`](#SimpleAuth-AuthenticationControllerMixin-authenticator)).
@class AuthenticationControllerMixin
@namespace SimpleAuth
@module simple-auth/mixins/authentication-controller-mixin
@extends Ember.Mixin
@deprecated use [`Session#authenticate`](#SimpleAuth-Session-authenticate) instead
*/
__exports__["default"] = Ember.Mixin.create({
/**
The authenticator factory to use as it is registered with Ember's
container, see
[Ember's API docs](http://emberjs.com/api/classes/Ember.Application.html#method_register).
@property authenticator
@type String
@default null
*/
authenticator: null,
actions: {
/**
This action will authenticate the session with the configured
authenticator (see
[`AuthenticationControllerMixin#authenticator`](#SimpleAuth-AuthenticationControllerMixin-authenticator),
[`Session#authenticate`](#SimpleAuth-Session-authenticate)).
@method actions.authenticate
@param {Object} options Any options the authenticator needs to authenticate the session
*/
authenticate: function(options) {
Ember.deprecate("The AuthenticationControllerMixin is deprecated. Use the session's authenticate method directly instead.");
var authenticator = this.get('authenticator');
Ember.assert('AuthenticationControllerMixin/LoginControllerMixin require the authenticator property to be set on the controller!', !Ember.isEmpty(authenticator));
return this.get(Configuration.sessionPropertyName).authenticate(authenticator, options);
}
}
});
});
define("simple-auth/mixins/login-controller-mixin",
["./../configuration","./authentication-controller-mixin","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var Configuration = __dependency1__["default"];
var AuthenticationControllerMixin = __dependency2__["default"];
/**
This mixin is for the controller that handles the
[`Configuration.authenticationRoute`](#SimpleAuth-Configuration-authenticationRoute)
if the used authentication mechanism works with a login form that asks for
user credentials. It provides the `authenticate` action that will
authenticate the session with the configured authenticator when invoked.
__This is a specialization of
[`AuthenticationControllerMixin`](#SimpleAuth-AuthenticationControllerMixin).__
Accompanying the controller that this mixin is mixed in the application needs
to have a `login` template with the fields `identification` and `password` as
well as an actionable button or link that triggers the `authenticate` action,
e.g.:
```handlebars
<form {{action 'authenticate' on='submit'}}>
<label for="identification">Login</label>
{{input value=identification placeholder='Enter Login'}}
<label for="password">Password</label>
{{input value=password placeholder='Enter Password' type='password'}}
<button type="submit">Login</button>
</form>
```
@class LoginControllerMixin
@namespace SimpleAuth
@module simple-auth/mixins/login-controller-mixin
@extends SimpleAuth.AuthenticationControllerMixin
@deprecated use [`Session#authenticate`](#SimpleAuth-Session-authenticate) instead
*/
__exports__["default"] = Ember.Mixin.create(AuthenticationControllerMixin, {
actions: {
/**
This action will authenticate the session with the configured
authenticator (see
[AuthenticationControllerMixin#authenticator](#SimpleAuth-Authentication-authenticator))
if both `identification` and `password` are non-empty. It passes both
values to the authenticator.
__The action also resets the `password` property so sensitive data does
not stay in memory for longer than necessary.__
@method actions.authenticate
*/
authenticate: function() {
Ember.deprecate("The LoginControllerMixin is deprecated. Use the session's authenticate method directly instead.");
var data = this.getProperties('identification', 'password');
this.set('password', null);
return this._super(data);
}
}
});
});
define("simple-auth/mixins/unauthenticated-route-mixin",
["./../configuration","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Configuration = __dependency1__["default"];
/**
This mixin is for routes that should only be accessible if the session is
not authenticated. This is e.g. the case for the login route that should not
be accessible when the session is already authenticated. Including this mixin
in a route automatically adds a hook that redirects to the
[`Configuration.routeIfAlreadyAuthenticated`](#SimpleAuth-Configuration-routeIfAlreadyAuthenticated),
which defaults to `'index'`.
```js
// app/routes/login.js
import UnauthenticatedRouteMixin from 'simple-auth/mixins/unauthenticated-route-mixin';
export default Ember.Route.extend(UnauthenticatedRouteMixin);
```
`UnauthenticatedRouteMixin` performs the redirect in the `beforeModel`
method. __If `beforeModel` is overridden, ensure that the custom
implementation calls `this._super(transition)`__.
@class UnauthenticatedRouteMixin
@namespace SimpleAuth
@module simple-auth/mixins/unauthenticated-route-mixin
@extends Ember.Mixin
@static
*/
__exports__["default"] = Ember.Mixin.create({
/**
This method implements the enforcement of the session not being
authenticated. If the session is authenticated, the current transition will
be aborted and a redirect will be triggered to the
[`Configuration.routeIfAlreadyAuthenticated`](#SimpleAuth-Configuration-routeIfAlreadyAuthenticated).
@method beforeModel
@param {Transition} transition The transition that lead to this route
*/
beforeModel: function(transition) {
if (this.get(Configuration.sessionPropertyName).get('isAuthenticated')) {
transition.abort();
Ember.assert('The route configured as Configuration.routeIfAlreadyAuthenticated cannot implement the UnauthenticatedRouteMixin mixin as that leads to an infinite transitioning loop!', this.get('routeName') !== Configuration.routeIfAlreadyAuthenticated);
this.transitionTo(Configuration.routeIfAlreadyAuthenticated);
}
}
});
});
define("simple-auth/session",
["exports"],
function(__exports__) {
"use strict";
/**
__The session provides access to the current authentication state as well as
any data the authenticator resolved with__ (see
[`Authenticators.Base#authenticate`](#SimpleAuth-Authenticators-Base-authenticate)).
It is created when Ember Simple Auth is set up and __injected into all
controllers and routes so that these parts of the application can always
access the current authentication state and other data__, depending on the
authenticator in use and whether the session is actually authenticated (see
[`Authenticators.Base`](#SimpleAuth-Authenticators-Base)).
The session also provides methods to authenticate and to invalidate itself
(see
[`Session#authenticate`](#SimpleAuth-Session-authenticate),
[`Session#invaldiate`](#SimpleAuth-Session-invaldiate)).
These methods are usually invoked through actions from routes or controllers.
To authenticate the session manually, simple call the
[`Session#authenticate`](#SimpleAuth-Session-authenticate)
method with the authenticator factory to use as well as any options the
authenticator needs to authenticate the session:
```js
this.get('session').authenticate('authenticator:custom', { some: 'option' }).then(function() {
// authentication was successful
}, function() {
// authentication failed
});
```
The session also observes the store and - if it is authenticated - the
authenticator for changes (see
[`Authenticators.Base`](#SimpleAuth-Authenticators-Base)
end [`Stores.Base`](#SimpleAuth-Stores-Base)).
@class Session
@namespace SimpleAuth
@module simple-auth/session
@extends Ember.ObjectProxy
@uses Ember.Evented
*/
__exports__["default"] = Ember.ObjectProxy.extend(Ember.Evented, {
/**
Triggered __whenever the session is successfully authenticated__. When the
application uses the
[`ApplicationRouteMixin` mixin](#SimpleAuth-ApplicationRouteMixin),
[`ApplicationRouteMixin.actions#sessionAuthenticationSucceeded`](#SimpleAuth-ApplicationRouteMixin-sessionAuthenticationSucceeded)
will be invoked whenever this event is triggered.
@event sessionAuthenticationSucceeded
*/
/**
Triggered __whenever an attempt to authenticate the session fails__. When
the application uses the
[`ApplicationRouteMixin` mixin](#SimpleAuth-ApplicationRouteMixin),
[`ApplicationRouteMixin.actions#sessionAuthenticationFailed`](#SimpleAuth-ApplicationRouteMixin-sessionAuthenticationFailed)
will be invoked whenever this event is triggered.
@event sessionAuthenticationFailed
@param {Object} error The error object; this depends on the authenticator in use, see [SimpleAuth.Authenticators.Base#authenticate](#SimpleAuth-Authenticators-Base-authenticate)
*/
/**
Triggered __whenever the session is successfully invalidated__. When the
application uses the
[`ApplicationRouteMixin` mixin](#SimpleAuth-ApplicationRouteMixin),
[`ApplicationRouteMixin.actions#sessionInvalidationSucceeded`](#SimpleAuth-ApplicationRouteMixin-sessionInvalidationSucceeded)
will be invoked whenever this event is triggered.
@event sessionInvalidationSucceeded
*/
/**
Triggered __whenever an attempt to invalidate the session fails__. When the
application uses the
[`ApplicationRouteMixin` mixin](#SimpleAuth-ApplicationRouteMixin),
[`ApplicationRouteMixin.actions#sessionInvalidationFailed`](#SimpleAuth-ApplicationRouteMixin-sessionInvalidationFailed)
will be invoked whenever this event is triggered.
@event sessionInvalidationFailed