<commit_before>/* jslint node:true */

'use strict';

exports = module.exports = {
    auth: auth,
    login: login,
    logout: logout,
    profile: profile,
    getAll: getAll,
    get: get,
    add: add,
    put: put,
    del: del,
    getPublic: getPublic,
    makePublic: makePublic,
    getTags: getTags,
    settingsSave: settingsSave,
    settingsGet: settingsGet,
    exportThings: exportThings,
    importThings: importThings,
    healthcheck: healthcheck,
    fileAdd: fileAdd,
    fileGet: fileGet
};

var assert = require('assert'),
    checksum = require('checksum'),
    config = require('./config.js'),
    fs = require('fs'),
    logic = require('./logic.js'),
    mkdirp = require('mkdirp'),
    path = require('path'),
    settings = require('./database/settings.js'),
    superagent = require('superagent'),
    tags = require('./database/tags.js'),
    tar = require('tar-fs'),
    tokens = require('./database/tokens.js'),
    uuid = require('uuid'),
    HttpError = require('connect-lastmile').HttpError,
    HttpSuccess = require('connect-lastmile').HttpSuccess;

function healthcheck(req, res, next) {
    next(new HttpSuccess(200, {}));
}

function auth(req, res, next) {
    if (!req.query.token) return next(new HttpError(401, 'missing token'));

    tokens.get(req.query.token, function (error, result) {
        if (error) return next(new HttpError(401, 'invalid credentials'));

        req.token = req.query.token;
        req.cloudronToken = result.cloudronToken;
        req.userId = result.userId;

        next();
    });
}

// These are set on a Cloudron only
var simpleAuth = process.env.SIMPLE_AUTH_URL && process.env.SIMPLE_AUTH_CLIENT_ID && process.env.API_ORIGIN;

function wrapRestError(error) {
    return new Error('Failed with status: ' + error.status + ' text: ' + (error.response && error.response.res.text));
}

var g_testUsers = {};

function welcomeIfNeeded(userId, callback) {
    assert.strictEqual(typeof userId, 'string');
    assert.strictEqual(typeof callback, 'function');

    logic.getAllLean(userId, function (error, result) {
        if (error) return callback(error);
        if (result.length > 0) return callback(null);

        logic.imp(userId, require('../things.json'), callback);
    });
}

function verifyUser(username, password, callback) {
    if (!simpleAuth) {
        if (password !== '<PASSWORD>') return callback(null, null);

        var userId = username + 'Id';

        g_testUsers[userId] = {
            accessToken: '',
            user: {
                id: userId,
                username: username,
                displayName: username.toUpperCase(),
                email: username + '@cloudron.io'
            }
        };

        console.log(g_testUsers[userId], userId)

        return callback(null, g_testUsers[userId]);
    }

    var authPayload = {
        clientId: process.env.SIMPLE_AUTH_CLIENT_ID,
        username: username,
        password: password
    };

    superagent.post(process.env.SIMPLE_AUTH_URL + '/api/v1/login').send(authPayload).end(function (error, result) {
        if (error && error.status === 401) return callback(null, null);
        if (error) return callback(wrapRestError(error));
        if (result.status !== 200) return callback(null, null);

        callback(null, result.body);
    });
}

function login(req, res, next) {
    if (typeof req.body.username !== 'string' || !req.body.username) return next(new HttpError(400, 'missing username'));
    if (typeof req.body.password !== 'string' || !req.body.password) return next(new HttpError(400, 'missing password'));

    verifyUser(req.body.username, req.body.password, function (error, result) {
        if (error) return next(new HttpError(500, error));
        if (!result) return next(new HttpError(401, 'invalid credentials'));
<commit_msg>Remove a console.log()<commit_after>/* jslint node:true */

'use strict';

exports = module.exports = {
    auth: auth,
    login: login,
    logout: logout,
    profile: profile,
    getAll: getAll,
    get: get,
    add: add,
    put: put,
    del: del,
    getPublic: getPublic,
    makePublic: makePublic,
    getTags: getTags,
    settingsSave: settingsSave,
    settingsGet: settingsGet,
    exportThings: exportThings,
    importThings: importThings,
    healthcheck: healthcheck,
    fileAdd: fileAdd,
    fileGet: fileGet
};

var assert = require('assert'),
    checksum = require('checksum'),
    config = require('./config.js'),
    fs = require('fs'),
    logic = require('./logic.js'),
    mkdirp = require('mkdirp'),
    path = require('path'),
    settings = require('./database/settings.js'),
    superagent = require('superagent'),
    tags = require('./database/tags.js'),
    tar = require('tar-fs'),
    tokens = require('./database/tokens.js'),
    uuid = require('uuid'),
    HttpError = require('connect-lastmile').HttpError,
    HttpSuccess = require('connect-lastmile').HttpSuccess;

function healthcheck(req, res, next) {
    next(new HttpSuccess(200, {}));
}

function auth(req, res, next) {
    if (!req.query.token) return next(new HttpError(401, 'missing token'));

    tokens.get(req.query.token, function (error, result) {
        if (error) return next(new HttpError(401, 'invalid credentials'));

        req.token = req.query.token;
        req.cloudronToken = result.cloudronToken;
        req.userId = result.userId;

        next();
    });
}

// These are set on a Cloudron only
var simpleAuth = process.env.SIMPLE_AUTH_URL && process.env.SIMPLE_AUTH_CLIENT_ID && process.env.API_ORIGIN;

function wrapRestError(error) {
    return new Error('Failed with status: ' + error.status + ' text: ' + (error.response && error.response.res.text));
}

var g_testUsers = {};

function welcomeIfNeeded(userId, callback) {
    assert.strictEqual(typeof userId, 'string');
    assert.strictEqual(typeof callback, 'function');

    logic.getAllLean(userId, function (error, result) {
        if (error) return callback(error);
        if (result.length > 0) return callback(null);

        logic.imp(userId, require('../things.json'), callback);
    });
}

function verifyUser(username, password, callback) {
    if (!simpleAuth) {
        if (password !== '<PASSWORD>') return callback(null, null);

        var userId = username + 'Id';

        g_testUsers[userId] = {
            accessToken: '',
            user: {
                id: userId,
                username: username,
                displayName: username.toUpperCase(),
                email: username + '@cloudron.io'
            }
        };

        return callback(null, g_testUsers[userId]);
    }

    var authPayload = {
        clientId: process.env.SIMPLE_AUTH_CLIENT_ID,
        username: username,
        password: password
    };

    superagent.post(process.env.SIMPLE_AUTH_URL + '/api/v1/login').send(authPayload).end(function (error, result) {
        if (error && error.status === 401) return callback(null, null);
        if (error) return callback(wrapRestError(error));
        if (result.status !== 200) return callback(null, null);

        callback(null, result.body);
    });
}

function login(req, res, next) {
    if (typeof req.body.username !== 'string' || !req.body.username) return next(new HttpError(400, 'missing username'));
    if (typeof req.body.password !== 'string' || !req.body.password) return next(new HttpError(400, 'missing password'));

    verifyUser(req.body.username, req.body.password, function (error, result) {
        if (error) return next(new HttpError(500, error));
        if (!result) return next(new HttpError(401, 'invalid credentials'));

{'fraction_non_alphanumeric': 0.14446922975987403, 'fraction_numerical': 0.0076105497966146175, 'mean_word_length': 3.045647558386412, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1622139', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2248, 'n_words': 534}