]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-auth.ts
Update translations
[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
8704acf4
RK
6import * as program from 'commander'
7import * as prompt from 'prompt'
8d2be0ed 8import { getNetrc, getSettings, writeSettings } from './cli'
8704acf4 9import { isUserUsernameValid } from '../helpers/custom-validators/users'
a1587156 10import { getAccessToken } from '../../shared/extra-utils'
26fcf2ef 11import * as CliTable3 from 'cli-table3'
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')
69 .action(options => {
70 prompt.override = options
71 prompt.start()
72 prompt.get({
73 properties: {
74 url: {
75 description: 'instance url',
76 conform: (value) => isURLaPeerTubeInstance(value),
fd0bfc3a 77 message: 'It should be an URL (https://peertube.example.com)',
8704acf4
RK
78 required: true
79 },
80 username: {
81 conform: (value) => isUserUsernameValid(value),
82 message: 'Name must be only letters, spaces, or dashes',
83 required: true
84 },
85 password: {
86 hidden: true,
87 replace: '*',
88 required: true
89 }
90 }
e5cb43e0 91 }, async (_, result) => {
8e76aa1d 92
8d2be0ed
C
93 // Check credentials
94 try {
8e76aa1d
TS
95 // Strip out everything after the domain:port.
96 // @see https://github.com/Chocobozzz/PeerTube/issues/3520
97 result.url = stripExtraneousFromPeerTubeUrl(result.url)
98
8d2be0ed
C
99 await getAccessToken(result.url, result.username, result.password)
100 } catch (err) {
101 console.error(err.message)
102 process.exit(-1)
103 }
104
1a12f66d 105 await setInstance(result.url, result.username, result.password, program['default'])
e5cb43e0
C
106
107 process.exit(0)
8704acf4
RK
108 })
109 })
110
111program
112 .command('del <url>')
113 .description('unregisters a remote instance')
e5cb43e0
C
114 .action(async url => {
115 await delInstance(url)
116
117 process.exit(0)
8704acf4
RK
118 })
119
120program
121 .command('list')
122 .description('lists registered remote instances')
e5cb43e0 123 .action(async () => {
2b4dd7e2
C
124 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
125
26fcf2ef 126 const table = new CliTable3({
a1587156
C
127 head: [ 'instance', 'login' ],
128 colWidths: [ 30, 30 ]
129 }) as any
2b4dd7e2 130
e5cb43e0 131 settings.remotes.forEach(element => {
9f167f12
C
132 if (!netrc.machines[element]) return
133
e5cb43e0
C
134 table.push([
135 element,
136 netrc.machines[element].login
137 ])
138 })
139
140 console.log(table.toString())
141
142 process.exit(0)
8704acf4
RK
143 })
144
145program
146 .command('set-default <url>')
147 .description('set an existing entry as default')
e5cb43e0
C
148 .action(async url => {
149 const settings = await getSettings()
bdd428a6 150 const instanceExists = settings.remotes.includes(url)
e5cb43e0
C
151
152 if (instanceExists) {
153 settings.default = settings.remotes.indexOf(url)
154 await writeSettings(settings)
155
156 process.exit(0)
157 } else {
158 console.log('<url> is not a registered instance.')
159 process.exit(-1)
160 }
8704acf4
RK
161 })
162
163program.on('--help', function () {
164 console.log(' Examples:')
165 console.log()
55059811
C
166 console.log(' $ peertube auth add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
167 console.log(' $ peertube auth add -u https://peertube.cpy.re -U root')
168 console.log(' $ peertube auth list')
169 console.log(' $ peertube auth del https://peertube.cpy.re')
8704acf4
RK
170 console.log()
171})
172
173if (!process.argv.slice(2).length) {
174 program.outputHelp()
175}
176
177program.parse(process.argv)