aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/peertube-auth.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tools/peertube-auth.ts')
-rw-r--r--server/tools/peertube-auth.ts140
1 files changed, 140 insertions, 0 deletions
diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts
new file mode 100644
index 000000000..33438811e
--- /dev/null
+++ b/server/tools/peertube-auth.ts
@@ -0,0 +1,140 @@
1import * as program from 'commander'
2import * as prompt from 'prompt'
3const Table = require('cli-table')
4import { getSettings, writeSettings, netrc } from './cli'
5import { isHostValid } from '../helpers/custom-validators/servers'
6import { isUserUsernameValid } from '../helpers/custom-validators/users'
7
8function delInstance (url: string) {
9 return new Promise((res, rej): void => {
10 getSettings()
11 .then(async (settings) => {
12 settings.remotes.splice(settings.remotes.indexOf(url))
13 await writeSettings(settings)
14 delete netrc.machines[url]
15 netrc.save()
16 res()
17 })
18 .catch(err => rej(err))
19 })
20}
21
22async function setInstance (url: string, username: string, password: string) {
23 return new Promise((res, rej): void => {
24 getSettings()
25 .then(async settings => {
26 if (settings.remotes.indexOf(url) === -1) {
27 settings.remotes.push(url)
28 }
29 await writeSettings(settings)
30 netrc.machines[url] = { login: username, password }
31 netrc.save()
32 res()
33 })
34 .catch(err => rej(err))
35 })
36}
37
38function isURLaPeerTubeInstance (url: string) {
39 return isHostValid(url) || (url.includes('localhost'))
40}
41
42program
43 .name('auth')
44 .usage('[command] [options]')
45
46program
47 .command('add')
48 .description('remember your accounts on remote instances for easier use')
49 .option('-u, --url <url>', 'Server url')
50 .option('-U, --username <username>', 'Username')
51 .option('-p, --password <token>', 'Password')
52 .option('--default', 'add the entry as the new default')
53 .action(options => {
54 prompt.override = options
55 prompt.start()
56 prompt.get({
57 properties: {
58 url: {
59 description: 'instance url',
60 conform: (value) => isURLaPeerTubeInstance(value),
61 required: true
62 },
63 username: {
64 conform: (value) => isUserUsernameValid(value),
65 message: 'Name must be only letters, spaces, or dashes',
66 required: true
67 },
68 password: {
69 hidden: true,
70 replace: '*',
71 required: true
72 }
73 }
74 }, (_, result) => {
75 setInstance(result.url, result.username, result.password)
76 })
77 })
78
79program
80 .command('del <url>')
81 .description('unregisters a remote instance')
82 .action((url) => {
83 delInstance(url)
84 })
85
86program
87 .command('list')
88 .description('lists registered remote instances')
89 .action(() => {
90 getSettings()
91 .then(settings => {
92 const table = new Table({
93 head: ['instance', 'login'],
94 colWidths: [30, 30]
95 })
96 netrc.loadSync()
97 settings.remotes.forEach(element => {
98 table.push([
99 element,
100 netrc.machines[element].login
101 ])
102 })
103
104 console.log(table.toString())
105 })
106 })
107
108program
109 .command('set-default <url>')
110 .description('set an existing entry as default')
111 .action((url) => {
112 getSettings()
113 .then(settings => {
114 const instanceExists = settings.remotes.indexOf(url) !== -1
115
116 if (instanceExists) {
117 settings.default = settings.remotes.indexOf(url)
118 writeSettings(settings)
119 } else {
120 console.log('<url> is not a registered instance.')
121 process.exit(-1)
122 }
123 })
124 })
125
126program.on('--help', function () {
127 console.log(' Examples:')
128 console.log()
129 console.log(' $ peertube add -u peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
130 console.log(' $ peertube add -u peertube.cpy.re -U root')
131 console.log(' $ peertube list')
132 console.log(' $ peertube del peertube.cpy.re')
133 console.log()
134})
135
136if (!process.argv.slice(2).length) {
137 program.outputHelp()
138}
139
140program.parse(process.argv)