cozy-proxy
Version:
Cozy Proxy redirects requests properly to the right application of the Cozy platform depending on given path. It also handles authentication to the Cozy for users and devices.
117 lines (101 loc) • 3.13 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var Client, User, client, cozydb, helpers, localization, ref, timezones, urlHelper,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
cozydb = require('cozydb');
Client = require('request-json').JsonClient;
urlHelper = require('cozy-url-sdk');
helpers = require('../lib/helpers');
timezones = require('../lib/timezones');
localization = require('../lib/localization_manager');
client = new Client(urlHelper.dataSystem.url());
if ((ref = process.env.NODE_ENV) === 'production' || ref === 'test') {
client.setBasicAuth(process.env.NAME, process.env.TOKEN);
}
module.exports = User = cozydb.getModel('User', {
email: String,
password: String,
salt: String,
public_name: String,
timezone: String,
owner: Boolean,
allow_stats: Boolean,
activated: Boolean,
otpKey: String,
hotpCounter: Number,
authType: String
});
User.createNew = function(data, callback) {
data.docType = "User";
return client.post("user/", data, function(err, res, body) {
if (err != null) {
return callback(err);
} else if (res.statusCode !== 201) {
err = res.statusCode + " -- " + body;
return callback(err);
} else {
return callback();
}
});
};
User.prototype.merge = function(data, callback) {
return client.put("user/merge/" + this.id + "/", data, function(err, res, body) {
if (err != null) {
return callback(err);
} else if (res.statusCode === 404) {
return callback(new Error("Model does not exist"));
} else if (res.statusCode !== 200) {
err = res.statusCode + " -- " + body;
return callback(err);
} else {
return callback();
}
});
};
User.first = function(callback) {
return User.request('all', function(err, users) {
if (err) {
return callback(err);
} else if (!users || users.length === 0) {
return callback(null, null);
} else {
return callback(null, users[0]);
}
});
};
User.getUsername = function(callback) {
return User.first(function(err, user) {
var ref1, username;
if (err) {
return callback(err);
}
if (!user) {
return callback();
}
username = ((ref1 = user.public_name) != null ? ref1.length : void 0) > 0 ? user.public_name : helpers.hideEmail(user.email).split(' ').map(function(word) {
return word[0].toUpperCase() + word.slice(1);
}).join(' ');
return callback(null, username);
});
};
User.validate = function(data, errors) {
var ref1;
if (errors == null) {
errors = {};
}
if (!helpers.checkEmail(data.email)) {
errors.email = localization.t('invalid email format');
}
if (!(ref1 = data.timezone, indexOf.call(timezones, ref1) >= 0)) {
errors.timezone = localization.t('invalid timezone');
}
return errors;
};
User.validatePassword = function(password, errors) {
if (errors == null) {
errors = {};
}
if ((password == null) || password.length < 8) {
errors.password = localization.t('password too short');
}
return errors;
};