|
| 1 | +const fs = require('fs-extra') |
| 2 | +const Handlebars = require('handlebars') |
| 3 | +const path = require('path') |
| 4 | +const { URL } = require('url') |
| 5 | + |
| 6 | +const { loadConfig } = require('./common') |
| 7 | +const { isValidUsername } = require('../../lib/common/user-utils') |
| 8 | +const blacklistService = require('../../lib/services/blacklist-service') |
| 9 | +const { initConfigDir, initTemplateDirs } = require('../../lib/server-config') |
| 10 | +const { fromServerConfig } = require('../../lib/models/oidc-manager') |
| 11 | + |
| 12 | +const AccountManager = require('../../lib/models/account-manager') |
| 13 | +const EmailService = require('../../lib/services/email-service') |
| 14 | +const LDP = require('../../lib/ldp') |
| 15 | +const SolidHost = require('../../lib/models/solid-host') |
| 16 | + |
| 17 | +module.exports = function (program) { |
| 18 | + program |
| 19 | + .command('invalidusernames') |
| 20 | + .option('--notify', 'Will notify users with usernames that are invalid') |
| 21 | + .option('--delete', 'Will delete users with usernames that are invalid') |
| 22 | + .description('Manage usernames that are invalid') |
| 23 | + .action(async (options) => { |
| 24 | + const config = loadConfig(program, options) |
| 25 | + if (!config.multiuser) { |
| 26 | + return console.error('You are running a single user server, no need to check for invalid usernames') |
| 27 | + } |
| 28 | + |
| 29 | + const invalidUsernames = getInvalidUsernames(config) |
| 30 | + const host = SolidHost.from({ port: config.port, serverUri: config.serverUri }) |
| 31 | + const accountManager = getAccountManager(config, host) |
| 32 | + |
| 33 | + if (options.notify) { |
| 34 | + return notifyUsers(invalidUsernames, accountManager, config) |
| 35 | + } |
| 36 | + |
| 37 | + if (options.delete) { |
| 38 | + return deleteUsers(invalidUsernames, accountManager, config, host) |
| 39 | + } |
| 40 | + |
| 41 | + listUsernames(invalidUsernames) |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +function backupIndexFile (username, accountManager, invalidUsernameTemplate, dateOfRemoval, supportEmail) { |
| 46 | + const userDirectory = accountManager.accountDirFor(username) |
| 47 | + const currentIndex = path.join(userDirectory, 'index.html') |
| 48 | + const currentIndexExists = fs.existsSync(currentIndex) |
| 49 | + const backupIndex = path.join(userDirectory, 'index.backup.html') |
| 50 | + const backupIndexExists = fs.existsSync(backupIndex) |
| 51 | + if (currentIndexExists && !backupIndexExists) { |
| 52 | + fs.renameSync(currentIndex, backupIndex) |
| 53 | + createNewIndexAcl(userDirectory) |
| 54 | + createNewIndex(username, invalidUsernameTemplate, dateOfRemoval, supportEmail, currentIndex) |
| 55 | + console.info(`index.html updated for user ${username}`) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function createNewIndex (username, invalidUsernameTemplate, dateOfRemoval, supportEmail, currentIndex) { |
| 60 | + const newIndexSource = invalidUsernameTemplate({ |
| 61 | + username, |
| 62 | + dateOfRemoval, |
| 63 | + supportEmail |
| 64 | + }) |
| 65 | + fs.writeFileSync(currentIndex, newIndexSource, 'utf-8') |
| 66 | +} |
| 67 | + |
| 68 | +function createNewIndexAcl (userDirectory) { |
| 69 | + const currentIndexAcl = path.join(userDirectory, 'index.html.acl') |
| 70 | + const backupIndexAcl = path.join(userDirectory, 'index.backup.html.acl') |
| 71 | + const currentIndexSource = fs.readFileSync(currentIndexAcl, 'utf-8') |
| 72 | + const backupIndexSource = currentIndexSource.replace(/index.html/g, 'index.backup.html') |
| 73 | + fs.writeFileSync(backupIndexAcl, backupIndexSource, 'utf-8') |
| 74 | +} |
| 75 | + |
| 76 | +async function deleteUsers (usernames, accountManager, config, host) { |
| 77 | + const oidcManager = fromServerConfig({ |
| 78 | + ...config, |
| 79 | + host |
| 80 | + }) |
| 81 | + const deletingUsers = usernames |
| 82 | + .map(async username => { |
| 83 | + try { |
| 84 | + const user = accountManager.userAccountFrom({ username }) |
| 85 | + await oidcManager.users.deleteUser(user) |
| 86 | + } catch (error) { |
| 87 | + if (error.message !== 'No email given') { |
| 88 | + // 'No email given' is an expected error that we want to ignore |
| 89 | + throw error |
| 90 | + } |
| 91 | + } |
| 92 | + const userDirectory = accountManager.accountDirFor(username) |
| 93 | + await fs.remove(userDirectory) |
| 94 | + }) |
| 95 | + await Promise.all(deletingUsers) |
| 96 | + console.info(`Deleted ${deletingUsers.length} users succeeded`) |
| 97 | +} |
| 98 | + |
| 99 | +function getAccountManager (config, host) { |
| 100 | + const ldp = new LDP(config) |
| 101 | + return AccountManager.from({ |
| 102 | + host, |
| 103 | + store: ldp, |
| 104 | + multiuser: config.multiuser |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +function getInvalidUsernames (config) { |
| 109 | + const files = fs.readdirSync(config.root) |
| 110 | + const hostname = new URL(config.serverUri).hostname |
| 111 | + const isUserDirectory = new RegExp(`.${hostname}$`) |
| 112 | + return files |
| 113 | + .filter(file => isUserDirectory.test(file)) |
| 114 | + .map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1)) |
| 115 | + .filter(username => !isValidUsername(username) || !blacklistService.validate(username)) |
| 116 | +} |
| 117 | + |
| 118 | +function listUsernames (usernames) { |
| 119 | + if (usernames.length === 0) { |
| 120 | + return console.info('No invalid usernames was found') |
| 121 | + } |
| 122 | + console.info(`${usernames.length} invalid usernames were found:${usernames.map(username => `\n- ${username}`)}`) |
| 123 | +} |
| 124 | + |
| 125 | +async function notifyUsers (usernames, accountManager, config) { |
| 126 | + const twoWeeksFromNow = Date.now() + 14 * 24 * 60 * 60 * 1000 |
| 127 | + const dateOfRemoval = (new Date(twoWeeksFromNow)).toLocaleDateString() |
| 128 | + const { supportEmail } = config |
| 129 | + |
| 130 | + updateIndexFiles(usernames, accountManager, dateOfRemoval, supportEmail) |
| 131 | + await sendEmails(config, usernames, accountManager, dateOfRemoval, supportEmail) |
| 132 | +} |
| 133 | + |
| 134 | +async function sendEmails (config, usernames, accountManager, dateOfRemoval, supportEmail) { |
| 135 | + if (config.email && config.email.host) { |
| 136 | + const configPath = initConfigDir(config) |
| 137 | + const templates = initTemplateDirs(configPath) |
| 138 | + const users = await Promise.all(await usernames.map(async username => { |
| 139 | + const emailAddress = await accountManager.loadAccountRecoveryEmail({ username }) |
| 140 | + const accountUri = accountManager.accountUriFor(username) |
| 141 | + return { username, emailAddress, accountUri } |
| 142 | + })) |
| 143 | + const emailService = new EmailService(templates.email, config.email) |
| 144 | + const sendingEmails = users |
| 145 | + .filter(user => !!user.emailAddress) |
| 146 | + .map(user => emailService.sendWithTemplate('invalid-username', { |
| 147 | + to: user.emailAddress, |
| 148 | + accountUri: user.accountUri, |
| 149 | + dateOfRemoval, |
| 150 | + supportEmail |
| 151 | + })) |
| 152 | + const emailsSent = await Promise.all(sendingEmails) |
| 153 | + console.info(`${emailsSent.length} emails sent to users with invalid usernames`) |
| 154 | + return |
| 155 | + } |
| 156 | + console.info('You have not configured an email service.') |
| 157 | + console.info('Please set it up to send users email about their accounts') |
| 158 | +} |
| 159 | + |
| 160 | +function updateIndexFiles (usernames, accountManager, dateOfRemoval, supportEmail) { |
| 161 | + const invalidUsernameFilePath = path.join(process.cwd(), 'default-views', 'account', 'invalid-username.hbs') |
| 162 | + const source = fs.readFileSync(invalidUsernameFilePath, 'utf-8') |
| 163 | + const invalidUsernameTemplate = Handlebars.compile(source) |
| 164 | + usernames.forEach(username => backupIndexFile(username, accountManager, invalidUsernameTemplate, dateOfRemoval, supportEmail)) |
| 165 | +} |
0 commit comments