diff options
-rw-r--r-- | server/tools/cli.ts | 31 | ||||
-rw-r--r-- | server/tools/peertube-auth.ts | 6 | ||||
-rw-r--r-- | server/tools/peertube-get-access-token.ts | 10 | ||||
-rw-r--r-- | server/tools/peertube-import-videos.ts | 12 | ||||
-rw-r--r-- | server/tools/peertube-plugins.ts | 18 | ||||
-rw-r--r-- | server/tools/peertube-redundancy.ts | 14 | ||||
-rw-r--r-- | server/tools/peertube-upload.ts | 9 | ||||
-rw-r--r-- | shared/extra-utils/users/login-command.ts | 7 |
8 files changed, 55 insertions, 52 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 0528859a4..3e0e03b97 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts | |||
@@ -3,12 +3,10 @@ import { Netrc } from 'netrc-parser' | |||
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { createLogger, format, transports } from 'winston' | 4 | import { createLogger, format, transports } from 'winston' |
5 | import { assignCommands, ServerInfo } from '@shared/extra-utils' | 5 | import { assignCommands, ServerInfo } from '@shared/extra-utils' |
6 | import { getAccessToken } from '@shared/extra-utils/users/login' | ||
7 | import { getMyUserInformation } from '@shared/extra-utils/users/users' | 6 | import { getMyUserInformation } from '@shared/extra-utils/users/users' |
8 | import { User, UserRole } from '@shared/models' | 7 | import { User, UserRole } from '@shared/models' |
9 | import { root } from '../../shared/extra-utils/miscs/miscs' | ||
10 | import { VideoPrivacy } from '../../shared/models/videos' | 8 | import { VideoPrivacy } from '../../shared/models/videos' |
11 | import { getAppNumber, isTestInstance } from '../helpers/core-utils' | 9 | import { getAppNumber, isTestInstance, root } from '../helpers/core-utils' |
12 | 10 | ||
13 | let configName = 'PeerTube/CLI' | 11 | let configName = 'PeerTube/CLI' |
14 | if (isTestInstance()) configName += `-${getAppNumber()}` | 12 | if (isTestInstance()) configName += `-${getAppNumber()}` |
@@ -17,9 +15,9 @@ const config = require('application-config')(configName) | |||
17 | 15 | ||
18 | const version = require('../../../package.json').version | 16 | const version = require('../../../package.json').version |
19 | 17 | ||
20 | async function getAdminTokenOrDie (url: string, username: string, password: string) { | 18 | async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { |
21 | const accessToken = await getAccessToken(url, username, password) | 19 | const accessToken = await server.loginCommand.getAccessToken(username, password) |
22 | const resMe = await getMyUserInformation(url, accessToken) | 20 | const resMe = await getMyUserInformation(server.url, accessToken) |
23 | const me: User = resMe.body | 21 | const me: User = resMe.body |
24 | 22 | ||
25 | if (me.role !== UserRole.ADMINISTRATOR) { | 23 | if (me.role !== UserRole.ADMINISTRATOR) { |
@@ -30,10 +28,6 @@ async function getAdminTokenOrDie (url: string, username: string, password: stri | |||
30 | return accessToken | 28 | return accessToken |
31 | } | 29 | } |
32 | 30 | ||
33 | async function getAccessTokenOrDie (url: string, username: string, password: string) { | ||
34 | return getAccessToken(url, username, password) | ||
35 | } | ||
36 | |||
37 | interface Settings { | 31 | interface Settings { |
38 | remotes: any[] | 32 | remotes: any[] |
39 | default: number | 33 | default: number |
@@ -187,13 +181,22 @@ function getServerCredentials (program: Command) { | |||
187 | }) | 181 | }) |
188 | } | 182 | } |
189 | 183 | ||
190 | function buildServer (url: string, accessToken?: string): ServerInfo { | 184 | function buildServer (url: string): ServerInfo { |
191 | const server = { url, accessToken, internalServerNumber: undefined } | 185 | const server = { url, internalServerNumber: undefined } |
192 | assignCommands(server) | 186 | assignCommands(server) |
193 | 187 | ||
194 | return server | 188 | return server |
195 | } | 189 | } |
196 | 190 | ||
191 | async function assignToken (server: ServerInfo, username: string, password: string) { | ||
192 | const bodyClient = await server.loginCommand.getClient() | ||
193 | const client = { id: bodyClient.client_id, secret: bodyClient.client_secret } | ||
194 | |||
195 | const body = await server.loginCommand.login({ client, user: { username, password } }) | ||
196 | |||
197 | server.accessToken = body.access_token | ||
198 | } | ||
199 | |||
197 | function getLogger (logLevel = 'info') { | 200 | function getLogger (logLevel = 'info') { |
198 | const logLevels = { | 201 | const logLevels = { |
199 | 0: 0, | 202 | 0: 0, |
@@ -241,6 +244,6 @@ export { | |||
241 | buildVideoAttributesFromCommander, | 244 | buildVideoAttributesFromCommander, |
242 | 245 | ||
243 | getAdminTokenOrDie, | 246 | getAdminTokenOrDie, |
244 | getAccessTokenOrDie, | 247 | buildServer, |
245 | buildServer | 248 | assignToken |
246 | } | 249 | } |
diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts index 1934e7986..b9f4ef4f8 100644 --- a/server/tools/peertube-auth.ts +++ b/server/tools/peertube-auth.ts | |||
@@ -5,9 +5,8 @@ registerTSPaths() | |||
5 | 5 | ||
6 | import { OptionValues, program } from 'commander' | 6 | import { OptionValues, program } from 'commander' |
7 | import * as prompt from 'prompt' | 7 | import * as prompt from 'prompt' |
8 | import { getNetrc, getSettings, writeSettings } from './cli' | 8 | import { assignToken, buildServer, getNetrc, getSettings, writeSettings } from './cli' |
9 | import { isUserUsernameValid } from '../helpers/custom-validators/users' | 9 | import { isUserUsernameValid } from '../helpers/custom-validators/users' |
10 | import { getAccessToken } from '../../shared/extra-utils' | ||
11 | import * as CliTable3 from 'cli-table3' | 10 | import * as CliTable3 from 'cli-table3' |
12 | 11 | ||
13 | async function delInstance (url: string) { | 12 | async function delInstance (url: string) { |
@@ -97,7 +96,8 @@ program | |||
97 | // @see https://github.com/Chocobozzz/PeerTube/issues/3520 | 96 | // @see https://github.com/Chocobozzz/PeerTube/issues/3520 |
98 | result.url = stripExtraneousFromPeerTubeUrl(result.url) | 97 | result.url = stripExtraneousFromPeerTubeUrl(result.url) |
99 | 98 | ||
100 | await getAccessToken(result.url, result.username, result.password) | 99 | const server = buildServer(result.url) |
100 | await assignToken(server, result.username, result.password) | ||
101 | } catch (err) { | 101 | } catch (err) { |
102 | console.error(err.message) | 102 | console.error(err.message) |
103 | process.exit(-1) | 103 | process.exit(-1) |
diff --git a/server/tools/peertube-get-access-token.ts b/server/tools/peertube-get-access-token.ts index 5868d0548..a67de9180 100644 --- a/server/tools/peertube-get-access-token.ts +++ b/server/tools/peertube-get-access-token.ts | |||
@@ -2,7 +2,7 @@ import { registerTSPaths } from '../helpers/register-ts-paths' | |||
2 | registerTSPaths() | 2 | registerTSPaths() |
3 | 3 | ||
4 | import { program } from 'commander' | 4 | import { program } from 'commander' |
5 | import { getAccessToken } from '../../shared/extra-utils' | 5 | import { assignToken, buildServer } from './cli' |
6 | 6 | ||
7 | program | 7 | program |
8 | .option('-u, --url <url>', 'Server url') | 8 | .option('-u, --url <url>', 'Server url') |
@@ -24,9 +24,11 @@ if ( | |||
24 | process.exit(-1) | 24 | process.exit(-1) |
25 | } | 25 | } |
26 | 26 | ||
27 | getAccessToken(options.url, options.username, options.password) | 27 | const server = buildServer(options.url) |
28 | .then(accessToken => { | 28 | |
29 | console.log(accessToken) | 29 | assignToken(server, options.username, options.password) |
30 | .then(() => { | ||
31 | console.log(server.accessToken) | ||
30 | process.exit(0) | 32 | process.exit(0) |
31 | }) | 33 | }) |
32 | .catch(err => { | 34 | .catch(err => { |
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index a546a8dbe..0a4d6fa6e 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts | |||
@@ -14,10 +14,10 @@ import { sha256 } from '../helpers/core-utils' | |||
14 | import { doRequestAndSaveToFile } from '../helpers/requests' | 14 | import { doRequestAndSaveToFile } from '../helpers/requests' |
15 | import { CONSTRAINTS_FIELDS } from '../initializers/constants' | 15 | import { CONSTRAINTS_FIELDS } from '../initializers/constants' |
16 | import { | 16 | import { |
17 | assignToken, | ||
17 | buildCommonVideoOptions, | 18 | buildCommonVideoOptions, |
18 | buildServer, | 19 | buildServer, |
19 | buildVideoAttributesFromCommander, | 20 | buildVideoAttributesFromCommander, |
20 | getAccessTokenOrDie, | ||
21 | getLogger, | 21 | getLogger, |
22 | getServerCredentials | 22 | getServerCredentials |
23 | } from './cli' | 23 | } from './cli' |
@@ -232,8 +232,8 @@ async function uploadVideoOnPeerTube (parameters: { | |||
232 | tags | 232 | tags |
233 | } | 233 | } |
234 | 234 | ||
235 | let accessToken = await getAccessTokenOrDie(url, username, password) | 235 | const server = buildServer(url) |
236 | const server = buildServer(url, accessToken) | 236 | await assignToken(server, username, password) |
237 | 237 | ||
238 | const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) | 238 | const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) |
239 | 239 | ||
@@ -247,14 +247,14 @@ async function uploadVideoOnPeerTube (parameters: { | |||
247 | log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) | 247 | log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) |
248 | 248 | ||
249 | try { | 249 | try { |
250 | await uploadVideo(url, accessToken, videoAttributes) | 250 | await uploadVideo(url, server.accessToken, videoAttributes) |
251 | } catch (err) { | 251 | } catch (err) { |
252 | if (err.message.indexOf('401') !== -1) { | 252 | if (err.message.indexOf('401') !== -1) { |
253 | log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') | 253 | log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') |
254 | 254 | ||
255 | accessToken = await getAccessTokenOrDie(url, username, password) | 255 | server.accessToken = await server.loginCommand.getAccessToken(username, password) |
256 | 256 | ||
257 | await uploadVideo(url, accessToken, videoAttributes) | 257 | await uploadVideo(url, server.accessToken, videoAttributes) |
258 | } else { | 258 | } else { |
259 | exitError(err.message) | 259 | exitError(err.message) |
260 | } | 260 | } |
diff --git a/server/tools/peertube-plugins.ts b/server/tools/peertube-plugins.ts index 63541bf2c..22a09b779 100644 --- a/server/tools/peertube-plugins.ts +++ b/server/tools/peertube-plugins.ts | |||
@@ -4,7 +4,7 @@ import { registerTSPaths } from '../helpers/register-ts-paths' | |||
4 | registerTSPaths() | 4 | registerTSPaths() |
5 | 5 | ||
6 | import { program, Command, OptionValues } from 'commander' | 6 | import { program, Command, OptionValues } from 'commander' |
7 | import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' | 7 | import { assignToken, buildServer, getServerCredentials } from './cli' |
8 | import { PluginType } from '../../shared/models' | 8 | import { PluginType } from '../../shared/models' |
9 | import { isAbsolute } from 'path' | 9 | import { isAbsolute } from 'path' |
10 | import * as CliTable3 from 'cli-table3' | 10 | import * as CliTable3 from 'cli-table3' |
@@ -62,8 +62,8 @@ program.parse(process.argv) | |||
62 | 62 | ||
63 | async function pluginsListCLI (command: Command, options: OptionValues) { | 63 | async function pluginsListCLI (command: Command, options: OptionValues) { |
64 | const { url, username, password } = await getServerCredentials(command) | 64 | const { url, username, password } = await getServerCredentials(command) |
65 | const token = await getAdminTokenOrDie(url, username, password) | 65 | const server = buildServer(url) |
66 | const server = buildServer(url, token) | 66 | await assignToken(server, username, password) |
67 | 67 | ||
68 | let pluginType: PluginType | 68 | let pluginType: PluginType |
69 | if (options.onlyThemes) pluginType = PluginType.THEME | 69 | if (options.onlyThemes) pluginType = PluginType.THEME |
@@ -105,8 +105,8 @@ async function installPluginCLI (command: Command, options: OptionValues) { | |||
105 | } | 105 | } |
106 | 106 | ||
107 | const { url, username, password } = await getServerCredentials(command) | 107 | const { url, username, password } = await getServerCredentials(command) |
108 | const token = await getAdminTokenOrDie(url, username, password) | 108 | const server = buildServer(url) |
109 | const server = buildServer(url, token) | 109 | await assignToken(server, username, password) |
110 | 110 | ||
111 | try { | 111 | try { |
112 | await server.pluginsCommand.install({ npmName: options.npmName, path: options.path }) | 112 | await server.pluginsCommand.install({ npmName: options.npmName, path: options.path }) |
@@ -132,8 +132,8 @@ async function updatePluginCLI (command: Command, options: OptionValues) { | |||
132 | } | 132 | } |
133 | 133 | ||
134 | const { url, username, password } = await getServerCredentials(command) | 134 | const { url, username, password } = await getServerCredentials(command) |
135 | const token = await getAdminTokenOrDie(url, username, password) | 135 | const server = buildServer(url) |
136 | const server = buildServer(url, token) | 136 | await assignToken(server, username, password) |
137 | 137 | ||
138 | try { | 138 | try { |
139 | await server.pluginsCommand.update({ npmName: options.npmName, path: options.path }) | 139 | await server.pluginsCommand.update({ npmName: options.npmName, path: options.path }) |
@@ -154,8 +154,8 @@ async function uninstallPluginCLI (command: Command, options: OptionValues) { | |||
154 | } | 154 | } |
155 | 155 | ||
156 | const { url, username, password } = await getServerCredentials(command) | 156 | const { url, username, password } = await getServerCredentials(command) |
157 | const token = await getAdminTokenOrDie(url, username, password) | 157 | const server = buildServer(url) |
158 | const server = buildServer(url, token) | 158 | await assignToken(server, username, password) |
159 | 159 | ||
160 | try { | 160 | try { |
161 | await server.pluginsCommand.uninstall({ npmName: options.npmName }) | 161 | await server.pluginsCommand.uninstall({ npmName: options.npmName }) |
diff --git a/server/tools/peertube-redundancy.ts b/server/tools/peertube-redundancy.ts index 76d633f9e..0f6b3086c 100644 --- a/server/tools/peertube-redundancy.ts +++ b/server/tools/peertube-redundancy.ts | |||
@@ -8,7 +8,7 @@ import { URL } from 'url' | |||
8 | import validator from 'validator' | 8 | import validator from 'validator' |
9 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | 9 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
10 | import { VideoRedundanciesTarget } from '@shared/models' | 10 | import { VideoRedundanciesTarget } from '@shared/models' |
11 | import { buildServer, getAdminTokenOrDie, getServerCredentials } from './cli' | 11 | import { assignToken, buildServer, getServerCredentials } from './cli' |
12 | 12 | ||
13 | import bytes = require('bytes') | 13 | import bytes = require('bytes') |
14 | 14 | ||
@@ -60,8 +60,8 @@ program.parse(process.argv) | |||
60 | 60 | ||
61 | async function listRedundanciesCLI (target: VideoRedundanciesTarget) { | 61 | async function listRedundanciesCLI (target: VideoRedundanciesTarget) { |
62 | const { url, username, password } = await getServerCredentials(program) | 62 | const { url, username, password } = await getServerCredentials(program) |
63 | const token = await getAdminTokenOrDie(url, username, password) | 63 | const server = buildServer(url) |
64 | const server = buildServer(url, token) | 64 | await assignToken(server, username, password) |
65 | 65 | ||
66 | const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target }) | 66 | const { data } = await server.redundancyCommand.listVideos({ start: 0, count: 100, sort: 'name', target }) |
67 | 67 | ||
@@ -104,8 +104,8 @@ async function listRedundanciesCLI (target: VideoRedundanciesTarget) { | |||
104 | 104 | ||
105 | async function addRedundancyCLI (options: { video: number }, command: Command) { | 105 | async function addRedundancyCLI (options: { video: number }, command: Command) { |
106 | const { url, username, password } = await getServerCredentials(command) | 106 | const { url, username, password } = await getServerCredentials(command) |
107 | const token = await getAdminTokenOrDie(url, username, password) | 107 | const server = buildServer(url) |
108 | const server = buildServer(url, token) | 108 | await assignToken(server, username, password) |
109 | 109 | ||
110 | if (!options.video || validator.isInt('' + options.video) === false) { | 110 | if (!options.video || validator.isInt('' + options.video) === false) { |
111 | console.error('You need to specify the video id to duplicate and it should be a number.\n') | 111 | console.error('You need to specify the video id to duplicate and it should be a number.\n') |
@@ -134,8 +134,8 @@ async function addRedundancyCLI (options: { video: number }, command: Command) { | |||
134 | 134 | ||
135 | async function removeRedundancyCLI (options: { video: number }, command: Command) { | 135 | async function removeRedundancyCLI (options: { video: number }, command: Command) { |
136 | const { url, username, password } = await getServerCredentials(command) | 136 | const { url, username, password } = await getServerCredentials(command) |
137 | const token = await getAdminTokenOrDie(url, username, password) | 137 | const server = buildServer(url) |
138 | const server = buildServer(url, token) | 138 | await assignToken(server, username, password) |
139 | 139 | ||
140 | if (!options.video || validator.isInt('' + options.video) === false) { | 140 | if (!options.video || validator.isInt('' + options.video) === false) { |
141 | console.error('You need to specify the video id to remove from your redundancies.\n') | 141 | console.error('You need to specify the video id to remove from your redundancies.\n') |
diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts index d1c5348d1..c94b05857 100644 --- a/server/tools/peertube-upload.ts +++ b/server/tools/peertube-upload.ts | |||
@@ -4,9 +4,8 @@ registerTSPaths() | |||
4 | import { program } from 'commander' | 4 | import { program } from 'commander' |
5 | import { access, constants } from 'fs-extra' | 5 | import { access, constants } from 'fs-extra' |
6 | import { isAbsolute } from 'path' | 6 | import { isAbsolute } from 'path' |
7 | import { getAccessToken } from '../../shared/extra-utils' | ||
8 | import { uploadVideo } from '../../shared/extra-utils/' | 7 | import { uploadVideo } from '../../shared/extra-utils/' |
9 | import { buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' | 8 | import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' |
10 | 9 | ||
11 | let command = program | 10 | let command = program |
12 | .name('upload') | 11 | .name('upload') |
@@ -46,8 +45,8 @@ getServerCredentials(command) | |||
46 | .catch(err => console.error(err)) | 45 | .catch(err => console.error(err)) |
47 | 46 | ||
48 | async function run (url: string, username: string, password: string) { | 47 | async function run (url: string, username: string, password: string) { |
49 | const token = await getAccessToken(url, username, password) | 48 | const server = buildServer(url) |
50 | const server = buildServer(url, token) | 49 | await assignToken(server, username, password) |
51 | 50 | ||
52 | await access(options.file, constants.F_OK) | 51 | await access(options.file, constants.F_OK) |
53 | 52 | ||
@@ -62,7 +61,7 @@ async function run (url: string, username: string, password: string) { | |||
62 | }) | 61 | }) |
63 | 62 | ||
64 | try { | 63 | try { |
65 | await uploadVideo(url, token, videoAttributes) | 64 | await uploadVideo(url, server.accessToken, videoAttributes) |
66 | console.log(`Video ${options.videoName} uploaded.`) | 65 | console.log(`Video ${options.videoName} uploaded.`) |
67 | process.exit(0) | 66 | process.exit(0) |
68 | } catch (err) { | 67 | } catch (err) { |
diff --git a/shared/extra-utils/users/login-command.ts b/shared/extra-utils/users/login-command.ts index 97efcb766..8af3531f9 100644 --- a/shared/extra-utils/users/login-command.ts +++ b/shared/extra-utils/users/login-command.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | import { PeerTubeRequestError } from '@server/helpers/requests' | ||
2 | import { HttpStatusCode } from '@shared/core-utils' | 1 | import { HttpStatusCode } from '@shared/core-utils' |
3 | import { PeerTubeProblemDocument } from '@shared/models' | 2 | import { PeerTubeProblemDocument } from '@shared/models' |
4 | import { unwrapBody } from '../requests' | 3 | import { unwrapBody } from '../requests' |
@@ -34,8 +33,8 @@ export class LoginCommand extends AbstractCommand { | |||
34 | })) | 33 | })) |
35 | } | 34 | } |
36 | 35 | ||
37 | getAccessToken (user?: { username: string, password: string }): Promise<string> | 36 | getAccessToken (arg1?: { username: string, password: string }): Promise<string> |
38 | getAccessToken (username: string, password: string): Promise<string> | 37 | getAccessToken (arg1: string, password: string): Promise<string> |
39 | async getAccessToken (arg1?: { username: string, password: string } | string, password?: string) { | 38 | async getAccessToken (arg1?: { username: string, password: string } | string, password?: string) { |
40 | let user: { username: string, password: string } | 39 | let user: { username: string, password: string } |
41 | 40 | ||
@@ -48,7 +47,7 @@ export class LoginCommand extends AbstractCommand { | |||
48 | 47 | ||
49 | return body.access_token | 48 | return body.access_token |
50 | } catch (err) { | 49 | } catch (err) { |
51 | throw new Error('Cannot authenticate. Please check your username/password.') | 50 | throw new Error(`Cannot authenticate. Please check your username/password. (${err})`) |
52 | } | 51 | } |
53 | } | 52 | } |
54 | 53 | ||