2016-03-28 20:33:20 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-08 08:01:32 +00:00
|
|
|
const cookies = require('js-cookie');
|
2016-03-28 20:33:20 +00:00
|
|
|
const request = require('superagent');
|
|
|
|
const config = require('./config.js');
|
2016-04-07 17:03:49 +00:00
|
|
|
const events = require('./events.js');
|
2016-03-28 20:33:20 +00:00
|
|
|
|
|
|
|
class Api {
|
2016-03-30 18:45:37 +00:00
|
|
|
constructor() {
|
2016-03-30 20:05:57 +00:00
|
|
|
this.user = null;
|
2016-03-30 18:45:37 +00:00
|
|
|
this.userName = null;
|
|
|
|
this.userPassword = null;
|
2016-04-11 16:45:58 +00:00
|
|
|
this.cache = {};
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 20:33:20 +00:00
|
|
|
get(url) {
|
2016-04-11 16:45:58 +00:00
|
|
|
if (url in this.cache) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(this.cache[url]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._process(url, request.get).then(response => {
|
|
|
|
this.cache[url] = response;
|
|
|
|
return Promise.resolve(response);
|
|
|
|
});
|
2016-03-28 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 13:55:56 +00:00
|
|
|
post(url, data, files) {
|
2016-04-11 16:45:58 +00:00
|
|
|
this.cache = {};
|
2016-04-10 13:55:56 +00:00
|
|
|
return this._process(url, request.post, data, files);
|
2016-03-28 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 13:55:56 +00:00
|
|
|
put(url, data, files) {
|
2016-04-11 16:45:58 +00:00
|
|
|
this.cache = {};
|
2016-04-10 13:55:56 +00:00
|
|
|
return this._process(url, request.put, data, files);
|
2016-04-07 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 13:55:56 +00:00
|
|
|
delete(url) {
|
2016-04-11 16:45:58 +00:00
|
|
|
this.cache = {};
|
2016-04-10 13:55:56 +00:00
|
|
|
return this._process(url, request.delete);
|
2016-04-09 07:52:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 13:55:56 +00:00
|
|
|
_process(url, requestFactory, data, files) {
|
|
|
|
const fullUrl = this.getFullUrl(url);
|
2016-03-28 20:33:20 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-04-10 13:55:56 +00:00
|
|
|
let req = requestFactory(fullUrl);
|
|
|
|
if (data) {
|
|
|
|
req.attach('metadata', new Blob([JSON.stringify(data)]));
|
|
|
|
}
|
|
|
|
if (files) {
|
|
|
|
for (let key of Object.keys(files)) {
|
|
|
|
req.attach(key, files[key]);
|
|
|
|
}
|
|
|
|
}
|
2016-03-28 20:33:20 +00:00
|
|
|
if (this.userName && this.userPassword) {
|
|
|
|
req.auth(this.userName, this.userPassword);
|
|
|
|
}
|
|
|
|
req.set('Accept', 'application/json')
|
|
|
|
.end((error, response) => {
|
|
|
|
if (error) {
|
2016-04-02 16:57:17 +00:00
|
|
|
reject(response && response.body ? response.body : {
|
|
|
|
'title': 'Networking error',
|
|
|
|
'description': error.message});
|
2016-03-28 20:33:20 +00:00
|
|
|
} else {
|
|
|
|
resolve(response.body);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:05:57 +00:00
|
|
|
hasPrivilege(lookup) {
|
|
|
|
let minViableRank = null;
|
|
|
|
for (let privilege of Object.keys(config.privileges)) {
|
|
|
|
if (!privilege.startsWith(lookup)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const rankName = config.privileges[privilege];
|
2016-04-06 18:38:45 +00:00
|
|
|
const rankIndex = config.ranks.indexOf(rankName);
|
2016-03-30 20:05:57 +00:00
|
|
|
if (minViableRank === null || rankIndex < minViableRank) {
|
|
|
|
minViableRank = rankIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (minViableRank === null) {
|
|
|
|
console.error('Bad privilege name: ' + lookup);
|
|
|
|
}
|
|
|
|
let myRank = this.user !== null ?
|
2016-04-06 18:38:45 +00:00
|
|
|
config.ranks.indexOf(this.user.rank) :
|
2016-03-30 20:05:57 +00:00
|
|
|
0;
|
|
|
|
return myRank >= minViableRank;
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 08:01:32 +00:00
|
|
|
loginFromCookies() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const auth = cookies.getJSON('auth');
|
|
|
|
if (auth && auth.user && auth.password) {
|
|
|
|
this.login(auth.user, auth.password, true)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(errorMessage => {
|
|
|
|
reject(errorMessage);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
login(userName, userPassword, doRemember) {
|
2016-03-30 19:01:18 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.userName = userName;
|
|
|
|
this.userPassword = userPassword;
|
2016-04-03 13:12:15 +00:00
|
|
|
this.get('/user/' + userName + '?bump-login=true')
|
2016-03-30 20:05:57 +00:00
|
|
|
.then(response => {
|
2016-04-08 08:01:32 +00:00
|
|
|
const options = {};
|
|
|
|
if (doRemember) {
|
|
|
|
options.expires = 365;
|
|
|
|
}
|
|
|
|
cookies.set(
|
|
|
|
'auth',
|
|
|
|
{'user': userName, 'password': userPassword},
|
|
|
|
options);
|
2016-03-30 20:05:57 +00:00
|
|
|
this.user = response.user;
|
|
|
|
resolve();
|
2016-04-07 17:03:49 +00:00
|
|
|
events.notify(events.Authentication);
|
2016-03-30 20:05:57 +00:00
|
|
|
}).catch(response => {
|
2016-03-30 19:01:18 +00:00
|
|
|
reject(response.description);
|
|
|
|
this.logout();
|
2016-04-07 17:03:49 +00:00
|
|
|
events.notify(events.Authentication);
|
2016-03-30 19:01:18 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logout() {
|
2016-04-01 11:09:07 +00:00
|
|
|
this.user = null;
|
2016-03-30 18:45:37 +00:00
|
|
|
this.userName = null;
|
|
|
|
this.userPassword = null;
|
2016-04-07 17:03:49 +00:00
|
|
|
events.notify(events.Authentication);
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 17:53:53 +00:00
|
|
|
forget() {
|
|
|
|
cookies.remove('auth');
|
|
|
|
}
|
|
|
|
|
2016-04-16 13:07:33 +00:00
|
|
|
isLoggedIn(user) {
|
|
|
|
if (user) {
|
|
|
|
return this.userName !== null &&
|
|
|
|
this.userName.toLowerCase() === user.name.toLowerCase();
|
|
|
|
} else {
|
|
|
|
return this.userName !== null;
|
|
|
|
}
|
2016-03-30 18:45:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 20:33:20 +00:00
|
|
|
getFullUrl(url) {
|
2016-04-06 18:38:45 +00:00
|
|
|
return (config.apiUrl + '/' + url).replace(/([^:])\/+/g, '$1/');
|
2016-03-28 20:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 22:20:34 +00:00
|
|
|
module.exports = new Api();
|