]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/tools/peertube-auth.ts
Update server dependencies
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-auth.ts
... / ...
CommitLineData
1// eslint-disable @typescript-eslint/no-unnecessary-type-assertion
2
3import { registerTSPaths } from '../helpers/register-ts-paths'
4registerTSPaths()
5
6import * as program from 'commander'
7import * as prompt from 'prompt'
8import { getNetrc, getSettings, writeSettings } from './cli'
9import { isUserUsernameValid } from '../helpers/custom-validators/users'
10import { getAccessToken } from '../../shared/extra-utils'
11import * as CliTable3 from 'cli-table3'
12
13async 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
28async 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
45function isURLaPeerTubeInstance (url: string) {
46 return url.startsWith('http://') || url.startsWith('https://')
47}
48
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
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: program.OptionValues) => {
70 /* eslint-disable no-import-assign */
71 prompt.override = options
72 prompt.start()
73 prompt.get({
74 properties: {
75 url: {
76 description: 'instance url',
77 conform: (value) => isURLaPeerTubeInstance(value),
78 message: 'It should be an URL (https://peertube.example.com)',
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 }
92 }, async (_, result) => {
93
94 // Check credentials
95 try {
96 // Strip out everything after the domain:port.
97 // @see https://github.com/Chocobozzz/PeerTube/issues/3520
98 result.url = stripExtraneousFromPeerTubeUrl(result.url)
99
100 await getAccessToken(result.url, result.username, result.password)
101 } catch (err) {
102 console.error(err.message)
103 process.exit(-1)
104 }
105
106 await setInstance(result.url, result.username, result.password, options.default)
107
108 process.exit(0)
109 })
110 })
111
112program
113 .command('del <url>')
114 .description('unregisters a remote instance')
115 .action(async url => {
116 await delInstance(url)
117
118 process.exit(0)
119 })
120
121program
122 .command('list')
123 .description('lists registered remote instances')
124 .action(async () => {
125 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
126
127 const table = new CliTable3({
128 head: [ 'instance', 'login' ],
129 colWidths: [ 30, 30 ]
130 }) as any
131
132 settings.remotes.forEach(element => {
133 if (!netrc.machines[element]) return
134
135 table.push([
136 element,
137 netrc.machines[element].login
138 ])
139 })
140
141 console.log(table.toString())
142
143 process.exit(0)
144 })
145
146program
147 .command('set-default <url>')
148 .description('set an existing entry as default')
149 .action(async url => {
150 const settings = await getSettings()
151 const instanceExists = settings.remotes.includes(url)
152
153 if (instanceExists) {
154 settings.default = settings.remotes.indexOf(url)
155 await writeSettings(settings)
156
157 process.exit(0)
158 } else {
159 console.log('<url> is not a registered instance.')
160 process.exit(-1)
161 }
162 })
163
164program.addHelpText('after', '\n\n Examples:\n\n' +
165 ' $ peertube auth add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"\n' +
166 ' $ peertube auth add -u https://peertube.cpy.re -U root\n' +
167 ' $ peertube auth list\n' +
168 ' $ peertube auth del https://peertube.cpy.re\n'
169)
170
171if (!process.argv.slice(2).length) {
172 program.outputHelp()
173}
174
175program.parse(process.argv)