peertube-auth.ts 4.2 KB

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