X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fpeertube-auth.ts;h=6597a5c367e78d3d0c9a92f920c70ae990584db2;hb=51b34a11b298b466faef9c8d24beec4442d7add3;hp=a962944a40735a37a593ae6a2b31739eb961b5c4;hpb=88108880bbdba473cfe36ecbebc1c3c4f972e102;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts index a962944a4..6597a5c36 100644 --- a/server/tools/peertube-auth.ts +++ b/server/tools/peertube-auth.ts @@ -1,25 +1,40 @@ +import { registerTSPaths } from '../helpers/register-ts-paths' +registerTSPaths() + import * as program from 'commander' import * as prompt from 'prompt' -const Table = require('cli-table') -import { getSettings, writeSettings, netrc } from './cli' -import { isHostValid } from '../helpers/custom-validators/servers' +import { getNetrc, getSettings, writeSettings } from './cli' import { isUserUsernameValid } from '../helpers/custom-validators/users' +import { getAccessToken, login } from '../../shared/extra-utils' + +const Table = require('cli-table') async function delInstance (url: string) { - const settings = await getSettings() + const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) + + const index = settings.remotes.indexOf(url) + settings.remotes.splice(index) + + if (settings.default === index) settings.default = -1 - settings.remotes.splice(settings.remotes.indexOf(url)) await writeSettings(settings) delete netrc.machines[url] + await netrc.save() } -async function setInstance (url: string, username: string, password: string) { - const settings = await getSettings() +async function setInstance (url: string, username: string, password: string, isDefault: boolean) { + const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) + if (settings.remotes.indexOf(url) === -1) { settings.remotes.push(url) } + + if (isDefault || settings.remotes.length === 1) { + settings.default = settings.remotes.length - 1 + } + await writeSettings(settings) netrc.machines[url] = { login: username, password } @@ -27,7 +42,7 @@ async function setInstance (url: string, username: string, password: string) { } function isURLaPeerTubeInstance (url: string) { - return isHostValid(url) || (url.includes('localhost')) + return url.startsWith('http://') || url.startsWith('https://') } program @@ -49,6 +64,7 @@ program url: { description: 'instance url', conform: (value) => isURLaPeerTubeInstance(value), + message: 'It should be an URL (https://peertube.example.com)', required: true }, username: { @@ -63,7 +79,15 @@ program } } }, async (_, result) => { - await setInstance(result.url, result.username, result.password) + // Check credentials + try { + await getAccessToken(result.url, result.username, result.password) + } catch (err) { + console.error(err.message) + process.exit(-1) + } + + await setInstance(result.url, result.username, result.password, program['default']) process.exit(0) }) @@ -82,13 +106,16 @@ program .command('list') .description('lists registered remote instances') .action(async () => { - const settings = await getSettings() + const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) + const table = new Table({ head: ['instance', 'login'], colWidths: [30, 30] }) - netrc.loadSync() + settings.remotes.forEach(element => { + if (!netrc.machines[element]) return + table.push([ element, netrc.machines[element].login @@ -121,10 +148,10 @@ program program.on('--help', function () { console.log(' Examples:') console.log() - console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"') - console.log(' $ peertube add -u peertube.cpy.re -U root') + console.log(' $ peertube add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"') + console.log(' $ peertube add -u https://peertube.cpy.re -U root') console.log(' $ peertube list') - console.log(' $ peertube del peertube.cpy.re') + console.log(' $ peertube del https://peertube.cpy.re') console.log() })