aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/peertube-auth.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/tools/peertube-auth.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/tools/peertube-auth.ts')
-rw-r--r--server/tools/peertube-auth.ts171
1 files changed, 0 insertions, 171 deletions
diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts
deleted file mode 100644
index c853469c2..000000000
--- a/server/tools/peertube-auth.ts
+++ /dev/null
@@ -1,171 +0,0 @@
1import CliTable3 from 'cli-table3'
2import { OptionValues, program } from 'commander'
3import { isUserUsernameValid } from '../helpers/custom-validators/users'
4import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './shared'
5
6import prompt = require('prompt')
7
8async function delInstance (url: string) {
9 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
10
11 const index = settings.remotes.indexOf(url)
12 settings.remotes.splice(index)
13
14 if (settings.default === index) settings.default = -1
15
16 await writeSettings(settings)
17
18 delete netrc.machines[url]
19
20 await netrc.save()
21}
22
23async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
24 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
25
26 if (settings.remotes.includes(url) === false) {
27 settings.remotes.push(url)
28 }
29
30 if (isDefault || settings.remotes.length === 1) {
31 settings.default = settings.remotes.length - 1
32 }
33
34 await writeSettings(settings)
35
36 netrc.machines[url] = { login: username, password }
37 await netrc.save()
38}
39
40function isURLaPeerTubeInstance (url: string) {
41 return url.startsWith('http://') || url.startsWith('https://')
42}
43
44function stripExtraneousFromPeerTubeUrl (url: string) {
45 // Get everything before the 3rd /.
46 const urlLength = url.includes('/', 8)
47 ? url.indexOf('/', 8)
48 : url.length
49
50 return url.substring(0, urlLength)
51}
52
53program
54 .name('auth')
55 .usage('[command] [options]')
56
57program
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')
64 .action((options: OptionValues) => {
65 /* eslint-disable no-import-assign */
66 prompt.override = options
67 prompt.start()
68 prompt.get({
69 properties: {
70 url: {
71 description: 'instance url',
72 conform: (value) => isURLaPeerTubeInstance(value),
73 message: 'It should be an URL (https://peertube.example.com)',
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 }
87 }, async (_, result) => {
88
89 // Check credentials
90 try {
91 // Strip out everything after the domain:port.
92 // See https://github.com/Chocobozzz/PeerTube/issues/3520
93 result.url = stripExtraneousFromPeerTubeUrl(result.url)
94
95 const server = buildServer(result.url)
96 await assignToken(server, result.username, result.password)
97 } catch (err) {
98 console.error(err.message)
99 process.exit(-1)
100 }
101
102 await setInstance(result.url, result.username, result.password, options.default)
103
104 process.exit(0)
105 })
106 })
107
108program
109 .command('del <url>')
110 .description('unregisters a remote instance')
111 .action(async url => {
112 await delInstance(url)
113
114 process.exit(0)
115 })
116
117program
118 .command('list')
119 .description('lists registered remote instances')
120 .action(async () => {
121 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
122
123 const table = new CliTable3({
124 head: [ 'instance', 'login' ],
125 colWidths: [ 30, 30 ]
126 }) as any
127
128 settings.remotes.forEach(element => {
129 if (!netrc.machines[element]) return
130
131 table.push([
132 element,
133 netrc.machines[element].login
134 ])
135 })
136
137 console.log(table.toString())
138
139 process.exit(0)
140 })
141
142program
143 .command('set-default <url>')
144 .description('set an existing entry as default')
145 .action(async url => {
146 const settings = await getSettings()
147 const instanceExists = settings.remotes.includes(url)
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 }
158 })
159
160program.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)
166
167if (!process.argv.slice(2).length) {
168 program.outputHelp()
169}
170
171program.parse(process.argv)