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