-
Notifications
You must be signed in to change notification settings - Fork 603
/
internal-session.js
213 lines (189 loc) · 6.89 KB
/
internal-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
import RSVP from 'rsvp';
import { isEmpty, isNone } from '@ember/utils';
import ObjectProxy from '@ember/object/proxy';
import Evented from '@ember/object/evented';
import { merge, assign as emberAssign } from '@ember/polyfills';
import { deprecate } from '@ember/application/deprecations';
import { set } from '@ember/object';
import { debug, assert } from '@ember/debug';
import { getOwner } from '@ember/application';
const assign = emberAssign || merge;
export default ObjectProxy.extend(Evented, {
authenticator: null,
store: null,
isAuthenticated: false,
attemptedTransition: null,
init() {
this._super(...arguments);
this.set('content', { authenticated: {} });
this._busy = false;
this._bindToStoreEvents();
},
authenticate(authenticatorFactory, ...args) {
this._busy = true;
assert(`Session#authenticate requires the authenticator to be specified, was "${authenticatorFactory}"!`, !isEmpty(authenticatorFactory));
const authenticator = this._lookupAuthenticator(authenticatorFactory);
assert(`No authenticator for factory "${authenticatorFactory}" could be found!`, !isNone(authenticator));
return authenticator.authenticate(...args).then((content) => {
this._busy = false;
return this._setup(authenticatorFactory, content, true);
}, (error) => {
const rejectWithError = () => RSVP.Promise.reject(error);
this._busy = false;
return this._clear().then(rejectWithError, rejectWithError);
});
},
invalidate() {
this._busy = true;
if (!this.get('isAuthenticated')) {
this._busy = false;
return RSVP.Promise.resolve();
}
let authenticator = this._lookupAuthenticator(this.authenticator);
return authenticator.invalidate(this.content.authenticated, ...arguments).then(() => {
authenticator.off('sessionDataUpdated', this, this._onSessionDataUpdated);
this._busy = false;
return this._clear(true);
}, (error) => {
this.trigger('sessionInvalidationFailed', error);
this._busy = false;
return RSVP.Promise.reject(error);
});
},
restore() {
this._busy = true;
const reject = () => RSVP.Promise.reject();
return this._callStoreAsync('restore').then((restoredContent) => {
let { authenticator: authenticatorFactory } = restoredContent.authenticated || {};
if (authenticatorFactory) {
delete restoredContent.authenticated.authenticator;
const authenticator = this._lookupAuthenticator(authenticatorFactory);
return authenticator.restore(restoredContent.authenticated).then((content) => {
this.set('content', restoredContent);
this._busy = false;
return this._setup(authenticatorFactory, content);
}, (err) => {
debug(`The authenticator "${authenticatorFactory}" rejected to restore the session - invalidating…`);
if (err) {
debug(err);
}
this._busy = false;
return this._clearWithContent(restoredContent).then(reject, reject);
});
} else {
delete (restoredContent || {}).authenticated;
this._busy = false;
return this._clearWithContent(restoredContent).then(reject, reject);
}
}, () => {
this._busy = false;
return this._clear().then(reject, reject);
});
},
_callStoreAsync(method, ...params) {
const result = this.store[method](...params);
if (typeof result === 'undefined' || typeof result.then === 'undefined') {
deprecate(`Ember Simple Auth: Synchronous stores have been deprecated. Make sure your custom store's ${method} method returns a promise.`, false, {
id: `ember-simple-auth.session-store.synchronous-${method}`,
until: '2.0.0'
});
return RSVP.Promise.resolve(result);
} else {
return result;
}
},
_setup(authenticator, authenticatedContent, trigger) {
trigger = Boolean(trigger) && !this.get('isAuthenticated');
this.setProperties({
isAuthenticated: true,
authenticator,
'content.authenticated': authenticatedContent
});
this._bindToAuthenticatorEvents();
return this._updateStore()
.then(() => {
if (trigger) {
this.trigger('authenticationSucceeded');
}
}, () => {
this.setProperties({
isAuthenticated: false,
authenticator: null,
'content.authenticated': {}
});
});
},
_clear(trigger) {
trigger = Boolean(trigger) && this.get('isAuthenticated');
this.setProperties({
isAuthenticated: false,
authenticator: null,
'content.authenticated': {}
});
return this._updateStore().then(() => {
if (trigger) {
this.trigger('invalidationSucceeded');
}
});
},
_clearWithContent(content, trigger) {
this.set('content', content);
return this._clear(trigger);
},
setUnknownProperty(key, value) {
assert('"authenticated" is a reserved key used by Ember Simple Auth!', key !== 'authenticated');
let result = this._super(key, value);
if (!(/^_/).test(key)) {
this._updateStore();
}
return result;
},
_updateStore() {
let data = this.content;
if (!isEmpty(this.authenticator)) {
set(data, 'authenticated', assign({ authenticator: this.authenticator }, data.authenticated || {}));
}
return this._callStoreAsync('persist', data);
},
_bindToAuthenticatorEvents() {
const authenticator = this._lookupAuthenticator(this.authenticator);
authenticator.on('sessionDataUpdated', this, this._onSessionDataUpdated);
authenticator.on('sessionDataInvalidated', this, this._onSessionDataInvalidated);
},
_onSessionDataUpdated(content) {
this._setup(this.authenticator, content);
},
_onSessionDataInvalidated() {
this._clear(true);
},
_bindToStoreEvents() {
this.store.on('sessionDataUpdated', (content) => {
if (!this._busy) {
this._busy = true;
let { authenticator: authenticatorFactory } = (content.authenticated || {});
if (authenticatorFactory) {
delete content.authenticated.authenticator;
const authenticator = this._lookupAuthenticator(authenticatorFactory);
authenticator.restore(content.authenticated).then((authenticatedContent) => {
this.set('content', content);
this._busy = false;
this._setup(authenticatorFactory, authenticatedContent, true);
}, (err) => {
debug(`The authenticator "${authenticatorFactory}" rejected to restore the session - invalidating…`);
if (err) {
debug(err);
}
this._busy = false;
this._clearWithContent(content, true);
});
} else {
this._busy = false;
this._clearWithContent(content, true);
}
}
});
},
_lookupAuthenticator(authenticator) {
return getOwner(this).lookup(authenticator);
}
});