]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-auth.ts
1035d664aa0af2fb391fbd4e6aa527d5f0463cd5
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-auth.ts
1 import * as program from 'commander'
2 import * as prompt from 'prompt'
3 import { getSettings, writeSettings, getNetrc } from './cli'
4 import { isHostValid } from '../helpers/custom-validators/servers'
5 import { isUserUsernameValid } from '../helpers/custom-validators/users'
6
7 const Table = require('cli-table')
8
9 async function delInstance (url: string) {
10 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
11
12 const index = settings.remotes.indexOf(url)
13 settings.remotes.splice(index)
14
15 if (settings.default === index) settings.default = -1
16
17 await writeSettings(settings)
18
19 delete netrc.machines[url]
20
21 await netrc.save()
22 }
23
24 async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
25 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
26
27 if (settings.remotes.indexOf(url) === -1) {
28 settings.remotes.push(url)
29 }
30
31 if (isDefault || settings.remotes.length === 1) {
32 settings.default = settings.remotes.length - 1
33 }
34
35 await writeSettings(settings)
36
37 netrc.machines[url] = { login: username, password }
38 await netrc.save()
39 }
40
41 function isURLaPeerTubeInstance (url: string) {
42 return url.startsWith('http://') || url.startsWith('https://')
43 }
44
45 program
46 .name('auth')
47 .usage('[command] [options]')
48
49 program
50 .command('add')
51 .description('remember your accounts on remote instances for easier use')
52 .option('-u, --url <url>', 'Server url')
53 .option('-U, --username <username>', 'Username')
54 .option('-p, --password <token>', 'Password')
55 .option('--default', 'add the entry as the new default')
56 .action(options => {
57 prompt.override = options
58 prompt.start()
59 prompt.get({
60 properties: {
61 url: {
62 description: 'instance url',
63 conform: (value) => isURLaPeerTubeInstance(value),
64 message: 'It should be an URL (https://peertube.example.com)',
65 required: true
66 },
67 username: {
68 conform: (value) => isUserUsernameValid(value),
69 message: 'Name must be only letters, spaces, or dashes',
70 required: true
71 },
72 password: {
73 hidden: true,
74 replace: '*',
75 required: true
76 }
77 }
78 }, async (_, result) => {
79 await setInstance(result.url, result.username, result.password, program['default'])
80
81 process.exit(0)
82 })
83 })
84
85 program
86 .command('del <url>')
87 .description('unregisters a remote instance')
88 .action(async url => {
89 await delInstance(url)
90
91 process.exit(0)
92 })
93
94 program
95 .command('list')
96 .description('lists registered remote instances')
97 .action(async () => {
98 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
99
100 const table = new Table({
101 head: ['instance', 'login'],
102 colWidths: [30, 30]
103 })
104
105 settings.remotes.forEach(element => {
106 if (!netrc.machines[element]) return
107
108 table.push([
109 element,
110 netrc.machines[element].login
111 ])
112 })
113
114 console.log(table.toString())
115
116 process.exit(0)
117 })
118
119 program
120 .command('set-default <url>')
121 .description('set an existing entry as default')
122 .action(async url => {
123 const settings = await getSettings()
124 const instanceExists = settings.remotes.indexOf(url) !== -1
125
126 if (instanceExists) {
127 settings.default = settings.remotes.indexOf(url)
128 await writeSettings(settings)
129
130 process.exit(0)
131 } else {
132 console.log('<url> is not a registered instance.')
133 process.exit(-1)
134 }
135 })
136
137 program.on('--help', function () {
138 console.log(' Examples:')
139 console.log()
140 console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
141 console.log(' $ peertube add -u peertube.cpy.re -U root')
142 console.log(' $ peertube list')
143 console.log(' $ peertube del peertube.cpy.re')
144 console.log()
145 })
146
147 if (!process.argv.slice(2).length) {
148 program.outputHelp()
149 }
150
151 program.parse(process.argv)