peertube-auth.ts 4.7 KB

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