]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-auth.ts
Proxy youtube-dl format command too
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-auth.ts
1 import { registerTSPaths } from '../helpers/register-ts-paths'
2 registerTSPaths()
3
4 import * as program from 'commander'
5 import * as prompt from 'prompt'
6 import { getNetrc, getSettings, writeSettings } from './cli'
7 import { isUserUsernameValid } from '../helpers/custom-validators/users'
8 import { getAccessToken, login } from '../../shared/extra-utils'
9
10 const Table = require('cli-table')
11
12 async function delInstance (url: string) {
13 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
14
15 const index = settings.remotes.indexOf(url)
16 settings.remotes.splice(index)
17
18 if (settings.default === index) settings.default = -1
19
20 await writeSettings(settings)
21
22 delete netrc.machines[url]
23
24 await netrc.save()
25 }
26
27 async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
28 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
29
30 if (settings.remotes.indexOf(url) === -1) {
31 settings.remotes.push(url)
32 }
33
34 if (isDefault || settings.remotes.length === 1) {
35 settings.default = settings.remotes.length - 1
36 }
37
38 await writeSettings(settings)
39
40 netrc.machines[url] = { login: username, password }
41 await netrc.save()
42 }
43
44 function isURLaPeerTubeInstance (url: string) {
45 return url.startsWith('http://') || url.startsWith('https://')
46 }
47
48 program
49 .name('auth')
50 .usage('[command] [options]')
51
52 program
53 .command('add')
54 .description('remember your accounts on remote instances for easier use')
55 .option('-u, --url <url>', 'Server url')
56 .option('-U, --username <username>', 'Username')
57 .option('-p, --password <token>', 'Password')
58 .option('--default', 'add the entry as the new default')
59 .action(options => {
60 prompt.override = options
61 prompt.start()
62 prompt.get({
63 properties: {
64 url: {
65 description: 'instance url',
66 conform: (value) => isURLaPeerTubeInstance(value),
67 message: 'It should be an URL (https://peertube.example.com)',
68 required: true
69 },
70 username: {
71 conform: (value) => isUserUsernameValid(value),
72 message: 'Name must be only letters, spaces, or dashes',
73 required: true
74 },
75 password: {
76 hidden: true,
77 replace: '*',
78 required: true
79 }
80 }
81 }, async (_, result) => {
82 // Check credentials
83 try {
84 await getAccessToken(result.url, result.username, result.password)
85 } catch (err) {
86 console.error(err.message)
87 process.exit(-1)
88 }
89
90 await setInstance(result.url, result.username, result.password, program['default'])
91
92 process.exit(0)
93 })
94 })
95
96 program
97 .command('del <url>')
98 .description('unregisters a remote instance')
99 .action(async url => {
100 await delInstance(url)
101
102 process.exit(0)
103 })
104
105 program
106 .command('list')
107 .description('lists registered remote instances')
108 .action(async () => {
109 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
110
111 const table = new Table({
112 head: ['instance', 'login'],
113 colWidths: [30, 30]
114 })
115
116 settings.remotes.forEach(element => {
117 if (!netrc.machines[element]) return
118
119 table.push([
120 element,
121 netrc.machines[element].login
122 ])
123 })
124
125 console.log(table.toString())
126
127 process.exit(0)
128 })
129
130 program
131 .command('set-default <url>')
132 .description('set an existing entry as default')
133 .action(async url => {
134 const settings = await getSettings()
135 const instanceExists = settings.remotes.indexOf(url) !== -1
136
137 if (instanceExists) {
138 settings.default = settings.remotes.indexOf(url)
139 await writeSettings(settings)
140
141 process.exit(0)
142 } else {
143 console.log('<url> is not a registered instance.')
144 process.exit(-1)
145 }
146 })
147
148 program.on('--help', function () {
149 console.log(' Examples:')
150 console.log()
151 console.log(' $ peertube add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
152 console.log(' $ peertube add -u https://peertube.cpy.re -U root')
153 console.log(' $ peertube list')
154 console.log(' $ peertube del https://peertube.cpy.re')
155 console.log()
156 })
157
158 if (!process.argv.slice(2).length) {
159 program.outputHelp()
160 }
161
162 program.parse(process.argv)