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.
171 lines (147 loc) • 5.14 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var ArrayHelper, Client, LAST_UNAUTHENTICATED_STEP, ONBOARDING_STEPS, User, client, cozydb, fixOnboardedSteps, helpers, localization, passwordHelper, 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');
ArrayHelper = require('../lib/array_helper');
passwordHelper = require('../lib/password_helper');
client = new Client(urlHelper.dataSystem.url());
if ((ref = process.env.NODE_ENV) === 'production' || ref === 'test') {
client.setBasicAuth(process.env.NAME, process.env.TOKEN);
}
ONBOARDING_STEPS = ['welcome', 'agreement', 'password', 'infos', 'accounts', 'confirmation'];
LAST_UNAUTHENTICATED_STEP = 'password';
fixOnboardedSteps = function(user) {
user.onboardedSteps = user.onboardedSteps || [];
if (Array.isArray(user.onboardedSteps[0])) {
user.onboardedSteps = user.onboardedSteps[0];
}
return user;
};
module.exports = User = cozydb.getModel('User', {
email: String,
password: String,
salt: String,
public_name: String,
timezone: String,
owner: Boolean,
allow_stats: Boolean,
isCGUaccepted: Boolean,
activated: Boolean,
encryptedOtpKey: String,
hotpCounter: Number,
authType: String,
encryptedRecoveryCodes: Array,
onboardedSteps: Array
});
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) {
var user;
if (err) {
return callback(err);
} else if (!users || users.length === 0) {
return callback(null, null);
} else {
user = fixOnboardedSteps(users[0]);
return callback(null, user);
}
});
};
User.getUsername = function(callback) {
return User.first(function(err, user) {
if (err) {
return callback(err);
}
if (!(user && user.public_name)) {
return callback();
}
return callback(null, user.public_name);
});
};
User.validate = function(data, errors) {
var ref1;
if (errors == null) {
errors = {};
}
['public_name', 'email', 'timezone'].reduce(function(errors, field) {
if (!(typeof data[field] === 'undefined') && data[field].trim().length === 0) {
errors[field] = "missing " + field;
}
return errors;
}, errors);
if (data.email && !helpers.checkEmail(data.email)) {
errors.email = 'invalid email format';
}
if (data.timezone && !(ref1 = data.timezone, indexOf.call(timezones, ref1) >= 0)) {
errors.timezone = 'invalid timezone';
}
return errors;
};
User.checkInfos = function(data) {
var hasEmail, hasTimezone, hasUserName;
hasEmail = data.email ? helpers.checkEmail(data.email) : false;
hasUserName = data != null ? data.public_name : void 0;
hasTimezone = data.timezone ? !(timezones.indexOf(data.timezone) === -1) : false;
return hasEmail && hasUserName && hasTimezone;
};
User.validatePassword = function(password, errors) {
var passwordStrength;
if (errors == null) {
errors = {};
}
if (!password) {
errors.password = localization.t('password missing');
} else {
passwordStrength = passwordHelper.getStrength(password);
if (passwordStrength.label === 'weak') {
errors.password = localization.t('password too weak');
}
}
return errors;
};
User.isRegistered = function(userData) {
var hasCompletedOnboarding, isLegacyUser;
hasCompletedOnboarding = ArrayHelper.areEquals(userData != null ? userData.onboardedSteps : void 0, ONBOARDING_STEPS);
isLegacyUser = userData && userData.password && (!userData.onboardedSteps || !userData.onboardedSteps.length);
return hasCompletedOnboarding || isLegacyUser;
};
User.isAuthenticatable = function(userData) {
var hasCompletedLastNotAuthenticatedStep, hasPassword;
if ((userData != null ? userData.onboardedSteps : void 0) == null) {
return false;
}
hasCompletedLastNotAuthenticatedStep = indexOf.call(userData.onboardedSteps, LAST_UNAUTHENTICATED_STEP) >= 0;
hasPassword = userData && userData.password && userData.salt;
return hasCompletedLastNotAuthenticatedStep && hasPassword;
};