peertube-auth.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. program
  35. .name('auth')
  36. .usage('[command] [options]')
  37. program
  38. .command('add')
  39. .description('remember your accounts on remote instances for easier use')
  40. .option('-u, --url <url>', 'Server url')
  41. .option('-U, --username <username>', 'Username')
  42. .option('-p, --password <token>', 'Password')
  43. .option('--default', 'add the entry as the new default')
  44. .action(options => {
  45. prompt.override = options
  46. prompt.start()
  47. prompt.get({
  48. properties: {
  49. url: {
  50. description: 'instance url',
  51. conform: (value) => isURLaPeerTubeInstance(value),
  52. message: 'It should be an URL (https://peertube.example.com)',
  53. required: true
  54. },
  55. username: {
  56. conform: (value) => isUserUsernameValid(value),
  57. message: 'Name must be only letters, spaces, or dashes',
  58. required: true
  59. },
  60. password: {
  61. hidden: true,
  62. replace: '*',
  63. required: true
  64. }
  65. }
  66. }, async (_, result) => {
  67. // Check credentials
  68. try {
  69. await getAccessToken(result.url, result.username, result.password)
  70. } catch (err) {
  71. console.error(err.message)
  72. process.exit(-1)
  73. }
  74. await setInstance(result.url, result.username, result.password, program['default'])
  75. process.exit(0)
  76. })
  77. })
  78. program
  79. .command('del <url>')
  80. .description('unregisters a remote instance')
  81. .action(async url => {
  82. await delInstance(url)
  83. process.exit(0)
  84. })
  85. program
  86. .command('list')
  87. .description('lists registered remote instances')
  88. .action(async () => {
  89. const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
  90. const table = new CliTable3({
  91. head: [ 'instance', 'login' ],
  92. colWidths: [ 30, 30 ]
  93. }) as any
  94. settings.remotes.forEach(element => {
  95. if (!netrc.machines[element]) return
  96. table.push([
  97. element,
  98. netrc.machines[element].login
  99. ])
  100. })
  101. console.log(table.toString())
  102. process.exit(0)
  103. })
  104. program
  105. .command('set-default <url>')
  106. .description('set an existing entry as default')
  107. .action(async url => {
  108. const settings = await getSettings()
  109. const instanceExists = settings.remotes.includes(url)
  110. if (instanceExists) {
  111. settings.default = settings.remotes.indexOf(url)
  112. await writeSettings(settings)
  113. process.exit(0)
  114. } else {
  115. console.log('<url> is not a registered instance.')
  116. process.exit(-1)
  117. }
  118. })
  119. program.on('--help', function () {
  120. console.log(' Examples:')
  121. console.log()
  122. console.log(' $ peertube add -u https://peertube.cpy.re -U "PEERTUBE_USER" --password "PEERTUBE_PASSWORD"')
  123. console.log(' $ peertube add -u https://peertube.cpy.re -U root')
  124. console.log(' $ peertube list')
  125. console.log(' $ peertube del https://peertube.cpy.re')
  126. console.log()
  127. })
  128. if (!process.argv.slice(2).length) {
  129. program.outputHelp()
  130. }
  131. program.parse(process.argv)