]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tools/peertube-auth.ts
Refactor middleware helpers
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-auth.ts
index 8bc3d332c9cb43f24759328fb52be2be87b6f6f7..d4ad56e47e633438510535f639a8485dfc352dd0 100644 (file)
@@ -1,15 +1,19 @@
 import * as program from 'commander'
 import * as prompt from 'prompt'
-import { getSettings, writeSettings, getNetrc } from './cli'
-import { isHostValid } from '../helpers/custom-validators/servers'
+import { getNetrc, getSettings, writeSettings } from './cli'
 import { isUserUsernameValid } from '../helpers/custom-validators/users'
+import { getAccessToken, login } from '../../shared/extra-utils'
 
 const Table = require('cli-table')
 
 async function delInstance (url: string) {
   const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
 
-  settings.remotes.splice(settings.remotes.indexOf(url))
+  const index = settings.remotes.indexOf(url)
+  settings.remotes.splice(index)
+
+  if (settings.default === index) settings.default = -1
+
   await writeSettings(settings)
 
   delete netrc.machines[url]
@@ -17,12 +21,17 @@ async function delInstance (url: string) {
   await netrc.save()
 }
 
-async function setInstance (url: string, username: string, password: string) {
+async function setInstance (url: string, username: string, password: string, isDefault: boolean) {
   const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
 
   if (settings.remotes.indexOf(url) === -1) {
     settings.remotes.push(url)
   }
+
+  if (isDefault || settings.remotes.length === 1) {
+    settings.default = settings.remotes.length - 1
+  }
+
   await writeSettings(settings)
 
   netrc.machines[url] = { login: username, password }
@@ -30,7 +39,7 @@ async function setInstance (url: string, username: string, password: string) {
 }
 
 function isURLaPeerTubeInstance (url: string) {
-  return isHostValid(url) || (url.includes('localhost'))
+  return url.startsWith('http://') || url.startsWith('https://')
 }
 
 program
@@ -52,6 +61,7 @@ program
         url: {
           description: 'instance url',
           conform: (value) => isURLaPeerTubeInstance(value),
+          message: 'It should be an URL (https://peertube.example.com)',
           required: true
         },
         username: {
@@ -66,7 +76,15 @@ program
         }
       }
     }, async (_, result) => {
-      await setInstance(result.url, result.username, result.password)
+      // Check credentials
+      try {
+        await getAccessToken(result.url, result.username, result.password)
+      } catch (err) {
+        console.error(err.message)
+        process.exit(-1)
+      }
+
+      await setInstance(result.url, result.username, result.password, program['default'])
 
       process.exit(0)
     })
@@ -93,6 +111,8 @@ program
     })
 
     settings.remotes.forEach(element => {
+      if (!netrc.machines[element]) return
+
       table.push([
         element,
         netrc.machines[element].login