]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-auth.ts
7673b92cd2db6adfb3b0fe8ec765ed3f3eb72325
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-auth.ts
1 // eslint-disable @typescript-eslint/no-unnecessary-type-assertion
2
3 import { registerTSPaths } from '../helpers/register-ts-paths'
4 registerTSPaths()
5
6 import * as program from 'commander'
7 import * as prompt from 'prompt'
8 import { getNetrc, getSettings, writeSettings } from './cli'
9 import { isUserUsernameValid } from '../helpers/custom-validators/users'
10 import { getAccessToken } from '../../shared/extra-utils'
11 import * as CliTable3 from 'cli-table3'
12
13 async function delInstance (url: string) {
14 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
15
16 const index = settings.remotes.indexOf(url)
17 settings.remotes.splice(index)
18
19 if (settings.default === index) settings.default = -1
20
21 await writeSettings(settings)
22
23 delete netrc.machines[url]
24
25 await netrc.save()
26 }
27
28 async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
29 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
30
31 if (settings.remotes.includes(url) === false) {
32 settings.remotes.push(url)
33 }
34
35 if (isDefault || settings.remotes.length === 1) {
36 settings.default = settings.remotes.length - 1
37 }
38
39 await writeSettings(settings)
40
41 netrc.machines[url] = { login: username, password }
42 await netrc.save()
43 }
44
45 function isURLaPeerTubeInstance (url: string) {
46 return url.startsWith('http://') || url.startsWith('https://')
47 }
48
49 program
50 .name('auth')
51 .usage('[command] [options]')
52
53 program
54 .command('add')
55 .description('remember your accounts on remote instances for easier use')
56 .option('-u, --url <url>', 'Server url')
57 .option('-U, --username <username>', 'Username')
58 .option('-p, --password <token>', 'Password')
59 .option('--default', 'add the entry as the new default')
60 .action(options => {
61 prompt.override = options
62 prompt.start()
63 prompt.get({
64 properties: {
65 url: {
66 description: 'instance url',
67 conform: (value) => isURLaPeerTubeInstance(value),
68 message: 'It should be an URL (https://peertube.example.com)',
69 required: true
70 },
71 username: {
72 conform: (value) => isUserUsernameValid(value),
73 message: 'Name must be only letters, spaces, or dashes',
74 required: true
75 },
76 password: {
77 hidden: true,
78 replace: '*',
79 required: true
80 }
81 }
82 }, async (_, result) => {
83 const stripExtraneousFromPeerTubeUrl = function (url: string) {
84 // Get everything before the 3rd /.
85 const urlLength: number = url.includes('/', 8) ? url.indexOf('/', 8) : url.length
86
87 return url.substr(0, urlLength)
88 }
89
90 // Check credentials
91 try {
92 // Strip out everything after the domain:port.
93 // @see https://github.com/Chocobozzz/PeerTube/issues/3520
94 result.url = stripExtraneousFromPeerTubeUrl(result.url)
95
96 await getAccessToken(result.url, 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, program['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.on('--help', function () {
161 console.log(' Examples:')
162 console.log()
163 console.log(' $ peertube auth add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
164 console.log(' $ peertube auth add -u https://peertube.cpy.re -U root')
165 console.log(' $ peertube auth list')
166 console.log(' $ peertube auth del https://peertube.cpy.re')
167 console.log()
168 })
169
170 if (!process.argv.slice(2).length) {
171 program.outputHelp()
172 }
173
174 program.parse(process.argv)