peertube-auth.ts 4.5 KB

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