UNPKG

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.

61 lines (49 loc) 1.59 kB
// Generated by CoffeeScript 1.10.0 var Client, Device, async, cache, client, cozydb, logger, urlHelper; Client = require('request-json').JsonClient; cozydb = require('cozydb'); urlHelper = require('cozy-url-sdk'); async = require('async'); logger = require('printit')({ date: false, prefix: 'models:device' }); module.exports = Device = cozydb.getModel('Device', { login: String, password: String, configuration: Object }); cache = {}; client = new Client(urlHelper.dataSystem.url()); if (process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test") { client.setBasicAuth(process.env.NAME, process.env.TOKEN); } Device.update = function(callback) { return client.post("request/access/all/", {}, function(err, res, accesses) { var access, i, len; cache = {}; if (err != null) { logger.error(err); return typeof callback === "function" ? callback(err) : void 0; } else { if (accesses != null) { for (i = 0, len = accesses.length; i < len; i++) { access = accesses[i]; cache[access.value.login] = access.value.token; } } return typeof callback === "function" ? callback() : void 0; } }); }; Device.isAuthenticated = function(login, password, callback) { var isPresent; isPresent = (cache[login] != null) && cache[login] === password; if (isPresent || process.env.NODE_ENV === "development") { return callback(true); } else { return this.update(function() { return callback((cache[login] != null) && cache[login] === password); }); } };