]>
Commit | Line | Data |
---|---|---|
f8360396 | 1 | import CliTable3 from 'cli-table3' |
12152aa0 | 2 | import { OptionValues, program } from 'commander' |
8704acf4 | 3 | import { isUserUsernameValid } from '../helpers/custom-validators/users' |
f8360396 | 4 | import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli' |
41fb13c3 C |
5 | |
6 | import prompt = require('prompt') | |
2b4dd7e2 | 7 | |
e5cb43e0 | 8 | async function delInstance (url: string) { |
2b4dd7e2 | 9 | const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) |
e5cb43e0 | 10 | |
1a12f66d C |
11 | const index = settings.remotes.indexOf(url) |
12 | settings.remotes.splice(index) | |
13 | ||
14 | if (settings.default === index) settings.default = -1 | |
15 | ||
e5cb43e0 C |
16 | await writeSettings(settings) |
17 | ||
18 | delete netrc.machines[url] | |
2b4dd7e2 | 19 | |
e5cb43e0 | 20 | await netrc.save() |
8704acf4 RK |
21 | } |
22 | ||
1a12f66d | 23 | async function setInstance (url: string, username: string, password: string, isDefault: boolean) { |
2b4dd7e2 C |
24 | const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) |
25 | ||
faa9d434 | 26 | if (settings.remotes.includes(url) === false) { |
e5cb43e0 C |
27 | settings.remotes.push(url) |
28 | } | |
1a12f66d C |
29 | |
30 | if (isDefault || settings.remotes.length === 1) { | |
31 | settings.default = settings.remotes.length - 1 | |
32 | } | |
33 | ||
e5cb43e0 C |
34 | await writeSettings(settings) |
35 | ||
36 | netrc.machines[url] = { login: username, password } | |
37 | await netrc.save() | |
8704acf4 RK |
38 | } |
39 | ||
40 | function isURLaPeerTubeInstance (url: string) { | |
fd0bfc3a | 41 | return url.startsWith('http://') || url.startsWith('https://') |
8704acf4 RK |
42 | } |
43 | ||
24198e97 C |
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 | ||
4abc7b05 | 50 | return url.substring(0, urlLength) |
24198e97 C |
51 | } |
52 | ||
8704acf4 RK |
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') | |
12152aa0 | 64 | .action((options: OptionValues) => { |
ba5a8d89 | 65 | /* eslint-disable no-import-assign */ |
8704acf4 RK |
66 | prompt.override = options |
67 | prompt.start() | |
68 | prompt.get({ | |
69 | properties: { | |
70 | url: { | |
71 | description: 'instance url', | |
72 | conform: (value) => isURLaPeerTubeInstance(value), | |
fd0bfc3a | 73 | message: 'It should be an URL (https://peertube.example.com)', |
8704acf4 RK |
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 | } | |
e5cb43e0 | 87 | }, async (_, result) => { |
8e76aa1d | 88 | |
8d2be0ed C |
89 | // Check credentials |
90 | try { | |
8e76aa1d | 91 | // Strip out everything after the domain:port. |
4abc7b05 | 92 | // See https://github.com/Chocobozzz/PeerTube/issues/3520 |
8e76aa1d TS |
93 | result.url = stripExtraneousFromPeerTubeUrl(result.url) |
94 | ||
d0a0fa42 C |
95 | const server = buildServer(result.url) |
96 | await assignToken(server, result.username, result.password) | |
8d2be0ed C |
97 | } catch (err) { |
98 | console.error(err.message) | |
99 | process.exit(-1) | |
100 | } | |
101 | ||
ba5a8d89 | 102 | await setInstance(result.url, result.username, result.password, options.default) |
e5cb43e0 C |
103 | |
104 | process.exit(0) | |
8704acf4 RK |
105 | }) |
106 | }) | |
107 | ||
108 | program | |
109 | .command('del <url>') | |
110 | .description('unregisters a remote instance') | |
e5cb43e0 C |
111 | .action(async url => { |
112 | await delInstance(url) | |
113 | ||
114 | process.exit(0) | |
8704acf4 RK |
115 | }) |
116 | ||
117 | program | |
118 | .command('list') | |
119 | .description('lists registered remote instances') | |
e5cb43e0 | 120 | .action(async () => { |
2b4dd7e2 C |
121 | const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) |
122 | ||
26fcf2ef | 123 | const table = new CliTable3({ |
a1587156 C |
124 | head: [ 'instance', 'login' ], |
125 | colWidths: [ 30, 30 ] | |
126 | }) as any | |
2b4dd7e2 | 127 | |
e5cb43e0 | 128 | settings.remotes.forEach(element => { |
9f167f12 C |
129 | if (!netrc.machines[element]) return |
130 | ||
e5cb43e0 C |
131 | table.push([ |
132 | element, | |
133 | netrc.machines[element].login | |
134 | ]) | |
135 | }) | |
136 | ||
137 | console.log(table.toString()) | |
138 | ||
139 | process.exit(0) | |
8704acf4 RK |
140 | }) |
141 | ||
142 | program | |
143 | .command('set-default <url>') | |
144 | .description('set an existing entry as default') | |
e5cb43e0 C |
145 | .action(async url => { |
146 | const settings = await getSettings() | |
bdd428a6 | 147 | const instanceExists = settings.remotes.includes(url) |
e5cb43e0 C |
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 | } | |
8704acf4 RK |
158 | }) |
159 | ||
ba5a8d89 C |
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 | ) | |
8704acf4 RK |
166 | |
167 | if (!process.argv.slice(2).length) { | |
168 | program.outputHelp() | |
169 | } | |
170 | ||
171 | program.parse(process.argv) |