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