]>
Commit | Line | Data |
---|---|---|
a1587156 C |
1 | // eslint-disable @typescript-eslint/no-unnecessary-type-assertion |
2 | ||
2aaa1a3f C |
3 | import { registerTSPaths } from '../helpers/register-ts-paths' |
4 | registerTSPaths() | |
5 | ||
12152aa0 | 6 | import { OptionValues, program } from 'commander' |
8704acf4 | 7 | import * as prompt from 'prompt' |
d0a0fa42 | 8 | import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli' |
8704acf4 | 9 | import { isUserUsernameValid } from '../helpers/custom-validators/users' |
26fcf2ef | 10 | import * as CliTable3 from 'cli-table3' |
2b4dd7e2 | 11 | |
e5cb43e0 | 12 | async function delInstance (url: string) { |
2b4dd7e2 | 13 | const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) |
e5cb43e0 | 14 | |
1a12f66d C |
15 | const index = settings.remotes.indexOf(url) |
16 | settings.remotes.splice(index) | |
17 | ||
18 | if (settings.default === index) settings.default = -1 | |
19 | ||
e5cb43e0 C |
20 | await writeSettings(settings) |
21 | ||
22 | delete netrc.machines[url] | |
2b4dd7e2 | 23 | |
e5cb43e0 | 24 | await netrc.save() |
8704acf4 RK |
25 | } |
26 | ||
1a12f66d | 27 | async function setInstance (url: string, username: string, password: string, isDefault: boolean) { |
2b4dd7e2 C |
28 | const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) |
29 | ||
faa9d434 | 30 | if (settings.remotes.includes(url) === false) { |
e5cb43e0 C |
31 | settings.remotes.push(url) |
32 | } | |
1a12f66d C |
33 | |
34 | if (isDefault || settings.remotes.length === 1) { | |
35 | settings.default = settings.remotes.length - 1 | |
36 | } | |
37 | ||
e5cb43e0 C |
38 | await writeSettings(settings) |
39 | ||
40 | netrc.machines[url] = { login: username, password } | |
41 | await netrc.save() | |
8704acf4 RK |
42 | } |
43 | ||
44 | function isURLaPeerTubeInstance (url: string) { | |
fd0bfc3a | 45 | return url.startsWith('http://') || url.startsWith('https://') |
8704acf4 RK |
46 | } |
47 | ||
24198e97 C |
48 | function stripExtraneousFromPeerTubeUrl (url: string) { |
49 | // Get everything before the 3rd /. | |
50 | const urlLength = url.includes('/', 8) | |
51 | ? url.indexOf('/', 8) | |
52 | : url.length | |
53 | ||
54 | return url.substr(0, urlLength) | |
55 | } | |
56 | ||
8704acf4 RK |
57 | program |
58 | .name('auth') | |
59 | .usage('[command] [options]') | |
60 | ||
61 | program | |
62 | .command('add') | |
63 | .description('remember your accounts on remote instances for easier use') | |
64 | .option('-u, --url <url>', 'Server url') | |
65 | .option('-U, --username <username>', 'Username') | |
66 | .option('-p, --password <token>', 'Password') | |
67 | .option('--default', 'add the entry as the new default') | |
12152aa0 | 68 | .action((options: OptionValues) => { |
ba5a8d89 | 69 | /* eslint-disable no-import-assign */ |
8704acf4 RK |
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 | ||
d0a0fa42 C |
99 | const server = buildServer(result.url) |
100 | await assignToken(server, result.username, result.password) | |
8d2be0ed C |
101 | } catch (err) { |
102 | console.error(err.message) | |
103 | process.exit(-1) | |
104 | } | |
105 | ||
ba5a8d89 | 106 | await setInstance(result.url, result.username, result.password, options.default) |
e5cb43e0 C |
107 | |
108 | process.exit(0) | |
8704acf4 RK |
109 | }) |
110 | }) | |
111 | ||
112 | program | |
113 | .command('del <url>') | |
114 | .description('unregisters a remote instance') | |
e5cb43e0 C |
115 | .action(async url => { |
116 | await delInstance(url) | |
117 | ||
118 | process.exit(0) | |
8704acf4 RK |
119 | }) |
120 | ||
121 | program | |
122 | .command('list') | |
123 | .description('lists registered remote instances') | |
e5cb43e0 | 124 | .action(async () => { |
2b4dd7e2 C |
125 | const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ]) |
126 | ||
26fcf2ef | 127 | const table = new CliTable3({ |
a1587156 C |
128 | head: [ 'instance', 'login' ], |
129 | colWidths: [ 30, 30 ] | |
130 | }) as any | |
2b4dd7e2 | 131 | |
e5cb43e0 | 132 | settings.remotes.forEach(element => { |
9f167f12 C |
133 | if (!netrc.machines[element]) return |
134 | ||
e5cb43e0 C |
135 | table.push([ |
136 | element, | |
137 | netrc.machines[element].login | |
138 | ]) | |
139 | }) | |
140 | ||
141 | console.log(table.toString()) | |
142 | ||
143 | process.exit(0) | |
8704acf4 RK |
144 | }) |
145 | ||
146 | program | |
147 | .command('set-default <url>') | |
148 | .description('set an existing entry as default') | |
e5cb43e0 C |
149 | .action(async url => { |
150 | const settings = await getSettings() | |
bdd428a6 | 151 | const instanceExists = settings.remotes.includes(url) |
e5cb43e0 C |
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 | } | |
8704acf4 RK |
162 | }) |
163 | ||
ba5a8d89 C |
164 | program.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 | ) | |
8704acf4 RK |
170 | |
171 | if (!process.argv.slice(2).length) { | |
172 | program.outputHelp() | |
173 | } | |
174 | ||
175 | program.parse(process.argv) |