2017-03-11 02:33:46 +08:00
|
|
|
import { Tracker } from 'meteor/tracker';
|
|
|
|
|
2016-07-07 20:50:32 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
2017-03-11 02:33:46 +08:00
|
|
|
|
|
|
|
import Users from '/imports/api/users';
|
2016-06-29 02:50:44 +08:00
|
|
|
import { callServer } from '/imports/ui/services/api';
|
2016-06-02 21:46:35 +08:00
|
|
|
|
2016-07-11 21:45:24 +08:00
|
|
|
class Auth {
|
|
|
|
constructor() {
|
|
|
|
this._meetingID = Storage.getItem('meetingID');
|
|
|
|
this._userID = Storage.getItem('userID');
|
|
|
|
this._authToken = Storage.getItem('authToken');
|
2017-03-11 02:33:46 +08:00
|
|
|
this._loggedIn = {
|
|
|
|
value: false,
|
|
|
|
tracker: new Tracker.Dependency,
|
|
|
|
};
|
2016-07-11 21:45:24 +08:00
|
|
|
}
|
2016-06-02 21:46:35 +08:00
|
|
|
|
2016-07-11 21:45:24 +08:00
|
|
|
get meetingID() {
|
|
|
|
return this._meetingID;
|
|
|
|
}
|
2016-06-02 21:46:35 +08:00
|
|
|
|
2016-07-11 21:45:24 +08:00
|
|
|
set meetingID(meetingID) {
|
|
|
|
this._meetingID = meetingID;
|
|
|
|
Storage.setItem('meetingID', this._meetingID);
|
|
|
|
}
|
2016-06-02 21:46:35 +08:00
|
|
|
|
2016-07-11 21:45:24 +08:00
|
|
|
get userID() {
|
|
|
|
return this._userID;
|
|
|
|
}
|
2016-06-02 21:46:35 +08:00
|
|
|
|
2016-07-11 21:45:24 +08:00
|
|
|
set userID(userID) {
|
|
|
|
this._userID = userID;
|
|
|
|
Storage.setItem('userID', this._userID);
|
|
|
|
}
|
2016-06-18 06:15:11 +08:00
|
|
|
|
2016-07-11 21:45:24 +08:00
|
|
|
get token() {
|
|
|
|
return this._authToken;
|
2016-06-25 07:09:32 +08:00
|
|
|
}
|
2016-06-18 06:15:11 +08:00
|
|
|
|
2016-07-11 21:45:24 +08:00
|
|
|
set token(authToken) {
|
|
|
|
this._authToken = authToken;
|
|
|
|
Storage.setItem('authToken', this._authToken);
|
|
|
|
}
|
|
|
|
|
2017-03-10 03:50:21 +08:00
|
|
|
get loggedIn() {
|
2017-03-11 02:33:46 +08:00
|
|
|
this._loggedIn.tracker.depend();
|
|
|
|
return this._loggedIn.value;
|
2017-03-10 03:50:21 +08:00
|
|
|
}
|
|
|
|
|
2017-03-11 02:33:46 +08:00
|
|
|
set loggedIn(value) {
|
|
|
|
this._loggedIn.value = value;
|
|
|
|
this._loggedIn.tracker.changed();
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:23:00 +08:00
|
|
|
get credentials() {
|
|
|
|
return {
|
|
|
|
meetingId: this.meetingID,
|
|
|
|
requesterUserId: this.userID,
|
|
|
|
requesterToken: this.token,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
set credentials(value) {
|
|
|
|
throw 'Credentials are read-only';
|
|
|
|
}
|
|
|
|
|
|
|
|
clearCredentials() {
|
|
|
|
this.meetingID = null;
|
|
|
|
this.userID = null;
|
|
|
|
this.token = null;
|
|
|
|
this.loggedIn = false;
|
|
|
|
|
|
|
|
return Promise.resolve(...arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
if (!this.loggedIn) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
callServer('userLogout', () => {
|
|
|
|
this.fetchLogoutUrl()
|
|
|
|
.then(this.clearCredentials)
|
|
|
|
.then(resolve);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-11 04:47:23 +08:00
|
|
|
authenticate(meetingID, userID, token) {
|
2017-03-11 02:33:46 +08:00
|
|
|
if (arguments.length) {
|
2017-03-11 04:47:23 +08:00
|
|
|
this.meetingID = meetingID;
|
|
|
|
this.userID = userID;
|
2017-03-11 02:33:46 +08:00
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._subscribeToCurrentUser()
|
|
|
|
.then(this._addObserverToValidatedField.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
_subscribeToCurrentUser() {
|
2017-03-17 22:23:00 +08:00
|
|
|
const credentials = this.credentials;
|
2017-03-11 02:33:46 +08:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Tracker.autorun((c) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
c.stop();
|
|
|
|
reject('Authentication subscription timeout.');
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
const subscription = Meteor.subscribe('current-user', credentials);
|
|
|
|
if (!subscription.ready()) return;
|
|
|
|
|
|
|
|
resolve(c);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_addObserverToValidatedField(prevComp) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const validationTimeout = setTimeout(() => {
|
|
|
|
this.clearCredentials();
|
|
|
|
reject('Authentication timeout.');
|
|
|
|
}, 2500);
|
|
|
|
|
|
|
|
const didValidate = () => {
|
|
|
|
this.loggedIn = true;
|
|
|
|
clearTimeout(validationTimeout);
|
|
|
|
prevComp.stop();
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
Tracker.autorun((c) => {
|
|
|
|
const selector = { meetingId: this.meetingID, userId: this.userID };
|
|
|
|
const query = Users.find(selector);
|
|
|
|
|
|
|
|
if (query.count() && query.fetch()[0].validated) {
|
|
|
|
c.stop();
|
|
|
|
didValidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
const handle = query.observeChanges({
|
|
|
|
changed: (id, fields) => {
|
2017-03-11 03:18:23 +08:00
|
|
|
if (id !== this.userID) return;
|
|
|
|
|
2017-03-11 02:33:46 +08:00
|
|
|
if (fields.validated === true) {
|
|
|
|
c.stop();
|
|
|
|
didValidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fields.validated === false) {
|
|
|
|
c.stop();
|
|
|
|
this.clearCredentials();
|
|
|
|
reject('Authentication failed.');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-17 22:23:00 +08:00
|
|
|
const credentials = this.credentials;
|
2017-03-11 02:33:46 +08:00
|
|
|
callServer('validateAuthToken', credentials);
|
|
|
|
});
|
2016-07-11 21:45:24 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:23:00 +08:00
|
|
|
fetchLogoutUrl() {
|
|
|
|
const url = `/bigbluebutton/api/enter`;
|
2016-06-18 06:15:11 +08:00
|
|
|
|
2017-03-17 22:23:00 +08:00
|
|
|
return fetch(url)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => Promise.resolve(data.response.logoutURL));
|
2016-07-11 21:45:24 +08:00
|
|
|
}
|
2016-06-29 02:50:44 +08:00
|
|
|
};
|
|
|
|
|
2016-07-11 21:45:24 +08:00
|
|
|
let AuthSingleton = new Auth();
|
|
|
|
export default AuthSingleton;
|